diff options
author | rescobar | 2010-12-20 22:21:53 +0000 |
---|---|---|
committer | Ryan D. Brooks | 2010-12-20 22:21:53 +0000 |
commit | 4d2ada20ed5c064e523996230a3b26e5eef1e30e (patch) | |
tree | bd68d4f17181f5f09129121908202d03248c56f3 | |
parent | 54f6ac748f4ea2edf06740cc29c9837f32ff78c4 (diff) | |
download | org.eclipse.osee-4d2ada20ed5c064e523996230a3b26e5eef1e30e.tar.gz org.eclipse.osee-4d2ada20ed5c064e523996230a3b26e5eef1e30e.tar.xz org.eclipse.osee-4d2ada20ed5c064e523996230a3b26e5eef1e30e.zip |
refinement: Add FileWatcherTest0.9.8.v201012201525_RC
4 files changed, 211 insertions, 2 deletions
diff --git a/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/mock/MockFileWatcherListener.java b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/mock/MockFileWatcherListener.java new file mode 100644 index 00000000000..bcadfe688d0 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/mock/MockFileWatcherListener.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2004, 2007 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.test.mock; + +import java.util.Collection; +import org.eclipse.osee.framework.jdk.core.util.io.FileChangeEvent; +import org.eclipse.osee.framework.jdk.core.util.io.IFileWatcherListener; + +/** + * @author Roberto E. Escobar + */ +public class MockFileWatcherListener implements IFileWatcherListener { + + private int fileModifiedCallCount = 0; + private int handleExceptionCallCount = 0; + private Exception ex; + private Collection<FileChangeEvent> fileChangeEvents; + + @Override + public void filesModified(Collection<FileChangeEvent> fileChangeEvents) { + fileModifiedCallCount++; + this.fileChangeEvents = fileChangeEvents; + synchronized (this) { + notify(); + } + } + + @Override + public void handleException(Exception ex) { + handleExceptionCallCount++; + this.ex = ex; + synchronized (this) { + notify(); + } + } + + public int getFileModifiedCallCount() { + return fileModifiedCallCount; + } + + public int getHandleExceptionCallCount() { + return handleExceptionCallCount; + } + + public Exception getEx() { + return ex; + } + + public Collection<FileChangeEvent> getFileChangeEvents() { + return fileChangeEvents; + } + + public void clear() { + fileModifiedCallCount = 0; + handleExceptionCallCount = 0; + ex = null; + fileChangeEvents = null; + } +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/JdkCoreUtilTestSuite.java b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/JdkCoreUtilTestSuite.java index 549e47440c5..95a84923128 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/JdkCoreUtilTestSuite.java +++ b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/JdkCoreUtilTestSuite.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.osee.framework.jdk.core.test.util; -import org.eclipse.osee.framework.jdk.core.test.util.io.xml.IoXmlTestSuite; +import org.eclipse.osee.framework.jdk.core.test.util.io.IoTestSuite; import org.eclipse.osee.framework.jdk.core.test.util.xml.XmlTestSuite; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -20,7 +20,7 @@ import org.junit.runners.Suite; */ @RunWith(Suite.class) @Suite.SuiteClasses({ - IoXmlTestSuite.class, + IoTestSuite.class, XmlTestSuite.class, CollectionsTest.class, CompareTest.class, diff --git a/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/io/FileWatcherTest.java b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/io/FileWatcherTest.java new file mode 100644 index 00000000000..ac56c6cbad8 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/io/FileWatcherTest.java @@ -0,0 +1,113 @@ +/******************************************************************************* + * Copyright (c) 2010 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.test.util.io; + +import java.io.File; +import java.util.Collection; +import java.util.concurrent.TimeUnit; +import junit.framework.Assert; +import org.eclipse.osee.framework.jdk.core.test.mock.MockFileWatcherListener; +import org.eclipse.osee.framework.jdk.core.util.io.FileChangeEvent; +import org.eclipse.osee.framework.jdk.core.util.io.FileChangeType; +import org.eclipse.osee.framework.jdk.core.util.io.FileWatcher; +import org.junit.Test; + +/** + * @author Roberto E. Escobar + */ +public class FileWatcherTest { + + @Test + public void testWatcher() throws InterruptedException { + MockFileWatcherListener listener = new MockFileWatcherListener(); + + FileWatcher watcher = new FileWatcher(320, TimeUnit.MILLISECONDS); + + MockFile file = new MockFile("abcde", true); + + Assert.assertTrue(file.exists()); + + watcher.addListener(listener); + watcher.addFile(file); + watcher.start(); + + file.setLastModified(10000); + synchronized (listener) { + listener.wait(1000); + } + checkEvent(listener, file, FileChangeType.CREATED); + + listener.clear(); + file.setLastModified(15000); + synchronized (listener) { + listener.wait(1000); + } + checkEvent(listener, file, FileChangeType.MODIFIED); + + listener.clear(); + file.setExists(false); + file.setLastModified(0); + synchronized (listener) { + listener.wait(1000); + } + checkEvent(listener, file, FileChangeType.DELETED); + + watcher.stop(); + watcher.removeFile(file); + watcher.removeListener(listener); + } + + private static void checkEvent(MockFileWatcherListener listener, File expectedFile, FileChangeType expectedType) { + Assert.assertEquals("File modified event was not received", 1, listener.getFileModifiedCallCount()); + Collection<FileChangeEvent> items = listener.getFileChangeEvents(); + Assert.assertEquals(1, items.size()); + FileChangeEvent event = items.iterator().next(); + Assert.assertNotNull(event); + Assert.assertEquals(expectedType, event.getChangeType()); + Assert.assertEquals(expectedFile, event.getFile()); + } + + private final class MockFile extends File { + + private static final long serialVersionUID = 8677714040873925615L; + private long lastModified; + private boolean exists; + + public MockFile(String pathname, boolean exists) { + super(pathname); + this.exists = exists; + } + + @Override + public long lastModified() { + return lastModified; + } + + @Override + public boolean setLastModified(long time) { + boolean change = lastModified() != time; + if (change) { + this.lastModified = time; + } + return change; + } + + @Override + public boolean exists() { + return exists; + } + + public void setExists(boolean exists) { + this.exists = exists; + } + + } +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/io/IoTestSuite.java b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/io/IoTestSuite.java new file mode 100644 index 00000000000..70eb4e041b6 --- /dev/null +++ b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/test/util/io/IoTestSuite.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2004, 2010 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.framework.jdk.core.test.util.io; + +import org.eclipse.osee.framework.jdk.core.test.util.io.xml.IoXmlTestSuite; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +/** + * @author Roberto E. Escobar + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ +//@formatter:off + IoXmlTestSuite.class, + FileWatcherTest.class +//@formatter:on +}) +public class IoTestSuite { + // Test Suite Class +} |