1
2
3
4
5
6
7
8
9
10
11 package psiprobe.model.stats;
12
13 import static org.junit.jupiter.api.Assertions.assertEquals;
14 import static org.junit.jupiter.api.Assertions.assertFalse;
15 import static org.junit.jupiter.api.Assertions.assertNotNull;
16 import static org.junit.jupiter.api.Assertions.assertNull;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
18
19 import java.util.List;
20 import java.util.Map;
21
22 import org.jfree.data.xy.XYDataItem;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25
26
27
28
29 class StatsCollectionTest {
30
31 private StatsCollection stats;
32
33 @BeforeEach
34 void setUp() {
35 stats = new StatsCollection();
36 stats.setSwapFileName("test-stats.xml");
37 stats.setMaxFiles(2);
38 }
39
40 @Test
41 void testNewStats() {
42 List<XYDataItem> list = stats.newStats("test.stat", 100);
43 assertNotNull(list);
44 assertTrue(list.isEmpty());
45 assertTrue(stats.isCollected("test.stat"));
46 }
47
48 @Test
49 void testIsCollectedFalseForMissing() {
50 assertFalse(stats.isCollected("nonexistent"));
51 }
52
53 @Test
54 void testGetStats() {
55 stats.newStats("my.stat", 10);
56 List<XYDataItem> result = stats.getStats("my.stat");
57 assertNotNull(result);
58 assertEquals(0, result.size());
59 }
60
61 @Test
62 void testGetStatsNull() {
63 assertNull(stats.getStats("missing"));
64 }
65
66 @Test
67 void testResetStats() {
68 List<XYDataItem> list = stats.newStats("reset.stat", 10);
69 list.add(new XYDataItem(1L, 100.0));
70 list.add(new XYDataItem(2L, 200.0));
71 assertEquals(2, list.size());
72
73 stats.resetStats("reset.stat");
74 assertEquals(0, list.size());
75 }
76
77 @Test
78 void testResetStatsNonExistent() {
79
80 stats.resetStats("nonexistent.stat");
81 }
82
83 @Test
84 void testGetLastValueForStat() {
85 List<XYDataItem> list = stats.newStats("last.stat", 10);
86 list.add(new XYDataItem(1L, 42.0));
87 list.add(new XYDataItem(2L, 99.0));
88
89 assertEquals(99L, stats.getLastValueForStat("last.stat"));
90 }
91
92 @Test
93 void testGetLastValueForStatEmpty() {
94 stats.newStats("empty.stat", 10);
95 assertEquals(0L, stats.getLastValueForStat("empty.stat"));
96 }
97
98 @Test
99 void testGetLastValueForStatNonExistent() {
100 assertEquals(0L, stats.getLastValueForStat("nonexistent"));
101 }
102
103 @Test
104 void testGetStatsByPrefix() {
105 stats.newStats("connector.abc.requests", 10);
106 stats.newStats("connector.abc.errors", 10);
107 stats.newStats("memory.heap", 10);
108
109 Map<String, List<XYDataItem>> result = stats.getStatsByPrefix("connector.");
110 assertEquals(2, result.size());
111 assertTrue(result.containsKey("connector.abc.requests"));
112 assertTrue(result.containsKey("connector.abc.errors"));
113 assertFalse(result.containsKey("memory.heap"));
114 }
115
116 @Test
117 void testGetStatsByPrefixNoMatch() {
118 stats.newStats("memory.heap", 10);
119 Map<String, List<XYDataItem>> result = stats.getStatsByPrefix("connector.");
120 assertTrue(result.isEmpty());
121 }
122
123 @Test
124 void testSetMaxFiles() {
125 stats.setMaxFiles(5);
126 assertEquals(5, stats.getMaxFiles());
127 }
128
129 @Test
130 void testSetMaxFilesZeroDefaultsToTwo() {
131 stats.setMaxFiles(0);
132 assertEquals(2, stats.getMaxFiles());
133 }
134
135 @Test
136 void testSetMaxFilesNegativeDefaultsToTwo() {
137 stats.setMaxFiles(-1);
138 assertEquals(2, stats.getMaxFiles());
139 }
140
141 @Test
142 void testGetSwapFileName() {
143 assertEquals("test-stats.xml", stats.getSwapFileName());
144 }
145
146 @Test
147 void testSetStoragePath() {
148 stats.setStoragePath("/tmp/mystats");
149 assertEquals("/tmp/mystats", stats.getStoragePath());
150 }
151
152 @Test
153 void testLockForUpdateAndRelease() throws InterruptedException {
154 stats.lockForUpdate();
155 stats.releaseLock();
156
157 }
158
159 @Test
160 void testNewStatsOverwritesPrevious() {
161 stats.newStats("dup.stat", 5);
162 stats.newStats("dup.stat", 10);
163
164 assertTrue(stats.isCollected("dup.stat"));
165 }
166 }