tmf.core: Introduce TmfTimestamp factory methods
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / trace / location / TmfLocationTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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.tracecompass.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.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;
34
35 /**
36 * Test suite for the TmfLocation class.
37 */
38 @SuppressWarnings("javadoc")
39 public class TmfLocationTest {
40
41 // ------------------------------------------------------------------------
42 // Variables
43 // ------------------------------------------------------------------------
44
45 private Long aLong = 12345L;
46 private ITmfTimestamp aTimestamp = TmfTimestamp.create(0, ITmfTimestamp.SECOND_SCALE);
47 private TmfLocationArray aLocationArray;
48
49 private TmfLongLocation fLocation1;
50 private TmfLongLocation fLocation2;
51 private TmfTimestampLocation fLocation3;
52 private TmfExperimentLocation fExpLocation;
53
54 // ------------------------------------------------------------------------
55 // Housekeeping
56 // ------------------------------------------------------------------------
57
58 @Before
59 public void setUp() {
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 }
66 );
67 fExpLocation = new TmfExperimentLocation(aLocationArray);
68 }
69
70 // ------------------------------------------------------------------------
71 // Constructors
72 // ------------------------------------------------------------------------
73
74 @Test
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());
80 }
81
82 @Test
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);
88
89 assertNull("TmfLocation", location1.getLocationInfo());
90 assertEquals("TmfLocation", aLong, location2.getLocationInfo());
91 assertEquals("TmfLocation", aTimestamp, location3.getLocationInfo());
92 assertEquals("TmfLocation", aLocationArray, expLocation.getLocationInfo());
93 }
94
95 // ------------------------------------------------------------------------
96 // hashCode
97 // ------------------------------------------------------------------------
98
99 @Test
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 },
111 new long[] { 1, 2 }
112 );
113 TmfExperimentLocation expLocation3 = new TmfExperimentLocation(locationArray3);
114
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());
119
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());
125 }
126
127 // ------------------------------------------------------------------------
128 // toEquals
129 // ------------------------------------------------------------------------
130
131 private static class TmfTestLongLocation extends TmfLocation {
132 public TmfTestLongLocation(Long location) {
133 super(location);
134 }
135
136 @Override
137 public void serialize(ByteBuffer bufferOut) {}
138 }
139
140 private static class TmfTestLongLocation2 extends TmfTestLongLocation {
141 public TmfTestLongLocation2(Long location) {
142 super(location);
143 }
144 }
145
146 @Test
147 public void testEqualsWrongTypes() {
148 ITmfLocation location1 = new TmfTestLongLocation(aLong);
149 TmfTestLongLocation location2 = new TmfTestLongLocation2(aLong);
150
151 assertFalse("equals", location1.equals(location2));
152 assertFalse("equals", location2.equals(location1));
153 }
154
155 @Test
156 public void testEqualsWithNulls() {
157 ITmfLocation location1 = new TmfLongLocation(aLong);
158 ITmfLocation location2 = new TmfLongLocation((Long) null);
159
160 assertFalse("equals", location1.equals(location2));
161 assertFalse("equals", location2.equals(location1));
162 }
163
164 @Test
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));
170
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 },
179 new long[] { 1, 2 }
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));
188 }
189
190 @Test
191 public void testEqualsSymmetry() {
192 TmfLongLocation location2 = new TmfLongLocation(aLong);
193 TmfTimestampLocation location3 = new TmfTimestampLocation(aTimestamp);
194 TmfExperimentLocation expLocation = new TmfExperimentLocation(fExpLocation);
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));
201
202 assertTrue("equals", expLocation.equals(fExpLocation));
203 assertTrue("equals", fExpLocation.equals(expLocation));
204 }
205
206 @Test
207 public void testEqualsTransivity() {
208 TmfLongLocation location1 = new TmfLongLocation(aLong);
209 TmfLongLocation location2 = new TmfLongLocation(aLong);
210 TmfLongLocation location3 = new TmfLongLocation(aLong);
211
212 TmfExperimentLocation expLocation1 = new TmfExperimentLocation(aLocationArray);
213 TmfExperimentLocation expLocation2 = new TmfExperimentLocation(aLocationArray);
214 TmfExperimentLocation expLocation3 = new TmfExperimentLocation(aLocationArray);
215
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));
222 }
223
224 @Test
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));
230 }
231
232 // ------------------------------------------------------------------------
233 // toString
234 // ------------------------------------------------------------------------
235
236 @Test
237 public void testToString() {
238 ITmfTimestamp ts = TmfTimestamp.create(0, ITmfTimestamp.SECOND_SCALE);
239
240 TmfLongLocation location1 = new TmfLongLocation(aLong);
241 TmfTimestampLocation location2 = new TmfTimestampLocation(ts);
242 TmfExperimentLocation expLocation = new TmfExperimentLocation(aLocationArray);
243
244 String expected1 = "TmfLongLocation [fLocationInfo=" + aLong + "]";
245 String expected2 = "TmfTimestampLocation [fLocationInfo=" + ts + "]";
246 String expected3 = "TmfExperimentLocation [" + aLocationArray + "]";
247
248 assertEquals("toString", expected1, location1.toString());
249 assertEquals("toString", expected2, location2.toString());
250 assertEquals("toString", expected3, expLocation.toString());
251 }
252
253 }
This page took 0.052609 seconds and 6 git commands to generate.