|
20 | 20 | @RunWith(PowerMockRunner.class)
|
21 | 21 | @PrepareForTest( {SystemWrapper.class, AgentBasedEnvironment.class} )
|
22 | 22 | public class AgentBasedEnvironmentTest {
|
23 |
| - public static class AgentBasedEnvironmentTestImplementation extends AgentBasedEnvironment { |
24 |
| - protected AgentBasedEnvironmentTestImplementation(Configuration config) { super(config); } |
25 |
| - @Override |
26 |
| - public boolean probe() { return false; } |
27 |
| - @Override |
28 |
| - public String getType() { return null; } |
29 |
| - @Override |
30 |
| - public void configureContext(MetricsContext context) { } |
31 |
| - } |
32 |
| - |
33 |
| - private Configuration configuration; |
34 |
| - |
35 |
| - @Before |
36 |
| - public void setup() { |
37 |
| - this.configuration = new Configuration(); |
38 |
| - } |
39 |
| - |
40 |
| - @Test |
41 |
| - public void testGetSinkWithDefaultEndpoint() throws Exception { |
42 |
| - AgentSink mockedSink = mock(AgentSink.class); |
43 |
| - PowerMockito.whenNew(AgentSink.class).withAnyArguments().then(invocation -> { |
44 |
| - Endpoint endpoint = invocation.getArgument(2); |
45 |
| - assertEquals(Endpoint.DEFAULT_TCP_ENDPOINT, endpoint); |
46 |
| - return mockedSink; |
47 |
| - }); |
48 |
| - |
49 |
| - AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
50 |
| - ISink sink = env.getSink(); |
51 |
| - |
52 |
| - assertEquals(mockedSink, sink); |
53 |
| - } |
54 |
| - |
55 |
| - @Test |
56 |
| - public void testGetSinkWithConfiguredEndpoint() throws Exception { |
57 |
| - String endpointUrl = "http://configured-endpoint:1234"; |
58 |
| - configuration.setAgentEndpoint(endpointUrl); |
59 |
| - AgentSink mockedSink = mock(AgentSink.class); |
60 |
| - PowerMockito.whenNew(AgentSink.class).withAnyArguments().then(invocation -> { |
61 |
| - Endpoint endpoint = invocation.getArgument(2); |
62 |
| - assertEquals(Endpoint.fromURL(endpointUrl), endpoint); |
63 |
| - return mockedSink; |
64 |
| - }); |
65 |
| - |
66 |
| - AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
67 |
| - ISink sink = env.getSink(); |
68 |
| - |
69 |
| - assertEquals(mockedSink, sink); |
70 |
| - } |
71 |
| - |
72 |
| - @Test |
73 |
| - public void testGetSinkOverrideToStdOut() { |
74 |
| - PowerMockito.mockStatic(SystemWrapper.class); |
75 |
| - PowerMockito.when(SystemWrapper.getenv("WRITE_TO_STDOUT")).thenReturn("true"); |
76 |
| - |
77 |
| - AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
78 |
| - ISink sink = env.getSink(); |
79 |
| - |
80 |
| - assertEquals(ConsoleSink.class, sink.getClass()); |
81 |
| - } |
82 |
| - |
83 |
| - @Test |
84 |
| - public void testGetSinkOverrideToStdOutFailFastOnImproperOverride() throws Exception { |
85 |
| - PowerMockito.mockStatic(SystemWrapper.class); |
86 |
| - // Will only override if this env is explicitly "true" |
87 |
| - PowerMockito.when(SystemWrapper.getenv("WRITE_TO_STDOUT")).thenReturn("notABool"); |
88 |
| - |
89 |
| - testGetSinkWithDefaultEndpoint(); |
90 |
| - } |
| 23 | + public static class AgentBasedEnvironmentTestImplementation extends AgentBasedEnvironment { |
| 24 | + protected AgentBasedEnvironmentTestImplementation(Configuration config) { super(config); } |
| 25 | + @Override |
| 26 | + public boolean probe() { return false; } |
| 27 | + @Override |
| 28 | + public String getType() { return null; } |
| 29 | + @Override |
| 30 | + public void configureContext(MetricsContext context) { } |
| 31 | + } |
| 32 | + |
| 33 | + private Configuration configuration; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setup() { |
| 37 | + this.configuration = new Configuration(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testGetSinkWithDefaultEndpoint() throws Exception { |
| 42 | + AgentSink mockedSink = mock(AgentSink.class); |
| 43 | + PowerMockito.whenNew(AgentSink.class).withAnyArguments().then(invocation -> { |
| 44 | + Endpoint endpoint = invocation.getArgument(2); |
| 45 | + assertEquals(Endpoint.DEFAULT_TCP_ENDPOINT, endpoint); |
| 46 | + return mockedSink; |
| 47 | + }); |
| 48 | + |
| 49 | + AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
| 50 | + ISink sink = env.getSink(); |
| 51 | + |
| 52 | + assertEquals(mockedSink, sink); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testGetSinkWithConfiguredEndpoint() throws Exception { |
| 57 | + String endpointUrl = "http://configured-endpoint:1234"; |
| 58 | + configuration.setAgentEndpoint(endpointUrl); |
| 59 | + AgentSink mockedSink = mock(AgentSink.class); |
| 60 | + PowerMockito.whenNew(AgentSink.class).withAnyArguments().then(invocation -> { |
| 61 | + Endpoint endpoint = invocation.getArgument(2); |
| 62 | + assertEquals(Endpoint.fromURL(endpointUrl), endpoint); |
| 63 | + return mockedSink; |
| 64 | + }); |
| 65 | + |
| 66 | + AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
| 67 | + ISink sink = env.getSink(); |
| 68 | + |
| 69 | + assertEquals(mockedSink, sink); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testGetSinkOverrideToStdOut() { |
| 74 | + PowerMockito.mockStatic(SystemWrapper.class); |
| 75 | + PowerMockito.when(SystemWrapper.getenv("WRITE_TO_STDOUT")).thenReturn("true"); |
| 76 | + |
| 77 | + AgentBasedEnvironment env = new AgentBasedEnvironmentTestImplementation(configuration); |
| 78 | + ISink sink = env.getSink(); |
| 79 | + |
| 80 | + assertEquals(ConsoleSink.class, sink.getClass()); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testGetSinkOverrideToStdOutFailFastOnImproperOverride() throws Exception { |
| 85 | + PowerMockito.mockStatic(SystemWrapper.class); |
| 86 | + // Will only override if this env is explicitly "true" |
| 87 | + PowerMockito.when(SystemWrapper.getenv("WRITE_TO_STDOUT")).thenReturn("notABool"); |
| 88 | + |
| 89 | + testGetSinkWithDefaultEndpoint(); |
| 90 | + } |
91 | 91 | }
|
0 commit comments