gradle, some refactoring, started object args

This commit is contained in:
shautvast 2014-07-06 20:01:28 +02:00
parent 0c12b6acc7
commit 505828a929
98 changed files with 467 additions and 505 deletions

View file

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="tst"/>
<classpathentry kind="src" path="testclasses"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<accessrules>
<accessrule kind="accessible" pattern="sun/reflect/**"/>
</accessrules>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="lib/org.jacoco.ant-0.7.1.20140427-1606.jar"/>
<classpathentry kind="lib" path="lib/org.jacoco.core-0.7.1.20140427-1606.jar"/>
<classpathentry kind="lib" path="lib/org.jacoco.report-0.7.1.20140427-1606.jar"/>
<classpathentry kind="lib" path="lib/asm-all-5.0.2.jar"/>
<classpathentry kind="lib" path="lib/mockito-all-1.9.5.jar"/>
<classpathentry kind="lib" path="lib/javassist.jar" sourcepath="C:/dev/javassist-3.18.0-GA/src/main"/>
<classpathentry kind="lib" path="lib/xstream-1.4.7.jar"/>
<classpathentry kind="lib" path="lib/xpp3_min-1.1.4c.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

12
.gitignore vendored Normal file
View file

@ -0,0 +1,12 @@
/build
/nl.jssl.autounit.testclasses.BooleanArguments.xml
/nl.jssl.autounit.testclasses.ByteArguments.xml
/nl.jssl.autounit.testclasses.FloatArguments.xml
/nl.jssl.autounit.testclasses.IntArguments.xml
/nl.jssl.autounit.testclasses.SomeBean.xml
/nl.jssl.autounit.testclasses.StringArguments.xml
/bin
/.settings
/.gradle
/.project
/.classpath

View file

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>autounit</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View file

@ -1,95 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.annotation.missingNonNullByDefaultAnnotation=ignore
org.eclipse.jdt.core.compiler.annotation.nonnull=org.eclipse.jdt.annotation.NonNull
org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annotation.NonNullByDefault
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.autoboxing=ignore
org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning
org.eclipse.jdt.core.compiler.problem.deadCode=warning
org.eclipse.jdt.core.compiler.problem.deprecation=warning
org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore
org.eclipse.jdt.core.compiler.problem.fatalOptionalError=disabled
org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore
org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning
org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning
org.eclipse.jdt.core.compiler.problem.forbiddenReference=error
org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning
org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts=disabled
org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning
org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=warning
org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore
org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore
org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning
org.eclipse.jdt.core.compiler.problem.missingDefaultCase=ignore
org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore
org.eclipse.jdt.core.compiler.problem.missingEnumCaseDespiteDefault=disabled
org.eclipse.jdt.core.compiler.problem.missingHashCodeMethod=ignore
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore
org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotationForInterfaceMethodImplementation=enabled
org.eclipse.jdt.core.compiler.problem.missingSerialVersion=warning
org.eclipse.jdt.core.compiler.problem.missingSynchronizedOnInheritedMethod=ignore
org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning
org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning
org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore
org.eclipse.jdt.core.compiler.problem.nullAnnotationInferenceConflict=error
org.eclipse.jdt.core.compiler.problem.nullReference=warning
org.eclipse.jdt.core.compiler.problem.nullSpecViolation=error
org.eclipse.jdt.core.compiler.problem.nullUncheckedConversion=warning
org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning
org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore
org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore
org.eclipse.jdt.core.compiler.problem.potentialNullReference=ignore
org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable=ignore
org.eclipse.jdt.core.compiler.problem.rawTypeReference=warning
org.eclipse.jdt.core.compiler.problem.redundantNullAnnotation=warning
org.eclipse.jdt.core.compiler.problem.redundantNullCheck=ignore
org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=ignore
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled
org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore
org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning
org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems=enabled
org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning
org.eclipse.jdt.core.compiler.problem.unclosedCloseable=warning
org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore
org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning
org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore
org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore
org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled
org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled
org.eclipse.jdt.core.compiler.problem.unusedImport=warning
org.eclipse.jdt.core.compiler.problem.unusedLabel=warning
org.eclipse.jdt.core.compiler.problem.unusedLocal=warning
org.eclipse.jdt.core.compiler.problem.unusedObjectAllocation=ignore
org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedParameterIncludeDocCommentReference=enabled
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled
org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled
org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.source=1.7

1
bin/.gitignore vendored
View file

@ -1 +0,0 @@
/nl

Binary file not shown.

Binary file not shown.

29
build.gradle Normal file
View file

