1 /**********************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Bernd Hufmann - Initial API and implementation
11 * Alexandre Montplaisir - Port to JUnit4
12 **********************************************************************/
14 package org
.eclipse
.tracecompass
.lttng2
.control
.core
.tests
.model
.impl
;
16 import static org
.junit
.Assert
.*;
18 import java
.util
.LinkedList
;
19 import java
.util
.List
;
21 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.IChannelInfo
;
22 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.IEventInfo
;
23 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.TraceChannelOutputType
;
24 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.TraceEnablement
;
25 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.impl
.BufferType
;
26 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.impl
.ChannelInfo
;
27 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.model
.impl
.EventInfo
;
28 import org
.junit
.Before
;
29 import org
.junit
.Test
;
32 * The class <code>ChannelInfoTest</code> contains tests for the class
33 * <code>{@link ChannelInfo}</code>.
35 public class ChannelInfoTest
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private IChannelInfo fChannelInfo1
= null;
42 private IChannelInfo fChannelInfo2
= null;
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
49 * Perform pre-test initialization.
53 ModelImplFactory factory
= new ModelImplFactory();
54 fChannelInfo1
= factory
.getChannel1();
55 fChannelInfo2
= factory
.getChannel2();
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * Run the ChannelInfo() constructor test.
70 public void testChannelInfo() {
71 ChannelInfo result
= new ChannelInfo("test");
72 assertNotNull(result
);
74 assertEquals("test", result
.getName());
75 assertEquals(0, result
.getNumberOfSubBuffers());
76 assertEquals("unknown", result
.getOutputType().getInName());
77 assertEquals(TraceChannelOutputType
.UNKNOWN
, result
.getOutputType());
78 assertEquals(false, result
.isOverwriteMode());
79 assertEquals(0, result
.getReadTimer());
80 assertEquals("disabled", result
.getState().getInName());
81 assertEquals(0, result
.getSubBufferSize());
82 assertEquals(0, result
.getSwitchTimer());
83 assertEquals(0, result
.getMaxSizeTraceFiles());
84 assertEquals(0, result
.getMaxNumberTraceFiles());
85 assertEquals(BufferType
.BUFFER_TYPE_UNKNOWN
, result
.getBufferType());
89 * Test copy constructor.
92 public void testChannelInfoCopy() {
93 ChannelInfo channelInfo
= new ChannelInfo((ChannelInfo
)fChannelInfo1
);
95 assertEquals(fChannelInfo1
.getName(), channelInfo
.getName());
96 assertEquals(fChannelInfo1
.getNumberOfSubBuffers(), channelInfo
.getNumberOfSubBuffers());
97 assertEquals(fChannelInfo1
.getOutputType(), channelInfo
.getOutputType());
98 assertEquals(fChannelInfo1
.isOverwriteMode(), channelInfo
.isOverwriteMode());
99 assertEquals(fChannelInfo1
.getReadTimer(), channelInfo
.getReadTimer());
100 assertEquals(fChannelInfo1
.getState(), channelInfo
.getState());
101 assertEquals(fChannelInfo1
.getSwitchTimer(), channelInfo
.getSwitchTimer());
102 assertEquals(fChannelInfo1
.getEvents().length
, channelInfo
.getEvents().length
);
103 assertEquals(fChannelInfo1
.getMaxSizeTraceFiles(), channelInfo
.getMaxSizeTraceFiles());
104 assertEquals(fChannelInfo1
.getMaxNumberTraceFiles(), channelInfo
.getMaxNumberTraceFiles());
105 assertEquals(fChannelInfo1
.getBufferType(), channelInfo
.getBufferType());
107 IEventInfo
[] orignalEvents
= fChannelInfo1
.getEvents();
108 IEventInfo
[] resultEvents
= channelInfo
.getEvents();
109 for (int i
= 0; i
< orignalEvents
.length
; i
++) {
110 assertEquals(orignalEvents
[i
], resultEvents
[i
]);
115 * Test copy constructor with a null value.
118 public void testChannelCopy2() {
120 ChannelInfo channel
= null;
121 new ChannelInfo(channel
);
124 catch (IllegalArgumentException e
) {
130 * Run the IEventInfo[] getEvents() method test.
133 public void testAddAndGetEvents_1() {
134 ChannelInfo fixture
= new ChannelInfo("test");
135 fixture
.setSwitchTimer(1L);
136 fixture
.setOverwriteMode(true);
137 fixture
.setReadTimer(1L);
138 fixture
.setState(TraceEnablement
.DISABLED
);
139 fixture
.setNumberOfSubBuffers(1);
140 fixture
.setOutputType("");
141 fixture
.setSubBufferSize(1L);
144 IEventInfo event
= new EventInfo("event");
145 fixture
.addEvent(event
);
147 // Verify the stored events
148 IEventInfo
[] result
= fixture
.getEvents();
150 assertNotNull(result
);
151 assertEquals(1, result
.length
);
152 assertNotNull(result
[0]);
153 assertTrue(event
.equals(result
[0]));
157 * Run the long getNumberOfSubBuffers() method test.
160 public void testGetAndSetters() {
161 ChannelInfo fixture
= new ChannelInfo("test");
162 fixture
.setSwitchTimer(2L);
163 fixture
.setOverwriteMode(true);
164 fixture
.setReadTimer(3L);
165 fixture
.setState(TraceEnablement
.DISABLED
);
166 fixture
.setNumberOfSubBuffers(4);
167 fixture
.setOutputType("splice()");
168 fixture
.setSubBufferSize(1L);
169 fixture
.setMaxSizeTraceFiles(1024);
170 fixture
.setMaxNumberTraceFiles(20);
171 fixture
.setBufferType(BufferType
.BUFFER_PER_UID
);
172 fixture
.addEvent(new EventInfo("event"));
174 long switchTimer
= fixture
.getSwitchTimer();
175 assertEquals(2L, switchTimer
);
177 boolean mode
= fixture
.isOverwriteMode();
180 long readTimer
= fixture
.getReadTimer();
181 assertEquals(3L, readTimer
);
183 TraceEnablement state
= fixture
.getState();
184 assertEquals("disabled", state
.getInName());
186 long numSubBuffers
= fixture
.getNumberOfSubBuffers();
187 assertEquals(4, numSubBuffers
);
189 String outputType
= fixture
.getOutputType().getInName();
190 assertEquals("splice()", outputType
);
192 long subBufferSize
= fixture
.getSubBufferSize();
193 assertEquals(1L, subBufferSize
);
195 long maxSizeTraceFiles
= fixture
.getMaxSizeTraceFiles();
196 assertEquals(1024, maxSizeTraceFiles
);
198 int maxNumberTraceFiles
= fixture
.getMaxNumberTraceFiles();
199 assertEquals(20, maxNumberTraceFiles
);
201 BufferType bufferType
= fixture
.getBufferType();
202 assertTrue(bufferType
== BufferType
.BUFFER_PER_UID
);
204 fixture
.setSwitchTimer(5L);
205 fixture
.setOverwriteMode(false);
206 fixture
.setReadTimer(6L);
207 fixture
.setState(TraceEnablement
.ENABLED
);
208 fixture
.setNumberOfSubBuffers(7);
209 fixture
.setOutputType("mmap()");
210 fixture
.setSubBufferSize(8L);
211 fixture
.setMaxSizeTraceFiles(4096);
212 fixture
.setMaxNumberTraceFiles(10);
213 fixture
.setBufferType(BufferType
.BUFFER_PER_PID
);
215 switchTimer
= fixture
.getSwitchTimer();
216 assertEquals(5L, switchTimer
);
218 mode
= fixture
.isOverwriteMode();
221 readTimer
= fixture
.getReadTimer();
222 assertEquals(6L, readTimer
);
224 state
= fixture
.getState();
225 assertEquals("enabled", state
.getInName());
227 numSubBuffers
= fixture
.getNumberOfSubBuffers();
228 assertEquals(7, numSubBuffers
);
230 outputType
= fixture
.getOutputType().getInName();
231 assertEquals("mmap()", outputType
);
233 subBufferSize
= fixture
.getSubBufferSize();
234 assertEquals(8L, subBufferSize
);
236 maxSizeTraceFiles
= fixture
.getMaxSizeTraceFiles();
237 assertEquals(4096, maxSizeTraceFiles
);
239 maxNumberTraceFiles
= fixture
.getMaxNumberTraceFiles();
240 assertEquals(10, maxNumberTraceFiles
);
242 bufferType
= fixture
.getBufferType();
243 assertTrue(bufferType
== BufferType
.BUFFER_PER_PID
);
247 * Run the void setEvents(List<IEventInfo>) method test.
250 public void testSetEvents_1() {
251 ChannelInfo fixture
= new ChannelInfo("test");
252 fixture
.setSwitchTimer(1L);
253 fixture
.setOverwriteMode(true);
254 fixture
.setReadTimer(1L);
255 fixture
.setState(TraceEnablement
.DISABLED
);
256 fixture
.setNumberOfSubBuffers(1);
257 fixture
.setOutputType("");
258 fixture
.setSubBufferSize(1L);
259 List
<IEventInfo
> events
= new LinkedList
<>();
261 for (int i
= 0; i
< 2; i
++) {
262 IEventInfo info
= new EventInfo("event" + i
);
263 info
.setEventType("tracepoint");
264 info
.setState((i
% 2 == 0 ?
"enabled" : "disabled"));
268 fixture
.setEvents(events
);
270 IEventInfo
[] infos
= fixture
.getEvents();
272 assertEquals(events
.size(), infos
.length
);
274 for (int i
= 0; i
< infos
.length
; i
++) {
275 assertEquals(events
.get(i
), infos
[i
]);
280 * Run the String toString() method test.
283 public void testToString_1() {
284 ChannelInfo fixture
= new ChannelInfo("channel");
285 fixture
.setSwitchTimer(1L);
286 fixture
.setOverwriteMode(true);
287 fixture
.setReadTimer(1L);
288 fixture
.setState(TraceEnablement
.DISABLED
);
289 fixture
.setNumberOfSubBuffers(1);
290 fixture
.setOutputType("splice()");
291 fixture
.setSubBufferSize(1L);
293 String result
= fixture
.toString();
295 // add additional test code here
296 assertEquals("[ChannelInfo([TraceInfo(Name=channel)],State=DISABLED,OverwriteMode=true,SubBuffersSize=1,NumberOfSubBuffers=1,SwitchTimer=1,ReadTimer=1,output=splice(),Events=None)]", result
);
300 * Run another String toString() method test.
303 public void testToString_2() {
304 String result
= fChannelInfo1
.toString();
306 // add additional test code here
307 assertEquals("[ChannelInfo([TraceInfo(Name=channel1)],State=DISABLED,OverwriteMode=true,SubBuffersSize=13,NumberOfSubBuffers=12,SwitchTimer=10,ReadTimer=11,output=splice(),Events=[EventInfo([BaseEventInfo([TraceInfo(Name=event1)],type=TRACEPOINT,level=TRACE_DEBUG)],State=ENABLED,levelType=LOGLEVEL_ONLY)])]", result
);
310 // ------------------------------------------------------------------------
312 // ------------------------------------------------------------------------
315 * Run the equals() method test.
318 public void testEqualsReflexivity() {
319 assertTrue("equals", fChannelInfo1
.equals(fChannelInfo1
));
320 assertTrue("equals", fChannelInfo2
.equals(fChannelInfo2
));
322 assertTrue("equals", !fChannelInfo1
.equals(fChannelInfo2
));
323 assertTrue("equals", !fChannelInfo2
.equals(fChannelInfo1
));
327 * Run the equals() method test.
330 public void testEqualsSymmetry() {
331 ChannelInfo event1
= new ChannelInfo((ChannelInfo
)fChannelInfo1
);
332 ChannelInfo event2
= new ChannelInfo((ChannelInfo
)fChannelInfo2
);
334 assertTrue("equals", event1
.equals(fChannelInfo1
));
335 assertTrue("equals", fChannelInfo1
.equals(event1
));
337 assertTrue("equals", event2
.equals(fChannelInfo2
));
338 assertTrue("equals", fChannelInfo2
.equals(event2
));
342 * Run the equals() method test.
345 public void testEqualsTransivity() {
346 ChannelInfo channel1
= new ChannelInfo((ChannelInfo
)fChannelInfo1
);
347 ChannelInfo channel2
= new ChannelInfo((ChannelInfo
)fChannelInfo1
);
348 ChannelInfo channel3
= new ChannelInfo((ChannelInfo
)fChannelInfo1
);
350 assertTrue("equals", channel1
.equals(channel2
));
351 assertTrue("equals", channel2
.equals(channel3
));
352 assertTrue("equals", channel1
.equals(channel3
));
356 * Run the equals() method test.
359 public void testEqualsNull() {
360 assertTrue("equals", !fChannelInfo1
.equals(null));
361 assertTrue("equals", !fChannelInfo2
.equals(null));
364 // ------------------------------------------------------------------------
366 // ------------------------------------------------------------------------
369 * Run the hashCode() method test.
372 public void testHashCode() {
373 ChannelInfo channel1
= new ChannelInfo((ChannelInfo
)fChannelInfo1
);
374 ChannelInfo channel2
= new ChannelInfo((ChannelInfo
)fChannelInfo2
);
376 assertTrue("hashCode", fChannelInfo1
.hashCode() == channel1
.hashCode());
377 assertTrue("hashCode", fChannelInfo2
.hashCode() == channel2
.hashCode());
379 assertTrue("hashCode", fChannelInfo1
.hashCode() != channel2
.hashCode());
380 assertTrue("hashCode", fChannelInfo2
.hashCode() != channel1
.hashCode());