xref: /aosp_15_r20/external/apache-commons-io/src/test/java/org/apache/commons/io/monitor/AbstractMonitorTest.java (revision 0c4d7b72e49a04598d65c566f44504b95342d75a)
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 static org.apache.commons.io.test.TestUtils.sleepQuietly;
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22 import static org.junit.jupiter.api.Assertions.fail;
23 
24 import java.io.File;
25 import java.io.FileFilter;
26 import java.io.IOException;
27 
28 import org.apache.commons.io.FileUtils;
29 import org.apache.commons.io.filefilter.FileFilterUtils;
30 import org.apache.commons.io.filefilter.HiddenFileFilter;
31 import org.apache.commons.io.filefilter.IOFileFilter;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.io.TempDir;
34 
35 /**
36  * {@link FileAlterationObserver} Test Case.
37  */
38 public abstract class AbstractMonitorTest {
39 
40     /** File observer */
41     protected FileAlterationObserver observer;
42 
43     /** Listener which collects file changes */
44     protected CollectionFileListener listener;
45 
46     /** Directory for test files */
47     @TempDir
48     protected File testDir;
49 
50     /** Time in milliseconds to pause in tests */
51     protected final long pauseTime = 100L;
52 
53     /**
54      * Check all the Collections are empty
55      *
56      * @param label the label to use for this check
57      */
checkCollectionsEmpty(final String label)58     protected void checkCollectionsEmpty(final String label) {
59         checkCollectionSizes("EMPTY-" + label, 0, 0, 0, 0, 0, 0);
60     }
61 
62     /**
63      * Check all the Collections have the expected sizes.
64      *
65      * @param label the label to use for this check
66      * @param dirCreate expected number of dirs created
67      * @param dirChange expected number of dirs changed
68      * @param dirDelete expected number of dirs deleted
69      * @param fileCreate expected number of files created
70      * @param fileChange expected number of files changed
71      * @param fileDelete expected number of files deleted
72      */
checkCollectionSizes(String label, final int dirCreate, final int dirChange, final int dirDelete, final int fileCreate, final int fileChange, final int fileDelete)73     protected void checkCollectionSizes(String label,
74                                         final int dirCreate,
75                                         final int dirChange,
76                                         final int dirDelete,
77                                         final int fileCreate,
78                                         final int fileChange,
79                                         final int fileDelete) {
80         label = label + "[" + listener.getCreatedDirectories().size() +
81                         " " + listener.getChangedDirectories().size() +
82                         " " + listener.getDeletedDirectories().size() +
83                         " " + listener.getCreatedFiles().size() +
84                         " " + listener.getChangedFiles().size() +
85                         " " + listener.getDeletedFiles().size() + "]";
86         assertEquals(dirCreate, listener.getCreatedDirectories().size(), label + ": No. of directories created");
87         assertEquals(dirChange, listener.getChangedDirectories().size(), label + ": No. of directories changed");
88         assertEquals(dirDelete, listener.getDeletedDirectories().size(), label + ": No. of directories deleted");
89         assertEquals(fileCreate, listener.getCreatedFiles().size(), label + ": No. of files created");
90         assertEquals(fileChange, listener.getChangedFiles().size(), label + ": No. of files changed");
91         assertEquals(fileDelete, listener.getDeletedFiles().size(), label + ": No. of files deleted");
92     }
93 
94     /**
95      * Create a {@link FileAlterationObserver}.
96      *
97      * @param file The directory to observe
98      * @param fileFilter The file filter to apply
99      */
createObserver(final File file, final FileFilter fileFilter)100     protected void createObserver(final File file, final FileFilter fileFilter) {
101         observer = new FileAlterationObserver(file, fileFilter);
102         observer.addListener(listener);
103         observer.addListener(new FileAlterationListenerAdaptor());
104         try {
105             observer.initialize();
106         } catch (final Exception e) {
107             fail("Observer init() threw " + e);
108         }
109     }
110 
111     @BeforeEach
setUp()112     public void setUp() {
113         final IOFileFilter files = FileFilterUtils.fileFileFilter();
114         final IOFileFilter javaSuffix = FileFilterUtils.suffixFileFilter(".java");
115         final IOFileFilter fileFilter = FileFilterUtils.and(files, javaSuffix);
116 
117         final IOFileFilter directories = FileFilterUtils.directoryFileFilter();
118         final IOFileFilter visible = HiddenFileFilter.VISIBLE;
119         final IOFileFilter dirFilter = FileFilterUtils.and(directories, visible);
120 
121         final IOFileFilter filter = FileFilterUtils.or(dirFilter, fileFilter);
122 
123         createObserver(testDir, filter);
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      * @throws IOException if an I/O error occurs.
133      */
touch(File file)134     protected File touch(File file) throws IOException {
135         final long lastModified = file.exists() ? FileUtils.lastModified(file) : 0;
136         try {
137             FileUtils.touch(file);
138             assertTrue(file.exists());
139             file = new File(file.getParent(), file.getName());
140             while (lastModified == FileUtils.lastModified(file)) {
141                 sleepQuietly(pauseTime);
142                 FileUtils.touch(file);
143                 file = new File(file.getParent(), file.getName());
144             }
145         } catch (final Exception e) {
146             fail("Touching " + file + ": " + e);
147         }
148         sleepQuietly(pauseTime);
149         return file;
150     }
151 
152 }
153