|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.commons.io.monitor; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileFilter; |
| 21 | + |
| 22 | +import org.apache.commons.io.FileUtils; |
| 23 | +import org.apache.commons.io.filefilter.FileFilterUtils; |
| 24 | +import org.apache.commons.io.filefilter.HiddenFileFilter; |
| 25 | +import org.apache.commons.io.filefilter.IOFileFilter; |
| 26 | + |
| 27 | +import junit.framework.TestCase; |
| 28 | + |
| 29 | +/** |
| 30 | + * {@link FilesystemObserver} Test Case. |
| 31 | + */ |
| 32 | +public abstract class AbstractMonitorTestCase extends TestCase { |
| 33 | + |
| 34 | + /** Filesystem observer */ |
| 35 | + protected FilesystemObserver observer; |
| 36 | + |
| 37 | + /** Listener which collects file changes */ |
| 38 | + protected CollectionFilesystemListener listener = new CollectionFilesystemListener(true); |
| 39 | + |
| 40 | + /** Test diretory name */ |
| 41 | + protected String testDirName = null; |
| 42 | + |
| 43 | + /** Directory for test files */ |
| 44 | + protected File testDir; |
| 45 | + |
| 46 | + /** Time in milliseconds to pause in tests */ |
| 47 | + protected long pauseTime = 100L; |
| 48 | + |
| 49 | + /** |
| 50 | + * Construct a new test case. |
| 51 | + * |
| 52 | + * @param name The name of the test |
| 53 | + */ |
| 54 | + public AbstractMonitorTestCase(String name) { |
| 55 | + super(name); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void setUp() throws Exception { |
| 60 | + testDir = new File(new File("."), testDirName); |
| 61 | + if (testDir.exists()) { |
| 62 | + FileUtils.cleanDirectory(testDir); |
| 63 | + } else { |
| 64 | + testDir.mkdir(); |
| 65 | + } |
| 66 | + |
| 67 | + IOFileFilter files = FileFilterUtils.fileFileFilter(); |
| 68 | + IOFileFilter javaSuffix = FileFilterUtils.suffixFileFilter(".java"); |
| 69 | + IOFileFilter fileFilter = FileFilterUtils.and(files, javaSuffix); |
| 70 | + |
| 71 | + IOFileFilter directories = FileFilterUtils.directoryFileFilter(); |
| 72 | + IOFileFilter visible = HiddenFileFilter.VISIBLE; |
| 73 | + IOFileFilter dirFilter = FileFilterUtils.and(directories, visible); |
| 74 | + |
| 75 | + IOFileFilter filter = FileFilterUtils.or(dirFilter, fileFilter); |
| 76 | + |
| 77 | + createObserver(testDir, filter); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Create a {@link FilesystemObserver}. |
| 82 | + * |
| 83 | + * @param file The directory to observe |
| 84 | + * @param fileFilter The file filter to apply |
| 85 | + */ |
| 86 | + protected void createObserver(File file, FileFilter fileFilter) { |
| 87 | + observer = new FilesystemObserver(file, fileFilter); |
| 88 | + observer.addListener(listener); |
| 89 | + try { |
| 90 | + observer.initialize(); |
| 91 | + } catch (Exception e) { |
| 92 | + fail("Observer init() threw " + e); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected void tearDown() throws Exception { |
| 98 | + FileUtils.deleteDirectory(testDir); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Check all the Collections are empty |
| 103 | + */ |
| 104 | + protected void checkCollectionsEmpty(String label) { |
| 105 | + checkCollectionSizes("EMPTY-" + label, 0, 0, 0, 0, 0, 0); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Check all the Collections have the expected sizes. |
| 110 | + */ |
| 111 | + protected void checkCollectionSizes(String label, int dirCreate, int dirChange, int dirDelete, int fileCreate, int fileChange, int fileDelete) { |
| 112 | + label = label + "[" + listener.getCreatedDirectories().size() + |
| 113 | + " " + listener.getChangedDirectories().size() + |
| 114 | + " " + listener.getDeletedDirectories().size() + |
| 115 | + " " + listener.getCreatedFiles().size() + |
| 116 | + " " + listener.getChangedFiles().size() + |
| 117 | + " " + listener.getDeletedFiles().size() + "]"; |
| 118 | + assertEquals(label + ": No. of directories created", dirCreate, listener.getCreatedDirectories().size()); |
| 119 | + assertEquals(label + ": No. of directories changed", dirChange, listener.getChangedDirectories().size()); |
| 120 | + assertEquals(label + ": No. of directories deleted", dirDelete, listener.getDeletedDirectories().size()); |
| 121 | + assertEquals(label + ": No. of files created", fileCreate, listener.getCreatedFiles().size()); |
| 122 | + assertEquals(label + ": No. of files changed", fileChange, listener.getChangedFiles().size()); |
| 123 | + assertEquals(label + ": No. of files deleted", fileDelete, listener.getDeletedFiles().size()); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Either creates a file if it doesn't exist or updates the last modified date/time |
| 128 | + * if it does. |
| 129 | + * |
| 130 | + * @param file The file to touch |
| 131 | + * @return The file |
| 132 | + */ |
| 133 | + protected File touch(File file) { |
| 134 | + long lastModified = file.exists() ? file.lastModified() : 0; |
| 135 | + try { |
| 136 | + FileUtils.touch(file); |
| 137 | + file = new File(file.getParent(), file.getName()); |
| 138 | + while (lastModified == file.lastModified()) { |
| 139 | + sleepHandleInterruped(50L); |
| 140 | + FileUtils.touch(file); |
| 141 | + file = new File(file.getParent(), file.getName()); |
| 142 | + } |
| 143 | + } catch (Exception e) { |
| 144 | + fail("Touching " + file + ": " + e); |
| 145 | + } |
| 146 | + return file; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Thread.sleep(timeInMilliseconds) - ignore InterruptedException |
| 151 | + */ |
| 152 | + protected void sleepHandleInterruped(long timeInMilliseconds) { |
| 153 | + try { |
| 154 | + Thread.sleep(timeInMilliseconds); |
| 155 | + } catch(InterruptedException ie) { |
| 156 | + // ignore |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments