Skip to content

Commit

Permalink
add Resource.java
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkhala committed Apr 30, 2024
1 parent 520a25d commit e19a1a8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/davidkhala/common/Resource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package davidkhala.common;

import com.google.common.io.Resources;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;

public class Resource {
public static File getFile(String name) throws URISyntaxException {
URL url = Resources.getResource(name);
return new File(url.toURI());
}
}
18 changes: 18 additions & 0 deletions src/test/java/FileTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import davidkhala.common.FileTool;
import davidkhala.common.Resource;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class FileTest {
@Test
Expand All @@ -30,4 +32,20 @@ public void fromURITest() {
File file = new File(uri);
assert file.toURI().toString().equals(uriStr);
}

@Test
public void getTestResourceFile() throws URISyntaxException, IOException {
String notFound = "fixture";
try {
Resource.getFile(notFound);
} catch (java.lang.IllegalArgumentException e) {
assert e.getMessage().equals("resource %s not found.".formatted(notFound));
}
String name = ".env";
File file = Resource.getFile(".env");
assert name.equals(file.getName());
String content= FileTool.read(file);
assert content.equals("module=test");

}
}
1 change: 1 addition & 0 deletions src/test/resources/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module=test

0 comments on commit e19a1a8

Please sign in to comment.