Skip to content

Commit

Permalink
Fixes the problem introduced in RPTools#1215
Browse files Browse the repository at this point in the history
getFileAsInputStream was using a try with resources, so it was always returning a closed InputStream.

Also, added a couple of unit tests as a starting point for properly testing this class.

Refers to RPTools#1215
  • Loading branch information
emmebi committed Feb 22, 2020
1 parent 5f448a0 commit fd08842
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,6 @@ workbench.xmi

# Keystore
build-resources/rptools-keystore
/log.html
/report.html
/output.xml
13 changes: 6 additions & 7 deletions src/main/java/net/rptools/lib/io/PackedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,12 @@ public InputStream getFileAsInputStream(String path) throws IOException {
ZipEntry entry = new ZipEntry(path);
ZipFile zipFile = getZipFile();

try (InputStream in = zipFile.getInputStream(entry)) {
if (in == null) throw new FileNotFoundException(path);
String type = FileUtil.getContentType(in);
if (log.isDebugEnabled() && type != null)
log.debug("FileUtil.getContentType() returned " + type);
return in;
}
InputStream in = zipFile.getInputStream(entry);
if (in == null) throw new FileNotFoundException(path);
String type = FileUtil.getContentType(in);
if (log.isDebugEnabled() && type != null)
log.debug("FileUtil.getContentType() returned " + type);
return in;
}

public void close() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* This software Copyright by the RPTools.net development team, and
* licensed under the Affero GPL Version 3 or, at your option, any later
* version.
*
* MapTool Source Code is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public
* License * along with this source Code. If not, please visit
* <http://www.gnu.org/licenses/> and specifically the Affero license
* text at <http://www.gnu.org/licenses/agpl.html>.
*/
package net.rptools.lib.swing.preference.net.rptools.lib.io;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import net.rptools.lib.io.PackedFile;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

public class PackedFileTest {

public static final String A_PATH_TXT = "a_path.txt";
public static final String PACKED_TEST_FILE = "packedTestFile";
private static int counter = 1;

@Test
public void emptySave(@TempDir File tempDir) throws IOException {
File f = new File(tempDir, PACKED_TEST_FILE);
PackedFile pf = new PackedFile(f);
pf.save();
assertTrue(f.exists());
}

@Test
public void saveWithOneResource(@TempDir File tempDir) throws IOException {
File f = new File(tempDir, PACKED_TEST_FILE);
String test_content = "some content";
try (PackedFile pf = new PackedFile(f)) {
pf.putFile(A_PATH_TXT, test_content.getBytes());
pf.save();
}

try (PackedFile loaded = new PackedFile(f)) {
InputStream is = loaded.getFileAsInputStream(A_PATH_TXT);
String s = new String(is.readAllBytes());
assertEquals(test_content, s);
}
}
}

0 comments on commit fd08842

Please sign in to comment.