public VariableReference appendIntPrimitive(int intValue) { IntPrimitiveStatement primitiveStmt = new IntPrimitiveStatement(tc, intValue); tc.addStatement(primitiveStmt); return primitiveStmt.getReturnValue(); }
@Test public void testBasicSimilarityDifferentLength() { TestCase test1 = new DefaultTestCase(); TestCase test2 = new DefaultTestCase(); PrimitiveStatement<?> aInt = new IntPrimitiveStatement(test1, 42); test1.addStatement(aInt); PrimitiveStatement<?> aInt2 = new IntPrimitiveStatement(test1, 42); test1.addStatement(aInt2); PrimitiveStatement<?> bInt = new IntPrimitiveStatement(test2, 42); test2.addStatement(bInt); double score = DiversityObserver.getNeedlemanWunschScore(test1, test2); Assert.assertTrue(score <= 0); }
@Test public void testBasicSimilarity() { TestCase test1 = new DefaultTestCase(); TestCase test2 = new DefaultTestCase(); PrimitiveStatement<?> aInt = new IntPrimitiveStatement(test1, 42); test1.addStatement(aInt); PrimitiveStatement<?> bInt = new IntPrimitiveStatement(test2, 42); test2.addStatement(bInt); double score = DiversityObserver.getNeedlemanWunschScore(test1, test2); Assert.assertTrue(score > 0); }
@Test public void testBasicSimilarityDifferentTypes() { TestCase test1 = new DefaultTestCase(); TestCase test2 = new DefaultTestCase(); PrimitiveStatement<?> aInt = new IntPrimitiveStatement(test1, 42); test1.addStatement(aInt); PrimitiveStatement<?> aInt2 = new IntPrimitiveStatement(test1, 42); test1.addStatement(aInt2); PrimitiveStatement<?> bInt = new IntPrimitiveStatement(test2, 42); test2.addStatement(bInt); Constructor<?> c = Object.class.getConstructors()[0]; ConstructorStatement cs = new ConstructorStatement(test2, new GenericConstructor(c, Object.class), new ArrayList<VariableReference>()); test2.addStatement(cs); double score = DiversityObserver.getNeedlemanWunschScore(test1, test2); Assert.assertTrue(score <= 0); }
private TestCase getTwoIntTest(int x, int y, int x1, int y1) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException { Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS); GenericClass clazz = new GenericClass(sut); DefaultTestCase test = new DefaultTestCase(); GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz); TestFactory testFactory = TestFactory.getInstance(); VariableReference callee = testFactory.addConstructor(test, gc, 0, 0); test.addStatement(new IntPrimitiveStatement(test, x)); test.addStatement(new IntPrimitiveStatement(test, y)); VariableReference wrongIntVar1 = test.addStatement(new IntPrimitiveStatement(test, x1)); VariableReference wrongIntVar2 = test.addStatement(new IntPrimitiveStatement(test, y1)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class}); GenericMethod method = new GenericMethod(m, sut); MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] {wrongIntVar1, wrongIntVar2})); test.addStatement(ms); return test; }
@Test public void testSelfSimilarity() { TestCase test = new DefaultTestCase(); PrimitiveStatement<?> aInt = new IntPrimitiveStatement(test, 42); test.addStatement(aInt); double score = DiversityObserver.getNeedlemanWunschScore(test, test); Assert.assertTrue(score > 0); }
@Test public void testBasicSimilarityDifferentTypes2() { TestCase test1 = new DefaultTestCase(); TestCase test2 = new DefaultTestCase(); PrimitiveStatement<?> aInt = new LongPrimitiveStatement(test1, 42L); test1.addStatement(aInt); PrimitiveStatement<?> aInt2 = new IntPrimitiveStatement(test1, 42); test1.addStatement(aInt2); PrimitiveStatement<?> bInt = new IntPrimitiveStatement(test2, 42); test2.addStatement(bInt); Constructor<?> c = Object.class.getConstructors()[0]; ConstructorStatement cs = new ConstructorStatement(test2, new GenericConstructor(c, Object.class), new ArrayList<VariableReference>()); test2.addStatement(cs); double score = DiversityObserver.getNeedlemanWunschScore(test1, test2); Assert.assertTrue(score <= 0); } }
private TestCase getIntTest(int x, int y) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException { Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS); GenericClass clazz = new GenericClass(sut); DefaultTestCase test = new DefaultTestCase(); GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz); TestFactory testFactory = TestFactory.getInstance(); VariableReference callee = testFactory.addConstructor(test, gc, 0, 0); test.addStatement(new IntPrimitiveStatement(test, x)); VariableReference wrongIntVar = test.addStatement(new IntPrimitiveStatement(test, y)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class}); GenericMethod method = new GenericMethod(m, sut); MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] {wrongIntVar})); test.addStatement(ms); return test; }
private TestCase getIntTest(int x, int y) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException { Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS); GenericClass clazz = new GenericClass(sut); DefaultTestCase test = new DefaultTestCase(); GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz); TestFactory testFactory = TestFactory.getInstance(); VariableReference callee = testFactory.addConstructor(test, gc, 0, 0); VariableReference intVar1 = test.addStatement(new IntPrimitiveStatement(test, x)); VariableReference intVar0 = test.addStatement(new IntPrimitiveStatement(test, y)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class }); GenericMethod method = new GenericMethod(m, sut); MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] {intVar0, intVar1})); test.addStatement(ms); return test; }
private TestCase getTwoIntTest(int x, int y) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException { Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS); GenericClass clazz = new GenericClass(sut); DefaultTestCase test = new DefaultTestCase(); GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz); TestFactory testFactory = TestFactory.getInstance(); VariableReference callee = testFactory.addConstructor(test, gc, 0, 0); VariableReference intVar1 = test.addStatement(new IntPrimitiveStatement(test, x)); VariableReference intVar2 = test.addStatement(new IntPrimitiveStatement(test, y)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class}); GenericMethod method = new GenericMethod(m, sut); MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] {intVar1, intVar2})); test.addStatement(ms); return test; }
private TestCase getTwoIntTest(int x, int y) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException { Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS); GenericClass clazz = new GenericClass(sut); DefaultTestCase test = new DefaultTestCase(); GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz); TestFactory testFactory = TestFactory.getInstance(); VariableReference callee = testFactory.addConstructor(test, gc, 0, 0); VariableReference intVar1 = test.addStatement(new IntPrimitiveStatement(test, x)); VariableReference intVar2 = test.addStatement(new IntPrimitiveStatement(test, y)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class}); GenericMethod method = new GenericMethod(m, sut); MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] {intVar1, intVar2})); test.addStatement(ms); return test; }
@Test public void testTwoTestsUniqueMethods() { TestCase test1 = new DefaultTestCase(); MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString"); test1.addCoveredGoal(goal1); TestCase test2 = new DefaultTestCase(); test2.addStatement(new IntPrimitiveStatement(test2, 0)); // Need to add statements to change hashCode MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "getSomeStuff"); test2.addCoveredGoal(goal2); List<TestCase> tests = new ArrayList<>(); tests.add(test1); tests.add(test2); CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests); assertEquals("testToString", naming.getName(test1)); assertEquals("testGetSomeStuff", naming.getName(test2)); }
@Test public void testOverloadedMethodWithObject() { TestCase test1 = new DefaultTestCase(); MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "foo()"); test1.addCoveredGoal(goal1); TestCase test2 = new DefaultTestCase(); test2.addStatement(new IntPrimitiveStatement(test2, 0)); // Need to add statements to change hashCode MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "foo(Ljava/util/List;)"); test2.addCoveredGoal(goal2); List<TestCase> tests = new ArrayList<>(); tests.add(test1); tests.add(test2); CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests); assertEquals("testFooTakingNoArguments", naming.getName(test1)); assertEquals("testFooTakingList", naming.getName(test2)); }
private TestCase getIntTest(int x) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException { Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS); GenericClass clazz = new GenericClass(sut); DefaultTestCase test = new DefaultTestCase(); GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz); TestFactory testFactory = TestFactory.getInstance(); VariableReference callee = testFactory.addConstructor(test, gc, 0, 0); VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, x)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class}); GenericMethod method = new GenericMethod(m, sut); MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] {intVar})); test.addStatement(ms); return test; }
private TestCase getIntTest(int x) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException { Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS); GenericClass clazz = new GenericClass(sut); DefaultTestCase test = new DefaultTestCase(); GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz); TestFactory testFactory = TestFactory.getInstance(); VariableReference callee = testFactory.addConstructor(test, gc, 0, 0); VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, x)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class}); GenericMethod method = new GenericMethod(m, sut); MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] {intVar})); test.addStatement(ms); return test; }
@Test public void testMethodWithAndWithoutMockException() { TestCase test1 = new DefaultTestCase(); MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString()"); test1.addCoveredGoal(goal1); MethodNoExceptionCoverageTestFitness goal1a = new MethodNoExceptionCoverageTestFitness("FooClass", "toString()"); test1.addCoveredGoal(goal1a); TestCase test2 = new DefaultTestCase(); test2.addStatement(new IntPrimitiveStatement(test2, 0)); // Need to add statements to change hashCode test2.addCoveredGoal(goal1); ExceptionCoverageTestFitness goal2 = new ExceptionCoverageTestFitness("FooClass", "toString()", MockArithmeticException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT); test2.addCoveredGoal(goal2); List<TestCase> tests = new ArrayList<>(); tests.add(test1); tests.add(test2); CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests); assertEquals("testToString", naming.getName(test1)); assertEquals("testToStringThrowsArithmeticException", naming.getName(test2)); }
@Test public void testTwoTestsMethodsWithSharedGoals() { TestCase test1 = new DefaultTestCase(); MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "toString"); test1.addCoveredGoal(goal1); TestCase test2 = new DefaultTestCase(); test2.addStatement(new IntPrimitiveStatement(test2, 0)); // Need to add statements to change hashCode MethodCoverageTestFitness goal2 = new MethodCoverageTestFitness("FooClass", "getSomeStuff"); test2.addCoveredGoal(goal2); MethodCoverageTestFitness goal3 = new MethodCoverageTestFitness("FooClass", "doFooBar"); test1.addCoveredGoal(goal3); test2.addCoveredGoal(goal3); List<TestCase> tests = new ArrayList<>(); tests.add(test1); tests.add(test2); CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests); assertEquals("testToString", naming.getName(test1)); assertEquals("testGetSomeStuff", naming.getName(test2)); }
@Test public void testConstructorWithAndWithoutException() { TestCase test1 = new DefaultTestCase(); MethodCoverageTestFitness goal1 = new MethodCoverageTestFitness("FooClass", "<init>()"); test1.addCoveredGoal(goal1); MethodNoExceptionCoverageTestFitness goal1a = new MethodNoExceptionCoverageTestFitness("FooClass", "<init>()"); test1.addCoveredGoal(goal1a); TestCase test2 = new DefaultTestCase(); test2.addStatement(new IntPrimitiveStatement(test2, 0)); // Need to add statements to change hashCode test2.addCoveredGoal(goal1); ExceptionCoverageTestFitness goal2 = new ExceptionCoverageTestFitness("FooClass", "<init>()", RuntimeException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT); test2.addCoveredGoal(goal2); List<TestCase> tests = new ArrayList<>(); tests.add(test1); tests.add(test2); CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests); assertEquals("testCreatesFooClass", naming.getName(test1)); assertEquals("testFailsToCreateFooClassThrowsRuntimeException", naming.getName(test2)); }
private TestCase getArrayTest(int length) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException { Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS); GenericClass clazz = new GenericClass(sut); DefaultTestCase test = new DefaultTestCase(); GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz); TestFactory testFactory = TestFactory.getInstance(); VariableReference callee = testFactory.addConstructor(test, gc, 0, 0); VariableReference arrayVar = test.addStatement(new ArrayStatement(test, int[].class, length)); for(int i = 0; i < length; i++) { // Add value VariableReference intVar = test.addStatement(new IntPrimitiveStatement(test, 0)); test.addStatement(new AssignmentStatement(test, new ArrayIndex(test, (ArrayReference) arrayVar, i), intVar)); } Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int[].class }); GenericMethod method = new GenericMethod(m, sut); MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] {arrayVar})); test.addStatement(ms); return test; }
@Test public void testSame(){ TestCase tc = new DefaultTestCase(); PrimitiveStatement<?> aInt = new IntPrimitiveStatement(tc,42); Assert.assertTrue(aInt.same(aInt)); Assert.assertFalse(aInt.same(null)); PrimitiveStatement<?> fooString = new StringPrimitiveStatement(tc,"foo"); Assert.assertFalse(aInt.same(fooString)); PrimitiveStatement<?> nullString = new StringPrimitiveStatement(tc,null); Assert.assertFalse(nullString.same(fooString)); Assert.assertFalse(fooString.same(nullString)); //TODO: how to make it work? //PrimitiveStatement<?> anotherNullString = new StringPrimitiveStatement(tc,null); //Assert.assertTrue(nullString.same(anotherNullString)); //Assert.assertTrue(anotherNullString.same(nullString)); } }