View Javadoc

1   // Copyright (C) 2009 - 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.engine.process;
23  
24  import static java.util.Arrays.asList;
25  import static java.util.Collections.emptyList;
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.fail;
28  import static org.mockito.Mockito.inOrder;
29  import static org.mockito.Mockito.verifyNoMoreInteractions;
30  import static org.mockito.Mockito.when;
31  
32  import java.util.List;
33  
34  import net.grinder.script.NonInstrumentableTypeException;
35  import net.grinder.script.NotWrappableTypeException;
36  import net.grinder.script.Test.InstrumentationFilter;
37  import net.grinder.scriptengine.Instrumenter;
38  import net.grinder.scriptengine.Recorder;
39  
40  import org.junit.Before;
41  import org.junit.Test;
42  import org.mockito.InOrder;
43  import org.mockito.Mock;
44  import org.mockito.MockitoAnnotations;
45  
46  
47  /**
48   * Unit tests for {@link MasterInstrumenter}.
49   *
50   * @author Philip Aston
51   */
52  public class TestMasterInstrumenter {
53  
54    @Mock private Recorder m_recorder;
55    @Mock private Instrumenter m_instrumenter1;
56    @Mock private Instrumenter m_instrumenter2;
57    @Mock private InstrumentationFilter m_filter;
58    @Mock private net.grinder.common.Test m_test;
59  
60    private Object m_target = new Object();
61  
62    @Before public void setUp() throws Exception {
63      MockitoAnnotations.initMocks(this);
64    }
65  
66    @Test public void testCreateInstrumentedProxy() throws Exception {
67  
68      when(m_instrumenter2.createInstrumentedProxy(m_test, m_recorder, m_target))
69        .thenReturn(m_target);
70  
71      final Instrumenter instrumenter =
72        new MasterInstrumenter(asList(m_instrumenter1, m_instrumenter2));
73  
74      instrumenter.createInstrumentedProxy(m_test, m_recorder, m_target);
75  
76      final InOrder inOrder = inOrder(m_instrumenter1, m_instrumenter2);
77  
78      inOrder.verify(m_instrumenter1)
79        .createInstrumentedProxy(m_test, m_recorder, m_target);
80      inOrder.verify(m_instrumenter2)
81        .createInstrumentedProxy(m_test, m_recorder, m_target);
82  
83      verifyNoMoreInteractions(m_instrumenter1, m_instrumenter2);
84    }
85  
86    @Test public void testCreateInstrumentedProxyWithNull() throws Exception {
87      final Instrumenter instrumenter =
88        new MasterInstrumenter(asList(m_instrumenter1, m_instrumenter2));
89  
90      try {
91        instrumenter.createInstrumentedProxy(m_test, m_recorder, null);
92        fail("Expected NotWrappableTypeException");
93      }
94      catch (NotWrappableTypeException e) {
95      }
96    }
97  
98    @Test public void testCreateInstrumentedProxyFailure() throws Exception {
99      final Instrumenter instrumenter =
100       new MasterInstrumenter(asList(m_instrumenter1, m_instrumenter2));
101 
102     try {
103       instrumenter.createInstrumentedProxy(m_test, m_recorder, m_target);
104       fail("Expected NotWrappableTypeException");
105     }
106     catch (NotWrappableTypeException e) {
107     }
108   }
109 
110   @Test public void testInstrument() throws Exception {
111     when(m_instrumenter2.instrument(m_test, m_recorder, m_target, m_filter))
112       .thenReturn(true);
113 
114     final Instrumenter instrumenter =
115       new MasterInstrumenter(asList(m_instrumenter1, m_instrumenter2));
116 
117     instrumenter.instrument(m_test, m_recorder, m_target, m_filter);
118 
119     final InOrder inOrder = inOrder(m_instrumenter1, m_instrumenter2);
120 
121     inOrder.verify(m_instrumenter1)
122       .instrument(m_test, m_recorder, m_target, m_filter);
123     inOrder.verify(m_instrumenter2)
124       .instrument(m_test, m_recorder, m_target, m_filter);
125 
126     verifyNoMoreInteractions(m_instrumenter1, m_instrumenter2);
127   }
128 
129   @Test public void testInstrumentWithNull() throws Exception {
130     final Instrumenter instrumenter =
131       new MasterInstrumenter(asList(m_instrumenter1, m_instrumenter2));
132 
133     try {
134       instrumenter.instrument(m_test, m_recorder, null, null);
135       fail("Expected NonInstrumentableTypeException");
136     }
137     catch (NonInstrumentableTypeException e) {
138     }
139   }
140 
141   @Test public void testCreateInstrumentedFailure() throws Exception {
142     final Instrumenter instrumenter =
143       new MasterInstrumenter(asList(m_instrumenter1, m_instrumenter2));
144 
145     try {
146       instrumenter.instrument(m_test, m_recorder, m_target, null);
147       fail("Expected NonInstrumentableTypeException");
148     }
149     catch (NonInstrumentableTypeException e) {
150     }
151   }
152 
153   @Test public void testGetDescription() throws Exception {
154     when(m_instrumenter1.getDescription()).thenReturn("I1");
155     when(m_instrumenter2.getDescription()).thenReturn("I2");
156 
157     final Instrumenter instrumenter =
158       new MasterInstrumenter(asList(m_instrumenter1, m_instrumenter2));
159 
160     assertEquals("I1; I2", instrumenter.getDescription());
161 
162     final Instrumenter instrumenter2 =
163       new MasterInstrumenter(asList(m_instrumenter1));
164 
165     assertEquals("I1", instrumenter2.getDescription());
166 
167     final List<Instrumenter> e = emptyList();
168 
169     assertEquals("NO INSTRUMENTER COULD BE LOADED",
170                  new MasterInstrumenter(e).getDescription());
171   }
172 }