KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > source > test > FileSourceTestCase


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  *
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.excalibur.source.test;
18
19 import java.io.File JavaDoc;
20 import java.io.OutputStream JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.ConcurrentModificationException JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.excalibur.source.ModifiableSource;
28 import org.apache.excalibur.source.Source;
29 import org.apache.excalibur.source.SourceUtil;
30 import org.apache.excalibur.source.SourceValidity;
31 import org.apache.excalibur.source.impl.FileSource;
32
33 /**
34  * Test case for FileSource.
35  *
36  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
37  * @version $Id: FileSourceTestCase.java,v 1.4 2004/02/28 11:47:22 cziegeler Exp $
38  */

39 public class FileSourceTestCase extends TestCase
40 {
41
42     private File JavaDoc m_tempDir;
43
44     public FileSourceTestCase()
45     {
46         this("FileSource");
47     }
48
49     public FileSourceTestCase(String JavaDoc name)
50     {
51         super(name);
52     }
53
54     protected void setUp() throws Exception JavaDoc
55     {
56         // Create a temp file
57
m_tempDir = File.createTempFile("filesource", "test");
58         // and make it a directory
59
m_tempDir.delete();
60         m_tempDir.mkdir();
61     }
62
63     public void testDirExistence() throws Exception JavaDoc
64     {
65         m_tempDir.mkdirs();
66         long time = m_tempDir.lastModified();
67         FileSource src = new FileSource("file", m_tempDir);
68         assertTrue("Temp dir doesn't exist", src.exists());
69         assertTrue("Temp dir is not traversable", src.isCollection());
70         // Check it was created less than 1 secs ago
71
assertEquals("Wrong creation date", time, src.getLastModified());
72
73         assertTrue("Temp dir is not empty", src.getChildren().isEmpty());
74     }
75
76     public void testChildCreation() throws Exception JavaDoc
77     {
78         final String JavaDoc text = "Writing to a source";
79
80         FileSource src = new FileSource("file", m_tempDir);
81
82         FileSource child = (FileSource) src.getChild("child.txt");
83         assertTrue("New file already exists", !child.exists());
84
85         // Should not have a validity, since it doesn't exist
86
assertNull("New file has a validity", child.getValidity());
87
88         // Test the name
89
assertEquals("Wrong name", "child.txt", child.getName());
90
91         // Feed with some content
92
fillSource(child, text);
93
94         // And test it
95
assertEquals(
96             "Wrong length",
97             text.length() + System.getProperty("line.separator").length(),
98             child.getContentLength());
99         assertEquals("Wrong content-type", "text/plain", child.getMimeType());
100         assertTrue("New file is traversable", !child.isCollection());
101
102         // Check that parent now has children
103
Collection JavaDoc children = src.getChildren();
104         assertEquals("Wrong number of children", 1, children.size());
105
106         // And also that crawling up the hierarchy is OK
107
Source parent = child.getParent();
108         assertEquals("Wrong parent URI", src.getURI(), parent.getURI());
109
110     }
111
112     public void testMove() throws Exception JavaDoc
113     {
114         final String JavaDoc text = "Original text";
115
116         FileSource src = new FileSource("file", m_tempDir);
117
118         FileSource child = (FileSource) src.getChild("child.txt");
119         assertTrue("New file already exists", !child.exists());
120
121         fillSource(child, text);
122         assertTrue("New file doesn't exist", child.exists());
123         long length = child.getContentLength();
124
125         FileSource child2 = (FileSource) src.getChild("child2.txt");
126         assertTrue("Second file already exist", !child2.exists());
127
128         SourceUtil.move(child, child2);
129         assertTrue("First file still exists", !child.exists());
130         assertTrue("Second file doesn't exist", child2.exists());
131         assertEquals("Wrong length of second file", length, child2.getContentLength());
132     }
133
134     public void testCopy() throws Exception JavaDoc
135     {
136         final String JavaDoc text = "Original text";
137
138         FileSource src = new FileSource("file", m_tempDir);
139
140         FileSource child = (FileSource) src.getChild("child.txt");
141         assertTrue("New file already exists", !child.exists());
142
143         fillSource(child, text);
144         assertTrue("New file doesn't exist", child.exists());
145         long length = child.getContentLength();
146
147         FileSource child2 = (FileSource) src.getChild("child2.txt");
148         assertTrue("Second file already exist", !child2.exists());
149
150         SourceUtil.copy(child, child2);
151
152         assertTrue("First file doesn't exist", child.exists());
153         assertTrue("Second file doesn't exist", child2.exists());
154         assertEquals("Wrong length of second file", length, child2.getContentLength());
155
156     }
157
158     public void testDelete() throws Exception JavaDoc
159     {
160         final String JavaDoc text = "Original text";
161
162         FileSource src = new FileSource("file", m_tempDir);
163
164         FileSource child = (FileSource) src.getChild("child.txt");
165         assertTrue("New file already exists", !child.exists());
166         fillSource(child, text);
167         assertTrue("New file doesn't exist", child.exists());
168
169         child.delete();
170         assertTrue("File still exists", !child.exists());
171     }
172
173     public void testConcurrentAccess() throws Exception JavaDoc
174     {
175         FileSource src = new FileSource("file", m_tempDir);
176
177         FileSource child = (FileSource) src.getChild("child.txt");
178         assertTrue("New file already exists", !child.exists());
179
180         child.getOutputStream();
181
182         try
183         {
184             // Get it a second time
185
child.getOutputStream();
186         }
187         catch (ConcurrentModificationException JavaDoc cme)
188         {
189             return; // This is what is expected
190
}
191         fail("Undedected concurrent modification");
192
193     }
194
195     public void testAtomicUpdate() throws Exception JavaDoc
196     {
197         final String JavaDoc text = "Blah, blah";
198         FileSource src = new FileSource("file", m_tempDir);
199
200         FileSource child = (FileSource) src.getChild("child.txt");
201         assertTrue("New file already exists", !child.exists());
202         fillSource(child, text + " and blah!");
203
204         long length = child.getContentLength();
205
206         SourceValidity validity = child.getValidity();
207         assertEquals("Validity is not valid", 1, validity.isValid());
208
209         // Wait 2 seconds before updating the file
210
Thread.sleep(2 * 1000L);
211
212         // Now change its content
213
PrintWriter JavaDoc pw = new PrintWriter JavaDoc(child.getOutputStream());
214         pw.write(text);
215
216         assertEquals("File length modified", length, child.getContentLength());
217
218         pw.close();
219
220         assertTrue("File length not modified", length != child.getContentLength());
221
222         assertEquals("Validity is valid", -1, validity.isValid());
223     }
224
225     protected void tearDown() throws Exception JavaDoc
226     {
227         deleteAll(m_tempDir);
228     }
229
230     // Recursively delete a file or directory
231
private void deleteAll(File JavaDoc f)
232     {
233         if (f.isDirectory())
234         {
235             File JavaDoc[] children = f.listFiles();
236             for (int i = 0; i < children.length; i++)
237             {
238                 deleteAll(children[i]);
239             }
240         }
241
242         f.delete();
243     }
244
245     private void fillSource(ModifiableSource src, String JavaDoc text) throws Exception JavaDoc
246     {
247         OutputStream JavaDoc os = src.getOutputStream();
248         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(os);
249
250         pw.println(text);
251         pw.close();
252     }
253
254 }
255
Popular Tags