@ -0,0 +1,29 @@
apply plugin: 'java'
apply plugin: 'eclipse'
repositories{
mavenCentral()
}
sourceCompatibility = "1.6"
targetCompatibility = "1.6"
sourceSets {
test {
java {
srcDir 'src/test/testclasses'
}
}
}
task wrapper{
}
dependencies{
compile 'javassist:javassist:3.12.1.GA'
compile 'com.thoughtworks.xstream:xstream:1.4.7'
compile 'org.mockito:mockito-all:1.9.5'
testCompile 'junit:junit:4.11'
testCompile 'org.jacoco:org.jacoco.core:0.7.1.201405082137'
}

View file

@ -1,16 +0,0 @@
<map>
<entry>
<string>public java.lang.String getText(boolean arg1,boolean arg2)</string>
<results>
<contents>
<case>
<input class="nl.jssl.autounit.utils.Permuter$Tuple">
<element1 class="boolean">false</element1>
<element2 class="boolean">false</element2>
</input>
<output class="string">falsefalse</output>
</case>
</contents>
</results>
</entry>
</map>

View file

@ -1,21 +0,0 @@
<map>
<entry>
<string>public int getDouble(byte arg1)</string>
<results>
<contents>
<case>
<input class="nl.jssl.autounit.utils.Permuter$Tuple">
<element1 class="byte">-128</element1>
</input>
<output class="int">-256</output>
</case>
<case>
<input class="nl.jssl.autounit.utils.Permuter$Tuple">
<element1 class="byte">0</element1>
</input>
<output class="int">11</output>
</case>
</contents>
</results>
</entry>
</map>

View file

@ -1,15 +0,0 @@
<map>
<entry>
<string>public int round(float arg1)</string>
<results>
<contents>
<case>
<input class="nl.jssl.autounit.utils.Permuter$Tuple">
<element1 class="float">-2.006128E38</element1>
</input>
<output class="int">-2147483648</output>
</case>
</contents>
</results>
</entry>
</map>

View file

@ -1,36 +0,0 @@
<map>
<entry>
<string>public java.lang.String evenOrUneven(int arg1,int arg2)</string>
<results>
<contents>
<case>
<input class="nl.jssl.autounit.utils.Permuter$Tuple">
<element1 class="int">-2147483648</element1>
<element2 class="int">-2147483648</element2>
</input>
<output class="string">even</output>
</case>
<case>
<input class="nl.jssl.autounit.utils.Permuter$Tuple">
<element1 class="int">-2147483648</element1>
<element2 class="int">-1</element2>
</input>
<output class="string">uneven</output>
</case>
</contents>
</results>
</entry>
<entry>
<string>public int randomOther(int arg1)</string>
<results>
<contents>
<case>
<input class="nl.jssl.autounit.utils.Permuter$Tuple">
<element1 class="int">-2147483648</element1>
</input>
<output class="int">0</output>
</case>
</contents>
</results>
</entry>
</map>

View file

