[Contestj-developer] contestj/contestj2/src/test/java/org/contestj/test/backport/test TestTestCase
Status: Inactive
Brought to you by:
thomasra
Update of /cvsroot/contestj/contestj/contestj2/src/test/java/org/contestj/test/backport/test In directory sc8-pr-cvs4.sourceforge.net:/tmp/cvs-serv14426/contestj2/src/test/java/org/contestj/test/backport/test Added Files: TestTestCase.java TestFactory.java Test2.java TestInterface.java Test.java Log Message: Initial upload of Contestj2 even though it supports most of the Contestj features its not ready to be used atm. --- NEW FILE: TestInterface.java --- package org.contestj.test.backport.test; /** * Part of the contestJ test framework. * * Distributed under the terms of the LGPL. * * Copyright (C) 2005 Thomas Roka-Aardal * * @author Thomas Roka-Aardal * * @mock * */ public interface TestInterface { public String doStuff(); } --- NEW FILE: TestTestCase.java --- package org.contestj.test.backport.test; import java.util.ArrayList; import java.util.List; import junit.framework.TestCase; import org.contestj.introduction.Mock; //import org.contestj.duplicator.Duplicator; import org.contestj.test.backport.example.BaseClass; import org.contestj.test.backport.example.Java5BaseClass; import org.contestj.mockservice.ContestJSetup; import org.contestj.mockservice.MockMethod; import org.contestj.mockservice.MockService; import org.contestj.mockservice.MockInvocationHandler; /** * Part of the contestJ test framework. * * Distributed under the terms of the LGPL. * * Copyright (C) 2005 Thomas Roka-Aardal * * @author Thomas Roka-Aardal * */ public class TestTestCase extends TestCase { public void setUp() throws Exception { super.setUp(); } public void tearDown() throws Exception { MockService.getInstance().reset(); super.tearDown(); } /** * "Replace a method call on a class with a configured MockMethod if available". * * This test attempts to do the same as the previous test, but without using interfaces and * mock objects. This is how we would do it if there is a specific method on a specific class * we would like to return something we can control. Enter MockMethod, which exposes similar * functionality as MockObjects, but is per method and can be used directly on classes. In * this example we obtain the Class and Method we would like to "swap" with a controlled version * by using reflection, and then we set the expected result value using setupInvoke(). Look in * jboss-aop.xml to see that the pointcut that allows this is the one that explicitly mentions * Test->doStuff and calls the "getMethod" advice. This advice checks if there are any mock methods * on our ConductMockService that match the intercepted Class::Method, and returns the mock method * result instead of the original one. Nice. * * @throws Exception */ public void testDoStuffWithMockMethod() throws Exception { Test test = new Test(); MockMethod mockMethod = new MockMethod(Test.class.getName(), "doStuff"); String result = "Mocked string by method"; mockMethod.setupInvoke(result); MockService.getInstance().addMockMethod(mockMethod); assertEquals("Should have been mocked by method string: ", result, test.doStuff()); test = null; } /** * "Replace a method call on a class with a configured MockMethod if available". * * This test attempts to do the same as the previous test waits a period before invoking. * * @throws Exception */ public void testDoStuffWithMockMethodAndWait() throws Exception { long waitTime = 500L; long startTime = System.currentTimeMillis(); Test test = new Test(); MockMethod mockMethod = new MockMethod(Test.class.getName(), "doStuff", waitTime); String result = "Mocked string by method"; mockMethod.setupInvoke(result); MockService.getInstance().addMockMethod(mockMethod); assertEquals("Should have been mocked by method string: ", result, test.doStuff()); long executionTime = System.currentTimeMillis() - startTime; assertTrue("Should have taken at least " + waitTime + " msec. executiontime", executionTime > waitTime); test = null; } /** * "Replace method call on class with method call on mock". * * This test makes use of classes mocked with mockobjects (not interfaces), and they need to be created * (for now) "manually" in the build script (see the __gen-mocks target). Look at jboss-aop.xml to see * that the pointcut that triggers this behaviour is the one that explicitly mentions all methods on * Test2 (Test2->*()). The advice method replaces a call to the real object to a call on the mock * object found on ConductMockService matching the Class on which the method is located. So when Test2->doStuff * is called, it checks to see if there are any MockTest2 classes registered, and delegates to them. * Note that this is different from the "Replace a call that returns an interface with one that returns * a mock of the interface if available" which you would use for instance for factories (like above). * It doesn't return your mock, it just calls it instead. Very magical. * * @throws Exception */ public void testDoStuffWithMockedClass() throws Exception { BaseClass test = new BaseClass(); // //BaseClass_duplicate mockTest = new BaseClass_duplicate(); // mockTest.setup_getValue(42); // MockService.getInstance().addMock(mockTest); //assertEquals("Should have been mocked value", 42, test.getValue()); // mockTest = null; // test = null; ((Mock) test).setupMock("getValue", 42); MockService.getInstance().addMock(test); assertEquals("Should have been mocked value", 42, test.getValue()); test = null; } /** * Same as test above, only now also tests the sequence in which things are returned * * @throws Exception */ public void testDoStuffWithMockedClassRepeatedly() throws Exception { BaseClass test = new BaseClass(); //BaseClass_duplicate mockTest = new BaseClass_duplicate(); // mockTest.setup_getValue(42); // mockTest.setup_getValue(666); // mockTest.setup_getValue(21); ((Mock) test).setupMock("getValue", 42); ((Mock) test).setupMock("getValue", 666); ((Mock) test).setupMock("getValue", 21); MockService.getInstance().addMock(test); assertEquals("Should have been mocked value", 42, test.getValue()); assertEquals("Should have been mocked value", 666, test.getValue()); assertEquals("Should have been mocked value", 21, test.getValue()); try { test.getValue(); fail("Should have thrown Exception when no more values exist!"); } catch(Exception e) { // Correct } //mockTest = null; test = null; } /** * "Replace static method call on class with method call on a configured MockMethod if available". * * @throws Exception */ public void testDoStuffWithStaticMockedMethod() throws Exception { MockMethod mm = new MockMethod(Test.class.getName(), "doStaticStuff"); String result = "This is a MOCKED result"; mm.setupInvoke(result); MockService.getInstance().addMockMethod(mm); assertEquals("Should have been mocked by mock method string: ", result, Test.doStaticStuff()); } /** * This test tests for that repreated calls to the same method will return the * configured objects (trough setupInvoke) in the proper order. * The order should be FIFO. (queue) */ public void testDoStuffWithMockMethodProperReturnedObjectsOrder() throws Exception { Test test = new Test(); MockMethod mockMethod = new MockMethod(Test.class.getName(), "doStuff"); String obj1 = "A1"; String obj2 = "B2"; String obj3 = "C3"; String obj4 = "D4"; mockMethod.setupInvoke(obj1); mockMethod.setupInvoke(obj2); mockMethod.setupInvoke(obj3); mockMethod.setupInvoke(obj4); MockService.getInstance().addMockMethod(mockMethod); assertEquals("Should have been mocked by method string: ", obj1, test.doStuff()); assertEquals("Should have been mocked by method string: ", obj2, test.doStuff()); assertEquals("Should have been mocked by method string: ", obj3, test.doStuff()); assertEquals("Should have been mocked by method string: ", obj4, test.doStuff()); test = null; } /** * This test verifies that we can make a duplicate class method throw an exception which * is set up by the test. This allows testing of various rainy day scenarios (like getting * HTTPClient to throw a timeout exception). * * @throws Exception */ public void testThrowExceptionFromDuplicate() throws Exception { // BaseInterface_duplicate dupe = new BaseInterface_duplicate(); // dupe.setup_getName_exception(new RuntimeException("From test")); BaseClass dupe = new BaseClass(); ((Mock) dupe).setupExceptionMock("getValue", new RuntimeException("From test")); try { dupe.getValue(); fail("Should have thrown exception!"); } catch(Exception e) { // Correct } } /** * This method tests that we can actually delay any method, but still return * whatever the original method should return. The alternative to this, timed * execution with a mocked result, can be achieved using the three-argument setupInvoke * method. * * @throws Exception */ public void testDoStuffWithMockMethodTimedOnly() throws Exception { Test test = new Test(); long timeToWait = 500L; MockMethod mockMethod = new MockMethod(Test.class.getName(), "doStuff"); mockMethod.setTimedOnly(true, timeToWait); //mockMethod.setupInvoke("Original string"); MockService.getInstance().addMockMethod(mockMethod); long startTime = System.currentTimeMillis(); assertEquals("Should have returned original string value", "Original string", test.doStuff()); assertTrue("Should have taken more than " + timeToWait + " msecs", System.currentTimeMillis() - startTime > timeToWait); } /** * This test just assures us that we support the mocking of methods with the same name * but with different sets of arguments. The Test class has two "doStuff" methods, one * of which takes a String as input. This test verifies (through the use of setTimedOnly) * that the original class is called and that the correct method of the two is called. * * @throws Exception */ public void testMockMethodWithMultipleMethodsWithSameName() throws Exception { Test test = new Test(); long timeToWait = 0L; String input = "input"; MockMethod mockMethod = new MockMethod(Test.class.getName(), "doStuff", new Class[] {String.class}); mockMethod.setTimedOnly(true, timeToWait); MockService.getInstance().addMockMethod(mockMethod); assertEquals("Should have been original value", "Input was: " + input, test.doStuff(input)); } /** * This test makes use of in-memory duplication so no class files are actually written to * disk. When using in-memory duplication one cannot cast the duplicate to its own type, * therefore one needs to use the utility class ContestJSetup (which uses reflection) to * setup the methods for testing accordingly. * * @throws Exception */ public void testInMemoryDuplication() throws Exception { BaseClass base = new BaseClass(); int result = 42; //BaseClass dupe = (BaseClass) Duplicator.duplicate(BaseClass.class.getName()); ContestJSetup.setup(base, "getValue", new Class[0], result); MockService.getInstance().addMock(base); assertEquals("Should have returned mocked value", 42, base.getValue()); } /** * Similar to the above in-memory test, except tests for java5 generics. * * @throws Exception */ public void testInMemoryJava5Duplication() throws Exception { Java5BaseClass base = new Java5BaseClass(); List<String> result = new ArrayList<String>(); result.add(new String("123")); // Java5BaseClass dupe = (Java5BaseClass) Duplicator.duplicate(Java5BaseClass.class.getName()); ContestJSetup.setup(base, "getValue", new Class[0], result); MockService.getInstance().addMock(base); assertEquals("Should have returned mocked value", result, base.getValue()); } public void testMockMethodWithInvocationObject() throws Exception { Test test = new Test(); //long timeToWait = 0L; String input = "input"; MockInvocationHandler handler = new MockInvocationHandler() { public Object invoke(Object[] args) { //String s = (String) args; System.out.println("Inside invoke object, got: "+args[0].toString()); //return "blada"; return args[0]; } public String toString() { return "blada"; } }; MockMethod mockMethod = new MockMethod(Test.class.getName(), "doInvocationStuff", new Class[] {String.class} ); mockMethod.setInvocationHandler(handler); //mockMethod.setTimedOnly(true, timeToWait); MockService.getInstance().addMockMethod(mockMethod); System.out.println("testing...."); assertEquals("Should have been original value", input, test.doInvocationStuff(input)); //assertEquals("Should have been original value", input, test.doStuff(input)); } } --- NEW FILE: TestFactory.java --- package org.contestj.test.backport.test; /** * Part of the contestJ test framework. * * Distributed under the terms of the LGPL. * * Copyright (C) 2005 Thomas Roka-Aardal * * @author Thomas Roka-Aardal * */ public class TestFactory { public TestInterface getTestInterfaceImplementation() { return new Test2(); } } --- NEW FILE: Test2.java --- package org.contestj.test.backport.test; /** * Part of the contestJ test framework. * * Distributed under the terms of the LGPL. * * Copyright (C) 2005 Thomas Roka-Aardal * * @author Thomas Roka-Aardal * */ public class Test2 implements TestInterface { public String doStuff() { return "Original string"; } } --- NEW FILE: Test.java --- package org.contestj.test.backport.test; /** * Part of the contestJ test framework. * * Distributed under the terms of the LGPL. * * Copyright (C) 2005 Thomas Roka-Aardal * * @author Thomas Roka-Aardal * */ public class Test { private TestInterface test2 = null; public String doStuff() { TestFactory factory = new TestFactory(); test2 = factory.getTestInterfaceImplementation(); return test2.doStuff(); } public String doStuff(String value) { return "Input was: " + value; } public static String doStaticStuff() { return "This is a static Hello"; } public String doInvocationStuff(String value) { return "foo"; } } |