private void addNestedEntries(List<Archive> lib) { // The parent archive might have "BOOT-INF/lib/" and "BOOT-INF/classes/" // directories, meaning we are running from an executable JAR. We add nested // entries from there with low priority (i.e. at end). try { lib.addAll(this.parent.getNestedArchives(new EntryFilter() { @Override public boolean matches(Entry entry) { if (entry.isDirectory()) { return entry.getName().startsWith(JarLauncher.BOOT_INF_CLASSES); } return entry.getName().startsWith(JarLauncher.BOOT_INF_LIB); } })); } catch (IOException ex) { // Ignore } }
@Test public void getFilteredArchive() throws Exception { Archive filteredArchive = this.archive .getFilteredArchive(new Archive.EntryRenameFilter() { @Override public AsciiBytes apply(AsciiBytes entryName, Entry entry) { if (entryName.toString().equals("1.dat")) { return entryName; } return null; } }); Map<String, Entry> entries = getEntriesMap(filteredArchive); assertThat(entries.size(), equalTo(1)); URLClassLoader classLoader = new URLClassLoader( new URL[] { filteredArchive.getUrl() }); assertThat(classLoader.getResourceAsStream("1.dat").read(), equalTo(1)); assertThat(classLoader.getResourceAsStream("2.dat"), nullValue()); classLoader.close(); }
@Override public List<Archive> getNestedArchives(final EntryFilter filter) throws IOException { return this.parent.getNestedArchives(new EntryFilter() { @Override public boolean matches(Entry entry) { return FilteredArchive.this.filter.matches(entry) && filter.matches(entry); } }); }
@Override protected List<Archive> getClassPathArchives() throws Exception { List<Archive> archives = new ArrayList<Archive>( this.archive.getNestedArchives(new EntryFilter() { @Override public boolean matches(Entry entry) { return isNestedArchive(entry); } })); postProcessClassPathArchives(archives); return archives; }
@Test public void getNestedArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); assertThat(nested.getUrl().toString()) .isEqualTo("jar:" + this.rootFolder.toURI() + "nested.jar!/"); }
@Test public void nestedDirArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("d/"); Archive nested = this.archive.getNestedArchive(entry); Map<String, Entry> nestedEntries = getEntriesMap(nested); assertThat(nestedEntries.size()).isEqualTo(1); assertThat(nested.getUrl().toString()) .isEqualTo("file:" + this.rootFolder.toURI().getPath() + "d/"); }
@Test public void getNonRecursiveManifest() throws Exception { ExplodedArchive archive = new ExplodedArchive( new File("src/test/resources/root")); assertThat(archive.getManifest()).isNotNull(); Map<String, Archive.Entry> entries = getEntriesMap(archive); assertThat(entries.size()).isEqualTo(4); }
@Test public void getNonRecursiveManifestEvenIfNonRecursive() throws Exception { ExplodedArchive archive = new ExplodedArchive(new File("src/test/resources/root"), false); assertThat(archive.getManifest()).isNotNull(); Map<String, Archive.Entry> entries = getEntriesMap(archive); assertThat(entries.size()).isEqualTo(3); }
private Map<String, Archive.Entry> getEntriesMap(Archive archive) { Map<String, Archive.Entry> entries = new HashMap<String, Archive.Entry>(); for (Archive.Entry entry : archive) { entries.put(entry.getName().toString(), entry); } return entries; }
@Test public void getNestedArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); assertThat(nested.getUrl().toString()) .isEqualTo("jar:" + this.rootJarFileUrl + "!/nested.jar!/"); }
@Test public void getNestedUnpackedArchive() throws Exception { setup(true); Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); assertThat(nested.getUrl().toString()).startsWith("file:"); assertThat(nested.getUrl().toString()).endsWith("/nested.jar"); }
@Test public void unpackedLocationsAreUniquePerArchive() throws Exception { setup(true); Entry entry = getEntriesMap(this.archive).get("nested.jar"); URL firstNested = this.archive.getNestedArchive(entry).getUrl(); setup(true); entry = getEntriesMap(this.archive).get("nested.jar"); URL secondNested = this.archive.getNestedArchive(entry).getUrl(); assertThat(secondNested).isNotEqualTo(firstNested); }
@Test public void getNestedArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); assertThat(nested.getUrl().toString(), equalTo("jar:" + this.rootFolder.toURI() + "nested.jar!/")); }
@Test public void nestedDirArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("d/"); Archive nested = this.archive.getNestedArchive(entry); Map<String, Entry> nestedEntries = getEntriesMap(nested); assertThat(nestedEntries.size(), equalTo(1)); assertThat(nested.getUrl().toString(), equalTo("file:" + this.rootFolder.toURI().getPath() + "d/")); }
@Test public void getNonRecursiveManifest() throws Exception { ExplodedArchive archive = new ExplodedArchive( new File("src/test/resources/root")); assertNotNull(archive.getManifest()); Map<String, Archive.Entry> entries = getEntriesMap(archive); assertThat(entries.size(), equalTo(4)); }
@Test public void getNonRecursiveManifestEvenIfNonRecursive() throws Exception { ExplodedArchive archive = new ExplodedArchive(new File("src/test/resources/root"), false); assertNotNull(archive.getManifest()); Map<String, Archive.Entry> entries = getEntriesMap(archive); assertThat(entries.size(), equalTo(3)); }
private Map<String, Archive.Entry> getEntriesMap(Archive archive) { Map<String, Archive.Entry> entries = new HashMap<String, Archive.Entry>(); for (Archive.Entry entry : archive.getEntries()) { entries.put(entry.getName().toString(), entry); } return entries; }
@Test public void getNestedArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); assertThat(nested.getUrl().toString(), equalTo("jar:" + this.rootJarFileUrl + "!/nested.jar!/")); }
@Test public void getNestedUnpackedArchive() throws Exception { setup(true); Entry entry = getEntriesMap(this.archive).get("nested.jar"); Archive nested = this.archive.getNestedArchive(entry); assertThat(nested.getUrl().toString(), startsWith("file:")); assertThat(nested.getUrl().toString(), endsWith("/nested.jar")); }
@Test public void unpackedLocationsAreUniquePerArchive() throws Exception { setup(true); Entry entry = getEntriesMap(this.archive).get("nested.jar"); URL firstNested = this.archive.getNestedArchive(entry).getUrl(); setup(true); entry = getEntriesMap(this.archive).get("nested.jar"); URL secondNested = this.archive.getNestedArchive(entry).getUrl(); assertThat(secondNested, is(not(equalTo(firstNested)))); }
@Test public void getFilteredArchive() throws Exception { Archive filteredArchive = this.archive .getFilteredArchive(new Archive.EntryRenameFilter() { @Override public AsciiBytes apply(AsciiBytes entryName, Entry entry) { if (entryName.toString().equals("1.dat")) { return entryName; } return null; } }); Map<String, Entry> entries = getEntriesMap(filteredArchive); assertThat(entries.size(), equalTo(1)); }
@Override public boolean matches(Entry entry) { return entry.getName().startsWith(this.prefix) && this.filter.matches(entry); }
@Override public boolean matches(Entry entry) { return entry.getName().endsWith(DOT_JAR) || entry.getName().endsWith(DOT_ZIP); }
@Override public Iterator<Entry> iterator() { throw new UnsupportedOperationException(); }
@Test public void getEntries() throws Exception { Map<String, Archive.Entry> entries = getEntriesMap(this.archive); assertThat(entries.size()).isEqualTo(10); }