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")
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
.getLocation());
72 assertEquals("TmfLocation", aString
, fLocation2
.getLocation());
73 assertEquals("TmfLocation", aLong
, fLocation3
.getLocation());
74 assertEquals("TmfLocation", aTimestamp
, fLocation4
.getLocation());
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
.getLocation());
84 assertEquals("TmfLocation", aString
, location2
.getLocation());
85 assertEquals("TmfLocation", aLong
, location3
.getLocation());
86 assertEquals("TmfLocation", aTimestamp
, location4
.getLocation());
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
.getLocation(), location1
.getLocation());
106 assertEquals("clone", fLocation2
.getLocation(), location2
.getLocation());
107 assertEquals("clone", fLocation3
.getLocation(), location3
.getLocation());
108 assertEquals("clone", fLocation4
.getLocation(), location4
.getLocation());
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
) {
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 boolean equals(Object o
) {
144 return fName
.compareTo(((MyCloneableClass
) o
).fName
) == 0;
148 public int compareTo(MyCloneableClass o
) {
149 return fName
.compareTo(o
.fName
);
153 public void testCloneCloneable() {
155 MyCloneableClass myClass
= new MyCloneableClass("myCloneableClass");
156 TmfLocation
<MyCloneableClass
> location
= new TmfLocation
<MyCloneableClass
>(myClass
);
157 TmfLocation
<MyCloneableClass
> clone
= location
.clone();
159 assertEquals("clone", location
, clone
);
160 assertEquals("clone", location
.getLocation(), clone
.getLocation());
161 assertEquals("clone", myClass
, location
.getLocation());
162 } catch (InternalError e
) {
163 fail("clone a cloneable class");
167 public static class MyUnCloneableClass
implements Comparable
<MyUnCloneableClass
> {
168 private String fName
;
170 public MyUnCloneableClass(String name
) {
175 public String
toString() {
180 public Object
clone() throws CloneNotSupportedException
{
181 throw new CloneNotSupportedException();
185 public boolean equals(Object o
) {
186 return fName
.compareTo(((MyUnCloneableClass
) o
).fName
) == 0;
190 public int compareTo(MyUnCloneableClass o
) {
191 return fName
.compareTo(o
.fName
);
195 public void testCloneUncloneable() {
197 MyUnCloneableClass myClass
= new MyUnCloneableClass("myUncloneableClass");
198 TmfLocation
<MyUnCloneableClass
> myLocation
= new TmfLocation
<MyUnCloneableClass
>(myClass
);
200 fail("clone an uncloneable class");
201 } catch (InternalError e
) {
205 // ------------------------------------------------------------------------
207 // ------------------------------------------------------------------------
209 public void testHashCode() throws Exception
{
210 TmfLocation
<String
> location1
= new TmfLocation
<String
>((String
) null);
211 TmfLocation
<String
> location2
= new TmfLocation
<String
>(aString
);
212 TmfLocation
<Long
> location3
= new TmfLocation
<Long
>(aLong
);
214 assertTrue("hashCode", fLocation1
.hashCode() == location1
.hashCode());
215 assertTrue("hashCode", fLocation2
.hashCode() == location2
.hashCode());
216 assertTrue("hashCode", fLocation3
.hashCode() == location3
.hashCode());
218 assertTrue("hashCode", fLocation2
.hashCode() != location3
.hashCode());
219 assertTrue("hashCode", fLocation3
.hashCode() != location2
.hashCode());
222 // ------------------------------------------------------------------------
224 // ------------------------------------------------------------------------
226 private static class TmfLocation2
extends TmfLocation
<String
> {
227 public TmfLocation2(String location
) {
232 public void testEqualsWrongTypes() throws Exception
{
233 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
234 TmfLocation2 location2
= new TmfLocation2(aString
);
236 assertFalse("equals", location1
.equals(location2
));
237 assertFalse("equals", location2
.equals(location1
));
240 public void testEqualsWithNulls() throws Exception
{
241 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
242 TmfLocation
<String
> location2
= new TmfLocation
<String
>((String
) null);
244 assertFalse("equals", location1
.equals(location2
));
245 assertFalse("equals", location2
.equals(location1
));
248 public void testEqualsReflexivity() throws Exception
{
249 assertTrue("equals", fLocation2
.equals(fLocation2
));
250 assertTrue("equals", fLocation3
.equals(fLocation3
));
252 assertTrue("equals", !fLocation2
.equals(fLocation3
));
253 assertTrue("equals", !fLocation3
.equals(fLocation2
));
256 public void testEqualsSymmetry() throws Exception
{
257 TmfLocation
<String
> location2
= new TmfLocation
<String
>(aString
);
258 TmfLocation
<Long
> location3
= new TmfLocation
<Long
>(aLong
);
260 assertTrue("equals", location2
.equals(fLocation2
));
261 assertTrue("equals", fLocation2
.equals(location2
));
263 assertTrue("equals", location3
.equals(fLocation3
));
264 assertTrue("equals", fLocation3
.equals(location3
));
267 public void testEqualsTransivity() throws Exception
{
268 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
269 TmfLocation
<String
> location2
= new TmfLocation
<String
>(aString
);
270 TmfLocation
<String
> location3
= new TmfLocation
<String
>(aString
);
272 assertTrue("equals", location1
.equals(location2
));
273 assertTrue("equals", location2
.equals(location3
));
274 assertTrue("equals", location3
.equals(location1
));
277 public void testEqualsNull() throws Exception
{
278 assertTrue("equals", !fLocation2
.equals(null));
279 assertTrue("equals", !fLocation2
.equals(null));
282 // ------------------------------------------------------------------------
284 // ------------------------------------------------------------------------
286 public void testToString() {
287 String aString
= "some location";
289 TmfTimestamp aTimestamp
= new TmfTimestamp();
291 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
292 TmfLocation
<Long
> location2
= new TmfLocation
<Long
>(aLong
);
293 TmfLocation
<ITmfTimestamp
> location3
= new TmfLocation
<ITmfTimestamp
>(aTimestamp
);
295 String expected1
= "TmfLocation [fLocation=" + aString
+ "]";
296 String expected2
= "TmfLocation [fLocation=" + aLong
+ "]";
297 String expected3
= "TmfLocation [fLocation=" + aTimestamp
+ "]";
299 assertEquals("toString", expected1
, location1
.toString());
300 assertEquals("toString", expected2
, location2
.toString());
301 assertEquals("toString", expected3
, location3
.toString());