1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
15 import junit
.framework
.TestCase
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
22 * Test suite for the TmfLocation class.
24 @SuppressWarnings({"nls","javadoc"})
25 public class TmfLocationTest
extends TestCase
{
27 // ------------------------------------------------------------------------
29 // ------------------------------------------------------------------------
31 String aString
= "some location";
33 TmfTimestamp aTimestamp
= new TmfTimestamp();
35 TmfLocation
<String
> fLocation1
;
36 TmfLocation
<String
> fLocation2
;
37 TmfLocation
<Long
> fLocation3
;
38 TmfLocation
<ITmfTimestamp
> fLocation4
;
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
48 public TmfLocationTest(String name
) {
53 protected void setUp() throws Exception
{
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
);
62 protected void tearDown() throws Exception
{
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 public void testTmfLocation() {
71 assertNull("TmfLocation", fLocation1
.getLocationData());
72 assertEquals("TmfLocation", aString
, fLocation2
.getLocationData());
73 assertEquals("TmfLocation", aLong
, fLocation3
.getLocationData());
74 assertEquals("TmfLocation", aTimestamp
, fLocation4
.getLocationData());
77 public void testTmfLocationCopy() {
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
);
83 assertNull("TmfLocation", location1
.getLocationData());
84 assertEquals("TmfLocation", aString
, location2
.getLocationData());
85 assertEquals("TmfLocation", aLong
, location3
.getLocationData());
86 assertEquals("TmfLocation", aTimestamp
, location4
.getLocationData());
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
93 public void testClone() {
95 TmfLocation
<String
> location1
= fLocation1
.clone();
96 TmfLocation
<String
> location2
= fLocation2
.clone();
97 TmfLocation
<Long
> location3
= fLocation3
.clone();
98 TmfLocation
<ITmfTimestamp
> location4
= fLocation4
.clone();
100 assertEquals("clone", fLocation1
, location1
);
101 assertEquals("clone", fLocation2
, location2
);
102 assertEquals("clone", fLocation3
, location3
);
103 assertEquals("clone", fLocation4
, location4
);
105 assertEquals("clone", fLocation1
.getLocationData(), location1
.getLocationData());
106 assertEquals("clone", fLocation2
.getLocationData(), location2
.getLocationData());
107 assertEquals("clone", fLocation3
.getLocationData(), location3
.getLocationData());
108 assertEquals("clone", fLocation4
.getLocationData(), location4
.getLocationData());
110 assertNull("clone", location1
.getLocationData());
111 assertEquals("clone", aString
, location2
.getLocationData());
112 assertEquals("clone", aLong
, location3
.getLocationData());
113 assertEquals("clone", aTimestamp
, location4
.getLocationData());
114 } catch (InternalError e
) {
119 public static class MyCloneableClass
implements Cloneable
, Comparable
<MyCloneableClass
> {
120 private String fName
;
122 public MyCloneableClass(String name
) {
127 public String
toString() {
132 public MyCloneableClass
clone() {
133 MyCloneableClass clone
= null;
135 clone
= (MyCloneableClass
) super.clone();
137 } catch (CloneNotSupportedException e
) {
143 public int compareTo(MyCloneableClass o
) {
144 return fName
.compareTo(o
.fName
);
148 public int hashCode() {
149 final int prime
= 31;
151 result
= prime
* result
+ ((fName
== null) ?
0 : fName
.hashCode());
156 public boolean equals(Object obj
) {
163 if (!(obj
instanceof MyCloneableClass
)) {
166 MyCloneableClass other
= (MyCloneableClass
) obj
;
168 if (other
.fName
!= null) {
171 } else if (!fName
.equals(other
.fName
)) {
178 public void testCloneCloneable() {
180 MyCloneableClass myClass
= new MyCloneableClass("myCloneableClass");
181 TmfLocation
<MyCloneableClass
> location
= new TmfLocation
<MyCloneableClass
>(myClass
);
182 TmfLocation
<MyCloneableClass
> clone
= location
.clone();
184 assertEquals("clone", location
, clone
);
185 assertEquals("clone", location
.getLocationData(), clone
.getLocationData());
186 assertEquals("clone", myClass
, location
.getLocationData());
187 } catch (InternalError e
) {
188 fail("clone a cloneable class");
192 private static class MyUnCloneableClass
implements Comparable
<MyUnCloneableClass
> {
193 private String fName
;
195 public MyUnCloneableClass(String name
) {
200 public String
toString() {
205 public Object
clone() throws CloneNotSupportedException
{
206 throw new CloneNotSupportedException();
210 public int compareTo(MyUnCloneableClass o
) {
211 return fName
.compareTo(o
.fName
);
215 public int hashCode() {
216 final int prime
= 31;
218 result
= prime
* result
+ ((fName
== null) ?
0 : fName
.hashCode());
223 public boolean equals(Object obj
) {
230 if (!(obj
instanceof MyUnCloneableClass
)) {
233 MyUnCloneableClass other
= (MyUnCloneableClass
) obj
;
235 if (other
.fName
!= null) {
238 } else if (!fName
.equals(other
.fName
)) {
245 public void testCloneUncloneable() {
247 MyUnCloneableClass myClass
= new MyUnCloneableClass("myUncloneableClass");
248 TmfLocation
<MyUnCloneableClass
> myLocation
= new TmfLocation
<MyUnCloneableClass
>(myClass
);
250 fail("clone an uncloneable class");
251 } catch (InternalError e
) {
255 // ------------------------------------------------------------------------
257 // ------------------------------------------------------------------------
259 public void testHashCode() {
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
);
264 assertTrue("hashCode", fLocation1
.hashCode() == location1
.hashCode());
265 assertTrue("hashCode", fLocation2
.hashCode() == location2
.hashCode());
266 assertTrue("hashCode", fLocation3
.hashCode() == location3
.hashCode());
268 assertTrue("hashCode", fLocation2
.hashCode() != location3
.hashCode());
269 assertTrue("hashCode", fLocation3
.hashCode() != location2
.hashCode());
272 // ------------------------------------------------------------------------
274 // ------------------------------------------------------------------------
276 private static class TmfLocation2
extends TmfLocation
<String
> {
277 public TmfLocation2(String location
) {
282 public void testEqualsWrongTypes() {
283 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
284 TmfLocation2 location2
= new TmfLocation2(aString
);
286 assertFalse("equals", location1
.equals(location2
));
287 assertFalse("equals", location2
.equals(location1
));
290 public void testEqualsWithNulls() {
291 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
292 TmfLocation
<String
> location2
= new TmfLocation
<String
>((String
) null);
294 assertFalse("equals", location1
.equals(location2
));
295 assertFalse("equals", location2
.equals(location1
));
298 public void testEqualsReflexivity() {
299 assertTrue("equals", fLocation2
.equals(fLocation2
));
300 assertTrue("equals", fLocation3
.equals(fLocation3
));
302 assertTrue("equals", !fLocation2
.equals(fLocation3
));
303 assertTrue("equals", !fLocation3
.equals(fLocation2
));
306 public void testEqualsSymmetry() {
307 TmfLocation
<String
> location2
= new TmfLocation
<String
>(aString
);
308 TmfLocation
<Long
> location3
= new TmfLocation
<Long
>(aLong
);
310 assertTrue("equals", location2
.equals(fLocation2
));
311 assertTrue("equals", fLocation2
.equals(location2
));
313 assertTrue("equals", location3
.equals(fLocation3
));
314 assertTrue("equals", fLocation3
.equals(location3
));
317 public void testEqualsTransivity() {
318 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
319 TmfLocation
<String
> location2
= new TmfLocation
<String
>(aString
);
320 TmfLocation
<String
> location3
= new TmfLocation
<String
>(aString
);
322 assertTrue("equals", location1
.equals(location2
));
323 assertTrue("equals", location2
.equals(location3
));
324 assertTrue("equals", location3
.equals(location1
));
327 public void testEqualsNull() {
328 assertTrue("equals", !fLocation2
.equals(null));
329 assertTrue("equals", !fLocation2
.equals(null));
332 // ------------------------------------------------------------------------
334 // ------------------------------------------------------------------------
336 @SuppressWarnings("hiding")
337 public void testToString() {
338 String aString
= "some location";
340 TmfTimestamp aTimestamp
= new TmfTimestamp();
342 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
343 TmfLocation
<Long
> location2
= new TmfLocation
<Long
>(aLong
);
344 TmfLocation
<ITmfTimestamp
> location3
= new TmfLocation
<ITmfTimestamp
>(aTimestamp
);
346 String expected1
= "TmfLocation [fLocation=" + aString
+ "]";
347 String expected2
= "TmfLocation [fLocation=" + aLong
+ "]";
348 String expected3
= "TmfLocation [fLocation=" + aTimestamp
+ "]";
350 assertEquals("toString", expected1
, location1
.toString());
351 assertEquals("toString", expected2
, location2
.toString());
352 assertEquals("toString", expected3
, location3
.toString());