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