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 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;
30
31 /**
32 * Test suite for the TmfLocation class.
33 */
34 @SuppressWarnings("javadoc")
35 public class TmfLocationTest {
36
37 // ------------------------------------------------------------------------
38 // Variables
39 // ------------------------------------------------------------------------
40
41 private String aString = "some location";
42 private Long aLong = 12345L;
43 private TmfTimestamp aTimestamp = new TmfTimestamp();
44 private TmfLocationArray aLocationArray;
45
46 private TmfStringLocation fLocation1;
47 private TmfStringLocation fLocation2;
48 private TmfLongLocation fLocation3;
49 private TmfTimestampLocation fLocation4;
50 private TmfExperimentLocation fExpLocation;
51
52 // ------------------------------------------------------------------------
53 // Housekeeping
54 // ------------------------------------------------------------------------
55
56 @Before
57 public void setUp() {
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 }
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", aString, fLocation2.getLocationInfo());
77 assertEquals("TmfLocation", aLong, fLocation3.getLocationInfo());
78 assertEquals("TmfLocation", aTimestamp, fLocation4.getLocationInfo());
79 assertEquals("TmfLocation", aLocationArray, fExpLocation.getLocationInfo());
80 }
81
82 @Test
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);
89
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());
95 }
96
97 // ------------------------------------------------------------------------
98 // hashCode
99 // ------------------------------------------------------------------------
100
101 @Test
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 }
114 );
115 TmfExperimentLocation expLocation3 = new TmfExperimentLocation(locationArray3);
116
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());
121
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());
127 }
128
129 // ------------------------------------------------------------------------
130 // toEquals
131 // ------------------------------------------------------------------------
132
133 private static class TmfLocation2 extends TmfStringLocation {
134 public TmfLocation2(String location) {
135 super(location);
136 }
137 }
138
139 @Test
140 public void testEqualsWrongTypes() {
141 ITmfLocation location1 = new TmfStringLocation(aString);
142 TmfLocation2 location2 = new TmfLocation2(aString);
143
144 assertFalse("equals", location1.equals(location2));
145 assertFalse("equals", location2.equals(location1));
146 }
147
148 @Test
149 public void testEqualsWithNulls() {
150 TmfStringLocation location1 = new TmfStringLocation(aString);
151 TmfStringLocation location2 = new TmfStringLocation((String) null);
152
153 assertFalse("equals", location1.equals(location2));
154 assertFalse("equals", location2.equals(location1));
155 }
156
157 @Test
158 public void testEqualsReflexivity() {
159 assertTrue("equals", fLocation2.equals(fLocation2));
160 assertTrue("equals", fLocation3.equals(fLocation3));
161 assertTrue("equals", fExpLocation.equals(fExpLocation));
162
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 }
172 );
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));
180 }
181
182 @Test
183 public void testEqualsSymmetry() {
184 TmfStringLocation location2 = new TmfStringLocation(aString);
185 TmfLongLocation location3 = new TmfLongLocation(aLong);
186 TmfExperimentLocation expLocation = new TmfExperimentLocation(fExpLocation);
187
188 assertTrue("equals", location2.equals(fLocation2));
189 assertTrue("equals", fLocation2.equals(location2));
190
191 assertTrue("equals", location3.equals(fLocation3));
192 assertTrue("equals", fLocation3.equals(location3));
193
194 assertTrue("equals", expLocation.equals(fExpLocation));
195 assertTrue("equals", fExpLocation.equals(expLocation));
196 }
197
198 @Test
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);
206
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));
213 }
214
215 @Test
216 public void testEqualsNull() {
217 assertTrue("equals", !fLocation2.equals(null));
218 assertTrue("equals", !fLocation2.equals(null));
219 assertTrue("equals", !fExpLocation.equals(null));
220 }
221
222 // ------------------------------------------------------------------------
223 // toString
224 // ------------------------------------------------------------------------
225
226 @Test
227 public void testToString() {
228 String str = "some location";
229 Long lng = 12345L;
230 TmfTimestamp ts = new TmfTimestamp();
231
232 TmfStringLocation location1 = new TmfStringLocation(str);
233 TmfLongLocation location2 = new TmfLongLocation(lng);
234 TmfTimestampLocation location3 = new TmfTimestampLocation(ts);
235 TmfExperimentLocation expLocation = new TmfExperimentLocation(aLocationArray);
236
237 String expected1 = "TmfStringLocation [fLocationInfo=" + str + "]";
238 String expected2 = "TmfLongLocation [fLocationInfo=" + lng + "]";
239 String expected3 = "TmfTimestampLocation [fLocationInfo=" + ts + "]";
240 String expected4 = "TmfExperimentLocation [" + aLocationArray + "]";
241
242 assertEquals("toString", expected1, location1.toString());
243 assertEquals("toString", expected2, location2.toString());
244 assertEquals("toString", expected3, location3.toString());
245 assertEquals("toString", expected4, expLocation.toString());
246 }
247
248 }
This page took 0.038846 seconds and 6 git commands to generate.