View Javadoc

1   // Copyright (C) 2002 - 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.script;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertTrue;
26  import static org.mockito.Matchers.*;
27  import static org.mockito.Mockito.verify;
28  import static org.mockito.Mockito.when;
29  
30  import java.io.ByteArrayOutputStream;
31  import java.io.ObjectOutputStream;
32  import java.util.ArrayList;
33  import java.util.Collections;
34  import java.util.List;
35  import java.util.Set;
36  import java.util.TreeSet;
37  
38  import net.grinder.engine.process.StubTestRegistry;
39  import net.grinder.script.Test.InstrumentationFilter;
40  import net.grinder.scriptengine.Instrumenter;
41  import net.grinder.scriptengine.Recorder;
42  
43  import org.junit.Before;
44  import org.mockito.Mock;
45  import org.mockito.MockitoAnnotations;
46  
47  
48  /**
49   * Unit tests for {@code Test}.
50   *
51   * @author Philip Aston
52   */
53  public class TestTest {
54  
55    @Mock private Instrumenter m_instrumenter;
56    @Mock private InstrumentationFilter m_instrumentationFilter;
57    @Mock private InternalScriptContext m_scriptContext;
58  
59  
60    @Before public void setUp() throws Exception {
61      MockitoAnnotations.initMocks(this);
62  
63      final TestRegistry testRegistry =
64        StubTestRegistry.stubTestRegistry(m_instrumenter);
65  
66      when(m_scriptContext.getTestRegistry()).thenReturn(testRegistry);
67  
68      Grinder.grinder = m_scriptContext;
69    }
70  
71    @org.junit.Test public void testGetters() throws Exception {
72      final Test test = new Test(1, "description");
73  
74      assertEquals(1, test.getNumber());
75      assertEquals("description", test.getDescription());
76    }
77  
78    @org.junit.Test public void testOrdering() throws Exception {
79      final int size = 100;
80  
81      final Set<Test> sorted = new TreeSet<Test>();
82      final List<Integer> keys = new ArrayList<Integer>(size);
83  
84      for (int i=0; i<size; i++) {
85        keys.add(new Integer(i));
86      }
87  
88      Collections.shuffle(keys);
89  
90      for (Integer i : keys) {
91        sorted.add(new Test(i, i.toString()));
92      }
93  
94      int i = 0;
95  
96      for (Test test : sorted) {
97        assertEquals(i++, test.getNumber());
98      }
99    }
100 
101   @org.junit.Test public void testEquality() throws Exception {
102 
103     // Equality depends only on test number.
104     final Test t1 = new Test(57, "one thing");
105     final Test t2 = new Test(57, "leads to");
106     final Test t3 = new Test(58, "another");
107 
108     assertEquals(t1, t2);
109     assertEquals(t2, t1);
110     assertTrue(!t1.equals(t3));
111     assertTrue(!t3.equals(t1));
112     assertTrue(!t2.equals(t3));
113     assertTrue(!t3.equals(t2));
114   }
115 
116   @org.junit.Test public void testIsSerializable() throws Exception {
117 
118     final Test test = new Test(123, "test");
119 
120     final ByteArrayOutputStream byteArrayOutputStream =
121       new ByteArrayOutputStream();
122 
123     final ObjectOutputStream objectOutputStream =
124       new ObjectOutputStream(byteArrayOutputStream);
125 
126     objectOutputStream.writeObject(test);
127   }
128 
129   @org.junit.Test public void testWrap() throws Exception {
130     final Test t1 = new Test(1, "six cars");
131     new Test(2, "house in ireland");
132 
133     final Integer i = new Integer(10);
134 
135     t1.wrap(i);
136 
137     verify(m_instrumenter).createInstrumentedProxy(same(t1),
138                                                    isA(Recorder.class),
139                                                    same(i));
140   }
141 
142   @org.junit.Test public void testRecord() throws Exception {
143     final Test t1 = new Test(1, "bigger than your dad");
144 
145     final Integer i = new Integer(10);
146 
147     t1.record(i);
148 
149     verify(m_instrumenter).instrument(same(t1), isA(Recorder.class), same(i));
150   }
151 
152   @org.junit.Test public void testSelectiveRecord() throws Exception {
153     final Test t1 = new Test(1, "travelling funk band");
154 
155     final Integer i = new Integer(10);
156 
157     t1.record(i, m_instrumentationFilter);
158 
159     verify(m_instrumenter).instrument(same(t1),
160                                       isA(Recorder.class),
161                                       same(i),
162                                       same(m_instrumentationFilter));
163   }
164 }