public VariableReference addConstructor(TestCase test, GenericConstructor constructor, int position, int recursionDepth) throws ConstructionFailedException { return addConstructor(test, constructor, null, position, recursionDepth); }
@Test public void testUniqueConstructors() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); Properties.JEE = true; factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); //doing it a second time should fail try { factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); Assert.fail(); } catch (Exception e){ //expected } }
@Test(expected=ConstructionFailedException.class) public void testFieldForWrongPosition() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException, NoSuchFieldException { TestFactory testFactory = TestFactory.getInstance(); Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName()); GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut); GenericField field = new GenericField(sut.getField("setMe"), sut); DefaultTestCase test = new DefaultTestCase(); Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0; Properties.OBJECT_REUSE_PROBABILITY = 0.0; VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0); testFactory.reset(); testFactory.addFieldFor(test, var1, field, 0); }
@Test(expected=ConstructionFailedException.class) public void testMethodForWrongPosition() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException { TestFactory testFactory = TestFactory.getInstance(); Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName()); GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut); GenericMethod method = new GenericMethod(sut.getMethod("testByte", byte.class, byte.class), sut); DefaultTestCase test = new DefaultTestCase(); Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0; Properties.OBJECT_REUSE_PROBABILITY = 0.0; VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0); testFactory.reset(); testFactory.addMethodFor(test, var1, method, 0); }
@Test public void testBaseTest() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); factory.addConstructor(tc.getTestCase(), new GenericConstructor(Object.class.getConstructor(), Object.class), 0, 0); Assert.assertEquals(1, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@Test public void testEvoSuiteClassExclude() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); //shouldn't be able to instantiate EvoServletConfig directly factory.addConstructor(tc.getTestCase(), new GenericConstructor(EvoServletConfig.class.getConstructor(), EvoServletConfig.class), 0, 0); Assert.assertEquals(1, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); } }
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 testInitializingBoundedVariable_wrong_callingMethodsBeforeInit() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); factory.addMethodFor(tc.getTestCase(), servlet, new GenericMethod(FakeServlet.class.getDeclaredMethod("foo"), FakeServlet.class), 1); //initializing bounding variable method cannot be called here after "foo" is called on the bounded variable factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct",Object.class), Injector.class), 2, 0); Assert.assertEquals(3, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); }
@Test public void testInitializingBoundedVariable_correct() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); //initializing bounding variable method called directly after the new factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct",Object.class), Injector.class), 1, 0); //method on servlet after the bounding variable initialization: it is ok factory.addMethodFor(tc.getTestCase(), servlet, new GenericMethod(FakeServlet.class.getDeclaredMethod("foo"), FakeServlet.class), 2); Assert.assertEquals(3, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@Test public void testInitializingBoundedVariable_wrong_inputInOtherMethodBeforeInit() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("takeServletAsInput", FakeServlet.class), ConstraintVerifierTest.class), 1, 0); //initializing bounding variable method cannot be called here after the bounded variable has been used as input in some other method factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct",Object.class), Injector.class), 2, 0); Assert.assertEquals(3, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); }
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 getFloatTest(float x, float 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 FloatPrimitiveStatement(test, x)); VariableReference intVar0 = test.addStatement(new FloatPrimitiveStatement(test, y)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { float.class, float.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 getDoubleTest(double x, double 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 DoublePrimitiveStatement(test, x)); VariableReference intVar0 = test.addStatement(new DoublePrimitiveStatement(test, y)); Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { double.class, double.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; }
@Test public void testInitializingBoundedVariable_correct_severalCalls() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); //both calls on same bounding variable factory.addMethod(tc.getTestCase(), new GenericMethod( ConstraintVerifierTest.class.getDeclaredMethod("fakeInjection",Servlet.class), ConstraintVerifierTest.class), 1, 0); //this is an atMostOnce type factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct",Object.class), Injector.class), 2, 0); Assert.assertEquals(3, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
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 testAfter() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); //this method has an "after" constraint on initServlet factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getRequest"), EvoServletState.class), 0, 0); Assert.assertEquals(1, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); VariableReference con = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet",Servlet.class), EvoServletState.class), 1, 0); Assert.assertEquals(3, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@Test public void testInitializingBoundedVariable_wrong_atMostOnce() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); //initializing bounding variable method called directly after the new factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0); Assert.assertEquals(2, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); // this should be invalid, as executePostConstruct can be used only once on same bounded variable factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct",Object.class), Injector.class), 2, 0); Assert.assertEquals(3, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); }