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