1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Alexandre Montplaisir - Port to JUnit4
13 * Patrick Tasse - Updated for location in checkpoint
14 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
18 import static org
.junit
.Assert
.assertEquals
;
19 import static org
.junit
.Assert
.assertFalse
;
20 import static org
.junit
.Assert
.assertTrue
;
21 import static org
.junit
.Assert
.fail
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpoint
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
28 import org
.junit
.Test
;
31 * Test suite for the TmfCheckpoint class.
33 @SuppressWarnings("javadoc")
34 public class TmfCheckpointTest
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 private ITmfTimestamp fTimestamp1
= new TmfTimestamp();
41 private ITmfTimestamp fTimestamp2
= TmfTimestamp
.BIG_BANG
;
42 private ITmfTimestamp fTimestamp3
= TmfTimestamp
.BIG_CRUNCH
;
44 private Long aLong1
= 12345L;
45 private Long aLong2
= 23456L;
46 private Long aLong3
= 34567L;
47 private ITmfLocation fLocation1
= new TmfLongLocation(aLong1
);
48 private ITmfLocation fLocation2
= new TmfLongLocation(aLong2
);
49 private ITmfLocation fLocation3
= new TmfLongLocation(aLong3
);
51 private TmfCheckpoint fCheckpoint1
= new TmfCheckpoint(fTimestamp1
, fLocation1
);
52 private TmfCheckpoint fCheckpoint2
= new TmfCheckpoint(fTimestamp2
, fLocation2
);
53 private TmfCheckpoint fCheckpoint3
= new TmfCheckpoint(fTimestamp3
, fLocation3
);
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 public void testTmfCheckpoint() {
61 assertEquals("TmfCheckpoint", fTimestamp1
, fCheckpoint1
.getTimestamp());
62 assertEquals("TmfCheckpoint", fLocation1
, fCheckpoint1
.getLocation());
65 public void testTmfLocationCopy() {
66 final TmfCheckpoint checkpoint
= new TmfCheckpoint(fCheckpoint1
);
68 assertEquals("TmfCheckpoint", fTimestamp1
, checkpoint
.getTimestamp());
69 assertEquals("TmfCheckpoint", fLocation1
, checkpoint
.getLocation());
73 public void testTmfLocationCopy2() {
75 new TmfCheckpoint(null);
78 catch (final IllegalArgumentException e
) {
81 catch (final Exception e
) {
82 fail("wrong exception");
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 public void testCompareTo() {
92 assertEquals("compareTo", 0, fCheckpoint1
.compareTo(fCheckpoint1
));
93 assertEquals("compareTo", 1, fCheckpoint1
.compareTo(fCheckpoint2
));
94 assertEquals("compareTo", -1, fCheckpoint1
.compareTo(fCheckpoint3
));
96 assertEquals("compareTo", -1, fCheckpoint2
.compareTo(fCheckpoint1
));
97 assertEquals("compareTo", 0, fCheckpoint2
.compareTo(fCheckpoint2
));
98 assertEquals("compareTo", -1, fCheckpoint2
.compareTo(fCheckpoint3
));
100 assertEquals("compareTo", 1, fCheckpoint3
.compareTo(fCheckpoint1
));
101 assertEquals("compareTo", 1, fCheckpoint3
.compareTo(fCheckpoint2
));
102 assertEquals("compareTo", 0, fCheckpoint3
.compareTo(fCheckpoint3
));
106 public void testCompareToNull() {
107 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(null, fLocation1
);
108 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(null, fLocation2
);
109 final TmfCheckpoint checkpoint3
= new TmfCheckpoint(null, fLocation3
);
110 final TmfCheckpoint checkpoint4
= new TmfCheckpoint(null, fLocation1
);
112 // Test the various 'null' vs. '!null' combinations
113 assertEquals("compareTo", 0, checkpoint1
.compareTo(fCheckpoint1
));
114 assertEquals("compareTo", 0, fCheckpoint1
.compareTo(checkpoint1
));
115 assertEquals("compareTo", -1, checkpoint1
.compareTo(fCheckpoint2
));
116 assertEquals("compareTo", 1, fCheckpoint2
.compareTo(checkpoint1
));
117 assertEquals("compareTo", -1, checkpoint1
.compareTo(fCheckpoint3
));
118 assertEquals("compareTo", 1, fCheckpoint3
.compareTo(checkpoint1
));
120 // Test the 'null' vs. 'null' combinations
121 assertEquals("compareTo", 0, checkpoint1
.compareTo(checkpoint4
));
122 assertEquals("compareTo", 0, checkpoint4
.compareTo(checkpoint1
));
123 assertEquals("compareTo", -1, checkpoint1
.compareTo(checkpoint2
));
124 assertEquals("compareTo", 1, checkpoint2
.compareTo(checkpoint1
));
125 assertEquals("compareTo", -1, checkpoint1
.compareTo(checkpoint3
));
126 assertEquals("compareTo", 1, checkpoint3
.compareTo(checkpoint1
));
129 // ------------------------------------------------------------------------
131 // ------------------------------------------------------------------------
134 public void testHashCode() {
135 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(fCheckpoint1
);
136 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(fCheckpoint2
);
138 assertTrue("hashCode", fCheckpoint1
.hashCode() == checkpoint1
.hashCode());
139 assertTrue("hashCode", fCheckpoint2
.hashCode() == checkpoint2
.hashCode());
141 assertTrue("hashCode", fCheckpoint1
.hashCode() != checkpoint2
.hashCode());
142 assertTrue("hashCode", fCheckpoint2
.hashCode() != checkpoint1
.hashCode());
146 public void testHashCodeNull() {
147 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(null, fLocation1
);
148 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(fTimestamp1
, null);
149 final TmfCheckpoint checkpoint3
= new TmfCheckpoint(checkpoint1
);
150 final TmfCheckpoint checkpoint4
= new TmfCheckpoint(checkpoint2
);
152 assertTrue("hashCode", fCheckpoint1
.hashCode() != checkpoint1
.hashCode());
153 assertTrue("hashCode", fCheckpoint1
.hashCode() != checkpoint2
.hashCode());
155 assertTrue("hashCode", checkpoint1
.hashCode() == checkpoint3
.hashCode());
156 assertTrue("hashCode", checkpoint2
.hashCode() == checkpoint4
.hashCode());
159 // ------------------------------------------------------------------------
161 // ------------------------------------------------------------------------
164 public void testEqualsReflexivity() {
165 assertTrue("equals", fCheckpoint1
.equals(fCheckpoint1
));
166 assertTrue("equals", fCheckpoint2
.equals(fCheckpoint2
));
168 assertTrue("equals", !fCheckpoint1
.equals(fCheckpoint2
));
169 assertTrue("equals", !fCheckpoint2
.equals(fCheckpoint1
));
173 public void testEqualsSymmetry() {
174 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(fCheckpoint1
);
175 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(fCheckpoint2
);
177 assertTrue("equals", checkpoint1
.equals(fCheckpoint1
));
178 assertTrue("equals", fCheckpoint1
.equals(checkpoint1
));
180 assertTrue("equals", checkpoint2
.equals(fCheckpoint2
));
181 assertTrue("equals", fCheckpoint2
.equals(checkpoint2
));
185 public void testEqualsTransivity() {
186 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(fCheckpoint1
);
187 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(checkpoint1
);
188 final TmfCheckpoint checkpoint3
= new TmfCheckpoint(checkpoint2
);
190 assertTrue("equals", checkpoint1
.equals(checkpoint2
));
191 assertTrue("equals", checkpoint2
.equals(checkpoint3
));
192 assertTrue("equals", checkpoint1
.equals(checkpoint3
));
196 public void testNotEqual() {
197 // Various checkpoints
198 final TmfCheckpoint checkpoint1
= new TmfCheckpoint(fTimestamp1
, fLocation1
);
199 final TmfCheckpoint checkpoint2
= new TmfCheckpoint(fTimestamp2
, fLocation1
);
200 final TmfCheckpoint checkpoint3
= new TmfCheckpoint(fTimestamp1
, fLocation2
);
201 final TmfCheckpoint checkpoint4
= new TmfCheckpoint(fTimestamp1
, null);
202 final TmfCheckpoint checkpoint5
= new TmfCheckpoint(null, fLocation1
);
205 assertFalse("equals", checkpoint1
.equals(null));
208 assertFalse("equals", checkpoint1
.equals(new TmfTimestamp()));
210 // Null locations/location
211 assertFalse("equals", checkpoint1
.equals(checkpoint4
));
212 assertFalse("equals", checkpoint1
.equals(checkpoint5
));
213 assertFalse("equals", checkpoint4
.equals(checkpoint1
));
214 assertFalse("equals", checkpoint5
.equals(checkpoint1
));
216 // Different locations/location
217 assertFalse("equals", checkpoint1
.equals(checkpoint2
));
218 assertFalse("equals", checkpoint1
.equals(checkpoint3
));
221 // ------------------------------------------------------------------------
223 // ------------------------------------------------------------------------
226 public void testToString() {
227 final String expected1
= "TmfCheckpoint [fLocation=" + fCheckpoint1
.getLocation() +
228 ", fTimestamp=" + fCheckpoint1
.getTimestamp() + "]";
229 final String expected2
= "TmfCheckpoint [fLocation=" + fCheckpoint2
.getLocation() +
230 ", fTimestamp=" + fCheckpoint2
.getTimestamp() + "]";
231 final String expected3
= "TmfCheckpoint [fLocation=" + fCheckpoint3
.getLocation() +
232 ", fTimestamp=" + fCheckpoint3
.getTimestamp() + "]";
234 assertEquals("toString", expected1
, fCheckpoint1
.toString());
235 assertEquals("toString", expected2
, fCheckpoint2
.toString());
236 assertEquals("toString", expected3
, fCheckpoint3
.toString());