1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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 * Alexandre Montplaisir - Port to JUnit4
12 * Patrick Tasse - Add tests for TmfExperimentLocation
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
.location
;
17 import static org
.junit
.Assert
.assertEquals
;
18 import static org
.junit
.Assert
.assertFalse
;
19 import static org
.junit
.Assert
.assertNull
;
20 import static org
.junit
.Assert
.assertTrue
;
22 import java
.nio
.ByteBuffer
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.experiment
.TmfExperimentLocation
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.trace
.experiment
.TmfLocationArray
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLocation
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLongLocation
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfTimestampLocation
;
32 import org
.junit
.Before
;
33 import org
.junit
.Test
;
36 * Test suite for the TmfLocation class.
38 @SuppressWarnings("javadoc")
39 public class TmfLocationTest
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 private Long aLong
= 12345L;
46 private ITmfTimestamp aTimestamp
= TmfTimestamp
.create(0, ITmfTimestamp
.SECOND_SCALE
);
47 private TmfLocationArray aLocationArray
;
49 private TmfLongLocation fLocation1
;
50 private TmfLongLocation fLocation2
;
51 private TmfTimestampLocation fLocation3
;
52 private TmfExperimentLocation fExpLocation
;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
60 fLocation1
= new TmfLongLocation((Long
) null);
61 fLocation2
= new TmfLongLocation(aLong
);
62 fLocation3
= new TmfTimestampLocation(aTimestamp
);
63 aLocationArray
= new TmfLocationArray(
64 new ITmfLocation
[] { fLocation1
, fLocation2
, fLocation3
},
65 new long[] { 1, 2, 3 }
67 fExpLocation
= new TmfExperimentLocation(aLocationArray
);
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 public void testTmfLocation() {
76 assertNull("TmfLocation", fLocation1
.getLocationInfo());
77 assertEquals("TmfLocation", aLong
, fLocation2
.getLocationInfo());
78 assertEquals("TmfLocation", aTimestamp
, fLocation3
.getLocationInfo());
79 assertEquals("TmfLocation", aLocationArray
, fExpLocation
.getLocationInfo());
83 public void testTmfLocationCopy() {
84 TmfLongLocation location1
= new TmfLongLocation(fLocation1
);
85 TmfLongLocation location2
= new TmfLongLocation(fLocation2
);
86 TmfTimestampLocation location3
= new TmfTimestampLocation(fLocation3
);
87 TmfExperimentLocation expLocation
= new TmfExperimentLocation(fExpLocation
);
89 assertNull("TmfLocation", location1
.getLocationInfo());
90 assertEquals("TmfLocation", aLong
, location2
.getLocationInfo());
91 assertEquals("TmfLocation", aTimestamp
, location3
.getLocationInfo());
92 assertEquals("TmfLocation", aLocationArray
, expLocation
.getLocationInfo());
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 public void testHashCode() {
101 TmfLongLocation location1
= new TmfLongLocation((Long
) null);
102 TmfLongLocation location2
= new TmfLongLocation(aLong
);
103 TmfTimestampLocation location3
= new TmfTimestampLocation(aTimestamp
);
104 TmfExperimentLocation expLocation
= new TmfExperimentLocation(fExpLocation
);
105 TmfLocationArray locationArray1
= new TmfLocationArray(aLocationArray
, 2, fLocation3
, 5);
106 TmfExperimentLocation expLocation1
= new TmfExperimentLocation(locationArray1
);
107 TmfLocationArray locationArray2
= new TmfLocationArray(aLocationArray
, 2, fLocation2
, 4);
108 TmfExperimentLocation expLocation2
= new TmfExperimentLocation(locationArray2
);
109 TmfLocationArray locationArray3
= new TmfLocationArray(
110 new ITmfLocation
[] { fLocation1
, fLocation2
},
113 TmfExperimentLocation expLocation3
= new TmfExperimentLocation(locationArray3
);
115 assertTrue("hashCode", fLocation1
.hashCode() == location1
.hashCode());
116 assertTrue("hashCode", fLocation2
.hashCode() == location2
.hashCode());
117 assertTrue("hashCode", fLocation3
.hashCode() == location3
.hashCode());
118 assertTrue("hashCode", fExpLocation
.hashCode() == expLocation
.hashCode());
120 assertTrue("hashCode", fLocation2
.hashCode() != location3
.hashCode());
121 assertTrue("hashCode", fLocation3
.hashCode() != location2
.hashCode());
122 assertTrue("hashCode", fExpLocation
.hashCode() != expLocation1
.hashCode());
123 assertTrue("hashCode", fExpLocation
.hashCode() != expLocation2
.hashCode());
124 assertTrue("hashCode", fExpLocation
.hashCode() != expLocation3
.hashCode());
127 // ------------------------------------------------------------------------
129 // ------------------------------------------------------------------------
131 private static class TmfTestLongLocation
extends TmfLocation
{
132 public TmfTestLongLocation(Long location
) {
137 public void serialize(ByteBuffer bufferOut
) {}
140 private static class TmfTestLongLocation2
extends TmfTestLongLocation
{
141 public TmfTestLongLocation2(Long location
) {
147 public void testEqualsWrongTypes() {
148 ITmfLocation location1
= new TmfTestLongLocation(aLong
);
149 TmfTestLongLocation location2
= new TmfTestLongLocation2(aLong
);
151 assertFalse("equals", location1
.equals(location2
));
152 assertFalse("equals", location2
.equals(location1
));
156 public void testEqualsWithNulls() {
157 ITmfLocation location1
= new TmfLongLocation(aLong
);
158 ITmfLocation location2
= new TmfLongLocation((Long
) null);
160 assertFalse("equals", location1
.equals(location2
));
161 assertFalse("equals", location2
.equals(location1
));
165 public void testEqualsReflexivity() {
166 assertTrue("equals", fLocation1
.equals(fLocation1
));
167 assertTrue("equals", fLocation2
.equals(fLocation2
));
168 assertTrue("equals", fLocation3
.equals(fLocation3
));
169 assertTrue("equals", fExpLocation
.equals(fExpLocation
));
171 assertTrue("equals", !fLocation2
.equals(fLocation3
));
172 assertTrue("equals", !fLocation3
.equals(fLocation2
));
173 TmfLocationArray locationArray1
= new TmfLocationArray(aLocationArray
, 2, fLocation3
, 5);
174 TmfExperimentLocation expLocation1
= new TmfExperimentLocation(locationArray1
);
175 TmfLocationArray locationArray2
= new TmfLocationArray(aLocationArray
, 2, fLocation2
, 4);
176 TmfExperimentLocation expLocation2
= new TmfExperimentLocation(locationArray2
);
177 TmfLocationArray locationArray3
= new TmfLocationArray(
178 new ITmfLocation
[] { fLocation1
, fLocation2
, fLocation3
},
181 TmfExperimentLocation expLocation3
= new TmfExperimentLocation(locationArray3
);
182 assertTrue("equals", !fExpLocation
.equals(expLocation1
));
183 assertTrue("equals", !expLocation1
.equals(fExpLocation
));
184 assertTrue("equals", !fExpLocation
.equals(expLocation2
));
185 assertTrue("equals", !expLocation2
.equals(fExpLocation
));
186 assertTrue("equals", !fExpLocation
.equals(expLocation3
));
187 assertTrue("equals", !expLocation3
.equals(fExpLocation
));
191 public void testEqualsSymmetry() {
192 TmfLongLocation location2
= new TmfLongLocation(aLong
);
193 TmfTimestampLocation location3
= new TmfTimestampLocation(aTimestamp
);
194 TmfExperimentLocation expLocation
= new TmfExperimentLocation(fExpLocation
);
196 assertTrue("equals", location2
.equals(fLocation2
));
197 assertTrue("equals", fLocation2
.equals(location2
));
199 assertTrue("equals", location3
.equals(fLocation3
));
200 assertTrue("equals", fLocation3
.equals(location3
));
202 assertTrue("equals", expLocation
.equals(fExpLocation
));
203 assertTrue("equals", fExpLocation
.equals(expLocation
));
207 public void testEqualsTransivity() {
208 TmfLongLocation location1
= new TmfLongLocation(aLong
);
209 TmfLongLocation location2
= new TmfLongLocation(aLong
);
210 TmfLongLocation location3
= new TmfLongLocation(aLong
);
212 TmfExperimentLocation expLocation1
= new TmfExperimentLocation(aLocationArray
);
213 TmfExperimentLocation expLocation2
= new TmfExperimentLocation(aLocationArray
);
214 TmfExperimentLocation expLocation3
= new TmfExperimentLocation(aLocationArray
);
216 assertTrue("equals", location1
.equals(location2
));
217 assertTrue("equals", location2
.equals(location3
));
218 assertTrue("equals", location3
.equals(location1
));
219 assertTrue("equals", expLocation1
.equals(expLocation2
));
220 assertTrue("equals", expLocation2
.equals(expLocation3
));
221 assertTrue("equals", expLocation3
.equals(expLocation1
));
225 public void testEqualsNull() {
226 assertTrue("equals", !fLocation1
.equals(null));
227 assertTrue("equals", !fLocation2
.equals(null));
228 assertTrue("equals", !fLocation3
.equals(null));
229 assertTrue("equals", !fExpLocation
.equals(null));
232 // ------------------------------------------------------------------------
234 // ------------------------------------------------------------------------
237 public void testToString() {
238 ITmfTimestamp ts
= TmfTimestamp
.create(0, ITmfTimestamp
.SECOND_SCALE
);
240 TmfLongLocation location1
= new TmfLongLocation(aLong
);
241 TmfTimestampLocation location2
= new TmfTimestampLocation(ts
);
242 TmfExperimentLocation expLocation
= new TmfExperimentLocation(aLocationArray
);
244 String expected1
= "TmfLongLocation [fLocationInfo=" + aLong
+ "]";
245 String expected2
= "TmfTimestampLocation [fLocationInfo=" + ts
+ "]";
246 String expected3
= "TmfExperimentLocation [" + aLocationArray
+ "]";
248 assertEquals("toString", expected1
, location1
.toString());
249 assertEquals("toString", expected2
, location2
.toString());
250 assertEquals("toString", expected3
, expLocation
.toString());