1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.engine.process;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertSame;
26 import static org.mockito.Matchers.eq;
27 import static org.mockito.Mockito.verify;
28 import net.grinder.common.StubTest;
29 import net.grinder.statistics.StatisticsIndexMap;
30 import net.grinder.statistics.StatisticsServices;
31 import net.grinder.statistics.StatisticsServicesTestFactory;
32 import net.grinder.statistics.StatisticsSet;
33
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Captor;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.slf4j.Logger;
41
42
43
44
45
46
47
48 public class TestThreadDataLogger {
49
50 @Mock private Logger m_dataLogger;
51 @Captor private ArgumentCaptor<DataLogArguments> m_argumentCaptor;
52
53 private final net.grinder.common.Test m_test1 = new StubTest(1, "T1");
54 private final net.grinder.common.Test m_test3 = new StubTest(3, "T3");
55
56 private final StatisticsServices m_statisticsServices =
57 StatisticsServicesTestFactory.createTestInstance();
58
59 private StatisticsIndexMap.LongIndex s_errorsIndex;
60 private StatisticsIndexMap.LongSampleIndex s_timedTestsIndex;
61 private StatisticsIndexMap.DoubleIndex s_userDouble0Index;
62
63 @Before public void setUp() {
64 MockitoAnnotations.initMocks(this);
65
66 final StatisticsIndexMap indexMap =
67 m_statisticsServices.getStatisticsIndexMap();
68
69 s_errorsIndex = indexMap.getLongIndex("errors");
70 s_timedTestsIndex = indexMap.getLongSampleIndex("timedTests");
71 s_userDouble0Index = indexMap.getDoubleIndex("userDouble0");
72 }
73
74 @Test public void testReport() throws Exception {
75
76 final ThreadDataLogger ThreadDataLogger =
77 new ThreadDataLogger(
78 m_dataLogger,
79 m_statisticsServices.getDetailStatisticsView().getExpressionViews(),
80 33);
81
82 final StatisticsSet statistics =
83 m_statisticsServices.getStatisticsSetFactory().create();
84
85 statistics.addSample(s_timedTestsIndex, 99);
86
87 ThreadDataLogger.report(10, m_test1, 123L, statistics);
88
89 verify(m_dataLogger).info(eq("33, 10, 1, 123, 99, 0"),
90 m_argumentCaptor.capture());
91
92 final DataLogArguments arguments = m_argumentCaptor.getValue();
93
94 assertEquals(33, arguments.getThreadNumber());
95 assertEquals(10, arguments.getRunNumber());
96 assertSame(m_test1, arguments.getTest());
97 assertEquals(123L, arguments.getTimeSinceExecutionStart());
98 assertSame(statistics, arguments.getStatistics());
99
100 ThreadDataLogger.report(10, m_test1, 125L, statistics);
101
102 verify(m_dataLogger).info(eq("33, 10, 1, 125, 99, 0"),
103 m_argumentCaptor.capture());
104
105 ThreadDataLogger.report(11, m_test3, 300L, statistics);
106
107 verify(m_dataLogger).info(eq("33, 11, 3, 300, 99, 0"),
108 m_argumentCaptor.capture());
109
110 statistics.reset();
111 statistics.setValue(s_errorsIndex, 1);
112
113 ThreadDataLogger.report(11, m_test3, 301L, statistics);
114
115 verify(m_dataLogger).info(eq("33, 11, 3, 301, 0, 1"),
116 m_argumentCaptor.capture());
117 }
118
119 @Test public void testReportCustomViews() throws Exception {
120
121 m_statisticsServices.getDetailStatisticsView().add(
122 m_statisticsServices.getStatisticExpressionFactory()
123 .createExpressionView("foo", "userDouble0", false));
124
125 final StatisticsSet statistics =
126 m_statisticsServices.getStatisticsSetFactory().create();
127
128 statistics.addSample(s_timedTestsIndex, 5);
129 statistics.addValue(s_userDouble0Index, 1.5);
130
131 final ThreadDataLogger ThreadDataLogger2 =
132 new ThreadDataLogger(
133 m_dataLogger,
134 m_statisticsServices.getDetailStatisticsView().getExpressionViews(),
135 33);
136
137 ThreadDataLogger2.report(11, m_test3, 530L, statistics);
138
139 verify(m_dataLogger).info(eq("33, 11, 3, 530, 5, 0, 1.5"),
140 m_argumentCaptor.capture());
141 }
142 }