Permalink
Browse files

#289: Improving test coverage for scan package

  • Loading branch information...
1 parent 7b77fd1 commit 380609cec141bcba86b893a1b067bd8bbf8b12c4 @garcia-jj garcia-jj committed Sep 9, 2012
@@ -25,6 +25,6 @@
*/
public interface BootstrapGenerator {
- void generate(Collection<String> components, ClasspathResolver resolver);
+ Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver);
}
@@ -40,7 +40,7 @@
*/
public class JavassistBootstrapGenerator implements BootstrapGenerator {
- public void generate(Collection<String> components, ClasspathResolver resolver) {
+ public Class<WebAppBootstrap> generate(Collection<String> components, ClasspathResolver resolver) {
// many initial variables
final String fullName = WebAppBootstrap.STATIC_BOOTSTRAP_NAME;
final String simpleName = fullName.substring(WebAppBootstrap.STATIC_BOOTSTRAP_NAME.lastIndexOf('.') + 1);
@@ -52,17 +52,7 @@ public void generate(Collection<String> components, ClasspathResolver resolver)
// create the entire package path
new File(path).mkdirs();
- // construct the method implementation
- StringBuilder methodDef = new StringBuilder()
- .append("public void configure (br.com.caelum.vraptor.ComponentRegistry registry){");
-
- for (String componentName : components) {
- methodDef.append("registry.deepRegister(")
- .append(componentName).append(".class")
- .append(");");
- }
-
- methodDef.append("}");
+ String methodDef = createMethodDef(components);
// generate class file
try {
@@ -76,7 +66,7 @@ public void generate(Collection<String> components, ClasspathResolver resolver)
clazz.addConstructor(constructor);
// add the method implementation
- CtMethod m = CtNewMethod.make(methodDef.toString(), clazz);
+ CtMethod m = CtNewMethod.make(methodDef, clazz);
clazz.addMethod(m);
// make this class implements WebAppBootstrap
@@ -92,8 +82,25 @@ public void generate(Collection<String> components, ClasspathResolver resolver)
// write the file
cf.write(new DataOutputStream(new FileOutputStream(filename)));
+
+ return clazz.toClass();
} catch (Exception e) {
throw new ScannerException("Error while generating the class file", e);
}
}
+
+ private String createMethodDef(Collection<String> components) {
+ StringBuilder methodDef = new StringBuilder()
+ .append("public void configure (br.com.caelum.vraptor.ComponentRegistry registry){");
+
+ for (String componentName : components) {
+ methodDef.append("registry.deepRegister(")
+ .append(componentName)
+ .append(".class);");
+ }
+
+ methodDef.append("}");
+
+ return methodDef.toString();
+ }
}
@@ -0,0 +1,54 @@
+package br.com.caelum.vraptor.scan;
+
+import static java.util.Arrays.asList;
+import static java.util.Collections.emptyList;
+import static org.junit.Assert.fail;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+
+import java.util.Collection;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import br.com.caelum.vraptor.ComponentRegistry;
+
+public class DynamicWebAppBootstrapTest {
+
+ private @Mock ComponentRegistry registry;
+
+ @Before
+ public void setup() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ }
+
+ @Test
+ public void shouldConfigureAllClasses() {
+ Collection<String> classNames = asList(ResourceA.class.getName());
+ new DynamicWebAppBootstrap(classNames).configure(registry);
+
+ verify(registry).deepRegister(ResourceA.class);
+ }
+
+ @Test
+ public void shouldNotDoAnythingIfHasNoClasses() {
+ Collection<String> classNames = emptyList();
+ new DynamicWebAppBootstrap(classNames).configure(registry);
+
+ verify(registry, never()).deepRegister(any(Class.class));
+ }
+
+ @Test
@lucascs

lucascs Sep 18, 2012

Member

You could use @test(expected=ScannerException.class) here.

+ public void shouldThrowScannerExceptionIfClassNotExists() {
+ Collection<String> classNames = asList("a.resource.that.NotExists");
+
+ try {
+ new DynamicWebAppBootstrap(classNames).configure(registry);
+ fail("If a class don't exists, a ScannerException should throw");
+ } catch (ScannerException e) {
+ }
+ }
+}
@@ -0,0 +1,59 @@
+package br.com.caelum.vraptor.scan;
+
+import static java.util.Arrays.asList;
+import static org.junit.Assert.fail;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Collection;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import br.com.caelum.vraptor.ComponentRegistry;
+
+public class JavassistBootstrapGeneratorTest {
+
+ private @Mock ComponentRegistry registry;
+ private @Mock ClasspathResolver resolver;
+ private JavassistBootstrapGenerator generator;
+
+ @Before
+ public void setup() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ generator = new JavassistBootstrapGenerator();
+ }
+
+ @Test
+ public void shouldRegisterClasses() throws Exception {
+ URL url = File.createTempFile("vraptor", "test").getParentFile().toURI().toURL();
+ when(resolver.findWebInfClassesLocation()).thenReturn(url);
+
+ Collection<String> components = asList(ResourceA.class.getName(), ResourceB.class.getName());
+ Class<WebAppBootstrap> generated = generator.generate(components, resolver);
+ WebAppBootstrap instance = generated.newInstance();
+
+ instance.configure(registry);
+
+ verify(registry).deepRegister(ResourceA.class);
+ verify(registry).deepRegister(ResourceB.class);
+ }
+
+ @Test
+ public void shouldThrowScannerExceptionIfAnErrorOccurs() throws Exception {
+ URL url = File.createTempFile("vraptor", "test").getParentFile().toURI().toURL();
+ when(resolver.findWebInfClassesLocation()).thenReturn(url);
+
+ try {
+ Collection<String> components = asList("a.resource.that.nobody.can.find");
+ generator.generate(components, resolver);
+ fail("Should throw ScannerException");
+ } catch (ScannerException e) {
+
+ }
+ }
+}
@@ -1,19 +1,26 @@
package br.com.caelum.vraptor.scan;
-import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import org.junit.Before;
import org.junit.Test;
-import org.mockito.Mockito;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
import br.com.caelum.vraptor.ComponentRegistry;
public class NullWebAppBootstrapTest {
+ private @Mock ComponentRegistry registry;
+
+ @Before
+ public void setup() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ }
+
@Test
public void shouldNotDoAnything() {
- ComponentRegistry registry = mock(ComponentRegistry.class);
new NullWebAppBootstrap().configure(registry);
- Mockito.verifyZeroInteractions(registry);
+ verifyZeroInteractions(registry);
}
-
}
@@ -0,0 +1,4 @@
+package br.com.caelum.vraptor.scan;
+
+public class ResourceA {
+}
@@ -0,0 +1,4 @@
+package br.com.caelum.vraptor.scan;
+
+public class ResourceB {
+}
@@ -2,14 +2,14 @@
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collection;
+import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -19,32 +19,31 @@
public class ScannotationComponentScannerTest {
private @Mock ClasspathResolver classPathResolver;
+
+ @Before
+ public void setup() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ }
@Test
public void shouldScanWEBINFClasses() {
- MockitoAnnotations.initMocks(this);
-
- this.classPathResolver = mock(ClasspathResolver.class);
when(classPathResolver.findBasePackages()).thenReturn(Arrays.asList(""));
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
when(classPathResolver.getClassLoader()).thenReturn(classLoader);
when(classPathResolver.findWebInfClassesLocation()).thenReturn(classLoader.getResource("br/com/caelum/vraptor/ioc/"));
ScannotationComponentScanner scanner = new ScannotationComponentScanner();
- assertThat(scanner.scan(this.classPathResolver), hasItem(ResourceInTheClasspath.class.getName()));
+ assertThat(scanner.scan(classPathResolver), hasItem(ResourceInTheClasspath.class.getName()));
}
@Test
public void shouldScanBasePackages() {
- MockitoAnnotations.initMocks(this);
-
- this.classPathResolver = mock(ClasspathResolver.class);
ClassLoader classLoader = new URLClassLoader(new URL[] {ScannotationComponentScannerTest.class.getResource("/test-fixture.jar")});
when(classPathResolver.findBasePackages()).thenReturn(Arrays.asList("br.com.caelum.vraptor.ioc"));
when(classPathResolver.getClassLoader()).thenReturn(classLoader);
when(classPathResolver.findWebInfClassesLocation()).thenReturn(classLoader.getResource("br/com/caelum/vraptor/test/"));
ScannotationComponentScanner scanner = new ScannotationComponentScanner();
- Collection<String> classes = scanner.scan(this.classPathResolver);
+ Collection<String> classes = scanner.scan(classPathResolver);
assertThat(classes, hasItem(ResourceInTheClasspath.class.getName()));
}
}

0 comments on commit 380609c

Please sign in to comment.