KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > repository > filesystem > BaseRepositoryTest


1 /*
2  * Created on Aug 7, 2004
3  */

4 package org.openedit.repository.filesystem;
5
6 import java.io.ByteArrayOutputStream JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileWriter JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10 import java.io.StringReader JavaDoc;
11 import java.io.Writer JavaDoc;
12 import java.util.List JavaDoc;
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 /**
22  * @author Matthew Avery, mavery@einnovation.com
23  */

24 public class BaseRepositoryTest extends TestCase
25 {
26     protected VersionedRepository fieldRepository;
27     protected DirectoryTool fieldDirectoryTool;
28
29
30     public BaseRepositoryTest( String JavaDoc 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 JavaDoc
54     {
55         ContentItem contentItem = getRepository().get( "/index.html");
56         assertNotNull( contentItem );
57         //assertEquals( "1", contentItem.getVersion() );
58
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     // assertEquals( "1", contentItem.getVersion() );
66
// Should the version number increment?
67
// assertEquals( "2", contentItem.getVersion() );
68
assertTrue( contentItem.exists() );
69         checkContentItemContent( contentItem );
70         System.out.println( contentItem.getMessage() );
71         
72     }
73     
74     public void checkContentItemContent( ContentItem inContentItem ) throws Exception JavaDoc
75     {
76         InputStreamReader JavaDoc reader = new InputStreamReader JavaDoc(inContentItem.getInputStream());
77         char[] buffer = new char[512];
78         int charsRead = reader.read(buffer);
79         assertTrue( charsRead > 0 );
80         String JavaDoc content = new String JavaDoc( 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 JavaDoc
87     {
88         putIndexFile();
89         ContentItem contentItem = getRepository().get( "/index.html");
90         assertNotNull( contentItem );
91         //assertEquals( "1", contentItem.getVersion() );
92
assertEquals( "index.html",((FileItem)contentItem).getFile().getName() );
93         assertTrue( contentItem.exists() );
94         
95         // If we touch the file, the repository should make a new contentItem
96
makeIndexFile();
97         contentItem = getRepository().get( "/index.html");
98         assertNotNull( contentItem );
99         //assertEquals( "1", contentItem.getVersion() );
100
assertEquals( "index.html",((FileItem)contentItem).getFile().getName() );
101         assertTrue( contentItem.exists() );
102     }
103     
104     public void testGet_Folder() throws Exception JavaDoc
105     {
106         putIndexFile();
107         ContentItem root = getRepository().get( "/");
108         assertNotNull( root );
109         //assertEquals( "1", root.getVersion() );
110
assertEquals( "/",root.getPath() );
111         assertTrue( root.exists() );
112         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc
119     {
120         makeIndexFile();
121         ContentItem contentItem = getRepository().get( "/index.html" );
122         checkContentItemContent( contentItem );
123         //assertEquals( "1", contentItem.getVersion() );
124
getRepository().put( contentItem );
125         checkContentItemContent( contentItem );
126         //assertEquals( "2", contentItem.getVersion() );
127
}
128     
129     public void testGetLatestContentItem() throws Exception JavaDoc
130     {
131         makeIndexFile();
132         putIndexFile();
133         List JavaDoc 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 JavaDoc
141     {
142         File JavaDoc indexFile = makeIndexFile();
143         assertEquals( 0, getRepository().maxVersionNumber( indexFile ) );
144         putIndexFile();
145         assertEquals( 1, getRepository().maxVersionNumber( indexFile ) );
146         putIndexFile();
147         putIndexFile();
148         putIndexFile();
149         //It had not changed so the version should still be one
150
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 JavaDoc
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 JavaDoc inPath ) throws Exception JavaDoc
177     {
178         try
179         {
180             getRepository().checkReservedPaths( inPath );
181             assertTrue( false );
182         }
183         catch( RepositoryException e )
184         {
185             assertTrue( true );
186         }
187     }
188
189     /**
190      * This method should be the exact number of version on file
191      * @throws Exception
192      */

193     public void testMaxVersionNumber() throws Exception JavaDoc
194     {
195         File JavaDoc 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 JavaDoc
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 JavaDoc
213     {
214         getRepository();
215         //original
216
File JavaDoc indexFile = makeIndexFile(); //original file
217

218         assertTrue ( indexFile.exists() );
219         
220         //added a file
221
//putIndexFile(); //created a version
222

223         List JavaDoc 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 JavaDoc
235     {
236         //original
237
File JavaDoc indexFile = makeIndexFile();
238         
239         assertTrue ( indexFile.exists() );
240         List JavaDoc contentItems = getRepository().getVersions( "/index.html" );
241         assertEquals( 1, contentItems.size() );
242         //assertFirstContentItem( automaticContentItem );
243

244         //added the second rev
245
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 JavaDoc makeIndexFile() throws Exception JavaDoc
262     {
263         File JavaDoc indexFile = new File JavaDoc( getRepository().getRootDirectory(), "index.html" );
264         StringReader JavaDoc reader = new StringReader JavaDoc("<p>This is a blank page</p>" );
265         Writer JavaDoc writer = new FileWriter JavaDoc( indexFile );
266         OutputFiller filler = new OutputFiller();
267         filler.fill( reader, writer );
268         writer.flush();
269         writer.close();
270
271         File JavaDoc versionsdir = new File JavaDoc( getRepository().getRootDirectory(), ".versions" );
272         versionsdir.mkdirs();
273         return indexFile;
274     }
275     
276     public void putIndexFile() throws Exception JavaDoc
277     {
278         //ContentItem contentItem = getRepository().get("/index.html");
279
//assertNotNull(contentItem);
280
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 JavaDoc
288     {
289         getRepository();
290         makeIndexFile();
291         
292         File JavaDoc versionsDirectory = getRepository().getVersionsDirectory( "/index.html" );
293         //assertTrue( versionsDirectory.exists() );
294
versionsDirectory = getRepository().getVersionsDirectory( "/junk.html" );
295         //assertTrue( versionsDirectory.exists() );
296
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 JavaDoc getRootDirectory()
307     {
308         return getDirectoryTool().getRootDirectory();
309     }
310     protected void tearDown() throws Exception JavaDoc
311     {
312         getDirectoryTool().tearDown();
313
314     }
315
316 }
317
Popular Tags