1 4 package org.openedit.repository.filesystem; 5 6 import java.io.ByteArrayOutputStream ; 7 import java.io.File ; 8 import java.io.FileWriter ; 9 import java.io.InputStreamReader ; 10 import java.io.StringReader ; 11 import java.io.Writer ; 12 import java.util.List ; 13 14 import junit.framework.TestCase; 15 16 import org.openedit.repository.ContentItem; 17 import org.openedit.repository.RepositoryException; 18 19 import com.openedit.util.OutputFiller; 20 21 24 public class BaseRepositoryTest extends TestCase 25 { 26 protected VersionedRepository fieldRepository; 27 protected DirectoryTool fieldDirectoryTool; 28 29 30 public BaseRepositoryTest( String arg0 ) 31 { 32 super( arg0 ); 33 } 34 35 public DirectoryTool getDirectoryTool() 36 { 37 if (fieldDirectoryTool == null) 38 { 39 fieldDirectoryTool = new DirectoryTool(); 40 } 41 return fieldDirectoryTool; 42 } 43 44 public VersionedRepository getRepository() 45 { 46 if (fieldRepository == null) 47 { 48 fieldRepository = new XmlVersionRepository( getRootDirectory() ); 49 } 50 return fieldRepository; 51 } 52 53 public void testGet_NoFile() throws Exception 54 { 55 ContentItem contentItem = getRepository().get( "/index.html"); 56 assertNotNull( contentItem ); 57 assertEquals( "index.html",((FileItem)contentItem).getFile().getName() ); 59 assertFalse( contentItem.exists() ); 60 System.out.println( contentItem.getMessage() ); 61 62 makeIndexFile(); 63 Thread.sleep(2000); 64 contentItem = getRepository().get( "/index.html" ); 65 assertTrue( contentItem.exists() ); 69 checkContentItemContent( contentItem ); 70 System.out.println( contentItem.getMessage() ); 71 72 } 73 74 public void checkContentItemContent( ContentItem inContentItem ) throws Exception 75 { 76 InputStreamReader reader = new InputStreamReader (inContentItem.getInputStream()); 77 char[] buffer = new char[512]; 78 int charsRead = reader.read(buffer); 79 assertTrue( charsRead > 0 ); 80 String content = new String ( buffer,0, charsRead); 81 System.out.println( content ); 82 assertEquals( "<p>This is a blank page</p>", content ); 83 reader.close(); 84 } 85 86 public void testGet_FileExists() throws Exception 87 { 88 putIndexFile(); 89 ContentItem contentItem = getRepository().get( "/index.html"); 90 assertNotNull( contentItem ); 91 assertEquals( "index.html",((FileItem)contentItem).getFile().getName() ); 93 assertTrue( contentItem.exists() ); 94 95 makeIndexFile(); 97 contentItem = getRepository().get( "/index.html"); 98 assertNotNull( contentItem ); 99 assertEquals( "index.html",((FileItem)contentItem).getFile().getName() ); 101 assertTrue( contentItem.exists() ); 102 } 103 104 public void testGet_Folder() throws Exception 105 { 106 putIndexFile(); 107 ContentItem root = getRepository().get( "/"); 108 assertNotNull( root ); 109 assertEquals( "/",root.getPath() ); 111 assertTrue( root.exists() ); 112 ByteArrayOutputStream out = new ByteArrayOutputStream (); 113 OutputFiller filler = new OutputFiller(); 114 filler.fill( root.getInputStream(), out ); 115 assertEquals( "index.html\n", out.toString() ); 116 } 117 118 public void testGetAndPut() throws Exception 119 { 120 makeIndexFile(); 121 ContentItem contentItem = getRepository().get( "/index.html" ); 122 checkContentItemContent( contentItem ); 123 getRepository().put( contentItem ); 125 checkContentItemContent( contentItem ); 126 } 128 129 public void testGetLatestContentItem() throws Exception 130 { 131 makeIndexFile(); 132 putIndexFile(); 133 List items = getRepository().getVersions("/index.html"); 134 ContentItem contentItem = (ContentItem)items.get(0); 135 assertEquals("1", contentItem.getVersion() ); 136 assertEquals( "index.html~1",((FileItem)contentItem).getFile().getName() ); 137 assertTrue( contentItem.exists() ); 138 } 139 140 public void testPut() throws Exception 141 { 142 File indexFile = makeIndexFile(); 143 assertEquals( 0, getRepository().maxVersionNumber( indexFile ) ); 144 putIndexFile(); 145 assertEquals( 1, getRepository().maxVersionNumber( indexFile ) ); 146 putIndexFile(); 147 putIndexFile(); 148 putIndexFile(); 149 assertEquals( 1, getRepository().maxVersionNumber( indexFile ) ); 151 152 StringItem item = new StringItem( "/index.html", "<p>New content XXX</p>" ,null); 153 item.setMessage("Automatic ContentItem from filesystem." ); 154 item.setAuthor("admin"); 155 getRepository().put(item); 156 assertEquals( 2, getRepository().maxVersionNumber( indexFile ) ); 157 158 159 160 } 161 162 163 public void testCheckReservedPaths() throws Exception 164 { 165 assertBadPath( "/foo/bar/.versions" ); 166 assertBadPath( "/foo/bar/.versions/.somestuff" ); 167 try 168 { 169 getRepository().checkReservedPaths( "/foo/bar/index.html" ); 170 } 171 catch( RepositoryException e ) 172 { 173 assertTrue( false ); 174 } 175 } 176 protected void assertBadPath( String inPath ) throws Exception 177 { 178 try 179 { 180 getRepository().checkReservedPaths( inPath ); 181 assertTrue( false ); 182 } 183 catch( RepositoryException e ) 184 { 185 assertTrue( true ); 186 } 187 } 188 189 193 public void testMaxVersionNumber() throws Exception 194 { 195 File indexFile = makeIndexFile(); 196 assertEquals( 0, getRepository().maxVersionNumber( indexFile ) ); 197 putIndexFile(); 198 assertEquals( 1, getRepository().maxVersionNumber( indexFile ) ); 199 } 200 201 202 public void testRemove() throws Exception 203 { 204 putIndexFile(); 205 ContentItem contentItem = getRepository().get("/index.html"); 206 assertTrue( contentItem.exists() ); 207 getRepository().remove( contentItem ); 208 assertTrue( !contentItem.exists() ); 209 } 210 211 212 public void testgetContentItems() throws Exception 213 { 214 getRepository(); 215 File indexFile = makeIndexFile(); 218 assertTrue ( indexFile.exists() ); 219 220 223 List contentItems = getRepository().getVersions( "/index.html" ); 224 assertEquals( 1, contentItems.size() ); 225 ContentItem automaticContentItem = (ContentItem) contentItems.get(0); 226 assertFirstContentItem( automaticContentItem ); 227 228 ContentItem contentItem = new StringItem("/foo/bar/file.html", "<p>New content</p>" ,null); 229 getRepository().put( contentItem ); 230 contentItems = getRepository().getVersions( "/foo/bar" ); 231 assertEquals( 0, contentItems.size() ); 232 } 233 234 public void testGetContentItemForExternalEdits() throws Exception 235 { 236 File indexFile = makeIndexFile(); 238 239 assertTrue ( indexFile.exists() ); 240 List contentItems = getRepository().getVersions( "/index.html" ); 241 assertEquals( 1, contentItems.size() ); 242 244 putIndexFile(); 246 247 contentItems = getRepository().getVersions( "/index.html" ); 248 assertEquals( 2, contentItems.size() ); 249 } 250 251 protected void assertFirstContentItem( ContentItem inContentItem ) 252 { 253 assertEquals( "admin", inContentItem.getAuthor() ); 254 assertEquals( "automatic version", inContentItem.getMessage() ); 255 assertEquals( "1", inContentItem.getVersion() ); 256 assertEquals( "/index.html", inContentItem.getPath() ); 257 assertEquals( ContentItem.TYPE_ADDED, inContentItem.getType() ); 258 assertNotNull( inContentItem.lastModified() ); 259 } 260 261 public File makeIndexFile() throws Exception 262 { 263 File indexFile = new File ( getRepository().getRootDirectory(), "index.html" ); 264 StringReader reader = new StringReader ("<p>This is a blank page</p>" ); 265 Writer writer = new FileWriter ( indexFile ); 266 OutputFiller filler = new OutputFiller(); 267 filler.fill( reader, writer ); 268 writer.flush(); 269 writer.close(); 270 271 File versionsdir = new File ( getRepository().getRootDirectory(), ".versions" ); 272 versionsdir.mkdirs(); 273 return indexFile; 274 } 275 276 public void putIndexFile() throws Exception 277 { 278 StringItem item = new StringItem( "/index.html", "<p>New content</p>" ,null); 281 item.setMessage("junit test" ); 282 item.setAuthor("admin"); 283 getRepository().put(item); 284 } 285 286 287 public void testGetVersionsDirectory() throws Exception 288 { 289 getRepository(); 290 makeIndexFile(); 291 292 File versionsDirectory = getRepository().getVersionsDirectory( "/index.html" ); 293 versionsDirectory = getRepository().getVersionsDirectory( "/junk.html" ); 295 versionsDirectory = getRepository().getVersionsDirectory( "/foo/bar/file.html" ); 297 assertFalse( versionsDirectory.exists() ); 298 299 300 ContentItem contentItem = new StringItem("/foo/bar/file.html", "<p>New content</p>",null ); 301 getRepository().put( contentItem ); 302 versionsDirectory = getRepository().getVersionsDirectory( "/foo/bar/file.html" ); 303 assertTrue( versionsDirectory.exists() ); 304 305 } 306 public File getRootDirectory() 307 { 308 return getDirectoryTool().getRootDirectory(); 309 } 310 protected void tearDown() throws Exception 311 { 312 getDirectoryTool().tearDown(); 313 314 } 315 316 } 317 | Popular Tags |