Coverage Report - biz.xsoftware.mock.testcase.MockTestCase
 
Classes in this File Line Coverage Branch Coverage Complexity
MockTestCase
72%
38/53
80%
4/5
0
 
 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  
  * This is a special test case that does two things
 15  
  * <ol>
 16  
  * <li>Verifies logs with either of the following</li>
 17  
  * <ol>
 18  
  * <li>Verifies there are no warning logs</li>
 19  
  * <li>Verifies warning logs exist as they should exist</li>
 20  
  * </ol>
 21  
  * <li>For mocks created using MockTestCase.createMock, this test case will
 22  
  * verify in teardown that no more methods were called on the mock</li>
 23  
  * </ol>
 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  
         // make sure the currentHandler gets removed and doesn't stay in memory
 84  
         // forever while tests run
 85  0
         Logger.getLogger("").removeHandler(currentHandler);
 86  0
     }
 87  
 
 88  
     /**
 89  
      * A method the subclass of MockTestCase needs to implement
 90  
      * 
 91  
      * @throws Exception
 92  
      */
 93  
     protected abstract void tearDownImpl() throws Exception;
 94  
 
 95  
     /**
 96  
      * A method the subclass of MockTestCase needs to implement
 97  
      * 
 98  
      * @throws Exception
 99  
      */
 100  
     protected abstract void setUpImpl() throws Exception;
 101  
 
 102  
     /**
 103  
      * @see junit.framework.TestCase#runBare()
 104  
      */
 105  
     @Override
 106  
     public void runBare() throws Throwable {
 107  
         // String method = getClass().getName()+"."+testName;
 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  
      * If this test will result in warnings recorded in the log, the test needs
 121  
      * to call this method so it won't fail. MockTestCase in tearDown checks the
 122  
      * number of warnings in the log and if they don't match, it will fail the
 123  
      * test.
 124  
      * 
 125  
      * @param num
 126  
      */
 127  
     protected void setNumberOfExpectedWarnings(int num) {
 128  1
         this.numberOfExpectedWarnings = num;
 129  1
     }
 130  
 
 131  
     /**
 132  
      * @deprecated Use {@link #setNumberOfExpectedWarnings(int)} instead
 133  
      * @param b
 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  
      * All mockobjects created through this will be checked at teardown to make
 150  
      * sure there are no methods called that your test forgot to check.
 151  
      * 
 152  
      * @param interfaze
 153  
      * @return
 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  
 }