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
.TmfTimestamp
;
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
;
23 * Test suite for the TmfLocation class.
25 @SuppressWarnings({"nls","javadoc"})
26 public class TmfLocationTest
extends TestCase
{
28 // ------------------------------------------------------------------------
30 // ------------------------------------------------------------------------
32 String aString
= "some location";
34 TmfTimestamp aTimestamp
= new TmfTimestamp();
36 TmfStringLocation fLocation1
;
37 TmfStringLocation fLocation2
;
38 TmfLongLocation fLocation3
;
39 TmfTimestampLocation fLocation4
;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
49 public TmfLocationTest(String name
) {
54 protected void setUp() throws Exception
{
56 fLocation1
= new TmfStringLocation((String
) null);
57 fLocation2
= new TmfStringLocation(aString
);
58 fLocation3
= new TmfLongLocation(aLong
);
59 fLocation4
= new TmfTimestampLocation(aTimestamp
);
63 protected void tearDown() throws Exception
{
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
71 public void testTmfLocation() {
72 assertNull("TmfLocation", fLocation1
.getLocationData());
73 assertEquals("TmfLocation", aString
, fLocation2
.getLocationData());
74 assertEquals("TmfLocation", aLong
, fLocation3
.getLocationData());
75 assertEquals("TmfLocation", aTimestamp
, fLocation4
.getLocationData());
78 public void testTmfLocationCopy() {
79 TmfStringLocation location1
= new TmfStringLocation(fLocation1
);
80 TmfStringLocation location2
= new TmfStringLocation(fLocation2
);
81 TmfLongLocation location3
= new TmfLongLocation(fLocation3
);
82 TmfTimestampLocation location4
= new TmfTimestampLocation(fLocation4
);
84 assertNull("TmfLocation", location1
.getLocationData());
85 assertEquals("TmfLocation", aString
, location2
.getLocationData());
86 assertEquals("TmfLocation", aLong
, location3
.getLocationData());
87 assertEquals("TmfLocation", aTimestamp
, location4
.getLocationData());
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
94 public void testClone() {
96 TmfStringLocation location1
= fLocation1
.clone();
97 TmfStringLocation location2
= fLocation2
.clone();
98 TmfLongLocation location3
= fLocation3
.clone();
99 TmfTimestampLocation location4
= fLocation4
.clone();
101 assertEquals("clone", fLocation1
, location1
);
102 assertEquals("clone", fLocation2
, location2
);
103 assertEquals("clone", fLocation3
, location3
);
104 assertEquals("clone", fLocation4
, location4
);
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());
111 assertNull("clone", location1
.getLocationData());
112 assertEquals("clone", aString
, location2
.getLocationData());
113 assertEquals("clone", aLong
, location3
.getLocationData());
114 assertEquals("clone", aTimestamp
, location4
.getLocationData());
115 } catch (InternalError e
) {
120 // public static class MyCloneableClass implements Cloneable, Comparable<MyCloneableClass> {
121 // private String fName;
123 // public MyCloneableClass(String name) {
128 // public String toString() {
133 // public MyCloneableClass clone() {
134 // MyCloneableClass clone = null;
136 // clone = (MyCloneableClass) super.clone();
137 // clone.fName = fName;
138 // } catch (CloneNotSupportedException e) {
144 // public int compareTo(MyCloneableClass o) {
145 // return fName.compareTo(o.fName);
149 // public int hashCode() {
150 // final int prime = 31;
152 // result = prime * result + ((fName == null) ? 0 : fName.hashCode());
157 // public boolean equals(Object obj) {
158 // if (this == obj) {
161 // if (obj == null) {
164 // if (!(obj instanceof MyCloneableClass)) {
167 // MyCloneableClass other = (MyCloneableClass) obj;
168 // if (fName == null) {
169 // if (other.fName != null) {
172 // } else if (!fName.equals(other.fName)) {
179 // public void testCloneCloneable() {
181 // MyCloneableClass myClass = new MyCloneableClass("myCloneableClass");
182 // TmfLocation<MyCloneableClass> location = new TmfLocation<MyCloneableClass>(myClass);
183 // TmfLocation<MyCloneableClass> clone = location.clone();
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");
193 // private static class MyUnCloneableClass implements Comparable<MyUnCloneableClass> {
194 // private String fName;
196 // public MyUnCloneableClass(String name) {
201 // public String toString() {
206 // public Object clone() throws CloneNotSupportedException {
207 // throw new CloneNotSupportedException();
211 // public int compareTo(MyUnCloneableClass o) {
212 // return fName.compareTo(o.fName);
216 // public int hashCode() {
217 // final int prime = 31;
219 // result = prime * result + ((fName == null) ? 0 : fName.hashCode());
224 // public boolean equals(Object obj) {
225 // if (this == obj) {
228 // if (obj == null) {
231 // if (!(obj instanceof MyUnCloneableClass)) {
234 // MyUnCloneableClass other = (MyUnCloneableClass) obj;
235 // if (fName == null) {
236 // if (other.fName != null) {
239 // } else if (!fName.equals(other.fName)) {
246 // public void testCloneUncloneable() {
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) {
256 // ------------------------------------------------------------------------
258 // ------------------------------------------------------------------------
260 public void testHashCode() {
261 TmfStringLocation location1
= new TmfStringLocation((String
) null);
262 TmfStringLocation location2
= new TmfStringLocation(aString
);
263 TmfLongLocation location3
= new TmfLongLocation(aLong
);
265 assertTrue("hashCode", fLocation1
.hashCode() == location1
.hashCode());
266 assertTrue("hashCode", fLocation2
.hashCode() == location2
.hashCode());
267 assertTrue("hashCode", fLocation3
.hashCode() == location3
.hashCode());
269 assertTrue("hashCode", fLocation2
.hashCode() != location3
.hashCode());
270 assertTrue("hashCode", fLocation3
.hashCode() != location2
.hashCode());
273 // ------------------------------------------------------------------------
275 // ------------------------------------------------------------------------
277 private static class TmfLocation2
extends TmfStringLocation
{
278 public TmfLocation2(String location
) {
283 public void testEqualsWrongTypes() {
284 ITmfLocation
<String
> location1
= new TmfStringLocation(aString
);
285 TmfLocation2 location2
= new TmfLocation2(aString
);
287 assertFalse("equals", location1
.equals(location2
));
288 assertFalse("equals", location2
.equals(location1
));
291 public void testEqualsWithNulls() {
292 TmfStringLocation location1
= new TmfStringLocation(aString
);
293 TmfStringLocation location2
= new TmfStringLocation((String
) null);
295 assertFalse("equals", location1
.equals(location2
));
296 assertFalse("equals", location2
.equals(location1
));
299 public void testEqualsReflexivity() {
300 assertTrue("equals", fLocation2
.equals(fLocation2
));
301 assertTrue("equals", fLocation3
.equals(fLocation3
));
303 assertTrue("equals", !fLocation2
.equals(fLocation3
));
304 assertTrue("equals", !fLocation3
.equals(fLocation2
));
307 public void testEqualsSymmetry() {
308 TmfStringLocation location2
= new TmfStringLocation(aString
);
309 TmfLongLocation location3
= new TmfLongLocation(aLong
);
311 assertTrue("equals", location2
.equals(fLocation2
));
312 assertTrue("equals", fLocation2
.equals(location2
));
314 assertTrue("equals", location3
.equals(fLocation3
));
315 assertTrue("equals", fLocation3
.equals(location3
));
318 public void testEqualsTransivity() {
319 TmfStringLocation location1
= new TmfStringLocation(aString
);
320 TmfStringLocation location2
= new TmfStringLocation(aString
);
321 TmfStringLocation location3
= new TmfStringLocation(aString
);
323 assertTrue("equals", location1
.equals(location2
));
324 assertTrue("equals", location2
.equals(location3
));
325 assertTrue("equals", location3
.equals(location1
));
328 public void testEqualsNull() {
329 assertTrue("equals", !fLocation2
.equals(null));
330 assertTrue("equals", !fLocation2
.equals(null));
333 // ------------------------------------------------------------------------
335 // ------------------------------------------------------------------------
337 @SuppressWarnings("hiding")
338 public void testToString() {
339 String aString
= "some location";
341 TmfTimestamp aTimestamp
= new TmfTimestamp();
343 TmfStringLocation location1
= new TmfStringLocation(aString
);
344 TmfLongLocation location2
= new TmfLongLocation(aLong
);
345 TmfTimestampLocation location3
= new TmfTimestampLocation(aTimestamp
);
347 String expected1
= "TmfLocation [fLocation=" + aString
+ "]";
348 String expected2
= "TmfLocation [fLocation=" + aLong
+ "]";
349 String expected3
= "TmfLocation [fLocation=" + aTimestamp
+ "]";
351 assertEquals("toString", expected1
, location1
.toString());
352 assertEquals("toString", expected2
, location2
.toString());
353 assertEquals("toString", expected3
, location3
.toString());