private static void handleArchive(final List<ClassPathRoot> rs, final File f) throws IOException { try { if (!f.canRead()) { throw new IOException("Can't read the file " + f); } rs.add(new ArchiveClassPathRoot(f)); } catch (final ZipException ex) { LOG.warning("Can't open the archive " + f); } }
@Override public Collection<String> classNames() { final List<String> names = new ArrayList<>(); final ZipFile root = getRoot(); try { final Enumeration<? extends ZipEntry> entries = root.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); if (!entry.isDirectory() && entry.getName().endsWith(".class")) { names.add(stringToClassName(entry.getName())); } } return names; } finally { closeQuietly(root); } }
@Override public URL getResource(final String name) throws MalformedURLException { final ZipFile zip = getRoot(); try { final ZipEntry entry = zip.getEntry(name); if (entry != null) { return new URL("jar:file:" + zip.getName() + "!/" + entry.getName()); } else { return null; } } finally { closeQuietly(zip); } }
@Test public void shouldReturnAReadableInputStream() { final byte b[] = new byte[100]; try { final InputStream actual = this.testee.getData("injar.p1.P1Test"); actual.read(b); } catch (final IOException ex) { fail(); } }
@Override public InputStream getData(final String name) throws IOException { try (ZipFile zip = getRoot()) { final ZipEntry entry = zip.getEntry(name.replace('.', '/') + ".class"); if (entry == null) { return null; } return StreamUtil.copyStream(zip.getInputStream(entry)); } }
@Test public void getResourceShouldReturnNullForAnUnknownResource() throws Exception { assertNull(this.testee.getResource("bar")); }
@Test public void classNamesShouldReturnAllClassNamesIArchive() { final Collection<String> expected = Arrays.asList( "injar.p1.P1NoTest$InnerTest", "injar.p1.P1NoTest", "injar.p1.P1Test", "injar.p2.P2Test"); assertEquals(expected, this.testee.classNames()); }
@Override public URL getResource(final String name) throws MalformedURLException { final ZipFile zip = getRoot(); try { final ZipEntry entry = zip.getEntry(name); if (entry != null) { return new URL("jar:file:" + zip.getName() + "!/" + entry.getName()); } else { return null; } } finally { closeQuietly(zip); } }
@Test public void getDataShouldReturnNullForUnknownClass() throws Exception { assertNull(this.testee.getData("bar")); }
@Override public InputStream getData(final String name) throws IOException { try (ZipFile zip = getRoot()) { final ZipEntry entry = zip.getEntry(name.replace('.', '/') + ".class"); if (entry == null) { return null; } return StreamUtil.copyStream(zip.getInputStream(entry)); } }
@Test public void getResourceShouldReturnURLForAKnownResource() throws Exception { assertNotNull(this.testee.getResource("injar/p1/P1Test.class")); }
@Before public void setup() throws Exception { // note mytests.jar is taken from // http://johanneslink.net/projects/cpsuite.jsp // assume GPL licence for this file. We do not link to any code within it // however this.testee = new ArchiveClassPathRoot(new File("mytests.jar")); }
@Override public Collection<String> classNames() { final List<String> names = new ArrayList<>(); final ZipFile root = getRoot(); try { final Enumeration<? extends ZipEntry> entries = root.entries(); while (entries.hasMoreElements()) { final ZipEntry entry = entries.nextElement(); if (!entry.isDirectory() && entry.getName().endsWith(".class")) { names.add(stringToClassName(entry.getName())); } } return names; } finally { closeQuietly(root); } }
@Test public void getDataShouldReturnInputStreamForAKnownClass() throws Exception { assertNotNull(this.testee.getData("injar.p1.P1Test")); }
@Test public void shouldTreatJarFilesAsDependencies() { final ClassPathRoot archiveRoot = new ArchiveClassPathRoot(new File( "foo.jar")); assertTrue(this.testee.test(archiveRoot)); }
@Test public void shouldNotTreatZipFilesAsCode() { final ClassPathRoot archiveRoot = new ArchiveClassPathRoot(new File( "foo.zip")); assertFalse(this.testee.test(archiveRoot)); }
@Test public void shouldNotTreatJarFilesAsCode() { final ClassPathRoot archiveRoot = new ArchiveClassPathRoot(new File( "foo.jar")); assertFalse(this.testee.test(archiveRoot)); }
private static void handleArchive(final List<ClassPathRoot> rs, final File f) throws IOException { try { if (!f.canRead()) { throw new IOException("Can't read the file " + f); } rs.add(new ArchiveClassPathRoot(f)); } catch (final ZipException ex) { LOG.warning("Can't open the archive " + f); } }