1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adapted for TMF Trace Model 1.0
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
16 import junit
.framework
.TestCase
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpoint
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
26 * Test suite for the TmfCheckpoint class.
28 @SuppressWarnings({"nls","javadoc"})
29 public class TmfCheckpointTest
extends TestCase
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 ITmfTimestamp fTimestamp1
= new TmfTimestamp();
36 ITmfTimestamp fTimestamp2
= TmfTimestamp
.BIG_BANG
;
37 ITmfTimestamp fTimestamp3
= TmfTimestamp
.BIG_CRUNCH
;
42 ITmfLocation
<Long
> fLocation1
= new TmfLongLocation(aLong1
);
43 ITmfLocation
<Long
> fLocation2
= new TmfLongLocation(aLong2
);
44 ITmfLocation
<Long
> fLocation3
= new TmfLongLocation(aLong3
);
46 TmfCheckpoint fCheckpoint1
= new TmfCheckpoint(fTimestamp1
, new TmfContext(fLocation1
));
47 TmfCheckpoint fCheckpoint2
= new TmfCheckpoint(fTimestamp2
, new TmfContext(fLocation2
));
48 TmfCheckpoint fCheckpoint3
= new TmfCheckpoint(fTimestamp3
, new TmfContext(fLocation3
));
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
55 * @param name the test name
57 public TmfCheckpointTest(final String name
) {
62 protected void setUp() throws Exception
{
67 protected void tearDown() throws Exception
{
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 public void testTmfCheckpoint() {
76 assertEquals("TmfCheckpoint", fTimestamp1
, fCheckpoint1
.getTimestamp());
77 assertEquals("TmfCheckpoint", fLocation1
, fCheckpoint1
.getLocation());
80 public void testTmfLocationCopy() {
81 final TmfCheckpoint checkpoint
= new TmfCheckpoint(fCheckpoint1
);
83 assertEquals("TmfCheckpoint", fTimestamp1
, checkpoint
.getTimestamp());
84 assertEquals("TmfCheckpoint", fLocation1
, checkpoint
.getLocation());
87 public void testTmfLocationCopy2() {
89 new TmfCheckpoint(null);
92 catch (final IllegalArgumentException e
) {
95 catch (final Exception e
) {
96 fail("wrong exception");
100 // ------------------------------------------------------------------------
102 // ------------------------------------------------------------------------
104 public void testClone() {
106 TmfCheckpoint checkpoint1
= fCheckpoint1
.clone();
107 TmfCheckpoint checkpoint2
= fCheckpoint1
.clone();
108 TmfCheckpoint checkpoint3
= fCheckpoint1
.clone();
110 assertEquals("clone", checkpoint1
, fCheckpoint1
);
111 assertEquals("clone", checkpoint2
, fCheckpoint1
);
112 assertEquals("clone", checkpoint3
, fCheckpoint1
);
114 checkpoint1
= new TmfCheckpoint(fTimestamp1
, null);
115 checkpoint2
= checkpoint1
.clone();
116 assertEquals("clone", checkpoint1
, checkpoint2
);
117 assertNull(checkpoint1
.getContext());
118 assertNull(checkpoint2
.getContext());
120 checkpoint1
= new TmfCheckpoint(null, new TmfContext(fLocation1
));
121 checkpoint3
= checkpoint1
.clone();
122 assertEquals("clone", checkpoint1
, checkpoint3
);
123 assertNull(checkpoint1
.getTimestamp());
124 assertNull(checkpoint3
.getTimestamp());
126 } catch (final InternalError e
) {
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
135 public void testCompareTo() {
136 assertEquals("compareTo", 0, fCheckpoint1
.compareTo(fCheckpoint1
));
137 assertEquals("compareTo", 1, fCheckpoint1
.compareTo(fCheckpoint2
));
138 assertEquals("compareTo", -1, fCheckpoint1
.compareTo(fCheckpoint3
));
140 assertEquals("compareTo", -1, fCheckpoint2
.compareTo(fCheckpoint1
));
141 assertEquals("compareTo", 0, fCheckpoint2
.compareTo(fCheckpoint2
));
142 assertEquals("compareTo", -1, fCheckpoint2
.compareTo(fCheckpoint3
));
144 assertEquals("compareTo", 1, fCheckpoint3
.compareTo(fCheckpoint1
));
145 assertEquals("compareTo", 1, fCheckpoint3
.compareTo(fCheckpoint2
));
146 assertEquals("compareTo", 0, fCheckpoint3
.compareTo(fCheckpoint3
));
149 public void testCompareToNull() {
150 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(null, new TmfContext(fLocation1
));
151 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(null, new TmfContext(fLocation2
));
152 final TmfCheckpoint checkpoint3
= new TmfCheckpoint(null, new TmfContext(fLocation3
));
153 final TmfCheckpoint checkpoint4
= new TmfCheckpoint(null, new TmfContext(fLocation1
));
155 // Test the various 'null' vs. '!null' combinations
156 assertEquals("compareTo", 0, checkpoint1
.compareTo(fCheckpoint1
));
157 assertEquals("compareTo", 0, fCheckpoint1
.compareTo(checkpoint1
));
158 assertEquals("compareTo", -1, checkpoint1
.compareTo(fCheckpoint2
));
159 assertEquals("compareTo", 1, fCheckpoint2
.compareTo(checkpoint1
));
160 assertEquals("compareTo", -1, checkpoint1
.compareTo(fCheckpoint3
));
161 assertEquals("compareTo", 1, fCheckpoint3
.compareTo(checkpoint1
));
163 // Test the 'null' vs. 'null' combinations
164 assertEquals("compareTo", 0, checkpoint1
.compareTo(checkpoint4
));
165 assertEquals("compareTo", 0, checkpoint4
.compareTo(checkpoint1
));
166 assertEquals("compareTo", -1, checkpoint1
.compareTo(checkpoint2
));
167 assertEquals("compareTo", 1, checkpoint2
.compareTo(checkpoint1
));
168 assertEquals("compareTo", -1, checkpoint1
.compareTo(checkpoint3
));
169 assertEquals("compareTo", 1, checkpoint3
.compareTo(checkpoint1
));
172 // ------------------------------------------------------------------------
174 // ------------------------------------------------------------------------
176 public void testHashCode() {
177 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(fCheckpoint1
);
178 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(fCheckpoint2
);
180 assertTrue("hashCode", fCheckpoint1
.hashCode() == checkpoint1
.hashCode());
181 assertTrue("hashCode", fCheckpoint2
.hashCode() == checkpoint2
.hashCode());
183 assertTrue("hashCode", fCheckpoint1
.hashCode() != checkpoint2
.hashCode());
184 assertTrue("hashCode", fCheckpoint2
.hashCode() != checkpoint1
.hashCode());
187 public void testHashCodeNull() {
188 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(null, new TmfContext(fLocation1
));
189 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(fTimestamp1
, null);
190 final TmfCheckpoint checkpoint3
= new TmfCheckpoint(checkpoint1
);
191 final TmfCheckpoint checkpoint4
= new TmfCheckpoint(checkpoint2
);
193 assertTrue("hashCode", fCheckpoint1
.hashCode() != checkpoint1
.hashCode());
194 assertTrue("hashCode", fCheckpoint1
.hashCode() != checkpoint2
.hashCode());
196 assertTrue("hashCode", checkpoint1
.hashCode() == checkpoint3
.hashCode());
197 assertTrue("hashCode", checkpoint2
.hashCode() == checkpoint4
.hashCode());
200 // ------------------------------------------------------------------------
202 // ------------------------------------------------------------------------
204 public void testEqualsReflexivity() {
205 assertTrue("equals", fCheckpoint1
.equals(fCheckpoint1
));
206 assertTrue("equals", fCheckpoint2
.equals(fCheckpoint2
));
208 assertTrue("equals", !fCheckpoint1
.equals(fCheckpoint2
));
209 assertTrue("equals", !fCheckpoint2
.equals(fCheckpoint1
));
212 public void testEqualsSymmetry() {
213 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(fCheckpoint1
);
214 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(fCheckpoint2
);
216 assertTrue("equals", checkpoint1
.equals(fCheckpoint1
));
217 assertTrue("equals", fCheckpoint1
.equals(checkpoint1
));
219 assertTrue("equals", checkpoint2
.equals(fCheckpoint2
));
220 assertTrue("equals", fCheckpoint2
.equals(checkpoint2
));
223 public void testEqualsTransivity() {
224 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(fCheckpoint1
);
225 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(checkpoint1
);
226 final TmfCheckpoint checkpoint3
= new TmfCheckpoint(checkpoint2
);
228 assertTrue("equals", checkpoint1
.equals(checkpoint2
));
229 assertTrue("equals", checkpoint2
.equals(checkpoint3
));
230 assertTrue("equals", checkpoint1
.equals(checkpoint3
));
233 public void testNotEqual() {
234 // Various checkpoints
235 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(fTimestamp1
, new TmfContext(fLocation1
));
236 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(fTimestamp2
, new TmfContext(fLocation1
));
237 final TmfCheckpoint checkpoint3
= new TmfCheckpoint(fTimestamp1
, new TmfContext(fLocation2
));
238 final TmfCheckpoint checkpoint4
= new TmfCheckpoint(fTimestamp1
, null);
239 final TmfCheckpoint checkpoint5
= new TmfCheckpoint(null, new TmfContext(fLocation1
));
242 assertFalse("equals", checkpoint1
.equals(null));
245 assertFalse("equals", checkpoint1
.equals(new TmfTimestamp()));
247 // Null locations/location
248 assertFalse("equals", checkpoint1
.equals(checkpoint4
));
249 assertFalse("equals", checkpoint1
.equals(checkpoint5
));
250 assertFalse("equals", checkpoint4
.equals(checkpoint1
));
251 assertFalse("equals", checkpoint5
.equals(checkpoint1
));
253 // Different locations/location
254 assertFalse("equals", checkpoint1
.equals(checkpoint2
));
255 assertFalse("equals", checkpoint1
.equals(checkpoint3
));
258 // ------------------------------------------------------------------------
260 // ------------------------------------------------------------------------
262 public void testToString() {
263 final String expected1
= "TmfCheckpoint [fContext=" + fCheckpoint1
.getContext() +
264 ", fTimestamp=" + fCheckpoint1
.getTimestamp() + "]";
265 final String expected2
= "TmfCheckpoint [fContext=" + fCheckpoint2
.getContext() +
266 ", fTimestamp=" + fCheckpoint2
.getTimestamp() + "]";
267 final String expected3
= "TmfCheckpoint [fContext=" + fCheckpoint3
.getContext() +
268 ", fTimestamp=" + fCheckpoint3
.getTimestamp() + "]";
270 assertEquals("toString", expected1
, fCheckpoint1
.toString());
271 assertEquals("toString", expected2
, fCheckpoint2
.toString());
272 assertEquals("toString", expected3
, fCheckpoint3
.toString());