Skip to content

Commit

Permalink
Add tests for MFile::relativize
Browse files Browse the repository at this point in the history
  • Loading branch information
Tara Drwenski committed Sep 30, 2024
1 parent 82b1fb3 commit 3fedc92
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cdm/core/src/test/java/thredds/filesystem/TestMFileOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public void shouldGetChildMFile() {
assertThat(newMFile.getParent().getPath()).isEqualTo(mFile.getPath());
assertThat(newMFile.getPath()).isEqualTo(Paths.get(mFile.getPath(), "newFile").toString());
}

@Test
public void shouldGetRelativePath() {
final MFileOS mFile = new MFileOS("/an/absolute/path/a/");
final MFileOS mFile2 = new MFileOS("/an/absolute/path/foo/bar/");
assertThat(mFile.relativize(mFile2)).isEqualTo("../foo/bar");
assertThat(mFile2.relativize(mFile)).isEqualTo("../../a");
}
}

private static File createTemporaryFile(int size) throws IOException {
Expand Down
8 changes: 8 additions & 0 deletions cdm/core/src/test/java/thredds/filesystem/TestMFileOS7.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ public void shouldGetInputStream() throws IOException {
assertThat(inputStream.read()).isNotEqualTo(-1);
}
}

@Test
public void shouldGetRelativePath() throws IOException {
final MFileOS7 mFile = new MFileOS7(Paths.get("/an/absolute/path"), null);
final MFileOS7 mFile2 = new MFileOS7(Paths.get("/an/absolute/path/foo"), null);
assertThat(mFile.relativize(mFile2)).isEqualTo("foo");
assertThat(mFile2.relativize(mFile)).isEqualTo("..");
}
}

private static File createTemporaryFile(int size) throws IOException {
Expand Down
35 changes: 35 additions & 0 deletions cdm/s3/src/test/java/thredds/inventory/s3/TestMFileS3.java
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,42 @@ private void checkS3MFilesAuxInfo(String uri) throws IOException {
@RunWith(Parameterized.class)
public static class TestMFileS3Parameterized {

@Parameterized.Parameters(name = "{0}, {1}, {2}")
public static List<Object[]> getTestParameters() {
return Arrays.asList(new Object[][] {

{"cdms3:bucket?a/long/key/", "cdms3:bucket?a/long/", ""},
{"cdms3:bucket?a/long/key/#delimiter=/", "cdms3:otherBucket?a/long/#delimiter=/", ""},
{"cdms3:bucket?a/long/#delimiter=/", "cdms3:bucket?a/long/key", ""},
{"cdms3:bucket", "cdms3:bucket?a/long/key", ""}, {"cdms3:bucket?a/long/key", "cdms3:bucket", ""},

{"cdms3:bucket?a/long/#delimiter=/", "cdms3:bucket?a/long/key/#delimiter=/", "key"},
{"cdms3:bucket?a/long/#delimiter=/", "cdms3:bucket?a/long/key#delimiter=/", "key"},
{"cdms3:bucket?a/long#delimiter=/", "cdms3:bucket?a/long/key/#delimiter=/", "key"},
{"cdms3:bucket?a/long#delimiter=/", "cdms3:bucket?a/long/key#delimiter=/", "key"},

{"cdms3:bucket?a/long/key/#delimiter=/", "cdms3:bucket?a/long/key/#delimiter=/", ""},
{"cdms3:bucket?a/long/key#delimiter=/", "cdms3:bucket?a/long#delimiter=/", ".."},
{"cdms3:bucket?a/long#delimiter=/", "cdms3:bucket?a/long_key#delimiter=/", "../long_key"},

});
}

@Parameterized.Parameter(value = 0)
public String uri1;

@Parameterized.Parameter(value = 1)
public String uri2;

@Parameterized.Parameter(value = 2)
public String expectedRelativePath;

@Test
public void shouldRelativizeKey() throws URISyntaxException, IOException {
final MFile mFile = new MFileS3(uri1);
final MFile mFile2 = new MFileS3(uri2);
assertThat(mFile.relativize(mFile2)).isEqualTo(expectedRelativePath);
}
}

}

0 comments on commit 3fedc92

Please sign in to comment.