KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > FileSystemTest


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sourcecontrols;
38
39 import junit.framework.TestCase;
40 import net.sourceforge.cruisecontrol.CruiseControlException;
41 import net.sourceforge.cruisecontrol.Modification;
42 import net.sourceforge.cruisecontrol.util.Util;
43
44 import java.io.BufferedWriter JavaDoc;
45 import java.io.File JavaDoc;
46 import java.io.FileWriter JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.io.PrintWriter JavaDoc;
49 import java.util.Date JavaDoc;
50 import java.util.GregorianCalendar JavaDoc;
51 import java.util.List JavaDoc;
52
53 public class FileSystemTest extends TestCase {
54
55     public FileSystemTest(String JavaDoc name) {
56         super(name);
57     }
58
59     public void testValidate() {
60         FileSystem fs = new FileSystem();
61
62         try {
63             fs.validate();
64             fail("FileSystem should throw exceptions when required attributes are not set.");
65         } catch (CruiseControlException e) {
66             assertEquals("'folder' is required for FileSystem", e.getMessage());
67         }
68
69         File JavaDoc tempDirectory = new File JavaDoc(System.getProperty("java.io.tmpdir"));
70         //Create a subdirectory in the temp directory for us to use.
71
tempDirectory = new File JavaDoc(tempDirectory, "filesystemtest2" + System.currentTimeMillis());
72         fs.setFolder(tempDirectory.getAbsolutePath());
73  
74          try {
75              fs.validate();
76             fail("FileSystem should throw exceptions when folder doesn't exist.");
77         } catch (CruiseControlException e) {
78             assertTrue(e.getMessage().indexOf("must exist") > -1);
79         }
80
81         assertTrue(tempDirectory.mkdir());
82
83         try {
84             fs.validate();
85         } catch (CruiseControlException e) {
86             fail("FileSystem should not throw exceptions when required attributes are set.");
87         } finally {
88             Util.deleteFile(tempDirectory);
89         }
90     }
91
92     public void testGettingModifications() throws Exception JavaDoc {
93         //Figure out where the temp directory is...
94
File JavaDoc tempDirectory = new File JavaDoc(System.getProperty("java.io.tmpdir"));
95
96         //Create a subdirectory in the temp directory for us to use.
97
tempDirectory = new File JavaDoc(tempDirectory,
98                 "filesystemtest" + System.currentTimeMillis());
99         assertTrue(tempDirectory.mkdir());
100
101         try {
102             //Setup a filesystem element that points at our test subdirectory...
103
FileSystem fsystem = new FileSystem();
104             fsystem.setFolder(tempDirectory.getAbsolutePath());
105     
106             //Check for modifications...there shouldn't be any
107
Date JavaDoc startTime = new GregorianCalendar JavaDoc(2000, 0, 1).getTime();
108             Date JavaDoc timeOne = new Date JavaDoc(startTime.getTime() + 2000);
109             Date JavaDoc timeTwo = new Date JavaDoc(timeOne.getTime() + 2000);
110             Date JavaDoc timeThree = new Date JavaDoc(timeTwo.getTime() + 2000);
111             List JavaDoc mods = fsystem.getModifications(startTime, timeOne);
112             assertNotNull(mods);
113             assertEquals(0, mods.size());
114     
115             //Write some files...
116
File JavaDoc tempFile;
117
118             tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory);
119             writeContent(tempFile, "testing");
120             tempFile.setLastModified(timeOne.getTime());
121     
122             tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory);
123             writeContent(tempFile, "testing 2");
124             tempFile.setLastModified(timeOne.getTime());
125     
126             //Check for mods...there should be some, one for each file written.
127
mods = fsystem.getModifications(startTime, timeOne);
128             assertNotNull(mods);
129             assertEquals(2, mods.size());
130     
131             //Write some new files...
132
tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory);
133             writeContent(tempFile, "testing 3");
134             tempFile.setLastModified(timeTwo.getTime());
135     
136             tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory);
137             writeContent(tempFile, "testing 4");
138             tempFile.setLastModified(timeTwo.getTime());
139     
140             tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory);
141             writeContent(tempFile, "testing 5");
142             tempFile.setLastModified(timeTwo.getTime());
143     
144             //Checking for mods again should turn up only the new files.
145
mods = fsystem.getModifications(timeOne, timeTwo);
146             assertNotNull(mods);
147             assertEquals(3, mods.size());
148     
149             //Create one modified file.
150
tempFile = File.createTempFile("CruiseControl", "TEST", tempDirectory);
151             writeContent(tempFile, "testing 6");
152             tempFile.setLastModified(timeThree.getTime());
153     
154             //Checking for mods again should turn up only the one file
155
mods = fsystem.getModifications(timeTwo, timeThree);
156             assertNotNull(mods);
157             assertEquals(1, mods.size());
158     
159             //Using this one mod, check the modification information for correctness.
160
Modification modification = (Modification) mods.get(0);
161             assertEquals(tempFile.getName(), modification.getFileName());
162             assertEquals(tempFile.getParent(), modification.getFolderName());
163             assertEquals(tempFile.lastModified(), modification.modifiedTime.getTime());
164         } finally {
165             Util.deleteFile(tempDirectory);
166         }
167     }
168
169     private static void writeContent(File JavaDoc file, String JavaDoc content)
170             throws IOException JavaDoc {
171         PrintWriter JavaDoc writer =
172                 new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new FileWriter JavaDoc(file)));
173         writer.print(content);
174         writer.flush();
175         writer.close();
176     }
177 }
Popular Tags