Skip to content

Commit

Permalink
[ISSUE #3168] IO streams closed, encoding set to UTF_8 (#3225)
Browse files Browse the repository at this point in the history
  • Loading branch information
DimuthuWeerathunga authored Feb 21, 2023
1 parent a4bfa6b commit f30f720
Showing 1 changed file with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
import org.apache.eventmesh.common.utils.ThreadUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

Expand All @@ -33,29 +36,36 @@
public class WatchFileManagerTest {

@Test
public void testWatchFile() throws IOException, InterruptedException {
String file = WatchFileManagerTest.class.getResource("/configuration.properties").getFile();
File f = new File(file);
public void testWatchFile() {
String resourceUrl = WatchFileManagerTest.class.getResource("/configuration.properties").getFile();
File file = new File(resourceUrl);
final FileChangeListener fileChangeListener = new FileChangeListener() {
@Override
public void onChanged(FileChangeContext changeContext) {
Assert.assertEquals(f.getName(), changeContext.getFileName());
Assert.assertEquals(f.getParent(), changeContext.getDirectoryPath());
Assert.assertEquals(file.getName(), changeContext.getFileName());
Assert.assertEquals(file.getParent(), changeContext.getDirectoryPath());
}

@Override
public boolean support(FileChangeContext changeContext) {
return changeContext.getWatchEvent().context().toString().contains(f.getName());
return changeContext.getWatchEvent().context().toString().contains(file.getName());
}
};
WatchFileManager.registerFileChangeListener(f.getParent(), fileChangeListener);
WatchFileManager.registerFileChangeListener(file.getParent(), fileChangeListener);

Path path = Paths.get(resourceUrl);
Properties properties = new Properties();
properties.load(new BufferedReader(new FileReader(file)));
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
properties.load(reader);
} catch (IOException e) {
Assert.fail("Test failed to load from file");
}
properties.setProperty("eventMesh.server.newAdd", "newAdd");
FileWriter fw = new FileWriter(file);
properties.store(fw, "newAdd");

try (BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
properties.store(writer, "newAdd");
} catch (IOException e) {
Assert.fail("Test failed to write to file");
}
ThreadUtils.sleep(500, TimeUnit.MILLISECONDS);
}
}

0 comments on commit f30f720

Please sign in to comment.