Skip to content

Commit

Permalink
[#194] Fix invalid path when accessing jar resources (#196)
Browse files Browse the repository at this point in the history
RepoSense v1.1-rc2 executed as a JAR throws an InvalidPathException
when accessing templateZip.zip.

This is because the location of the templateZip.zip cannot be located
by its path on the disk but by its relative path in the JAR file. 

So let's change the way to retrieve templateZip.zip from 
RepoSense.class.getClassLoader().getResource("templateZip.zip") to 
RepoSense.class.getResourceAsStream("/templateZip.zip").
  • Loading branch information
yong24s authored and eugenepeh committed Jul 16, 2018
1 parent e2c8d55 commit d1ec823
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
4 changes: 0 additions & 4 deletions sample.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ CS2103AUG2017-W09-B1,main,master,nbriannl,Nbr,
CS2103AUG2017-W09-B1,main,master,zacharytang,Zac,Zachary Tang
CS2103AUG2017-W09-B1,main,master,April0616,Fan,LAPTOP-7KFM2KSP\User;Fan Yuting
CS2103AUG2017-W09-B1,main,master,CindyTsai1,Cin,YuHsuan
CS2103AUG2017-W09-B2,main,master,charlesgoh,Cha,Charles Goh
CS2103AUG2017-W09-B2,main,master,Esilocke,Esi,
CS2103AUG2017-W09-B2,main,master,jeffreygohkw,Jef,Jeffrey Goh
CS2103AUG2017-W09-B2,main,master,wangyiming1019,Wan,ACER\kyle;Wang Yiming
CS2103JAN2018-F14-B1,main,master,lithiumlkid,Ahm,
CS2103JAN2018-F14-B1,main,master,codeeong,Cod,
CS2103JAN2018-F14-B1,main,master,jordancjq,Jor,
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/reposense/report/ReportGenerator.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package reposense.report;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
Expand All @@ -24,15 +24,17 @@ public class ReportGenerator {
private static final Logger logger = LogsManager.getLogger(ReportGenerator.class);

// zip file which contains all the dashboard template files
private static final String TEMPLATE_FILE = new File(RepoSense.class.getClassLoader()
.getResource("templateZip.zip").getFile()).toString();
private static final String TEMPLATE_FILE = "/templateZip.zip";

/**
* Generates the authorship and commits JSON file for each repo in {@code configs} at {@code outputPath}, as
* well as the summary JSON file of all the repos.
*
* @throws IOException if templateZip.zip does not exists in jar file.
*/
public static void generateReposReport(List<RepoConfiguration> configs, String outputPath) {
FileUtil.copyTemplate(TEMPLATE_FILE, outputPath);
public static void generateReposReport(List<RepoConfiguration> configs, String outputPath) throws IOException {
InputStream is = RepoSense.class.getResourceAsStream(TEMPLATE_FILE);
FileUtil.copyTemplate(is, outputPath);

for (RepoConfiguration config : configs) {
Path repoReportDirectory = Paths.get(outputPath, config.getDisplayName());
Expand Down
27 changes: 17 additions & 10 deletions src/main/java/reposense/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,21 @@ public static void zip(Path sourcePath, Path outputPath, String... fileTypes) {

/**
* Unzips the contents of the {@code zipSourcePath} into {@code outputPath}.
* @throws IOException if {@code zipSourcePath} is an invalid path.
*/
public static void unzip(Path zipSourcePath, Path outputPath) {
ZipEntry entry;
try (
InputStream is = Files.newInputStream(zipSourcePath);
ZipInputStream zis = new ZipInputStream(is)
) {
public static void unzip(Path zipSourcePath, Path outputPath) throws IOException {
try (InputStream is = Files.newInputStream(zipSourcePath)) {
unzip(is, outputPath);
}
}

/**
* Unzips the contents of the {@code is} into {@code outputPath}.
* @throws IOException if {@code is} refers to an invalid path.
*/
public static void unzip(InputStream is, Path outputPath) throws IOException {
try (ZipInputStream zis = new ZipInputStream(is)) {
ZipEntry entry;
Files.createDirectories(outputPath);
while ((entry = zis.getNextEntry()) != null) {
Path path = Paths.get(outputPath.toString(), entry.getName());
Expand All @@ -129,16 +137,15 @@ public static void unzip(Path zipSourcePath, Path outputPath) {
}
zis.closeEntry();
}
} catch (IOException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
}

/**
* Copies the template files from {@code sourcePath} to the {@code outputPath}.
* @throws IOException if {@code is} refers to an invalid path.
*/
public static void copyTemplate(String sourcePath, String outputPath) {
FileUtil.unzip(Paths.get(sourcePath), Paths.get(outputPath));
public static void copyTemplate(InputStream is, String outputPath) throws IOException {
FileUtil.unzip(is, Paths.get(outputPath));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/reposense/util/FileUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void unzip_validZipFile_success() throws IOException {
}

@Test
public void unzip_invalidZipFile_fail() {
public void unzip_invalidZipFile_fail() throws IOException {
Path invalidZipFile = Paths.get(FILE_UTIL_TEST_DIRECTORY.toString(), "test.csv");
FileUtil.unzip(invalidZipFile, FILE_UTIL_TEST_DIRECTORY);
Assert.assertFalse(Files.exists(Paths.get(FILE_UTIL_TEST_DIRECTORY.toString(), "test")));
Expand Down

0 comments on commit d1ec823

Please sign in to comment.