1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.scriptengine;
23
24 import static java.util.Arrays.asList;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNull;
28 import static org.mockito.Mockito.inOrder;
29 import static org.mockito.Mockito.verifyNoMoreInteractions;
30 import static org.mockito.Mockito.when;
31 import net.grinder.script.Test.InstrumentationFilter;
32 import net.grinder.scriptengine.CompositeInstrumenter;
33 import net.grinder.scriptengine.Instrumenter;
34 import net.grinder.scriptengine.Recorder;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.InOrder;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41
42
43
44
45
46
47
48 public class TestCompositeInstrumenter {
49
50 @Mock private Recorder m_recorder;
51 @Mock private Instrumenter m_instrumenter1;
52 @Mock private Instrumenter m_instrumenter2;
53 @Mock private InstrumentationFilter m_filter;
54 @Mock private net.grinder.common.Test m_test;
55
56 private Object m_target = new Object();
57
58 @Before public void setUp() throws Exception {
59 MockitoAnnotations.initMocks(this);
60 }
61
62 @Test public void testCreateInstrumentedProxy() throws Exception {
63
64 when(m_instrumenter2.createInstrumentedProxy(m_test, m_recorder, m_target))
65 .thenReturn(m_target);
66
67 final Instrumenter instrumenter =
68 new CompositeInstrumenter(m_instrumenter1, m_instrumenter2);
69
70 instrumenter.createInstrumentedProxy(m_test, m_recorder, m_target);
71
72 final InOrder inOrder = inOrder(m_instrumenter1, m_instrumenter2);
73
74 inOrder.verify(m_instrumenter1)
75 .createInstrumentedProxy(m_test, m_recorder, m_target);
76 inOrder.verify(m_instrumenter2)
77 .createInstrumentedProxy(m_test, m_recorder, m_target);
78
79 verifyNoMoreInteractions(m_instrumenter1, m_instrumenter2);
80 }
81
82 @Test public void testCreateInstrumentedProxyWithNull() throws Exception {
83 final Instrumenter instrumenter =
84 new CompositeInstrumenter(asList(m_instrumenter1, m_instrumenter2));
85
86 final Object result =
87 instrumenter.createInstrumentedProxy(m_test, m_recorder, null);
88 assertNull(result);
89 }
90
91 @Test public void testCreateInstrumentedProxyFailure() throws Exception {
92 final Instrumenter instrumenter =
93 new CompositeInstrumenter(asList(m_instrumenter1, m_instrumenter2));
94
95 final Object result =
96 instrumenter.createInstrumentedProxy(m_test, m_recorder, m_target);
97 assertNull(result);
98 }
99
100 @Test public void testInstrument() throws Exception {
101 when(m_instrumenter2.instrument(m_test, m_recorder, m_target))
102 .thenReturn(true);
103
104 final Instrumenter instrumenter =
105 new CompositeInstrumenter(asList(m_instrumenter1, m_instrumenter2));
106
107 instrumenter.instrument(m_test, m_recorder, m_target);
108
109 final InOrder inOrder = inOrder(m_instrumenter1, m_instrumenter2);
110
111 inOrder.verify(m_instrumenter1)
112 .instrument(m_test, m_recorder, m_target, Instrumenter.ALL_INSTRUMENTATION);
113 inOrder.verify(m_instrumenter2)
114 .instrument(m_test, m_recorder, m_target, Instrumenter.ALL_INSTRUMENTATION);
115
116 verifyNoMoreInteractions(m_instrumenter1, m_instrumenter2);
117 }
118
119 @Test public void testInstrumentWithNull() throws Exception {
120 final Instrumenter instrumenter =
121 new CompositeInstrumenter(asList(m_instrumenter1, m_instrumenter2));
122
123 final boolean result =
124 instrumenter.instrument(m_test, m_recorder, null, m_filter);
125 assertFalse(result);
126 }
127
128 @Test public void testCreateInstrumentedFailure() throws Exception {
129 final Instrumenter instrumenter =
130 new CompositeInstrumenter(asList(m_instrumenter1, m_instrumenter2));
131
132 final boolean result =
133 instrumenter.instrument(m_test, m_recorder, null, m_filter);
134 assertFalse(result);
135 }
136
137 @Test public void testGetDescription() throws Exception {
138 when(m_instrumenter1.getDescription()).thenReturn("I1");
139 when(m_instrumenter2.getDescription()).thenReturn("I2");
140
141 assertEquals("I1; I2",
142 new CompositeInstrumenter(m_instrumenter1, m_instrumenter2)
143 .getDescription());
144
145 assertEquals("I1",
146 new CompositeInstrumenter(m_instrumenter1).getDescription());
147
148 assertEquals("", new CompositeInstrumenter().getDescription());
149
150 when(m_instrumenter2.getDescription()).thenReturn(null);
151
152 assertEquals("I1",
153 new CompositeInstrumenter(m_instrumenter1, m_instrumenter2)
154 .getDescription());
155 }
156 }