@ -1,7 +1,6 @@
package nl.jssl.autounit;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Map;
@ -14,7 +13,7 @@ import java.util.Map;
public class AutoTester {
ResultsWriter resultsWriter = new ResultsWriter();
public Map<String, MethodCallResults> record(Class instance) {
public Map<String, MethodCallResults> record(Class<?> instance) {
Map<String, MethodCallResults> results = new Recorder(instance).record();
resultsWriter.write(instance.getName(), results);
return results;
@ -35,7 +34,7 @@ public class AutoTester {
Map<String, MethodCallResults> results = (Map<String, MethodCallResults>) objectInputStream.readObject();
objectInputStream.close();
return results;
} catch (IOException | ClassNotFoundException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}

View file

@ -3,6 +3,10 @@ package nl.jssl.autounit;
import javassist.ClassPool;
import javassist.NotFoundException;
/**
* TODO make configurable
*
*/
public class Configuration {
public static ClassPool getClassPool() {
ClassPool classPool = new ClassPool();

View file

@ -21,6 +21,7 @@ public class CoverageAnalyser {
private IRuntime runtime;
private RuntimeData data = new RuntimeData();
@SuppressWarnings("unchecked")
public <T> T instrument(Class<T> testTarget) {
try {
String targetName = testTarget.getName();

View file

@ -3,7 +3,6 @@ package nl.jssl.autounit;
import java.lang.reflect.Method;
import java.util.List;
import nl.jssl.autounit.inputs.ArgumentsForSingleCall;
import nl.jssl.autounit.utils.Permuter.Tuple;
/**
@ -39,16 +38,24 @@ public class MethodcallExecutor {
break;
}
}
if (previous == null) {
result.setCoverageResult(lastInvocationResult.coverage);
} else {
result.setCoverageResult(previous.coverage);
if (lastInvocationResult != null) {
if (previous == null) {
result.setCoverageResult(lastInvocationResult.coverage);
} else {
result.setCoverageResult(previous.coverage);
}
}
return result;
}
private InvocationResult analyseMethodCall(Method m, Tuple input) {
return coverageAnalyser.analyse(instrumentedTestTarget, m, input.toArray());
Object[] argumentarray = input.toArray();
for (int i = 0; i < argumentarray.length; i++) {
if (argumentarray[i].toString().equals("autounit:[NULL]")) {
argumentarray[i] = null;
}
}
return coverageAnalyser.analyse(instrumentedTestTarget, m, argumentarray);
}
public MethodCallResults getResult() {

View file

@ -7,8 +7,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nl.jssl.autounit.inputs.ArgumentsForSingleCall;
import nl.jssl.autounit.inputs.CombinedInputSetFactory;
import nl.jssl.autounit.inputs.MethodcallArgumentsFactory;
import nl.jssl.autounit.utils.Permuter.Tuple;
public class Recorder {
@ -43,7 +42,7 @@ public class Recorder {
}
private MethodCallResults recordMethod(Method m) {
List<Tuple> inputSet = new CombinedInputSetFactory().getInputs(testTarget, m);
List<Tuple> inputSet = new MethodcallArgumentsFactory().getInputs(testTarget, m);
MethodcallExecutor methodcallExecutor = new MethodcallExecutor(testTarget, m);
methodcallExecutor.execute(inputSet);
return methodcallExecutor.getResult();

View file

@ -4,8 +4,8 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Map;
import nl.jssl.autounit.inputs.ArgumentsForSingleCall;
import nl.jssl.autounit.utils.ArgumentsConverter;
import nl.jssl.autounit.utils.Permuter.Tuple;
import nl.jssl.autounit.utils.XStreamArgumentsConverter;
import com.thoughtworks.xstream.XStream;
@ -19,8 +19,9 @@ public class ResultsWriter {
private void setup() {
xstream.alias("case", InAndOutput.class);
xstream.alias("results", MethodCallResults.class);
xstream.alias("args", ArgumentsForSingleCall.class);
xstream.registerConverter(new ArgumentsConverter());
xstream.alias("args", Tuple.class);
xstream.registerConverter(new XStreamArgumentsConverter());
}
public void write(String classname, Map<String, MethodCallResults> results) {

View file

@ -0,0 +1,10 @@
package nl.jssl.autounit.inputs;
import java.util.ArrayList;
/**
* Meant to keep lists apart. Not to get confused. Might loose its necessity.
*/
@SuppressWarnings("serial")
public class ArgumentsForSingleParameter<T> extends ArrayList<T> {
}

View file

@ -0,0 +1,94 @@
package nl.jssl.autounit.inputs;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nl.jssl.autounit.inputs.objects.ObjectArgumentFactory;
import nl.jssl.autounit.inputs.objects.StringArgumentFactory;
import nl.jssl.autounit.inputs.primtives.ArgumentFactory;
import nl.jssl.autounit.inputs.primtives.BooleanArgumentFactory;
import nl.jssl.autounit.inputs.primtives.ByteArgumentFactory;
import nl.jssl.autounit.inputs.primtives.DoubleArgumentFactory;
import nl.jssl.autounit.inputs.primtives.FloatArgumentFactory;
import nl.jssl.autounit.inputs.primtives.IntegerArgumentFactory;
import nl.jssl.autounit.utils.Permuter;
import nl.jssl.autounit.utils.Permuter.Tuple;
public class MethodcallArgumentsFactory {
private final Map<Class<?>, ArgumentFactory<?>> primitivesFactories;
public MethodcallArgumentsFactory() {
primitivesFactories = new HashMap<Class<?>, ArgumentFactory<?>>();
populateFactories();
}
public List<Tuple> getInputs(Class<?> testTarget, Method m) {
return combine(getArgumentsForAllParameters(testTarget, m));
}
private List<Tuple> combine(List<List<?>> inputSetsForAllArguments) {
int nrOfParameters = inputSetsForAllArguments.size();
if (nrOfParameters == 0) {
return Collections.emptyList();
} else if (nrOfParameters == 1) {
// simple case
return makeArgumentsForSingleParameterCall(inputSetsForAllArguments);
} else {
return Permuter.permute(inputSetsForAllArguments);
}
}
private List<Tuple> makeArgumentsForSingleParameterCall(List<List<?>> generatedInputSetsForAllArguments) {
List<Tuple> allPossibleArguments = new ArrayList<Tuple>();
List<?> generatedInputs = generatedInputSetsForAllArguments.iterator().next();
for (Object variable : generatedInputs) {
Tuple argument = new Tuple(variable);
allPossibleArguments.add(argument);
}
return allPossibleArguments;
}
List<List<?>> getArgumentsForAllParameters(Class<?> testTarget, Method m) {
List<List<?>> singleInputSets = new ArrayList<List<?>>();
for (Class<?> parametertype : m.getParameterTypes()) {
List<?> inputs = tryPrimitives(testTarget, parametertype);
if (inputs == null) {
inputs = new ObjectArgumentFactory().getObjectArgument(testTarget, parametertype);
}
if (inputs != null) {
singleInputSets.add(inputs);
}
}
return singleInputSets;
}
private ArgumentsForSingleParameter<?> tryPrimitives(Class<?> testTarget, Class<?> parametertype) {
ArgumentFactory<?> inputsFactory = primitivesFactories.get(parametertype);
if (inputsFactory != null) {
return inputsFactory.getInputs(testTarget);
} else {
return null;
}
}
private void populateFactories() {
primitivesFactories.put(int.class, new IntegerArgumentFactory());
primitivesFactories.put(Integer.class, new IntegerArgumentFactory());
primitivesFactories.put(double.class, new DoubleArgumentFactory());
primitivesFactories.put(Double.class, new DoubleArgumentFactory());
primitivesFactories.put(float.class, new FloatArgumentFactory());
primitivesFactories.put(Float.class, new FloatArgumentFactory());
primitivesFactories.put(byte.class, new ByteArgumentFactory());
primitivesFactories.put(Byte.class, new ByteArgumentFactory());
primitivesFactories.put(Boolean.class, new BooleanArgumentFactory());
primitivesFactories.put(boolean.class, new BooleanArgumentFactory());
primitivesFactories.put(String.class, new StringArgumentFactory());
}
}

View file

@ -0,0 +1,55 @@
package nl.jssl.autounit.inputs.objects;
import java.lang.reflect.Method;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
import nl.jssl.autounit.inputs.MethodcallArgumentsFactory;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
/**
* Creates arguments if they are objects. Methods that return values are populated like in regular mocking.
*
*/
public class ObjectArgumentFactory {
private MethodcallArgumentsFactory argumentsFactory;
public ArgumentsForSingleParameter<?> getObjectArgument(Class<?> testTarget, Class<?> parametertype) {
ArgumentsForSingleParameter<Object> inputs = new ArgumentsForSingleParameter<Object>();
Object mock = createMock(parametertype);
inputs.add(mock);
return inputs;
}
private Object createMock(Class<?> parametertype) {
Object mock = Mockito.mock(parametertype, new DefaultAnswer());
return mock;
}
static class DefaultAnswer implements Answer<Object> {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Method method = invocation.getMethod();
// necessary?
if (returnsVoid(method)) {
return Void.TYPE;
}
return null;
}
}
static boolean returnsVoid(Method m) {
Class<?> returnType = m.getReturnType();
return returnType != Void.TYPE;
}
public void setArgumentsFactory(MethodcallArgumentsFactory argumentsFactory) {
this.argumentsFactory = argumentsFactory;
}
}

View file

@ -0,0 +1,26 @@
package nl.jssl.autounit.inputs.objects;
import javassist.ClassPool;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
import nl.jssl.autounit.inputs.primtives.ArgumentFactory;
import nl.jssl.autounit.utils.ConstantpoolReader;
/**
* Creates Strings as arguments for a method call. Uses bytecode analysis to scan the class-under-test for "interesting"
* strings and adds them to the argument set.
*
* Also adds null to check for NPE. //is this feasible?
*/
public class StringArgumentFactory implements ArgumentFactory<String> {
private static ConstantpoolReader constantpoolReader = new ConstantpoolReader(ClassPool.getDefault());
@Override
public ArgumentsForSingleParameter<String> getInputs(Class<?> testTarget) {
ArgumentsForSingleParameter<String> inputs = new ArgumentsForSingleParameter<String>();
inputs.add(null);
inputs.add("some");
inputs.addAll(constantpoolReader.scanStrings(testTarget));
return inputs;
}
}

View file

@ -0,0 +1,7 @@
package nl.jssl.autounit.inputs.primtives;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
public interface ArgumentFactory<T> {
ArgumentsForSingleParameter<T> getInputs(Class<?> testTarget);
}

View file

@ -0,0 +1,14 @@
package nl.jssl.autounit.inputs.primtives;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
public class BooleanArgumentFactory implements ArgumentFactory<Boolean> {
public ArgumentsForSingleParameter<Boolean> getInputs(Class<?> testTarget) {
ArgumentsForSingleParameter<Boolean> inputs = new ArgumentsForSingleParameter<Boolean>();
inputs.add(Boolean.FALSE);
inputs.add(Boolean.TRUE);
return inputs;
}
}

View file

@ -0,0 +1,15 @@
package nl.jssl.autounit.inputs.primtives;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
public class ByteArgumentFactory implements ArgumentFactory<Byte> {
public ArgumentsForSingleParameter<Byte> getInputs(Class<?> testTarget) {
ArgumentsForSingleParameter<Byte> inputs = new ArgumentsForSingleParameter<Byte>();
for (byte i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {
inputs.add(i);
}
return inputs;
}
}

View file

@ -0,0 +1,23 @@
package nl.jssl.autounit.inputs.primtives;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
public class DoubleArgumentFactory implements ArgumentFactory<Double> {
public ArgumentsForSingleParameter<Double> getInputs(Class<?> testTarget) {
ArgumentsForSingleParameter<Double> inputs = new ArgumentsForSingleParameter<Double>();
for (int i = 0; i < 1000; i++) {
inputs.add(((2 * Math.random() - 2) * Double.MAX_VALUE));
}
inputs.add(Double.MIN_VALUE);
inputs.add(-2D);
inputs.add(-1D);
inputs.add(0D);
inputs.add(1D);
inputs.add(2D);
inputs.add(Double.MAX_VALUE);
return inputs;
}
}

View file

@ -0,0 +1,22 @@
package nl.jssl.autounit.inputs.primtives;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
public class FloatArgumentFactory implements ArgumentFactory<Float> {
public ArgumentsForSingleParameter<Float> getInputs(Class<?> testTarget) {
ArgumentsForSingleParameter<Float> inputs = new ArgumentsForSingleParameter<Float>();
for (int i = 0; i < 1000; i++) {
inputs.add((float) ((2 * Math.random() - 2) * Float.MAX_VALUE));
}
inputs.add(Float.MIN_VALUE);
inputs.add(-2F);
inputs.add(-1F);
inputs.add(0F);
inputs.add(1F);
inputs.add(2F);
inputs.add(Float.MAX_VALUE);
return inputs;
}
}

View file

@ -0,0 +1,20 @@
package nl.jssl.autounit.inputs.primtives;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
public class IntegerArgumentFactory implements ArgumentFactory<Integer> {
public ArgumentsForSingleParameter<Integer> getInputs(Class<?> testTarget) {
ArgumentsForSingleParameter<Integer> inputs = new ArgumentsForSingleParameter<Integer>();
// for (int i = 0; i < 2; i++) {
// inputs.add((int) ((2 * Math.random() - 2) * Integer.MAX_VALUE));
// }
inputs.add(Integer.MIN_VALUE);
inputs.add(-1);
inputs.add(0);
inputs.add(1);
inputs.add(Integer.MAX_VALUE);
return inputs;
}
}

View file

@ -0,0 +1,23 @@
package nl.jssl.autounit.inputs.primtives;
import nl.jssl.autounit.inputs.ArgumentsForSingleParameter;
public class LongArgumentFactory implements ArgumentFactory<Long> {
public ArgumentsForSingleParameter<Long> getInputs(Class<?> testTarget) {
ArgumentsForSingleParameter<Long> inputs = new ArgumentsForSingleParameter<Long>();
for (int i = 0; i < 1000; i++) {
inputs.add((long) ((2 * Math.random() - 2) * Long.MAX_VALUE));
}
inputs.add(Long.MIN_VALUE);
inputs.add(-2L);
inputs.add(-1L);
inputs.add(0L);
inputs.add(1L);
inputs.add(2L);
inputs.add(Long.MAX_VALUE);
return inputs;
}
}

View file

@ -22,7 +22,7 @@ public class ConstantpoolReader {
CtClass ctClass;
try {
ctClass = pool.get(target.getName());
List<String> strings = new ArrayList<>();
List<String> strings = new ArrayList<String>();
if (isScannable(ctClass)) {
ConstPool constPool = ctClass.getClassFile().getConstPool();
int size = constPool.getSize();

View file

@ -25,7 +25,7 @@ public class Permuter {
}
private static List<Tuple> permutePairs(List<?> list1, List<?> list2) {
List<Tuple> pairs = new ArrayList<>();
List<Tuple> pairs = new ArrayList<Tuple>();
for (Object element1 : list1) {
for (Object element2 : list2) {
pairs.add(new Tuple(element1, element2));
@ -34,7 +34,7 @@ public class Permuter {
return pairs;
}
public static class Tuple implements Iterable {
public static class Tuple implements Iterable<Object> {
public final Object element1;
public final Object element2;
@ -55,9 +55,11 @@ public class Permuter {
}
private List<Object> asList() {
List<Object> list = new ArrayList<>();
List<Object> list = new ArrayList<Object>();
if (element1 != null) {
add(element1, list);
} else {
add("autounit:[NULL]", list);
}
if (element2 != null) {
add(element2, list);

View file

@ -18,7 +18,7 @@ public class SilentObjectCreator {
try {
ReflectionFactory rf = ReflectionFactory.getReflectionFactory();
Constructor<?> objDef = parent.getDeclaredConstructor();
Constructor<T> intConstr = rf.newConstructorForSerialization(clazz, objDef);
Constructor<T> intConstr = (Constructor<T>) rf.newConstructorForSerialization(clazz, objDef);
return clazz.cast(intConstr.newInstance());
} catch (RuntimeException e) {
throw e;

View file

@ -1,6 +1,6 @@
package nl.jssl.autounit.utils;
import nl.jssl.autounit.inputs.ArgumentsForSingleCall;
import nl.jssl.autounit.utils.Permuter.Tuple;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
@ -8,16 +8,17 @@ import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class ArgumentsConverter implements Converter {
public class XStreamArgumentsConverter implements Converter {
@SuppressWarnings("rawtypes")
public boolean canConvert(Class clazz) {
return (clazz == ArgumentsForSingleCall.class);
return (clazz == Tuple.class);
}
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
ArgumentsForSingleCall person = (ArgumentsForSingleCall) value;
Tuple arguments = (Tuple) value;
int index = 1;
for (Object arg : person) {
for (Object arg : arguments) {
writer.startNode("arg" + index);
writer.setValue(arg.toString());
writer.endNode();

View file

@ -1,7 +0,0 @@
package nl.jssl.autounit.inputs;
import java.util.ArrayList;
public class ArgumentsForSingleCall extends ArrayList<Object> {
}

View file

@ -1,12 +0,0 @@
package nl.jssl.autounit.inputs;
public class BooleanInputsFactory implements InputsFactory<Boolean> {
public SingleTypeInputs<Boolean> getInputs(Class<?> testTarget) {
SingleTypeInputs<Boolean> inputs = new SingleTypeInputs<Boolean>();
inputs.add(Boolean.FALSE);
inputs.add(Boolean.TRUE);
return inputs;
}
}

View file

@ -1,13 +0,0 @@
package nl.jssl.autounit.inputs;
public class ByteInputsFactory implements InputsFactory<Byte> {
public SingleTypeInputs<Byte> getInputs(Class<?> testTarget) {
SingleTypeInputs<Byte> inputs = new SingleTypeInputs<Byte>();
for (byte i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {
inputs.add(i);
}
return inputs;
}
}

View file

@ -1,84 +0,0 @@
package nl.jssl.autounit.inputs;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nl.jssl.autounit.utils.Permuter;
import nl.jssl.autounit.utils.Permuter.Tuple;
public class CombinedInputSetFactory {
private final Map<Class<?>, InputsFactory<?>> primitivesFactories;
public CombinedInputSetFactory() {
primitivesFactories = new HashMap<>();
populateFactories();
}
public List<Tuple> getInputs(Class<?> testTarget, Method m) {
return combine(getInputSetsForAllArguments(testTarget, m));
}
private List<Tuple> combine(List<List<?>> inputSetsForAllArguments) {
int nrOfParameters = inputSetsForAllArguments.size();
if (nrOfParameters == 1) {
// simple case
return makeArgumentsForSingleParameterCall(inputSetsForAllArguments);
} else {
return Permuter.permute(inputSetsForAllArguments);
}
}
private List<Tuple> makeArgumentsForSingleParameterCall(
List<List<?>> generatedInputSetsForAllArguments) {
List<Tuple> allPossibleArguments = new ArrayList<Tuple>();
List<?> generatedInputs = generatedInputSetsForAllArguments.iterator().next();
for (Object variable : generatedInputs) {
Tuple argument = new Tuple(variable);
allPossibleArguments.add(argument);
}
return allPossibleArguments;
}
private List<List<?>> getInputSetsForAllArguments(Class<?> testTarget, Method m) {
List<List<?>> singleInputSets = new ArrayList<>();
for (Class<?> parametertype : m.getParameterTypes()) {
List<?> inputs = tryPrimitives(testTarget, parametertype);
if (inputs == null) {
inputs = new MocksFactory().getMockInputs(testTarget, parametertype);
}
if (inputs != null) {
singleInputSets.add(inputs);
}
}
return singleInputSets;
}
private SingleTypeInputs<?> tryPrimitives(Class<?> testTarget, Class<?> parametertype) {
InputsFactory<?> inputsFactory = primitivesFactories.get(parametertype);
if (inputsFactory != null) {
return inputsFactory.getInputs(testTarget);
} else {
return null;
}
}
private void populateFactories() {
primitivesFactories.put(int.class, new IntegerInputsFactory());
primitivesFactories.put(Integer.class, new IntegerInputsFactory());
primitivesFactories.put(double.class, new DoubleInputsFactory());
primitivesFactories.put(Double.class, new DoubleInputsFactory());
primitivesFactories.put(float.class, new FloatInputsFactory());
primitivesFactories.put(Float.class, new FloatInputsFactory());
primitivesFactories.put(byte.class, new ByteInputsFactory());
primitivesFactories.put(Byte.class, new ByteInputsFactory());
primitivesFactories.put(Boolean.class, new BooleanInputsFactory());
primitivesFactories.put(boolean.class, new BooleanInputsFactory());
primitivesFactories.put(String.class, new StringInputsFactory());
}
}

View file

@ -1,21 +0,0 @@
package nl.jssl.autounit.inputs;
public class DoubleInputsFactory implements InputsFactory<Double> {
public SingleTypeInputs<Double> getInputs(Class<?> testTarget) {
SingleTypeInputs<Double> inputs = new SingleTypeInputs<Double>();
for (int i = 0; i < 1000; i++) {
inputs.add(((2 * Math.random() - 2) * Double.MAX_VALUE));
}
inputs.add(Double.MIN_VALUE);
inputs.add(-2D);
inputs.add(-1D);
inputs.add(0D);
inputs.add(1D);
inputs.add(2D);
inputs.add(Double.MAX_VALUE);
return inputs;
}
}

View file

@ -1,20 +0,0 @@
package nl.jssl.autounit.inputs;
public class FloatInputsFactory implements InputsFactory<Float> {
public SingleTypeInputs<Float> getInputs(Class<?> testTarget) {
SingleTypeInputs<Float> inputs = new SingleTypeInputs<Float>();
for (int i = 0; i < 1000; i++) {
inputs.add((float) ((2 * Math.random() - 2) * Float.MAX_VALUE));
}
inputs.add(Float.MIN_VALUE);
inputs.add(-2F);
inputs.add(-1F);
inputs.add(0F);
inputs.add(1F);
inputs.add(2F);
inputs.add(Float.MAX_VALUE);
return inputs;
}
}

View file

@ -1,5 +0,0 @@
package nl.jssl.autounit.inputs;
public interface InputsFactory<T> {
SingleTypeInputs<T> getInputs(Class<?> testTarget);
}

View file

@ -1,18 +0,0 @@
package nl.jssl.autounit.inputs;
public class IntegerInputsFactory implements InputsFactory<Integer> {
public SingleTypeInputs<Integer> getInputs(Class<?> testTarget) {
SingleTypeInputs<Integer> inputs = new SingleTypeInputs<Integer>();
// for (int i = 0; i < 2; i++) {
// inputs.add((int) ((2 * Math.random() - 2) * Integer.MAX_VALUE));
// }
inputs.add(Integer.MIN_VALUE);
inputs.add(-1);
inputs.add(0);
inputs.add(1);
inputs.add(Integer.MAX_VALUE);
return inputs;
}
}

View file

@ -1,21 +0,0 @@
package nl.jssl.autounit.inputs;
public class LongInputsFactory implements InputsFactory<Long> {
public SingleTypeInputs<Long> getInputs(Class<?> testTarget) {
SingleTypeInputs<Long> inputs = new SingleTypeInputs<Long>();
for (int i = 0; i < 1000; i++) {
inputs.add((long) ((2 * Math.random() - 2) * Long.MAX_VALUE));
}
inputs.add(Long.MIN_VALUE);
inputs.add(-2L);
inputs.add(-1L);
inputs.add(0L);
inputs.add(1L);
inputs.add(2L);
inputs.add(Long.MAX_VALUE);
return inputs;
}
}

View file

@ -1,25 +0,0 @@
package nl.jssl.autounit.inputs;
import java.lang.reflect.Method;
import org.mockito.Mockito;
public class MocksFactory {
CombinedInputSetFactory inputSetFactory = new CombinedInputSetFactory();
public SingleTypeInputs<?> getMockInputs(Class<?> testTarget, Class<?> parametertype) {
SingleTypeInputs<Object> inputs = new SingleTypeInputs<>();
setExpectations(parametertype);
// inputs.add(mock);
return inputs;
}
private void setExpectations(Class<?> parametertype) {
Method[] declaredMethods = parametertype.getDeclaredMethods();
for (Method m : declaredMethods) {
// Set<SingleTypeInputs<?>> inputs =
// inputSetFactory.getInputs(parametertype, m);
}
Object mock = Mockito.mock(parametertype);
}
}

View file

@ -1,6 +0,0 @@
package nl.jssl.autounit.inputs;
import java.util.ArrayList;
public class SingleTypeInputs<T> extends ArrayList<T> {
}

View file

@ -1,18 +0,0 @@
package nl.jssl.autounit.inputs;
import nl.jssl.autounit.Configuration;
import nl.jssl.autounit.utils.ConstantpoolReader;
public class StringInputsFactory implements InputsFactory<String> {
private static ConstantpoolReader constantpoolReader = new ConstantpoolReader(Configuration.getClassPool());
@Override
public SingleTypeInputs<String> getInputs(Class<?> testTarget) {
SingleTypeInputs<String> inputs = new SingleTypeInputs<String>();
inputs.add(null);
inputs.add("some");
inputs.addAll(constantpoolReader.scanStrings(testTarget));
return inputs;
}
}

View file

@ -0,0 +1,23 @@
package nl.jssl.autounit.inputs.objects;
import java.util.Map;
import java.util.Set;
import nl.jssl.autounit.AutoTester;
import nl.jssl.autounit.MethodCallResults;
import nl.jssl.autounit.testclasses.SomeBean;
import org.junit.Assert;
import org.junit.Test;
public class JavaBeanTests {
@Test
public void testJavaBeanArgument() {
Map<String, MethodCallResults> results = new AutoTester().record(SomeBean.class);
Set<String> keys = results.keySet();
Assert.assertTrue(keys.contains("public void setFoo(java.lang.String arg1)"));
MethodCallResults mcr = results.values().iterator().next();
System.out.println(mcr.getCoverageResult().getLineCounter().getMissedCount() + " missed lines");
System.out.println(mcr.getReport());
}
}

View file

@ -0,0 +1,23 @@
package nl.jssl.autounit.inputs.objects;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Method;
import org.junit.Test;
public class ObjectArgumentFactoryTests {
@Test
public void testVoidReturn() throws NoSuchMethodException, SecurityException {
ObjectArgumentFactory o = new ObjectArgumentFactory();
Method testVoidReturnMethod = ObjectArgumentFactoryTests.class.getMethod("testVoidReturn", new Class<?>[] {});
assertEquals(false, o.returnsVoid(testVoidReturnMethod));
Method getBarMethod = ObjectArgumentFactoryTests.class.getMethod("getBar", new Class<?>[] {});
assertEquals(true, o.returnsVoid(getBarMethod));
}
public String getBar() {
return "foo";
}
}

View file

@ -13,13 +13,13 @@ import org.junit.Test;
public class PermuterTests {
@Test
public void testPairs() {
List<Integer> integers = new ArrayList<>();
List<Integer> integers = new ArrayList<Integer>();
integers.add(1);
integers.add(2);
List<String> strings = new ArrayList<>();
List<String> strings = new ArrayList<String>();
strings.add("A");
strings.add("B");
List<List<?>> outer = new ArrayList<>();
List<List<?>> outer = new ArrayList<List<?>>();
outer.add(integers);
outer.add(strings);
List<Tuple> permuted = Permuter.permute(outer);
@ -31,17 +31,17 @@ public class PermuterTests {
@Test
public void testTriplets() {
List<Integer> integers = new ArrayList<>();
List<Integer> integers = new ArrayList<Integer>();
integers.add(1);
integers.add(2);
List<String> strings = new ArrayList<>();
List<String> strings = new ArrayList<String>();
strings.add("A");
strings.add("B");
List<Vogon> vogons = new ArrayList<>();
List<Vogon> vogons = new ArrayList<Vogon>();
vogons.add(new Vogon("Jeltz"));
vogons.add(new Vogon("Gummbah"));
List<List<?>> outer = new ArrayList<>();
List<List<?>> outer = new ArrayList<List<?>>();
outer.add(integers);
outer.add(strings);
outer.add(vogons);

View file

@ -0,0 +1,24 @@
package nl.jssl.autounit.testclasses;
public class SomeBean {
private String foo;
private int bar;
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public int getBar() {
return bar;
}
public void setBar(int bar) {
this.bar = bar;
}
}