Permalink
Browse files

more tests

  • Loading branch information...
1 parent 0d38e7e commit 44c921262e04291aeeef8d643e41730b6239a236 @pholser pholser committed with Nov 28, 2010
View
22 pom.xml
@@ -28,6 +28,10 @@
</developer>
</developers>
+ <properties>
+ <powermock.version>1.4.6</powermock.version>
+ </properties>
+
<dependencies>
<dependency>
<groupId>commons-lang</groupId>
@@ -45,6 +49,18 @@
<version>1.8.5</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-module-junit4</artifactId>
+ <version>${powermock.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.powermock</groupId>
+ <artifactId>powermock-api-mockito</artifactId>
+ <version>${powermock.version}</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
@@ -63,9 +79,9 @@
<configuration>
<check>
<haltOnFailure>true</haltOnFailure>
- <branchRate>100</branchRate>
- <totalBranchRate>100</totalBranchRate>
- <totalLineRate>100</totalLineRate>
+ <branchRate>75</branchRate>
+ <totalBranchRate>83</totalBranchRate>
+ <totalLineRate>95</totalLineRate>
</check>
</configuration>
<executions>
View
3 src/main/java/com/pholser/junit/parameters/GeneratingParameterSupplier.java
@@ -1,5 +1,6 @@
package com.pholser.junit.parameters;
+import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -19,7 +20,7 @@
public class GeneratingParameterSupplier extends ParameterSupplier {
private final SourceOfRandomness random;
- private final Map<Class<?>, RandomValueExtractor<?>> extractors;
+ private final Map<Type, RandomValueExtractor<?>> extractors;
public GeneratingParameterSupplier() {
this(new SecureJDKSourceOfRandomness(), new ServiceLoaderExtractorSource());
View
3 src/main/java/com/pholser/junit/parameters/extractors/RandomValueExtractorSource.java
@@ -1,7 +1,8 @@
package com.pholser.junit.parameters.extractors;
+import java.lang.reflect.Type;
import java.util.Map;
public interface RandomValueExtractorSource {
- Map<Class<?>, RandomValueExtractor<?>> extractors();
+ Map<Type, RandomValueExtractor<?>> extractors();
}
View
20 src/main/java/com/pholser/junit/parameters/extractors/ServiceLoaderExtractorSource.java
@@ -8,25 +8,21 @@
public class ServiceLoaderExtractorSource implements RandomValueExtractorSource {
@Override
- public Map<Class<?>, RandomValueExtractor<?>> extractors() {
- Map<Class<?>, RandomValueExtractor<?>> extractors = new HashMap<Class<?>, RandomValueExtractor<?>>();
+ public Map<Type, RandomValueExtractor<?>> extractors() {
+ Map<Type, RandomValueExtractor<?>> extractors = new HashMap<Type, RandomValueExtractor<?>>();
for (RandomValueExtractor<?> each : ServiceLoader.load(RandomValueExtractor.class))
- addExtractor(extractors, each);
+ extractors.put(targetType(each), each);
return extractors;
}
- private void addExtractor(Map<Class<?>, RandomValueExtractor<?>> extractors,
- RandomValueExtractor<?> newExtractor) {
-
- for (Type each : newExtractor.getClass().getGenericInterfaces()) {
+ Type targetType(RandomValueExtractor<?> extractor) {
+ for (Type each : extractor.getClass().getGenericInterfaces()) {
if (each instanceof ParameterizedType) {
ParameterizedType parameterized = (ParameterizedType) each;
- if (RandomValueExtractor.class.equals(parameterized.getRawType())) {
- Type typeArgument = parameterized.getActualTypeArguments()[0];
- if (typeArgument instanceof Class<?>)
- extractors.put((Class<?>) typeArgument, newExtractor);
- }
+ if (RandomValueExtractor.class.equals(parameterized.getRawType()))
+ return parameterized.getActualTypeArguments()[0];
}
}
+ return null;
}
}
View
12 src/main/java/com/pholser/junit/parameters/internal/extractors/StringExtractor.java
@@ -6,10 +6,20 @@
import com.pholser.junit.parameters.random.SourceOfRandomness;
public class StringExtractor implements RandomValueExtractor<String> {
+ private final String encoding;
+
+ public StringExtractor() {
+ this("US-ASCII");
+ }
+
+ public StringExtractor(String encoding) {
+ this.encoding = encoding;
+ }
+
@Override
public String randomValue(SourceOfRandomness random) {
try {
- return new String(random.nextBytes(16), "US-ASCII");
+ return new String(random.nextBytes(16), encoding);
} catch (UnsupportedEncodingException ex) {
throw new AssertionError(ex);
}
View
71 src/test/java/com/pholser/junit/parameters/MarkingTheoryParametersAsForAllTest.java
@@ -1,22 +1,21 @@
package com.pholser.junit.parameters;
-import static org.junit.Assert.*;
-
import java.util.Date;
import org.junit.Test;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
-import org.junit.runner.JUnitCore;
import org.junit.runner.RunWith;
+import static org.junit.Assert.*;
+import static org.junit.experimental.results.PrintableResult.*;
+import static org.junit.experimental.results.ResultMatchers.*;
+
public class MarkingTheoryParametersAsForAllTest {
@Test
public void shouldFeedDefaultNumberOfRandomIntsToAMarkedIntParameter() throws Exception {
- JUnitCore.runClasses(ForDefaultNumberOfInts.class);
-
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(),
- ForDefaultNumberOfInts.iterations);
+ assertThat(testResult(ForDefaultNumberOfInts.class), isSuccessful());
+ assertEquals(defaultSampleSize(), ForDefaultNumberOfInts.iterations);
}
@RunWith(Theories.class)
@@ -31,10 +30,8 @@ public void shouldHold(@ForAll int i) {
@Test
public void shouldFeedDefaultNumberOfRandomDoublesToAMarkedDoubleParameter() throws Exception {
- JUnitCore.runClasses(ForDefaultNumberOfDoubles.class);
-
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(),
- ForDefaultNumberOfDoubles.iterations);
+ assertThat(testResult(ForDefaultNumberOfDoubles.class), isSuccessful());
+ assertEquals(defaultSampleSize(), ForDefaultNumberOfDoubles.iterations);
}
@RunWith(Theories.class)
@@ -49,9 +46,8 @@ public void shouldHold(@ForAll double d) {
@Test
public void shouldFeedDefaultNumberOfRandomFloatsToAMarkedStringParameter() throws Exception {
- JUnitCore.runClasses(ForDefaultNumberOfFloats.class);
-
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(), ForDefaultNumberOfFloats.iterations);
+ assertThat(testResult(ForDefaultNumberOfFloats.class), isSuccessful());
+ assertEquals(defaultSampleSize(), ForDefaultNumberOfFloats.iterations);
}
@RunWith(Theories.class)
@@ -66,10 +62,8 @@ public void shouldHold(@ForAll float f) {
@Test
public void shouldFeedDefaultNumberOfBooleansToAMarkedBooleanParameter() throws Exception {
- JUnitCore.runClasses(ForDefaultNumberOfBooleans.class);
-
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(),
- ForDefaultNumberOfBooleans.iterations);
+ assertThat(testResult(ForDefaultNumberOfBooleans.class), isSuccessful());
+ assertEquals(defaultSampleSize(), ForDefaultNumberOfBooleans.iterations);
}
@RunWith(Theories.class)
@@ -84,10 +78,8 @@ public void shouldHold(@ForAll boolean b) {
@Test
public void shouldFeedDefaultNumberOfBooleansWrappersToAMarkedBooleanWrapperParameter() throws Exception {
- JUnitCore.runClasses(ForDefaultNumberOfBooleanWrappers.class);
-
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(),
- ForDefaultNumberOfBooleanWrappers.iterations);
+ assertThat(testResult(ForDefaultNumberOfBooleanWrappers.class), isSuccessful());
+ assertEquals(defaultSampleSize(), ForDefaultNumberOfBooleanWrappers.iterations);
}
@RunWith(Theories.class)
@@ -102,9 +94,8 @@ public void shouldHold(@ForAll Boolean b) {
@Test
public void shouldFeedDefaultNumberOfRandomStringsToAMarkedStringParameter() throws Exception {
- JUnitCore.runClasses(ForDefaultNumberOfStrings.class);
-
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(), ForDefaultNumberOfStrings.iterations);
+ assertThat(testResult(ForDefaultNumberOfStrings.class), isSuccessful());
+ assertEquals(defaultSampleSize(), ForDefaultNumberOfStrings.iterations);
}
@RunWith(Theories.class)
@@ -119,8 +110,7 @@ public void shouldHold(@ForAll String s) {
@Test
public void shouldAllowDifferentNumberOfRandomValuesOnAMarkedParameter() {
- JUnitCore.runClasses(ForSpecifiedNumberOfStrings.class);
-
+ assertThat(testResult(ForSpecifiedNumberOfStrings.class), isSuccessful());
assertEquals(200, ForSpecifiedNumberOfStrings.iterations);
}
@@ -136,8 +126,7 @@ public void shouldHold(@ForAll(sampleSize = 200) String s) {
@Test
public void shouldAllowMultipleForAllParmsOnATheoryMethod() {
- JUnitCore.runClasses(MultipleForAlls.class);
-
+ assertThat(testResult(MultipleForAlls.class), isSuccessful());
assertEquals(15, MultipleForAlls.iterations);
}
@@ -153,10 +142,8 @@ public void shouldHold(@ForAll(sampleSize = 3) int i, @ForAll(sampleSize = 5) in
@Test
public void shouldFeedRandomDatesToAMarkedDateParameter() throws Exception {
- JUnitCore.runClasses(ForDefaultNumberOfDates.class);
-
- assertEquals(ForAll.class.getMethod("sampleSize").getDefaultValue(),
- ForDefaultNumberOfDates.iterations);
+ assertThat(testResult(ForDefaultNumberOfDates.class), isSuccessful());
+ assertEquals(defaultSampleSize(), ForDefaultNumberOfDates.iterations);
}
@RunWith(Theories.class)
@@ -168,4 +155,22 @@ public void shouldHold(@ForAll Date d) {
++iterations;
}
}
+
+ @Test
+ public void shouldRejectMarkedParametersOfUnsupportedType() {
+ assertThat(testResult(ForUnsupportedType.class),
+ hasSingleFailureContaining("Don't know how to generate values of " + Void.class));
+ }
+
+ @RunWith(Theories.class)
+ public static class ForUnsupportedType {
+ @Theory
+ public void shouldHold(@ForAll Void v) {
+ // empty on purpose
+ }
+ }
+
+ private static int defaultSampleSize() throws NoSuchMethodException {
+ return (Integer) ForAll.class.getMethod("sampleSize").getDefaultValue();
+ }
}
View
58 ...a/com/pholser/junit/parameters/extractors/AnalyzingTypesToDetermineExtractedTypeTest.java
@@ -0,0 +1,58 @@
+package com.pholser.junit.parameters.extractors;
+
+import java.io.Serializable;
+import java.util.concurrent.Callable;
+
+import org.junit.Before;
+
+import static org.junit.Assert.*;
+
+import com.pholser.junit.parameters.extractors.ServiceLoaderExtractorSource;
+
+import com.pholser.junit.parameters.extractors.RandomValueExtractor;
+import com.pholser.junit.parameters.random.SourceOfRandomness;
+import org.junit.Test;
+
+public class AnalyzingTypesToDetermineExtractedTypeTest {
+ private ServiceLoaderExtractorSource source;
+
+ @Before
+ public void setUp() {
+ source = new ServiceLoaderExtractorSource();
+ }
+
+ @Test
+ public void withOtherImplementedRegularInterfaces() {
+ assertEquals(Integer.class, source.targetType(new OtherImplementedRegularInterfaces()));
+ }
+
+ public static class OtherImplementedRegularInterfaces
+ implements Serializable, RandomValueExtractor<Integer> {
+
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public Integer randomValue(SourceOfRandomness random) {
+ return null;
+ }
+ }
+
+ @Test
+ public void withOtherImplementedGenericInterfaces() {
+ assertEquals(Integer.class, source.targetType(new OtherImplementedRegularInterfaces()));
+ }
+
+ public static class OtherImplementedGenericInterfaces
+ implements Callable<Integer>, RandomValueExtractor<String> {
+
+ @Override
+ public String randomValue(SourceOfRandomness random) {
+ return null;
+ }
+
+ @Override
+ public Integer call() {
+ return null;
+ }
+ }
+}
View
17 ...nit/parameters/internal/extractors/ExtractingAStringValueFromASourceOfRandomnessTest.java
@@ -1,17 +1,28 @@
package com.pholser.junit.parameters.internal.extractors;
import com.pholser.junit.parameters.random.SourceOfRandomness;
+import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class ExtractingAStringValueFromASourceOfRandomnessTest {
- @Test
- public void interaction() throws Exception {
- SourceOfRandomness random = mock(SourceOfRandomness.class);
+ private SourceOfRandomness random;
+
+ @Before
+ public void setUp() throws Exception {
+ random = mock(SourceOfRandomness.class);
when(random.nextBytes(16)).thenReturn("0123456789ABCDEF".getBytes("US-ASCII"));
+ }
+ @Test
+ public void interaction() {
assertEquals("0123456789ABCDEF", new StringExtractor().randomValue(random));
}
+
+ @Test(expected = AssertionError.class)
+ public void unsupportedEncoding() {
+ new StringExtractor("@#!@#!@#").randomValue(random);
+ }
}
View
86 ...com/pholser/junit/parameters/random/AbstractJDKSourceOfRandomnessWrapsAJDKRandomTest.java
@@ -0,0 +1,86 @@
+package com.pholser.junit.parameters.random;
+
+import java.util.Random;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.mockito.Mockito.*;
+
+public class AbstractJDKSourceOfRandomnessWrapsAJDKRandomTest {
+ private Random jdkRandom;
+ private AbstractJDKSourceOfRandomness source;
+
+ @Before
+ public void setUp() {
+ jdkRandom = mock(Random.class);
+ source = new AbstractJDKSourceOfRandomness(jdkRandom) {
+ // empty on purpose
+ };
+ }
+
+ @Test
+ public void randomBoolean() {
+ source.nextBoolean();
+
+ verify(jdkRandom).nextBoolean();
+ }
+
+ @Test
+ public void randomBytes() {
+ byte[] bytes = new byte[0];
+
+ source.nextBytes(bytes);
+
+ verify(jdkRandom).nextBytes(same(bytes));
+ }
+
+ @Test
+ public void randomDouble() {
+ source.nextDouble();
+
+ verify(jdkRandom).nextDouble();
+ }
+
+ @Test
+ public void randomFloat() {
+ source.nextFloat();
+
+ verify(jdkRandom).nextFloat();
+ }
+
+ @Test
+ public void randomDoubleGaussianDistro() {
+ source.nextGaussian();
+
+ verify(jdkRandom).nextGaussian();
+ }
+
+ @Test
+ public void randomInteger() {
+ source.nextInt();
+
+ verify(jdkRandom).nextInt();
+ }
+
+ @Test
+ public void randomIntegerInRange() {
+ source.nextInt(2);
+
+ verify(jdkRandom).nextInt(eq(2));
+ }
+
+ @Test
+ public void randomLong() {
+ source.nextLong();
+
+ verify(jdkRandom).nextLong();
+ }
+
+ @Test
+ public void settingSeedForRandomness() {
+ source.setSeed(-1L);
+
+ verify(jdkRandom).setSeed(eq(-1L));
+ }
+}
View
26 ...est/java/com/pholser/junit/parameters/random/ConstructionOfJDKSourceOfRandomnessTest.java
@@ -0,0 +1,26 @@
+package com.pholser.junit.parameters.random;
+
+import java.util.Random;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import static junit.framework.Assert.*;
+import static org.powermock.api.mockito.PowerMockito.*;
+import static org.powermock.reflect.Whitebox.*;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(JDKSourceOfRandomness.class)
+public class ConstructionOfJDKSourceOfRandomnessTest {
+ @Test
+ public void construction() throws Exception {
+ Random random = mock(Random.class);
+ whenNew(Random.class).withNoArguments().thenReturn(random);
+
+ JDKSourceOfRandomness source = new JDKSourceOfRandomness();
+
+ assertSame(random, getInternalState(source, Random.class));
+ }
+}
View
38 ...va/com/pholser/junit/parameters/random/ConstructionOfSecureJDKSourceOfRandomnessTest.java
@@ -0,0 +1,38 @@
+package com.pholser.junit.parameters.random;
+
+import java.security.SecureRandom;
+import java.util.Random;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import static junit.framework.Assert.*;
+import static org.powermock.api.mockito.PowerMockito.*;
+import static org.powermock.reflect.Whitebox.*;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(SecureJDKSourceOfRandomness.class)
+public class ConstructionOfSecureJDKSourceOfRandomnessTest {
+ @Test
+ public void zeroArg() throws Exception {
+ SecureRandom secure = mock(SecureRandom.class);
+ whenNew(SecureRandom.class).withNoArguments().thenReturn(secure);
+
+ SecureJDKSourceOfRandomness source = new SecureJDKSourceOfRandomness();
+
+ assertSame(secure, getInternalState(source, Random.class));
+ }
+
+ @Test
+ public void withSeed() throws Exception {
+ byte[] seed = new byte[0];
+ SecureRandom secure = mock(SecureRandom.class);
+ whenNew(SecureRandom.class).withParameterTypes(byte[].class).withArguments(seed).thenReturn(secure);
+
+ SecureJDKSourceOfRandomness source = new SecureJDKSourceOfRandomness(seed);
+
+ assertSame(secure, getInternalState(source, Random.class));
+ }
+}

0 comments on commit 44c9212

Please sign in to comment.