| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MockObjectFactory |
|
| 0.0;0 |
| 1 | package biz.xsoftware.mock; | |
| 2 | ||
| 3 | ||
| 4 | /** | |
| 5 | * The factory class used to create MockObjects from one or many interfaces. | |
| 6 | * | |
| 7 | * @author Dean Hiller | |
| 8 | */ | |
| 9 | public abstract class MockObjectFactory { | |
| 10 | ||
| 11 | private static MockObjectFactory factory; | |
| 12 | ||
| 13 | 14 | protected MockObjectFactory() {} |
| 14 | ||
| 15 | /** | |
| 16 | * Creates a MockObject based on your interface. One way to use it | |
| 17 | * for a mock listener is below.<br/><br/> | |
| 18 | * See examples for other non-listener mock objects. | |
| 19 | * <PRE> | |
| 20 | * MockSuperclass mockActionList = MockObjectFactory.createMock(ActionListener.class); | |
| 21 | * button.addActionListener((ActionListener)mockActionList); | |
| 22 | * button.setPressed(true); | |
| 23 | * Object o = mockActionList.expectEvent("actionPerformed"); | |
| 24 | * ActionEvent evt = (ActionEvent)o; | |
| 25 | * assertEquals(evt.getSource(), button); | |
| 26 | * </PRE> | |
| 27 | * @param theInterface you want the mockObject to implement. | |
| 28 | * @param id The object id used in the logs so you can differentiate mockobjects | |
| 29 | * @return The mockObject that can receive events, and JUnit test | |
| 30 | * cases can call expect event on. | |
| 31 | */ | |
| 32 | public static MockObject createMock(String id, Class<?> theInterface) { | |
| 33 | 0 | return createMock(id, new Class[] { theInterface }); |
| 34 | } | |
| 35 | ||
| 36 | /** | |
| 37 | * Creates a MockObject based on your interface. One way to use it | |
| 38 | * for a mock listener is below.<br/><br/> | |
| 39 | * See examples for other non-listener mock objects. | |
| 40 | * <PRE> | |
| 41 | * MockSuperclass mockActionList = MockObjectFactory.createMock(ActionListener.class); | |
| 42 | * button.addActionListener((ActionListener)mockActionList); | |
| 43 | * button.setPressed(true); | |
| 44 | * Object o = mockActionList.expectEvent("actionPerformed"); | |
| 45 | * ActionEvent evt = (ActionEvent)o; | |
| 46 | * assertEquals(evt.getSource(), button); | |
| 47 | * </PRE> | |
| 48 | * @param theInterface you want the mockObject to implement. | |
| 49 | * @return The mockObject that can receive events, and JUnit test | |
| 50 | * cases can call expect event on. | |
| 51 | */ | |
| 52 | public static MockObject createMock(Class<?> theInterface) { | |
| 53 | 98 | return createMock(new Class<?>[] { theInterface }); |
| 54 | } | |
| 55 | ||
| 56 | /** | |
| 57 | * Creates a MockObject based on your interface. One way to use it | |
| 58 | * for mock listeners is below(This can help guarantee order between | |
| 59 | * two different listeners).<br/><br/> | |
| 60 | * See examples for other non-listener mock objects. | |
| 61 | * <PRE> | |
| 62 | * Class[] c = new Class[] {ActionListener.class, WindowListener.class}; | |
| 63 | * MockSuperclass mockList = MockObjectFactory.createMock(c); | |
| 64 | * button.addActionListener((ActionListener)mockList); | |
| 65 | * window.addWindowListener((WindowListener)mockList); | |
| 66 | * button.setPressed(true); | |
| 67 | * Object o = mockList.expectEvent("actionPerformed"); | |
| 68 | * ActionEvent evt = (ActionEvent)o; | |
| 69 | * assertEquals(evt.getSource(), button); | |
| 70 | * </PRE> | |
| 71 | * @param interfaces The interfaces you want the mockObject to implement. | |
| 72 | * @return The mockObject that can receive events, and JUnit test | |
| 73 | * cases can call expect event on. | |
| 74 | */ | |
| 75 | public static MockObject createMock(Class<?>[] interfaces) { | |
| 76 | 98 | return createMock("no id specified", interfaces); |
| 77 | } | |
| 78 | ||
| 79 | /** | |
| 80 | * Creates a MockObject based on your interface. One way to use it | |
| 81 | * for mock listeners is below(This can help guarantee order between | |
| 82 | * two different listeners).<br/><br/> | |
| 83 | * See examples for other non-listener mock objects. | |
| 84 | * <PRE> | |
| 85 | * Class[] c = new Class[] {ActionListener.class, WindowListener.class}; | |
| 86 | * MockSuperclass mockList = MockObjectFactory.createMock(c); | |
| 87 | * button.addActionListener((ActionListener)mockList); | |
| 88 | * window.addWindowListener((WindowListener)mockList); | |
| 89 | * button.setPressed(true); | |
| 90 | * Object o = mockList.expectEvent("actionPerformed"); | |
| 91 | * ActionEvent evt = (ActionEvent)o; | |
| 92 | * assertEquals(evt.getSource(), button); | |
| 93 | * </PRE> | |
| 94 | * @param interfaces The interfaces you want the mockObject to implement. | |
| 95 | * @return The mockObject that can receive events, and JUnit test | |
| 96 | * cases can call expect event on. | |
| 97 | */ | |
| 98 | public static synchronized MockObject createMock(String id, Class<?>[] interfaces) { | |
| 99 | 98 | if(factory == null) { |
| 100 | 14 | loadFactory(); |
| 101 | } | |
| 102 | 98 | return factory.createMockImpl("[" + id + "]", interfaces); |
| 103 | } | |
| 104 | ||
| 105 | private static void loadFactory() { | |
| 106 | 14 | String className = "biz.xsoftware.impl.mock.MockObjectFactoryImpl"; |
| 107 | try { | |
| 108 | 14 | Class<?> clazz = Class.forName(className); |
| 109 | 14 | factory = (MockObjectFactory) clazz.newInstance(); |
| 110 | 0 | } catch (ClassNotFoundException e) { |
| 111 | 0 | throw new RuntimeException(e); |
| 112 | 0 | } catch (InstantiationException e) { |
| 113 | 0 | throw new RuntimeException(e); |
| 114 | 0 | } catch (IllegalAccessException e) { |
| 115 | 0 | throw new RuntimeException(e); |
| 116 | 14 | } |
| 117 | 14 | } |
| 118 | ||
| 119 | public abstract MockObject createMockImpl(String id, Class<?>[] interfaces); | |
| 120 | } |