View Javadoc

1   // Copyright (C) 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Unit tests for {@link CompositeInstrumenter}.
45   *
46   * @author Philip Aston
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 }