Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
fcccd900 | 2 | * Copyright (c) 2009, 2010, 2012 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 | |
11 | *******************************************************************************/ | |
12 | ||
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
d18dd09b ASL |
14 | |
15 | import junit.framework.TestCase; | |
16 | ||
6c13869b | 17 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
cb8c854e FC |
18 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
19 | import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation; | |
20 | import org.eclipse.linuxtools.tmf.core.trace.TmfTimestampLocation; | |
d18dd09b ASL |
21 | |
22 | /** | |
d18dd09b ASL |
23 | * Test suite for the TmfLocation class. |
24 | */ | |
54a7a54c | 25 | @SuppressWarnings({"nls","javadoc"}) |
d18dd09b ASL |
26 | public class TmfLocationTest extends TestCase { |
27 | ||
fcccd900 FC |
28 | // ------------------------------------------------------------------------ |
29 | // Variables | |
30 | // ------------------------------------------------------------------------ | |
d18dd09b | 31 | |
fcccd900 FC |
32 | String aString = "some location"; |
33 | Long aLong = 12345L; | |
34 | TmfTimestamp aTimestamp = new TmfTimestamp(); | |
d18dd09b | 35 | |
cb8c854e FC |
36 | TmfStringLocation fLocation1; |
37 | TmfStringLocation fLocation2; | |
38 | TmfLongLocation fLocation3; | |
39 | TmfTimestampLocation fLocation4; | |
d18dd09b ASL |
40 | |
41 | // ------------------------------------------------------------------------ | |
42 | // Housekeeping | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
fcccd900 FC |
45 | /** |
46 | * @param name | |
47 | * the test name | |
48 | */ | |
49 | public TmfLocationTest(String name) { | |
50 | super(name); | |
51 | } | |
52 | ||
53 | @Override | |
54 | protected void setUp() throws Exception { | |
55 | super.setUp(); | |
cb8c854e FC |
56 | fLocation1 = new TmfStringLocation((String) null); |
57 | fLocation2 = new TmfStringLocation(aString); | |
58 | fLocation3 = new TmfLongLocation(aLong); | |
59 | fLocation4 = new TmfTimestampLocation(aTimestamp); | |
fcccd900 FC |
60 | } |
61 | ||
62 | @Override | |
63 | protected void tearDown() throws Exception { | |
64 | super.tearDown(); | |
65 | } | |
d18dd09b ASL |
66 | |
67 | // ------------------------------------------------------------------------ | |
68 | // Constructors | |
69 | // ------------------------------------------------------------------------ | |
70 | ||
fcccd900 | 71 | public void testTmfLocation() { |
6bab4511 AM |
72 | assertNull("TmfLocation", fLocation1.getLocationData()); |
73 | assertEquals("TmfLocation", aString, fLocation2.getLocationData()); | |
74 | assertEquals("TmfLocation", aLong, fLocation3.getLocationData()); | |
75 | assertEquals("TmfLocation", aTimestamp, fLocation4.getLocationData()); | |
fcccd900 FC |
76 | } |
77 | ||
78 | public void testTmfLocationCopy() { | |
cb8c854e FC |
79 | TmfStringLocation location1 = new TmfStringLocation(fLocation1); |
80 | TmfStringLocation location2 = new TmfStringLocation(fLocation2); | |
81 | TmfLongLocation location3 = new TmfLongLocation(fLocation3); | |
82 | TmfTimestampLocation location4 = new TmfTimestampLocation(fLocation4); | |
fcccd900 | 83 | |
6bab4511 AM |
84 | assertNull("TmfLocation", location1.getLocationData()); |
85 | assertEquals("TmfLocation", aString, location2.getLocationData()); | |
86 | assertEquals("TmfLocation", aLong, location3.getLocationData()); | |
87 | assertEquals("TmfLocation", aTimestamp, location4.getLocationData()); | |
fcccd900 FC |
88 | } |
89 | ||
90 | // ------------------------------------------------------------------------ | |
91 | // clone | |
92 | // ------------------------------------------------------------------------ | |
93 | ||
94 | public void testClone() { | |
95 | try { | |
cb8c854e FC |
96 | TmfStringLocation location1 = fLocation1.clone(); |
97 | TmfStringLocation location2 = fLocation2.clone(); | |
98 | TmfLongLocation location3 = fLocation3.clone(); | |
99 | TmfTimestampLocation location4 = fLocation4.clone(); | |
fcccd900 FC |
100 | |
101 | assertEquals("clone", fLocation1, location1); | |
102 | assertEquals("clone", fLocation2, location2); | |
103 | assertEquals("clone", fLocation3, location3); | |
104 | assertEquals("clone", fLocation4, location4); | |
105 | ||
6bab4511 AM |
106 | assertEquals("clone", fLocation1.getLocationData(), location1.getLocationData()); |
107 | assertEquals("clone", fLocation2.getLocationData(), location2.getLocationData()); | |
108 | assertEquals("clone", fLocation3.getLocationData(), location3.getLocationData()); | |
109 | assertEquals("clone", fLocation4.getLocationData(), location4.getLocationData()); | |
fcccd900 | 110 | |
6bab4511 AM |
111 | assertNull("clone", location1.getLocationData()); |
112 | assertEquals("clone", aString, location2.getLocationData()); | |
113 | assertEquals("clone", aLong, location3.getLocationData()); | |
114 | assertEquals("clone", aTimestamp, location4.getLocationData()); | |
fcccd900 FC |
115 | } catch (InternalError e) { |
116 | fail("clone()"); | |
117 | } | |
118 | } | |
119 | ||
cb8c854e FC |
120 | // public static class MyCloneableClass implements Cloneable, Comparable<MyCloneableClass> { |
121 | // private String fName; | |
122 | // | |
123 | // public MyCloneableClass(String name) { | |
124 | // fName = name; | |
125 | // } | |
126 | // | |
127 | // @Override | |
128 | // public String toString() { | |
129 | // return fName; | |
130 | // } | |
131 | // | |
132 | // @Override | |
133 | // public MyCloneableClass clone() { | |
134 | // MyCloneableClass clone = null; | |
135 | // try { | |
136 | // clone = (MyCloneableClass) super.clone(); | |
137 | // clone.fName = fName; | |
138 | // } catch (CloneNotSupportedException e) { | |
139 | // } | |
140 | // return clone; | |
141 | // } | |
142 | // | |
143 | // @Override | |
144 | // public int compareTo(MyCloneableClass o) { | |
145 | // return fName.compareTo(o.fName); | |
146 | // } | |
147 | // | |
148 | // @Override | |
149 | // public int hashCode() { | |
150 | // final int prime = 31; | |
151 | // int result = 1; | |
152 | // result = prime * result + ((fName == null) ? 0 : fName.hashCode()); | |
153 | // return result; | |
154 | // } | |
155 | // | |
156 | // @Override | |
157 | // public boolean equals(Object obj) { | |
158 | // if (this == obj) { | |
159 | // return true; | |
160 | // } | |
161 | // if (obj == null) { | |
162 | // return false; | |
163 | // } | |
164 | // if (!(obj instanceof MyCloneableClass)) { | |
165 | // return false; | |
166 | // } | |
167 | // MyCloneableClass other = (MyCloneableClass) obj; | |
168 | // if (fName == null) { | |
169 | // if (other.fName != null) { | |
170 | // return false; | |
171 | // } | |
172 | // } else if (!fName.equals(other.fName)) { | |
173 | // return false; | |
174 | // } | |
175 | // return true; | |
176 | // } | |
177 | // } | |
178 | // | |
179 | // public void testCloneCloneable() { | |
180 | // try { | |
181 | // MyCloneableClass myClass = new MyCloneableClass("myCloneableClass"); | |
182 | // TmfLocation<MyCloneableClass> location = new TmfLocation<MyCloneableClass>(myClass); | |
183 | // TmfLocation<MyCloneableClass> clone = location.clone(); | |
184 | // | |
185 | // assertEquals("clone", location, clone); | |
186 | // assertEquals("clone", location.getLocationData(), clone.getLocationData()); | |
187 | // assertEquals("clone", myClass, location.getLocationData()); | |
188 | // } catch (InternalError e) { | |
189 | // fail("clone a cloneable class"); | |
190 | // } | |
191 | // } | |
192 | // | |
193 | // private static class MyUnCloneableClass implements Comparable<MyUnCloneableClass> { | |
194 | // private String fName; | |
195 | // | |
196 | // public MyUnCloneableClass(String name) { | |
197 | // fName = name; | |
198 | // } | |
199 | // | |
200 | // @Override | |
201 | // public String toString() { | |
202 | // return fName; | |
203 | // } | |
204 | // | |
205 | // @Override | |
206 | // public Object clone() throws CloneNotSupportedException { | |
207 | // throw new CloneNotSupportedException(); | |
208 | // } | |
209 | // | |
210 | // @Override | |
211 | // public int compareTo(MyUnCloneableClass o) { | |
212 | // return fName.compareTo(o.fName); | |
213 | // } | |
214 | // | |
215 | // @Override | |
216 | // public int hashCode() { | |
217 | // final int prime = 31; | |
218 | // int result = 1; | |
219 | // result = prime * result + ((fName == null) ? 0 : fName.hashCode()); | |
220 | // return result; | |
221 | // } | |
222 | // | |
223 | // @Override | |
224 | // public boolean equals(Object obj) { | |
225 | // if (this == obj) { | |
226 | // return true; | |
227 | // } | |
228 | // if (obj == null) { | |
229 | // return false; | |
230 | // } | |
231 | // if (!(obj instanceof MyUnCloneableClass)) { | |
232 | // return false; | |
233 | // } | |
234 | // MyUnCloneableClass other = (MyUnCloneableClass) obj; | |
235 | // if (fName == null) { | |
236 | // if (other.fName != null) { | |
237 | // return false; | |
238 | // } | |
239 | // } else if (!fName.equals(other.fName)) { | |
240 | // return false; | |
241 | // } | |
242 | // return true; | |
243 | // } | |
244 | // } | |
245 | // | |
246 | // public void testCloneUncloneable() { | |
247 | // try { | |
248 | // MyUnCloneableClass myClass = new MyUnCloneableClass("myUncloneableClass"); | |
249 | // TmfLocation<MyUnCloneableClass> myLocation = new TmfLocation<MyUnCloneableClass>(myClass); | |
250 | // myLocation.clone(); | |
251 | // fail("clone an uncloneable class"); | |
252 | // } catch (InternalError e) { | |
253 | // } | |
254 | // } | |
d18dd09b ASL |
255 | |
256 | // ------------------------------------------------------------------------ | |
fcccd900 | 257 | // hashCode |
d18dd09b ASL |
258 | // ------------------------------------------------------------------------ |
259 | ||
54a7a54c | 260 | public void testHashCode() { |
cb8c854e FC |
261 | TmfStringLocation location1 = new TmfStringLocation((String) null); |
262 | TmfStringLocation location2 = new TmfStringLocation(aString); | |
263 | TmfLongLocation location3 = new TmfLongLocation(aLong); | |
d18dd09b | 264 | |
fcccd900 FC |
265 | assertTrue("hashCode", fLocation1.hashCode() == location1.hashCode()); |
266 | assertTrue("hashCode", fLocation2.hashCode() == location2.hashCode()); | |
267 | assertTrue("hashCode", fLocation3.hashCode() == location3.hashCode()); | |
268 | ||
269 | assertTrue("hashCode", fLocation2.hashCode() != location3.hashCode()); | |
270 | assertTrue("hashCode", fLocation3.hashCode() != location2.hashCode()); | |
271 | } | |
d18dd09b ASL |
272 | |
273 | // ------------------------------------------------------------------------ | |
274 | // toEquals | |
275 | // ------------------------------------------------------------------------ | |
276 | ||
cb8c854e | 277 | private static class TmfLocation2 extends TmfStringLocation { |
fcccd900 FC |
278 | public TmfLocation2(String location) { |
279 | super(location); | |
280 | } | |
281 | } | |
282 | ||
54a7a54c | 283 | public void testEqualsWrongTypes() { |
cb8c854e | 284 | ITmfLocation<String> location1 = new TmfStringLocation(aString); |
fcccd900 | 285 | TmfLocation2 location2 = new TmfLocation2(aString); |
54a7a54c | 286 | |
fcccd900 FC |
287 | assertFalse("equals", location1.equals(location2)); |
288 | assertFalse("equals", location2.equals(location1)); | |
289 | } | |
290 | ||
54a7a54c | 291 | public void testEqualsWithNulls() { |
cb8c854e FC |
292 | TmfStringLocation location1 = new TmfStringLocation(aString); |
293 | TmfStringLocation location2 = new TmfStringLocation((String) null); | |
54a7a54c | 294 | |
fcccd900 FC |
295 | assertFalse("equals", location1.equals(location2)); |
296 | assertFalse("equals", location2.equals(location1)); | |
297 | } | |
298 | ||
54a7a54c | 299 | public void testEqualsReflexivity() { |
fcccd900 FC |
300 | assertTrue("equals", fLocation2.equals(fLocation2)); |
301 | assertTrue("equals", fLocation3.equals(fLocation3)); | |
302 | ||
303 | assertTrue("equals", !fLocation2.equals(fLocation3)); | |
304 | assertTrue("equals", !fLocation3.equals(fLocation2)); | |
305 | } | |
306 | ||
54a7a54c | 307 | public void testEqualsSymmetry() { |
cb8c854e FC |
308 | TmfStringLocation location2 = new TmfStringLocation(aString); |
309 | TmfLongLocation location3 = new TmfLongLocation(aLong); | |
fcccd900 FC |
310 | |
311 | assertTrue("equals", location2.equals(fLocation2)); | |
312 | assertTrue("equals", fLocation2.equals(location2)); | |
313 | ||
314 | assertTrue("equals", location3.equals(fLocation3)); | |
315 | assertTrue("equals", fLocation3.equals(location3)); | |
316 | } | |
317 | ||
54a7a54c | 318 | public void testEqualsTransivity() { |
cb8c854e FC |
319 | TmfStringLocation location1 = new TmfStringLocation(aString); |
320 | TmfStringLocation location2 = new TmfStringLocation(aString); | |
321 | TmfStringLocation location3 = new TmfStringLocation(aString); | |
fcccd900 FC |
322 | |
323 | assertTrue("equals", location1.equals(location2)); | |
324 | assertTrue("equals", location2.equals(location3)); | |
325 | assertTrue("equals", location3.equals(location1)); | |
326 | } | |
327 | ||
54a7a54c | 328 | public void testEqualsNull() { |
fcccd900 FC |
329 | assertTrue("equals", !fLocation2.equals(null)); |
330 | assertTrue("equals", !fLocation2.equals(null)); | |
331 | } | |
332 | ||
d18dd09b ASL |
333 | // ------------------------------------------------------------------------ |
334 | // toString | |
335 | // ------------------------------------------------------------------------ | |
336 | ||
54a7a54c | 337 | @SuppressWarnings("hiding") |
fcccd900 FC |
338 | public void testToString() { |
339 | String aString = "some location"; | |
340 | Long aLong = 12345L; | |
341 | TmfTimestamp aTimestamp = new TmfTimestamp(); | |
d18dd09b | 342 | |
cb8c854e FC |
343 | TmfStringLocation location1 = new TmfStringLocation(aString); |
344 | TmfLongLocation location2 = new TmfLongLocation(aLong); | |
345 | TmfTimestampLocation location3 = new TmfTimestampLocation(aTimestamp); | |
d18dd09b | 346 | |
fcccd900 FC |
347 | String expected1 = "TmfLocation [fLocation=" + aString + "]"; |
348 | String expected2 = "TmfLocation [fLocation=" + aLong + "]"; | |
349 | String expected3 = "TmfLocation [fLocation=" + aTimestamp + "]"; | |
d18dd09b | 350 | |
fcccd900 FC |
351 | assertEquals("toString", expected1, location1.toString()); |
352 | assertEquals("toString", expected2, location2.toString()); | |
353 | assertEquals("toString", expected3, location3.toString()); | |
354 | } | |
d18dd09b ASL |
355 | |
356 | } |