tylerbertrand commented on code in PR #4716:
URL: https://github.com/apache/eventmesh/pull/4716#discussion_r1444870175
##########
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:
Because creating a new `FileWriter` actually clears the file, the temp
config file was empty when `Properties.load()` was called, so I instead created
the reader on the original config file to ensure that all properties were read.
I'll just split this into two try-with-resources blocks instead - I think that
is a better way to handle it. The first block will create the buffered reader
and load the properties from the temp config file, the second will create the
file writer and write the updated properties.
--
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]