Skip to content

Commit

Permalink
Merge pull request #8 from TNG/enable-to-import-multiple-jars
Browse files Browse the repository at this point in the history
Allows to import multiple Jars at once, resolves #7
  • Loading branch information
codecholeric authored May 20, 2017
2 parents 233b934 + d64616e commit ebdc4a7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Set;
import java.util.jar.JarFile;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.tngtech.archunit.PublicAPI;
Expand Down Expand Up @@ -68,7 +69,21 @@ public JavaClasses importPath(Path path) {

@PublicAPI(usage = ACCESS)
public JavaClasses importJar(JarFile jar) {
return importLocations(singleton(Location.of(jar)));
return importJars(jar);
}

@PublicAPI(usage = ACCESS)
public JavaClasses importJars(JarFile... jarFiles) {
return importJars(ImmutableList.copyOf(jarFiles));
}

@PublicAPI(usage = ACCESS)
public JavaClasses importJars(Iterable<JarFile> jarFiles) {
Set<Location> locations = new HashSet<>();
for (JarFile jarFile : jarFiles) {
locations.add(Location.of(jarFile));
}
return importLocations(locations);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.tngtech.archunit.core.importer;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;
Expand All @@ -25,6 +24,7 @@
import com.google.common.base.Predicate;
import com.google.common.base.Suppliers;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.tngtech.archunit.ArchConfiguration;
Expand Down Expand Up @@ -1670,16 +1670,7 @@ public void imports_class_objects() throws Exception {
}

@Test
public void ImportOptions_are_respected() throws Exception {
ClassFileImporter importer = new ClassFileImporter().withImportOption(importNothing());

assertThat(importer.importPath(Paths.get(urlOf(getClass()).toURI()))).isEmpty();
assertThat(importer.importUrl(urlOf(getClass()))).isEmpty();
assertThat(importer.importJar(jarFileOf(Rule.class))).isEmpty();
}

@Test
public void imports_package() {
public void imports_packages() {
Set<Class<?>> expectedClasses = ImmutableSet.of(getClass(), Rule.class);
Set<String> packages = packagesOf(expectedClasses);

Expand All @@ -1688,6 +1679,31 @@ public void imports_package() {
assertThatClasses(classes).contain(expectedClasses);
}

@Test
public void imports_jars() throws Exception {
JavaClasses classes = new ClassFileImporter().importJar(jarFileOf(Rule.class));
assertThatClasses(classes).contain(Rule.class);
assertThatClasses(classes).dontContain(Object.class, ImmutableList.class);

classes = new ClassFileImporter().importJars(jarFileOf(Rule.class), jarFileOf(ImmutableList.class));
assertThatClasses(classes).contain(Rule.class, ImmutableList.class);
assertThatClasses(classes).dontContain(Object.class);

classes = new ClassFileImporter().importJars(ImmutableList.of(
jarFileOf(Rule.class), jarFileOf(ImmutableList.class)));
assertThatClasses(classes).contain(Rule.class, ImmutableList.class);
assertThatClasses(classes).dontContain(Object.class);
}

@Test
public void ImportOptions_are_respected() throws Exception {
ClassFileImporter importer = new ClassFileImporter().withImportOption(importNothing());

assertThat(importer.importPath(Paths.get(urlOf(getClass()).toURI()))).isEmpty();
assertThat(importer.importUrl(urlOf(getClass()))).isEmpty();
assertThat(importer.importJar(jarFileOf(Rule.class))).isEmpty();
}

private Set<String> packagesOf(Set<Class<?>> classes) {
Set<String> result = new HashSet<>();
for (Class<?> c : classes) {
Expand All @@ -1696,11 +1712,10 @@ private Set<String> packagesOf(Set<Class<?>> classes) {
return result;
}

private JarFile jarFileOf(Class<Rule> clazzInJar) throws IOException {
private JarFile jarFileOf(Class<?> clazzInJar) throws IOException {
String fileName = urlOf(clazzInJar).getFile();
checkArgument(fileName.contains(".jar!/"), "Class %s is not contained in a JAR", clazzInJar.getName());
File file = new File(fileName.replaceAll(".*:", "").replaceAll("!/.*", ""));
return new JarFile(file);
return new JarFile(fileName.replaceAll(".*:", "").replaceAll("!/.*", ""));
}

private ImportOption importNothing() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,25 @@ public void matchExactly(Class<?>... classes) {
}
}

public void contain(Class<?>... classes) {
contain(ImmutableSet.copyOf(classes));
}

public void dontContain(Class<?>... classes) {
assertThat(actualNames()).doesNotContainAnyElementsOf(JavaClass.namesOf(classes));
}

public void contain(Iterable<Class<?>> classes) {
List<String> expectedNames = JavaClass.namesOf(Lists.newArrayList(classes));
assertThat(actualNames()).as("actual classes").containsAll(expectedNames);
}

private Set<String> actualNames() {
Set<String> actualNames = new HashSet<>();
for (JavaClass javaClass : actual) {
actualNames.add(javaClass.getName());
}
List<String> expectedNames = JavaClass.namesOf(Lists.newArrayList(classes));
assertThat(actualNames).as("actual classes").containsAll(expectedNames);
return actualNames;
}
}

Expand Down

0 comments on commit ebdc4a7

Please sign in to comment.