Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2009, 2014 Ericsson |
54a7a54c | 3 | * |
d18dd09b ASL |
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 | |
54a7a54c | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
6e1886bc | 11 | * Alexandre Montplaisir - Port to JUnit4 |
980a93c6 | 12 | * Patrick Tasse - Add tests for TmfExperimentLocation |
d18dd09b ASL |
13 | *******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.core.tests.trace.location; |
d18dd09b | 16 | |
6e1886bc AM |
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; | |
d18dd09b | 21 | |
46965b5e AM |
22 | import java.nio.ByteBuffer; |
23 | ||
5c5fa260 AM |
24 | import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfExperimentLocation; |
25 | import org.eclipse.tracecompass.internal.tmf.core.trace.experiment.TmfLocationArray; | |
b2c971ec | 26 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; |
2bdf0193 AM |
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; | |
6e1886bc AM |
32 | import org.junit.Before; |
33 | import org.junit.Test; | |
d18dd09b ASL |
34 | |
35 | /** | |
d18dd09b ASL |
36 | * Test suite for the TmfLocation class. |
37 | */ | |
cad06250 | 38 | @SuppressWarnings("javadoc") |
6e1886bc | 39 | public class TmfLocationTest { |
d18dd09b | 40 | |
fcccd900 FC |
41 | // ------------------------------------------------------------------------ |
42 | // Variables | |
43 | // ------------------------------------------------------------------------ | |
d18dd09b | 44 | |
6e1886bc | 45 | private Long aLong = 12345L; |
b2c971ec | 46 | private ITmfTimestamp aTimestamp = TmfTimestamp.create(0, ITmfTimestamp.SECOND_SCALE); |
980a93c6 | 47 | private TmfLocationArray aLocationArray; |
d18dd09b | 48 | |
8eaa4a57 MAL |
49 | private TmfLongLocation fLocation1; |
50 | private TmfLongLocation fLocation2; | |
51 | private TmfTimestampLocation fLocation3; | |
980a93c6 | 52 | private TmfExperimentLocation fExpLocation; |
d18dd09b ASL |
53 | |
54 | // ------------------------------------------------------------------------ | |
55 | // Housekeeping | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
6e1886bc AM |
58 | @Before |
59 | public void setUp() { | |
8eaa4a57 MAL |
60 | fLocation1 = new TmfLongLocation((Long) null); |
61 | fLocation2 = new TmfLongLocation(aLong); | |
62 | fLocation3 = new TmfTimestampLocation(aTimestamp); | |
980a93c6 | 63 | aLocationArray = new TmfLocationArray( |
8eaa4a57 MAL |
64 | new ITmfLocation[] { fLocation1, fLocation2, fLocation3 }, |
65 | new long[] { 1, 2, 3 } | |
980a93c6 PT |
66 | ); |
67 | fExpLocation = new TmfExperimentLocation(aLocationArray); | |
fcccd900 FC |
68 | } |
69 | ||
d18dd09b ASL |
70 | // ------------------------------------------------------------------------ |
71 | // Constructors | |
72 | // ------------------------------------------------------------------------ | |
73 | ||
6e1886bc | 74 | @Test |
fcccd900 | 75 | public void testTmfLocation() { |
5976d44a | 76 | assertNull("TmfLocation", fLocation1.getLocationInfo()); |
8eaa4a57 MAL |
77 | assertEquals("TmfLocation", aLong, fLocation2.getLocationInfo()); |
78 | assertEquals("TmfLocation", aTimestamp, fLocation3.getLocationInfo()); | |
980a93c6 | 79 | assertEquals("TmfLocation", aLocationArray, fExpLocation.getLocationInfo()); |
fcccd900 FC |
80 | } |
81 | ||
6e1886bc | 82 | @Test |
fcccd900 | 83 | public void testTmfLocationCopy() { |
8eaa4a57 MAL |
84 | TmfLongLocation location1 = new TmfLongLocation(fLocation1); |
85 | TmfLongLocation location2 = new TmfLongLocation(fLocation2); | |
86 | TmfTimestampLocation location3 = new TmfTimestampLocation(fLocation3); | |
980a93c6 | 87 | TmfExperimentLocation expLocation = new TmfExperimentLocation(fExpLocation); |
fcccd900 | 88 | |
5976d44a | 89 | assertNull("TmfLocation", location1.getLocationInfo()); |
8eaa4a57 MAL |
90 | assertEquals("TmfLocation", aLong, location2.getLocationInfo()); |
91 | assertEquals("TmfLocation", aTimestamp, location3.getLocationInfo()); | |
980a93c6 | 92 | assertEquals("TmfLocation", aLocationArray, expLocation.getLocationInfo()); |
fcccd900 FC |
93 | } |
94 | ||
d18dd09b | 95 | // ------------------------------------------------------------------------ |
fcccd900 | 96 | // hashCode |
d18dd09b ASL |
97 | // ------------------------------------------------------------------------ |
98 | ||
6e1886bc | 99 | @Test |
54a7a54c | 100 | public void testHashCode() { |
8eaa4a57 MAL |
101 | TmfLongLocation location1 = new TmfLongLocation((Long) null); |
102 | TmfLongLocation location2 = new TmfLongLocation(aLong); | |
103 | TmfTimestampLocation location3 = new TmfTimestampLocation(aTimestamp); | |
980a93c6 | 104 | TmfExperimentLocation expLocation = new TmfExperimentLocation(fExpLocation); |
8eaa4a57 | 105 | TmfLocationArray locationArray1 = new TmfLocationArray(aLocationArray, 2, fLocation3, 5); |
980a93c6 | 106 | TmfExperimentLocation expLocation1 = new TmfExperimentLocation(locationArray1); |
8eaa4a57 | 107 | TmfLocationArray locationArray2 = new TmfLocationArray(aLocationArray, 2, fLocation2, 4); |
980a93c6 PT |
108 | TmfExperimentLocation expLocation2 = new TmfExperimentLocation(locationArray2); |
109 | TmfLocationArray locationArray3 = new TmfLocationArray( | |
8eaa4a57 MAL |
110 | new ITmfLocation[] { fLocation1, fLocation2 }, |
111 | new long[] { 1, 2 } | |
980a93c6 PT |
112 | ); |
113 | TmfExperimentLocation expLocation3 = new TmfExperimentLocation(locationArray3); | |
d18dd09b | 114 | |
fcccd900 FC |
115 | assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode()); |
116 | assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode()); | |
117 | assertTrue("hashCode", fLocation3.hashCode() == location3.hashCode()); | |
980a93c6 | 118 | assertTrue("hashCode", fExpLocation.hashCode() == expLocation.hashCode()); |
fcccd900 FC |
119 | |
120 | assertTrue("hashCode", fLocation2.hashCode() != location3.hashCode()); | |
121 | assertTrue("hashCode", fLocation3.hashCode() != location2.hashCode()); | |
980a93c6 PT |
122 | assertTrue("hashCode", fExpLocation.hashCode() != expLocation1.hashCode()); |
123 | assertTrue("hashCode", fExpLocation.hashCode() != expLocation2.hashCode()); | |
124 | assertTrue("hashCode", fExpLocation.hashCode() != expLocation3.hashCode()); | |
fcccd900 | 125 | } |
d18dd09b ASL |
126 | |
127 | // ------------------------------------------------------------------------ | |
128 | // toEquals | |
129 | // ------------------------------------------------------------------------ | |
130 | ||
8eaa4a57 MAL |
131 | private static class TmfTestLongLocation extends TmfLocation { |
132 | public TmfTestLongLocation(Long location) { | |
133 | super(location); | |
134 | } | |
46965b5e AM |
135 | |
136 | @Override | |
137 | public void serialize(ByteBuffer bufferOut) {} | |
8eaa4a57 MAL |
138 | } |
139 | ||
140 | private static class TmfTestLongLocation2 extends TmfTestLongLocation { | |
141 | public TmfTestLongLocation2(Long location) { | |
fcccd900 FC |
142 | super(location); |
143 | } | |
144 | } | |
145 | ||
6e1886bc | 146 | @Test |
54a7a54c | 147 | public void testEqualsWrongTypes() { |
8eaa4a57 MAL |
148 | ITmfLocation location1 = new TmfTestLongLocation(aLong); |
149 | TmfTestLongLocation location2 = new TmfTestLongLocation2(aLong); | |
54a7a54c | 150 | |
fcccd900 FC |
151 | assertFalse("equals", location1.equals(location2)); |
152 | assertFalse("equals", location2.equals(location1)); | |
153 | } | |
154 | ||
6e1886bc | 155 | @Test |
54a7a54c | 156 | public void testEqualsWithNulls() { |
8eaa4a57 MAL |
157 | ITmfLocation location1 = new TmfLongLocation(aLong); |
158 | ITmfLocation location2 = new TmfLongLocation((Long) null); | |
54a7a54c | 159 | |
fcccd900 FC |
160 | assertFalse("equals", location1.equals(location2)); |
161 | assertFalse("equals", location2.equals(location1)); | |
162 | } | |
163 | ||
6e1886bc | 164 | @Test |
54a7a54c | 165 | public void testEqualsReflexivity() { |
8eaa4a57 | 166 | assertTrue("equals", fLocation1.equals(fLocation1)); |
fcccd900 FC |
167 | assertTrue("equals", fLocation2.equals(fLocation2)); |
168 | assertTrue("equals", fLocation3.equals(fLocation3)); | |
980a93c6 | 169 | assertTrue("equals", fExpLocation.equals(fExpLocation)); |
fcccd900 FC |
170 | |
171 | assertTrue("equals", !fLocation2.equals(fLocation3)); | |
172 | assertTrue("equals", !fLocation3.equals(fLocation2)); | |
8eaa4a57 | 173 | TmfLocationArray locationArray1 = new TmfLocationArray(aLocationArray, 2, fLocation3, 5); |
980a93c6 | 174 | TmfExperimentLocation expLocation1 = new TmfExperimentLocation(locationArray1); |
8eaa4a57 | 175 | TmfLocationArray locationArray2 = new TmfLocationArray(aLocationArray, 2, fLocation2, 4); |
980a93c6 PT |
176 | TmfExperimentLocation expLocation2 = new TmfExperimentLocation(locationArray2); |
177 | TmfLocationArray locationArray3 = new TmfLocationArray( | |
178 | new ITmfLocation[] { fLocation1, fLocation2, fLocation3 }, | |
8eaa4a57 | 179 | new long[] { 1, 2 } |
980a93c6 PT |
180 | ); |
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)); | |
fcccd900 FC |
188 | } |
189 | ||
6e1886bc | 190 | @Test |
54a7a54c | 191 | public void testEqualsSymmetry() { |
8eaa4a57 MAL |
192 | TmfLongLocation location2 = new TmfLongLocation(aLong); |
193 | TmfTimestampLocation location3 = new TmfTimestampLocation(aTimestamp); | |
980a93c6 | 194 | TmfExperimentLocation expLocation = new TmfExperimentLocation(fExpLocation); |
fcccd900 FC |
195 | |
196 | assertTrue("equals", location2.equals(fLocation2)); | |
197 | assertTrue("equals", fLocation2.equals(location2)); | |
198 | ||
199 | assertTrue("equals", location3.equals(fLocation3)); | |
200 | assertTrue("equals", fLocation3.equals(location3)); | |
980a93c6 PT |
201 | |
202 | assertTrue("equals", expLocation.equals(fExpLocation)); | |
203 | assertTrue("equals", fExpLocation.equals(expLocation)); | |
fcccd900 FC |
204 | } |
205 | ||
6e1886bc | 206 | @Test |
54a7a54c | 207 | public void testEqualsTransivity() { |
8eaa4a57 MAL |
208 | TmfLongLocation location1 = new TmfLongLocation(aLong); |
209 | TmfLongLocation location2 = new TmfLongLocation(aLong); | |
210 | TmfLongLocation location3 = new TmfLongLocation(aLong); | |
211 | ||
980a93c6 PT |
212 | TmfExperimentLocation expLocation1 = new TmfExperimentLocation(aLocationArray); |
213 | TmfExperimentLocation expLocation2 = new TmfExperimentLocation(aLocationArray); | |
214 | TmfExperimentLocation expLocation3 = new TmfExperimentLocation(aLocationArray); | |
fcccd900 FC |
215 | |
216 | assertTrue("equals", location1.equals(location2)); | |
217 | assertTrue("equals", location2.equals(location3)); | |
218 | assertTrue("equals", location3.equals(location1)); | |
980a93c6 PT |
219 | assertTrue("equals", expLocation1.equals(expLocation2)); |
220 | assertTrue("equals", expLocation2.equals(expLocation3)); | |
221 | assertTrue("equals", expLocation3.equals(expLocation1)); | |
fcccd900 FC |
222 | } |
223 | ||
6e1886bc | 224 | @Test |
54a7a54c | 225 | public void testEqualsNull() { |
8eaa4a57 | 226 | assertTrue("equals", !fLocation1.equals(null)); |
fcccd900 | 227 | assertTrue("equals", !fLocation2.equals(null)); |
8eaa4a57 | 228 | assertTrue("equals", !fLocation3.equals(null)); |
980a93c6 | 229 | assertTrue("equals", !fExpLocation.equals(null)); |
fcccd900 FC |
230 | } |
231 | ||
d18dd09b ASL |
232 | // ------------------------------------------------------------------------ |
233 | // toString | |
234 | // ------------------------------------------------------------------------ | |
235 | ||
6e1886bc | 236 | @Test |
fcccd900 | 237 | public void testToString() { |
b2c971ec | 238 | ITmfTimestamp ts = TmfTimestamp.create(0, ITmfTimestamp.SECOND_SCALE); |
d18dd09b | 239 | |
8eaa4a57 MAL |
240 | TmfLongLocation location1 = new TmfLongLocation(aLong); |
241 | TmfTimestampLocation location2 = new TmfTimestampLocation(ts); | |
980a93c6 | 242 | TmfExperimentLocation expLocation = new TmfExperimentLocation(aLocationArray); |
d18dd09b | 243 | |
8eaa4a57 MAL |
244 | String expected1 = "TmfLongLocation [fLocationInfo=" + aLong + "]"; |
245 | String expected2 = "TmfTimestampLocation [fLocationInfo=" + ts + "]"; | |
246 | String expected3 = "TmfExperimentLocation [" + aLocationArray + "]"; | |
d18dd09b | 247 | |
fcccd900 FC |
248 | assertEquals("toString", expected1, location1.toString()); |
249 | assertEquals("toString", expected2, location2.toString()); | |
8eaa4a57 | 250 | assertEquals("toString", expected3, expLocation.toString()); |
fcccd900 | 251 | } |
d18dd09b ASL |
252 | |
253 | } |