001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.jci.monitor;
019:
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.FileWriter;
023: import java.io.IOException;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.commons.io.FileUtils;
028: import org.apache.commons.jci.listeners.AbstractFilesystemAlterationListener;
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: /**
033: *
034: * @author tcurdt
035: */
036: public final class FilesystemAlterationMonitorTestCase extends TestCase {
037:
038: private final Log log = LogFactory
039: .getLog(FilesystemAlterationMonitorTestCase.class);
040:
041: private FilesystemAlterationMonitor fam;
042: private MyFilesystemAlterationListener listener;
043:
044: private File directory;
045:
046: protected void setUp() throws Exception {
047: directory = createTempDirectory();
048: assertTrue(directory.exists());
049: assertTrue(directory.isDirectory());
050: }
051:
052: protected void tearDown() throws Exception {
053: FileUtils.deleteDirectory(directory);
054: }
055:
056: protected File createDirectory(final String pName) throws Exception {
057: final File newDirectory = new File(directory, pName);
058: assertTrue(newDirectory.mkdir());
059: assertTrue(newDirectory.exists());
060: assertTrue(newDirectory.isDirectory());
061: return newDirectory;
062: }
063:
064: protected File writeFile(final String pName, final byte[] pData)
065: throws Exception {
066: final File file = new File(directory, pName);
067: final File parent = file.getParentFile();
068: if (!parent.exists()) {
069: if (!parent.mkdirs()) {
070: throw new IOException("could not create" + parent);
071: }
072: }
073:
074: log.debug("writing file " + pName + " (" + pData.length
075: + " bytes)");
076:
077: final FileOutputStream os = new FileOutputStream(file);
078: os.write(pData);
079: os.close();
080:
081: assertTrue(file.exists());
082: assertTrue(file.isFile());
083:
084: return file;
085: }
086:
087: protected File writeFile(final String pName, final String pText)
088: throws Exception {
089: final File file = new File(directory, pName);
090: final File parent = file.getParentFile();
091: if (!parent.exists()) {
092: if (!parent.mkdirs()) {
093: throw new IOException("could not create" + parent);
094: }
095: }
096: log.debug("writing " + file);
097: final FileWriter writer = new FileWriter(file);
098: writer.write(pText);
099: writer.close();
100:
101: assertTrue(file.exists());
102: assertTrue(file.isFile());
103:
104: return file;
105: }
106:
107: protected File createTempDirectory() throws IOException {
108: final File tempFile = File.createTempFile("jci", null);
109:
110: if (!tempFile.delete()) {
111: throw new IOException();
112: }
113:
114: if (!tempFile.mkdir()) {
115: throw new IOException();
116: }
117:
118: return tempFile;
119: }
120:
121: protected void delay() {
122: try {
123: Thread.sleep(1500);
124: } catch (final InterruptedException e) {
125: }
126: }
127:
128: private static class MyFilesystemAlterationListener extends
129: AbstractFilesystemAlterationListener {
130: }
131:
132: private void start() throws Exception {
133: fam = new FilesystemAlterationMonitor();
134: listener = new MyFilesystemAlterationListener();
135: fam.addListener(directory, listener);
136: fam.start();
137: listener.waitForFirstCheck();
138: }
139:
140: private void stop() {
141: fam.stop();
142: }
143:
144: public void testListenerDoublication() throws Exception {
145: fam = new FilesystemAlterationMonitor();
146: listener = new MyFilesystemAlterationListener();
147:
148: fam.addListener(directory, listener);
149: assertTrue(fam.getListenersFor(directory).length == 1);
150:
151: fam.addListener(directory, listener);
152: assertTrue(fam.getListenersFor(directory).length == 1);
153: }
154:
155: public void testDirectoryDoublication() throws Exception {
156: fam = new FilesystemAlterationMonitor();
157:
158: fam
159: .addListener(directory,
160: new MyFilesystemAlterationListener());
161: assertTrue(fam.getListenersFor(directory).length == 1);
162:
163: fam
164: .addListener(directory,
165: new MyFilesystemAlterationListener());
166: assertTrue(fam.getListenersFor(directory).length == 2);
167: }
168:
169: public void testCreateFileDetection() throws Exception {
170: start();
171:
172: writeFile("file", "file");
173:
174: listener.waitForCheck();
175:
176: assertTrue(listener.getCreatedFiles().size() == 1);
177:
178: stop();
179: }
180:
181: public void testCreateDirectoryDetection() throws Exception {
182: start();
183:
184: createDirectory("dir");
185:
186: listener.waitForCheck();
187:
188: assertTrue(listener.getCreatedDirectories().size() == 1);
189:
190: stop();
191: }
192:
193: public void testDeleteFileDetection() throws Exception {
194: start();
195:
196: final File file = writeFile("file", "file");
197:
198: listener.waitForCheck();
199:
200: assertTrue(listener.getCreatedFiles().size() == 1);
201:
202: file.delete();
203: assertTrue(!file.exists());
204:
205: listener.waitForCheck();
206:
207: assertTrue(listener.getDeletedFiles().size() == 1);
208:
209: stop();
210: }
211:
212: public void testDeleteDirectoryDetection() throws Exception {
213: start();
214:
215: final File dir = createDirectory("dir");
216: createDirectory("dir/sub");
217: final File file = writeFile("dir/sub/file", "file");
218:
219: listener.waitForCheck();
220:
221: assertEquals(2, listener.getCreatedDirectories().size());
222: assertEquals(1, listener.getCreatedFiles().size());
223:
224: delay();
225:
226: FileUtils.deleteDirectory(dir);
227: assertTrue(!dir.exists());
228: assertTrue(!file.exists());
229:
230: listener.waitForCheck();
231:
232: assertEquals(2, listener.getDeletedDirectories().size());
233: assertEquals(1, listener.getDeletedFiles().size());
234:
235: stop();
236: }
237:
238: public void testModifyFileDetection() throws Exception {
239: start();
240:
241: writeFile("file", "file");
242:
243: listener.waitForCheck();
244:
245: assertTrue(listener.getCreatedFiles().size() == 1);
246:
247: delay();
248:
249: writeFile("file", "changed file");
250:
251: listener.waitForCheck();
252:
253: assertTrue(listener.getChangedFiles().size() == 1);
254:
255: stop();
256: }
257:
258: public void testCreatingLocalDirectoryChangesLastModified()
259: throws Exception {
260: final long modified = directory.lastModified();
261:
262: delay();
263:
264: createDirectory("directory");
265:
266: delay();
267:
268: assertTrue(directory.lastModified() != modified);
269: }
270:
271: public void testCreatingLocalFileChangesLastModified()
272: throws Exception {
273: final long modified = directory.lastModified();
274:
275: delay();
276:
277: writeFile("file", "file");
278:
279: delay();
280:
281: assertTrue(directory.lastModified() != modified);
282: }
283:
284: public void testCreatingSubDirectoryChangesLastModified()
285: throws Exception {
286: createDirectory("dir");
287:
288: final long modified = directory.lastModified();
289:
290: delay();
291:
292: createDirectory("dir/sub");
293:
294: assertTrue(directory.lastModified() == modified);
295: }
296:
297: public void testCreatingFileInSubDirectoryChangesLastModified()
298: throws Exception {
299: createDirectory("dir");
300:
301: final long modified = directory.lastModified();
302:
303: delay();
304:
305: writeFile("dir/file", "file");
306:
307: assertTrue(directory.lastModified() == modified);
308: }
309:
310: public void testInterval() throws Exception {
311:
312: final long interval = 1000;
313:
314: start();
315: fam.setInterval(interval);
316:
317: listener.waitForCheck();
318: long t1 = System.currentTimeMillis();
319:
320: listener.waitForCheck();
321: long t2 = System.currentTimeMillis();
322:
323: long diff = t2 - t1;
324:
325: // interval should be at around the same interval
326: assertTrue("the interval was set to " + interval
327: + " but the time difference was " + diff,
328: (diff > (interval - 50)) && (diff < (interval + 50)));
329:
330: stop();
331: }
332: }
|