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 * Alexandre Montplaisir - Port to JUnit4
12 * Patrick Tasse - Add tests for TmfExperimentLocation
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.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 org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
23 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfLocationArray
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.ITmfLocation
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfTimestampLocation
;
28 import org
.junit
.Before
;
29 import org
.junit
.Test
;
32 * Test suite for the TmfLocation class.
34 @SuppressWarnings("javadoc")
35 public class TmfLocationTest
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private String aString
= "some location";
42 private Long aLong
= 12345L;
43 private TmfTimestamp aTimestamp
= new TmfTimestamp();
44 private TmfLocationArray aLocationArray
;
46 private TmfStringLocation fLocation1
;
47 private TmfStringLocation fLocation2
;
48 private TmfLongLocation fLocation3
;
49 private TmfTimestampLocation fLocation4
;
50 private TmfExperimentLocation fExpLocation
;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
58 fLocation1
= new TmfStringLocation((String
) null);
59 fLocation2
= new TmfStringLocation(aString
);
60 fLocation3
= new TmfLongLocation(aLong
);
61 fLocation4
= new TmfTimestampLocation(aTimestamp
);
62 aLocationArray
= new TmfLocationArray(
63 new ITmfLocation
[] { fLocation1
, fLocation2
, fLocation3
, fLocation4
},
64 new long[] { 1, 2, 3, 4 }
66 fExpLocation
= new TmfExperimentLocation(aLocationArray
);
69 // ------------------------------------------------------------------------
71 // ------------------------------------------------------------------------
74 public void testTmfLocation() {
75 assertNull("TmfLocation", fLocation1
.getLocationInfo());
76 assertEquals("TmfLocation", aString
, fLocation2
.getLocationInfo());
77 assertEquals("TmfLocation", aLong
, fLocation3
.getLocationInfo());
78 assertEquals("TmfLocation", aTimestamp
, fLocation4
.getLocationInfo());
79 assertEquals("TmfLocation", aLocationArray
, fExpLocation
.getLocationInfo());
83 public void testTmfLocationCopy() {
84 TmfStringLocation location1
= new TmfStringLocation(fLocation1
);
85 TmfStringLocation location2
= new TmfStringLocation(fLocation2
);
86 TmfLongLocation location3
= new TmfLongLocation(fLocation3
);
87 TmfTimestampLocation location4
= new TmfTimestampLocation(fLocation4
);
88 TmfExperimentLocation expLocation
= new TmfExperimentLocation(fExpLocation
);
90 assertNull("TmfLocation", location1
.getLocationInfo());
91 assertEquals("TmfLocation", aString
, location2
.getLocationInfo());
92 assertEquals("TmfLocation", aLong
, location3
.getLocationInfo());
93 assertEquals("TmfLocation", aTimestamp
, location4
.getLocationInfo());
94 assertEquals("TmfLocation", aLocationArray
, expLocation
.getLocationInfo());
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 public void testHashCode() {
103 TmfStringLocation location1
= new TmfStringLocation((String
) null);
104 TmfStringLocation location2
= new TmfStringLocation(aString
);
105 TmfLongLocation location3
= new TmfLongLocation(aLong
);
106 TmfExperimentLocation expLocation
= new TmfExperimentLocation(fExpLocation
);
107 TmfLocationArray locationArray1
= new TmfLocationArray(aLocationArray
, 3, fLocation4
, 5);
108 TmfExperimentLocation expLocation1
= new TmfExperimentLocation(locationArray1
);
109 TmfLocationArray locationArray2
= new TmfLocationArray(aLocationArray
, 3, fLocation3
, 4);
110 TmfExperimentLocation expLocation2
= new TmfExperimentLocation(locationArray2
);
111 TmfLocationArray locationArray3
= new TmfLocationArray(
112 new ITmfLocation
[] { fLocation1
, fLocation2
, fLocation3
},
113 new long[] { 1, 2, 3 }
115 TmfExperimentLocation expLocation3
= new TmfExperimentLocation(locationArray3
);
117 assertTrue("hashCode", fLocation1
.hashCode() == location1
.hashCode());
118 assertTrue("hashCode", fLocation2
.hashCode() == location2
.hashCode());
119 assertTrue("hashCode", fLocation3
.hashCode() == location3
.hashCode());
120 assertTrue("hashCode", fExpLocation
.hashCode() == expLocation
.hashCode());
122 assertTrue("hashCode", fLocation2
.hashCode() != location3
.hashCode());
123 assertTrue("hashCode", fLocation3
.hashCode() != location2
.hashCode());
124 assertTrue("hashCode", fExpLocation
.hashCode() != expLocation1
.hashCode());
125 assertTrue("hashCode", fExpLocation
.hashCode() != expLocation2
.hashCode());
126 assertTrue("hashCode", fExpLocation
.hashCode() != expLocation3
.hashCode());
129 // ------------------------------------------------------------------------
131 // ------------------------------------------------------------------------
133 private static class TmfLocation2
extends TmfStringLocation
{
134 public TmfLocation2(String location
) {
140 public void testEqualsWrongTypes() {
141 ITmfLocation location1
= new TmfStringLocation(aString
);
142 TmfLocation2 location2
= new TmfLocation2(aString
);
144 assertFalse("equals", location1
.equals(location2
));
145 assertFalse("equals", location2
.equals(location1
));
149 public void testEqualsWithNulls() {
150 TmfStringLocation location1
= new TmfStringLocation(aString
);
151 TmfStringLocation location2
= new TmfStringLocation((String
) null);
153 assertFalse("equals", location1
.equals(location2
));
154 assertFalse("equals", location2
.equals(location1
));
158 public void testEqualsReflexivity() {
159 assertTrue("equals", fLocation2
.equals(fLocation2
));
160 assertTrue("equals", fLocation3
.equals(fLocation3
));
161 assertTrue("equals", fExpLocation
.equals(fExpLocation
));
163 assertTrue("equals", !fLocation2
.equals(fLocation3
));
164 assertTrue("equals", !fLocation3
.equals(fLocation2
));
165 TmfLocationArray locationArray1
= new TmfLocationArray(aLocationArray
, 3, fLocation4
, 5);
166 TmfExperimentLocation expLocation1
= new TmfExperimentLocation(locationArray1
);
167 TmfLocationArray locationArray2
= new TmfLocationArray(aLocationArray
, 3, fLocation3
, 4);
168 TmfExperimentLocation expLocation2
= new TmfExperimentLocation(locationArray2
);
169 TmfLocationArray locationArray3
= new TmfLocationArray(
170 new ITmfLocation
[] { fLocation1
, fLocation2
, fLocation3
},
171 new long[] { 1, 2, 3 }
173 TmfExperimentLocation expLocation3
= new TmfExperimentLocation(locationArray3
);
174 assertTrue("equals", !fExpLocation
.equals(expLocation1
));
175 assertTrue("equals", !expLocation1
.equals(fExpLocation
));
176 assertTrue("equals", !fExpLocation
.equals(expLocation2
));
177 assertTrue("equals", !expLocation2
.equals(fExpLocation
));
178 assertTrue("equals", !fExpLocation
.equals(expLocation3
));
179 assertTrue("equals", !expLocation3
.equals(fExpLocation
));
183 public void testEqualsSymmetry() {
184 TmfStringLocation location2
= new TmfStringLocation(aString
);
185 TmfLongLocation location3
= new TmfLongLocation(aLong
);
186 TmfExperimentLocation expLocation
= new TmfExperimentLocation(fExpLocation
);
188 assertTrue("equals", location2
.equals(fLocation2
));
189 assertTrue("equals", fLocation2
.equals(location2
));
191 assertTrue("equals", location3
.equals(fLocation3
));
192 assertTrue("equals", fLocation3
.equals(location3
));
194 assertTrue("equals", expLocation
.equals(fExpLocation
));
195 assertTrue("equals", fExpLocation
.equals(expLocation
));
199 public void testEqualsTransivity() {
200 TmfStringLocation location1
= new TmfStringLocation(aString
);
201 TmfStringLocation location2
= new TmfStringLocation(aString
);
202 TmfStringLocation location3
= new TmfStringLocation(aString
);
203 TmfExperimentLocation expLocation1
= new TmfExperimentLocation(aLocationArray
);
204 TmfExperimentLocation expLocation2
= new TmfExperimentLocation(aLocationArray
);
205 TmfExperimentLocation expLocation3
= new TmfExperimentLocation(aLocationArray
);
207 assertTrue("equals", location1
.equals(location2
));
208 assertTrue("equals", location2
.equals(location3
));
209 assertTrue("equals", location3
.equals(location1
));
210 assertTrue("equals", expLocation1
.equals(expLocation2
));
211 assertTrue("equals", expLocation2
.equals(expLocation3
));
212 assertTrue("equals", expLocation3
.equals(expLocation1
));
216 public void testEqualsNull() {
217 assertTrue("equals", !fLocation2
.equals(null));
218 assertTrue("equals", !fLocation2
.equals(null));
219 assertTrue("equals", !fExpLocation
.equals(null));
222 // ------------------------------------------------------------------------
224 // ------------------------------------------------------------------------
227 public void testToString() {
228 String str
= "some location";
230 TmfTimestamp ts
= new TmfTimestamp();
232 TmfStringLocation location1
= new TmfStringLocation(str
);
233 TmfLongLocation location2
= new TmfLongLocation(lng
);
234 TmfTimestampLocation location3
= new TmfTimestampLocation(ts
);
235 TmfExperimentLocation expLocation
= new TmfExperimentLocation(aLocationArray
);
237 String expected1
= "TmfStringLocation [fLocationInfo=" + str
+ "]";
238 String expected2
= "TmfLongLocation [fLocationInfo=" + lng
+ "]";
239 String expected3
= "TmfTimestampLocation [fLocationInfo=" + ts
+ "]";
240 String expected4
= "TmfExperimentLocation [" + aLocationArray
+ "]";
242 assertEquals("toString", expected1
, location1
.toString());
243 assertEquals("toString", expected2
, location2
.toString());
244 assertEquals("toString", expected3
, location3
.toString());
245 assertEquals("toString", expected4
, expLocation
.toString());