pandaapo commented on code in PR #4716:
URL: https://github.com/apache/eventmesh/pull/4716#discussion_r1443988771


##########
eventmesh-common/src/test/java/org/apache/eventmesh/common/file/WatchFileManagerTest.java:
##########
@@ -40,31 +38,32 @@ public class WatchFileManagerTest {
     @Test
     public void testWatchFile() throws IOException, InterruptedException {
         String file = 
WatchFileManagerTest.class.getResource("/configuration.properties").getFile();
-        File f = new File(file);
+        File configFile = new File(file);
         File tempConfigFile = new File(tempConfigDir, 
"configuration.properties");
-        Files.copy(f.toPath(), tempConfigFile.toPath());
-
-        final FileChangeListener fileChangeListener = new FileChangeListener() 
{
+        Files.copy(configFile.toPath(), tempConfigFile.toPath());
 
-            @Override
-            public void onChanged(FileChangeContext changeContext) {
-                Assertions.assertEquals(tempConfigFile.getName(), 
changeContext.getFileName());
-                Assertions.assertEquals(tempConfigFile.getParent(), 
changeContext.getDirectoryPath());
-            }
+        final FileChangeListener mockFileChangeListener = 
Mockito.mock(FileChangeListener.class);
+        Mockito.when(mockFileChangeListener.support(
+                Mockito.argThat(isFileUnderTest(tempConfigFile.getParent(), 
tempConfigFile.getName())))
+        ).thenReturn(true);
 
-            @Override
-            public boolean support(FileChangeContext changeContext) {
-                return 
changeContext.getWatchEvent().context().toString().contains(tempConfigFile.getName());
-            }
-        };
-        
WatchFileManager.registerFileChangeListener(tempConfigFile.getParent(), 
fileChangeListener);
+        
WatchFileManager.registerFileChangeListener(tempConfigFile.getParent(), 
mockFileChangeListener);
 
         Properties properties = new Properties();
-        properties.load(new BufferedReader(new FileReader(tempConfigFile)));
-        properties.setProperty("eventMesh.server.newAdd", "newAdd");
-        FileWriter fw = new FileWriter(tempConfigFile);
-        properties.store(fw, "newAdd");
+        try (
+                BufferedReader bufferedReader = new BufferedReader(new 
FileReader(configFile));
+                FileWriter fw = new FileWriter(tempConfigFile)
+        ) {
+            properties.load(bufferedReader);
+            properties.setProperty("eventMesh.server.newAdd", "newAdd");
+            properties.store(fw, "newAdd");
+        }
+
+        Mockito.verify(mockFileChangeListener, Mockito.timeout(15_000))

Review Comment:
   Perhaps you can use `Mockito.timeout(15_000).atLeast(1)` to address 
`TooManyActualInvocations` error in certain environments. But I don't know why 
the error occurs in some environments and not in others.



##########
eventmesh-common/src/test/java/org/apache/eventmesh/common/file/WatchFileManagerTest.java:
##########
@@ -40,31 +38,32 @@ public class WatchFileManagerTest {
     @Test
     public void testWatchFile() throws IOException, InterruptedException {
         String file = 
WatchFileManagerTest.class.getResource("/configuration.properties").getFile();
-        File f = new File(file);
+        File configFile = new File(file);
         File tempConfigFile = new File(tempConfigDir, 
"configuration.properties");
-        Files.copy(f.toPath(), tempConfigFile.toPath());
-
-        final FileChangeListener fileChangeListener = new FileChangeListener() 
{
+        Files.copy(configFile.toPath(), tempConfigFile.toPath());
 
-            @Override
-            public void onChanged(FileChangeContext changeContext) {
-                Assertions.assertEquals(tempConfigFile.getName(), 
changeContext.getFileName());
-                Assertions.assertEquals(tempConfigFile.getParent(), 
changeContext.getDirectoryPath());
-            }
+        final FileChangeListener mockFileChangeListener = 
Mockito.mock(FileChangeListener.class);
+        Mockito.when(mockFileChangeListener.support(
+                Mockito.argThat(isFileUnderTest(tempConfigFile.getParent(), 
tempConfigFile.getName())))
+        ).thenReturn(true);
 
-            @Override
-            public boolean support(FileChangeContext changeContext) {
-                return 
changeContext.getWatchEvent().context().toString().contains(tempConfigFile.getName());
-            }
-        };
-        
WatchFileManager.registerFileChangeListener(tempConfigFile.getParent(), 
fileChangeListener);
+        
WatchFileManager.registerFileChangeListener(tempConfigFile.getParent(), 
mockFileChangeListener);
 
         Properties properties = new Properties();
-        properties.load(new BufferedReader(new FileReader(tempConfigFile)));
-        properties.setProperty("eventMesh.server.newAdd", "newAdd");
-        FileWriter fw = new FileWriter(tempConfigFile);
-        properties.store(fw, "newAdd");
+        try (
+                BufferedReader bufferedReader = new BufferedReader(new 
FileReader(configFile));

Review Comment:
   Should `tempConfigFile` be used to create `FileReader` here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to