Skip to content

Commit

Permalink
added ability to add an entire folder to an in-memory template
Browse files Browse the repository at this point in the history
  • Loading branch information
drallgood committed Dec 26, 2015
1 parent ed4f1ba commit 2bc5f3a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
Expand Up @@ -48,7 +48,7 @@ public void provisionPassAtDirectory(File tempPassDir) throws IOException {
@Override
public Map<String, ByteBuffer> getAllFiles() throws IOException {
Map<String, ByteBuffer> allFiles = new HashMap<>();
for (File file : new File(pathToTemplateDirectory).listFiles()) {
for (File file : FileUtils.listFiles(new File(pathToTemplateDirectory), null, true)) {
byte[] byteArray = IOUtils.toByteArray(new FileInputStream(file));
String filePath = file.getAbsolutePath().replace(pathToTemplateDirectory, "");
allFiles.put(filePath, ByteBuffer.wrap(byteArray));
Expand Down
Expand Up @@ -22,6 +22,9 @@
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -102,6 +105,20 @@ public void addFile(String pathInTemplate, Locale locale, URL contentURL) throws
addFile(pathForLocale(pathInTemplate, locale), contentURL.openStream());
}

public void addAllFiles(String directoryWithFilesToAdd) throws IOException {
File directoryWithFilesToAddAsFile = new File(directoryWithFilesToAdd);
if (!directoryWithFilesToAddAsFile.isDirectory()) {
throw new IllegalArgumentException("Provided file is not a directory");
}

Path pathToSourceFolder = Paths.get(directoryWithFilesToAddAsFile.getAbsolutePath());
Collection<File> filesInDir = FileUtils.listFiles(directoryWithFilesToAddAsFile, null, true);
for (File file : filesInDir) {
Path relativePathOfFile = pathToSourceFolder.relativize(Paths.get(file.getAbsolutePath()));
addFile(relativePathOfFile.toString(), file);
}
}

public Map<String, InputStream> getFiles() {
return files;
}
Expand Down
Expand Up @@ -56,7 +56,7 @@ public void provisionTest() throws IOException, URISyntaxException {
public void test_getAllFiles() throws IOException, URISyntaxException {
Map<String, ByteBuffer> allFiles = pkPassTemplateFolder.getAllFiles();
Assert.assertNotNull(allFiles);
Assert.assertEquals(allFiles.size(), 6);
Assert.assertEquals(allFiles.size(), 8);

File templateFolder = new File(PASS_TEMPLATE_FOLDER);
for (Entry<String, ByteBuffer> entry : allFiles.entrySet()) {
Expand Down
Expand Up @@ -40,6 +40,8 @@

public class PKPassTemplateInMemoryTest {

private static final String PASS_TEMPLATE_FOLDER = PKPassTemplateFolderTest.class.getClassLoader().getResource("StoreCard.raw").getPath();

private PKPassTemplateInMemory pkPassTemplateInMemory;

@BeforeMethod
Expand Down Expand Up @@ -123,6 +125,14 @@ public void addFile_fromURL_withLocale() throws IOException {
Assert.assertTrue(IOUtils.contentEquals(url.openStream(), files.get("en.lproj/" + PKPassTemplateInMemory.PK_ICON_RETINA)));
}

@Test
public void addAllFiles() throws IOException {

pkPassTemplateInMemory.addAllFiles(PASS_TEMPLATE_FOLDER);
Map<String, InputStream> files = pkPassTemplateInMemory.getFiles();
Assert.assertEquals(files.size(), 8);
}

@Test
public void provisionPass() throws IOException {

Expand All @@ -135,11 +145,10 @@ public void provisionPass() throws IOException {
Assert.assertEquals(createdFiles.size(), 2);
}


@Test
public void test_getAllFiles() throws IOException, URISyntaxException {
prepareTemplate();

Map<String, ByteBuffer> allFiles = pkPassTemplateInMemory.getAllFiles();
Assert.assertNotNull(allFiles);
Assert.assertEquals(allFiles.size(), 2);
Expand All @@ -148,7 +157,7 @@ public void test_getAllFiles() throws IOException, URISyntaxException {
Assert.assertTrue(entry.getValue().remaining() > 0);
}
}

private void prepareTemplate() throws IOException {
// icon
URL iconFileURL = PKPassTemplateInMemoryTest.class.getClassLoader().getResource("StoreCard.raw/icon@2x.png");
Expand All @@ -158,5 +167,5 @@ private void prepareTemplate() throws IOException {
// icon for language
pkPassTemplateInMemory.addFile(PKPassTemplateInMemory.PK_ICON_RETINA, Locale.ENGLISH, iconFile);
}

}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2bc5f3a

Please sign in to comment.