@Override public Optional<String> cacheLocation() { return this.child.cacheLocation(); }
@Override public Collection<String> classNames() { final List<String> arrayList = new ArrayList<>(); for (final ClassPathRoot root : this.roots) { arrayList.addAll(root.classNames()); } return arrayList; }
@Override public InputStream getData(final String name) throws IOException { for (final ClassPathRoot each : this.roots) { final InputStream is = each.getData(name); if (is != null) { return is; } } return null; }
@Before public void setUp() { MockitoAnnotations.initMocks(this); this.testee = new ClassPath(this.firstComponent, this.secondComponent); when(this.firstComponent.cacheLocation()).thenReturn(Optional.ofNullable("foo")); when(this.firstComponent.classNames()).thenReturn( Collections.singletonList("FooClass")); when(this.secondComponent.cacheLocation()).thenReturn(Optional.ofNullable("bar")); when(this.secondComponent.classNames()).thenReturn( Collections.singletonList("BarClass")); }
@Override public URL getResource(String name) throws MalformedURLException { return this.child.getResource(name); }
private URL findRootForResource(final String name) throws IOException { for (final ClassPathRoot root : this.roots) { final URL u = root.getResource(name); if (u != null) { return u; } } return null; }
private boolean cacheLocationOptionExists(final ClassPathRoot a) { return a.cacheLocation().isPresent(); }
@Override public Collection<String> classNames() { if (this.cache != null) { final Collection<String> cachedNames = this.cache.get(); if (cachedNames != null) { return cachedNames; } } final Collection<String> names = this.child.classNames(); this.cache = new SoftReference<>(names); return names; }
@Override public InputStream getData(String name) throws IOException { final Collection<String> names = classNames(); if (!names.contains(ClassName.fromString(name).asJavaName())) { return null; } return this.child.getData(name); }
@Test public void shouldReturnResourcesFromChildren() throws IOException { when(this.child1.getResource(any(String.class))).thenReturn(null); final URL url = new URL("http://localhost"); when(this.child1.getResource(any(String.class))).thenReturn(url); assertThat(this.testee.getResource("Foo")).isSameAs(url); }
private Boolean cacheLocationMatchesFilter(final ClassPathRoot classPathRoot) { final String cacheLocationValue = classPathRoot.cacheLocation().get(); return this.stringFilter.test(cacheLocationValue); }
@Test public void shouldReturnNamesOfAllClassesKnownByChildren() { when(this.child1.classNames()).thenReturn(Collections.singletonList("Foo")); when(this.child2.classNames()).thenReturn(Collections.singletonList("Bar")); when(this.heavyChild.classNames()).thenReturn(Collections.singletonList("Heavy")); assertThat(this.testee.classNames()).containsOnly("Foo", "Bar", "Heavy"); }
@Test public void shouldReturnBytesFromClasspathInputStream() throws IOException { final InputStream stream = Mockito.mock(InputStream.class); when(this.firstComponent.getData(any(String.class))).thenReturn(stream); when(stream.read(any(byte[].class), anyInt(), anyInt())).thenReturn(-1); this.testee.getClassData("foo"); verify(stream).read(any(byte[].class), anyInt(), anyInt()); verify(stream).close(); }
private URL findRootForResource(final String name) throws IOException { for (final ClassPathRoot root : this.roots) { final URL u = root.getResource(name); if (u != null) { return u; } } return null; }
@Override public boolean test(final ClassPathRoot a) { return a.cacheLocation().isPresent() && isADependencyPath(a.cacheLocation().get()); }
@Test public void shouldNotQueryHeavyRootsForClassesTheyDoNotContain() throws IOException { when(this.child1.classNames()).thenReturn(Collections.singletonList("Foo")); when(this.heavyChild.classNames()).thenReturn(Collections.singletonList("Heavy")); when(this.child2.classNames()).thenReturn(Collections.singletonList("Bar")); this.testee.getData("Bar"); verify(this.heavyChild,never()).getData("Bar"); this.testee.getData("Heavy"); verify(this.heavyChild).getData("Heavy"); }
@Test public void shouldReturnClassDataFromChildren() throws IOException { when(this.child1.getData(any(String.class))).thenReturn(null); final InputStream is = Mockito.mock(InputStream.class); when(this.child1.getData(any(String.class))).thenReturn(is); assertThat(this.testee.getData("Foo")).isSameAs(is); }
@Override public URL getResource(String name) throws MalformedURLException { return this.child.getResource(name); }
@Override public Optional<String> cacheLocation() { StringBuilder classpath = new StringBuilder(); for (final ClassPathRoot each : this.roots) { final Optional<String> additional = each.cacheLocation(); if (additional.isPresent()) { classpath = classpath.append(File.pathSeparator + additional.get()); } } return Optional.ofNullable(classpath.toString()); }
@Override public Collection<String> classNames() { final List<String> arrayList = new ArrayList<>(); for (final ClassPathRoot root : this.roots) { arrayList.addAll(root.classNames()); } return arrayList; }