Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File data source lists names of files only if they start with basename #2005

Merged
merged 1 commit into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -205,21 +205,22 @@ private void testExportImportCgmes(Network network, ReadOnlyDataSource originalD

Path path = fs.getPath("temp-export-cgmes");
Files.createDirectories(path);
new CgmesExport().export(network, exportParams, new FileDataSource(path, "bar"));
String baseName = "bar";
new CgmesExport().export(network, exportParams, new FileDataSource(path, baseName));

DataSource ds = new FileDataSource(path, "bar");
String expected = originalDs.listNames(".*EQ.*").stream().filter(name -> !name.contains("BD")).findFirst().orElseThrow(() -> new CgmesModelException("Should contain EQ profile"));
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(network.getNameOrId() + "_EQ.xml", false));
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(baseName + "_EQ.xml", false));
InputStream in = originalDs.newInputStream(expected)) {
ByteStreams.copy(in, out);
}
expected = originalDs.listNames(".*TP.*").stream().filter(name -> !name.contains("BD")).findFirst().orElseThrow(() -> new CgmesModelException("Should contain TP profile"));
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(network.getNameOrId() + "_TP.xml", false));
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(baseName + "_TP.xml", false));
InputStream in = originalDs.newInputStream(expected)) {
ByteStreams.copy(in, out);
}
for (String boundary : originalDs.listNames(".*BD.*")) {
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(boundary, false));
try (OutputStream out = new BufferedOutputStream(ds.newOutputStream(baseName + boundary, false));
InputStream in = originalDs.newInputStream(boundary)) {
ByteStreams.copy(in, out);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public Set<String> listNames(String regex) throws IOException {
.filter(Files::isRegularFile)
.map(Path::getFileName)
.map(Path::toString)
.filter(name -> name.startsWith(baseName))
// Return names after removing the compression extension
.map(name -> name.replace(getCompressionExt(), ""))
.filter(s -> p.matcher(s).matches())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
*/
package com.powsybl.commons.datasource;

import org.junit.Test;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Set;

import static org.junit.Assert.*;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
Expand All @@ -15,4 +25,66 @@ public class FileDataSourceTest extends AbstractDataSourceTest {
protected DataSource createDataSource() {
return new FileDataSource(testDir, getBaseName());
}

@Test
public void listNamesTest() throws IOException {
// Create a couple of files in the test folder
// One using the basename
String validFilename = getBaseName() + ".txt";
try (OutputStream os = Files.newOutputStream(testDir.resolve(validFilename))) {
os.write("basename_line".getBytes(StandardCharsets.UTF_8));
}
// Other that has a different name
String otherFilename = "other.txt";
try (OutputStream os = Files.newOutputStream(testDir.resolve(otherFilename))) {
os.write("other_line".getBytes(StandardCharsets.UTF_8));
}

// A file data source created using the complete filename does not return other filenames
Set<String> names = new FileDataSource(testDir, getBaseName() + ".txt").listNames(".*");
assertEquals(1, names.size());
assertTrue(names.contains(validFilename));
assertFalse(names.contains(otherFilename));

// A file data source created using the test folder and the basename sees only the right file
names = new FileDataSource(testDir, getBaseName()).listNames(".*");
assertEquals(1, names.size());
assertTrue(names.contains(validFilename));
assertFalse(names.contains(otherFilename));
}

@Test
public void createNewFilesTest() throws IOException {
DataSource ds = createDataSource();

// use the data source to write a file that contains basename
String suffix = "suffix";
String ext = "ext";
try (OutputStream os = ds.newOutputStream(suffix, ext, false)) {
os.write("line".getBytes(StandardCharsets.UTF_8));
}

// it is allowed to use the data source to write a file that does not contain the basename
try (OutputStream os = ds.newOutputStream("dummy.txt", false)) {
os.write("dummy_line".getBytes(StandardCharsets.UTF_8));
}

// write another file in the same directory of data source that does not contain the basename
// do not use the data source, just write in the same directory
try (OutputStream os = Files.newOutputStream(testDir.resolve("dummy2.txt"))) {
os.write("dummy2_line".getBytes(StandardCharsets.UTF_8));
}

// check the three files exists when checked through the data source
assertTrue(ds.exists(suffix, ext));
assertTrue(ds.exists("dummy.txt"));
assertTrue(ds.exists("dummy2.txt"));

// but only the files that contain the basename can be accessed through list names
Set<String> names = ds.listNames(".*");
assertEquals(1, names.size());
assertTrue(names.contains(getBaseName() + suffix + "." + ext));
assertFalse(names.contains("dummy.txt"));
assertFalse(names.contains("dummy2.txt"));
}
}