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.dcr;
23  
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  import static org.junit.Assert.fail;
28  import static org.mockito.Matchers.contains;
29  import static org.mockito.Mockito.verify;
30  import static org.mockito.Mockito.verifyNoMoreInteractions;
31  import static org.mockito.Mockito.when;
32  
33  import java.lang.instrument.Instrumentation;
34  
35  import net.grinder.engine.process.ExternalLoggerScopeTunnel;
36  import net.grinder.scriptengine.DCRContext;
37  import net.grinder.util.weave.agent.ExposeInstrumentation;
38  
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Test;
42  import org.mockito.Mock;
43  import org.mockito.MockitoAnnotations;
44  import org.slf4j.Logger;
45  
46  
47  /**
48   * Unit tests for {@link DCRContextImplementation}.
49   *
50   * @author Philip Aston
51   */
52  public class TestDCRContextImplementation {
53  
54    private Instrumentation m_originalInstrumentation;
55  
56    @Mock private Instrumentation m_instrumentation;
57  
58    @Mock private Logger m_logger;
59  
60    @Before public void setUp() throws Exception {
61      m_originalInstrumentation = ExposeInstrumentation.getInstrumentation();
62      assertNotNull(m_originalInstrumentation);
63  
64      MockitoAnnotations.initMocks(this);
65      when(m_instrumentation.isRetransformClassesSupported()).thenReturn(true);
66    }
67  
68    @After public void tearDown() throws Exception {
69      ExposeInstrumentation.premain("", m_originalInstrumentation);
70    }
71  
72    @Test public void testCreateWithNoInstrumentation() throws Exception {
73      ExposeInstrumentation.premain("", null);
74  
75      assertNull(DCRContextImplementation.create(m_logger));
76  
77      verify(m_logger).info(contains("does not support"));
78      verifyNoMoreInteractions(m_logger);
79    }
80  
81    @Test public void testCreateWithNoRetransformation() throws Exception {
82      ExposeInstrumentation.premain("", m_instrumentation);
83  
84      when(m_instrumentation.isRetransformClassesSupported()).thenReturn(false);
85  
86      assertNull(DCRContextImplementation.create(m_logger));
87  
88      verify(m_logger).info(contains("does not support"));
89      verifyNoMoreInteractions(m_logger);
90    }
91  
92    @Test public void testCreateWithBadRetransformation() throws Exception {
93      ExposeInstrumentation.premain("", m_instrumentation);
94  
95      when(m_instrumentation.isRetransformClassesSupported())
96        .thenThrow(new NoSuchMethodError());
97  
98      assertNull(DCRContextImplementation.create(m_logger));
99  
100     verify(m_logger).info(contains("does not support"));
101     verifyNoMoreInteractions(m_logger);
102   }
103 
104   @Test public void testWithBadAdvice() throws Exception {
105     try {
106       new DCRContextImplementation(m_instrumentation,
107                      TestDCRContextImplementation.class,
108                      RecorderLocator.getRecorderRegistry());
109       fail("Expected AssertionError");
110     }
111     catch (AssertionError e) {
112     }
113   }
114 
115   // Bug 3411728.
116   @Test public void testExternalLoggerIsInstrumentable() throws Exception {
117     final DCRContext context = DCRContextImplementation.create(m_logger);
118 
119     final Class<?> loggerClass =
120       ExternalLoggerScopeTunnel.getExternalLogger(m_logger).getClass();
121 
122     assertTrue(context.isInstrumentable(loggerClass));
123   }
124 }