View Javadoc

1   // Copyright (C) 2004 - 2009 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 junit.framework.TestCase;
25  
26  import net.grinder.common.StubTest;
27  import net.grinder.common.Test;
28  import net.grinder.scriptengine.Instrumenter;
29  import net.grinder.statistics.StatisticsServicesImplementation;
30  import net.grinder.statistics.StatisticsSetFactory;
31  import net.grinder.testutility.RandomStubFactory;
32  import net.grinder.util.TimeAuthority;
33  import net.grinder.util.TimeAuthorityStubFactory;
34  
35  
36  /**
37   * Unit test case for <code>TestRegistryImplementation</code>.
38   *
39   * @author Philip Aston
40   */
41  public class TestTestRegistry extends TestCase {
42    private final RandomStubFactory<TestStatisticsHelper>
43      m_testStatisticsHelperStubFactory =
44        RandomStubFactory.create(TestStatisticsHelper.class);
45    private final TestStatisticsHelper m_testStatisticsHelper =
46      m_testStatisticsHelperStubFactory.getStub();
47  
48    private final TimeAuthorityStubFactory m_timeAuthorityStubFactory =
49      new TimeAuthorityStubFactory();
50    private final TimeAuthority m_timeAuthority =
51      m_timeAuthorityStubFactory.getStub();
52  
53    public TestTestRegistry(String name) {
54      super(name);
55    }
56  
57    public void testConstructor() throws Exception {
58      final ThreadContextLocator threadContextLocator =
59        new StubThreadContextLocator();
60      final StatisticsSetFactory statisticsSetFactory =
61        StatisticsServicesImplementation.getInstance().getStatisticsSetFactory();
62  
63      final TestRegistryImplementation testRegistryImplementation =
64        new TestRegistryImplementation(
65          threadContextLocator, statisticsSetFactory, m_testStatisticsHelper,
66          m_timeAuthority);
67  
68      assertNotNull(testRegistryImplementation.getTestStatisticsMap());
69  
70      m_testStatisticsHelperStubFactory.assertNoMoreCalls();
71      m_timeAuthorityStubFactory.assertNoMoreCalls();
72    }
73  
74    public void testRegister() throws Exception {
75      final ThreadContextLocator threadContextLocator =
76        new StubThreadContextLocator();
77      final StatisticsSetFactory statisticsSetFactory =
78        StatisticsServicesImplementation.getInstance().getStatisticsSetFactory();
79  
80      final TestRegistryImplementation testRegistryImplementation =
81        new TestRegistryImplementation(
82          threadContextLocator, statisticsSetFactory, m_testStatisticsHelper,
83          m_timeAuthority);
84  
85      assertNull(testRegistryImplementation.getNewTests());
86  
87      final Test test1 = new StubTest(1, "Test 1");
88      final Test test2 = new StubTest(2, "Test 2");
89  
90      try {
91        testRegistryImplementation.register(test1);
92        fail("Expected AssertionError");
93      }
94      catch (AssertionError e) {
95      }
96  
97      final RandomStubFactory<Instrumenter> instrumenterStubFactory =
98        RandomStubFactory.create(Instrumenter.class);
99      testRegistryImplementation.setInstrumenter(
100       instrumenterStubFactory.getStub());
101 
102     final TestRegistryImplementation.RegisteredTest registeredTest1a =
103       testRegistryImplementation.register(test1);
104 
105     final TestRegistryImplementation.RegisteredTest registeredTest1b =
106       testRegistryImplementation.register(test1);
107 
108     final TestRegistryImplementation.RegisteredTest registeredTest2 =
109       testRegistryImplementation.register(test2);
110 
111     assertSame(registeredTest1a, registeredTest1b);
112     assertNotSame(registeredTest2, registeredTest1a);
113 
114     assertTrue(testRegistryImplementation.getNewTests().contains(test1));
115     assertNull(testRegistryImplementation.getNewTests());
116 
117     m_testStatisticsHelperStubFactory.assertNoMoreCalls();
118     m_timeAuthorityStubFactory.assertNoMoreCalls();
119   }
120 }