| 1 | |
package biz.xsoftware.mock.testcase; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.List; |
| 5 | |
import java.util.logging.Level; |
| 6 | |
import java.util.logging.Logger; |
| 7 | |
import java.util.logging.SimpleFormatter; |
| 8 | |
|
| 9 | |
import junit.framework.TestCase; |
| 10 | |
import biz.xsoftware.mock.MockObject; |
| 11 | |
import biz.xsoftware.mock.MockObjectFactory; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
public abstract class MockTestCase extends TestCase { |
| 26 | 1 | private static final Logger log = Logger.getLogger(MockTestCase.class.getName()); |
| 27 | |
|
| 28 | |
private String testName; |
| 29 | |
|
| 30 | |
private List<MockObject> mocks; |
| 31 | |
|
| 32 | |
private HandlerForTests currentHandler; |
| 33 | |
|
| 34 | 3 | private int numberOfExpectedWarnings = 0; |
| 35 | |
|
| 36 | |
public MockTestCase() { |
| 37 | 0 | super(); |
| 38 | 0 | } |
| 39 | |
|
| 40 | |
public MockTestCase(String name) { |
| 41 | 3 | super(name); |
| 42 | 3 | testName = name; |
| 43 | 3 | } |
| 44 | |
|
| 45 | |
@Override |
| 46 | |
protected final void setUp() throws Exception { |
| 47 | 3 | super.setUp(); |
| 48 | 3 | currentHandler = new HandlerForTests(); |
| 49 | 3 | Logger.getLogger("").addHandler(currentHandler); |
| 50 | 3 | mocks = new ArrayList<MockObject>(); |
| 51 | 3 | numberOfExpectedWarnings = 0; |
| 52 | 3 | setUpImpl(); |
| 53 | 3 | } |
| 54 | |
|
| 55 | |
@Override |
| 56 | |
protected final void tearDown() throws Exception { |
| 57 | 3 | super.tearDown(); |
| 58 | 3 | tearDownImpl(); |
| 59 | |
|
| 60 | 3 | List<LogAndStack> failures = currentHandler.getFailures(); |
| 61 | |
|
| 62 | 3 | if (numberOfExpectedWarnings == 0 && failures.size() != 0) { |
| 63 | 1 | SimpleFormatter form = new SimpleFormatter(); |
| 64 | 1 | LogAndStack logAndStack = failures.get(0); |
| 65 | 1 | String logRec = form.format(logAndStack.getRecord()); |
| 66 | 1 | Throwable howItWasCalled = logAndStack.getHowItWasCalled(); |
| 67 | 1 | log.log(Level.INFO, howItWasCalled.getMessage()); |
| 68 | |
|
| 69 | 1 | throw new LogHasWarningException("Log contains " + failures.size() + " warnings, or errors. " |
| 70 | |
+ "If these are expected, call super.setNumberOfExpectedWarnings(" + failures.size() + ") " |
| 71 | |
+ "anywhere in the test case. first failure record=\n" + logRec |
| 72 | |
+ "\nHere is how Logger.log method was called on the first log....\n", |
| 73 | |
logAndStack.getRecord().getMessage()); |
| 74 | 2 | } else if (numberOfExpectedWarnings != failures.size()) { |
| 75 | 1 | throw new LogHasNoWarningsException("The number of warnings was=" + failures.size() + " but" |
| 76 | |
+ "you expected " + numberOfExpectedWarnings + " warnings or errors"); |
| 77 | |
} |
| 78 | |
|
| 79 | 1 | for (MockObject mock : mocks) { |
| 80 | 1 | mock.expect(MockObject.NONE); |
| 81 | 0 | } |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | 0 | Logger.getLogger("").removeHandler(currentHandler); |
| 86 | 0 | } |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
protected abstract void tearDownImpl() throws Exception; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
protected abstract void setUpImpl() throws Exception; |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
@Override |
| 106 | |
public void runBare() throws Throwable { |
| 107 | |
|
| 108 | 3 | String method = testName; |
| 109 | 3 | log.info("TEST STARTING=" + method); |
| 110 | |
try { |
| 111 | 3 | super.runBare(); |
| 112 | 3 | } catch (Throwable e) { |
| 113 | 3 | log.warning("TEST FAILED=" + method); |
| 114 | 3 | throw e; |
| 115 | 0 | } |
| 116 | 0 | log.info("TEST PASSED=" + method); |
| 117 | 0 | } |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
protected void setNumberOfExpectedWarnings(int num) { |
| 128 | 1 | this.numberOfExpectedWarnings = num; |
| 129 | 1 | } |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
@Deprecated |
| 136 | |
protected void setExpectWarning(boolean b) { |
| 137 | 0 | if (b) { |
| 138 | 0 | numberOfExpectedWarnings = 1; |
| 139 | 0 | } else { |
| 140 | 0 | numberOfExpectedWarnings = 0; |
| 141 | |
} |
| 142 | 0 | } |
| 143 | |
|
| 144 | |
protected List<LogAndStack> getWarningsLogged() { |
| 145 | 0 | return currentHandler.getFailures(); |
| 146 | |
} |
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
protected MockObject createMock(Class<?> interfaze) { |
| 156 | 1 | if (mocks == null) |
| 157 | 0 | throw new IllegalStateException( |
| 158 | |
"You must call createMock in the setUpImpl method or in the test not before"); |
| 159 | |
|
| 160 | 1 | MockObject object = MockObjectFactory.createMock(interfaze); |
| 161 | 1 | mocks.add(object); |
| 162 | 1 | return object; |
| 163 | |
} |
| 164 | |
|
| 165 | |
} |