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 * <b><u>TmfLocationTest</u></b>
24 * Test suite for the TmfLocation class.
26 @SuppressWarnings("nls")
27 public class TmfLocationTest
extends TestCase
{
29 // ------------------------------------------------------------------------
31 // ------------------------------------------------------------------------
33 String aString
= "some location";
35 TmfTimestamp aTimestamp
= new TmfTimestamp();
37 TmfLocation
<Boolean
> fLocation0
;
38 TmfLocation
<String
> fLocation1
;
39 TmfLocation
<String
> fLocation2
;
40 TmfLocation
<Long
> fLocation3
;
41 TmfLocation
<ITmfTimestamp
> fLocation4
;
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
51 public TmfLocationTest(String name
) {
56 protected void setUp() throws Exception
{
58 fLocation0
= TmfLocation
.NULL_LOCATION
;
59 fLocation1
= new TmfLocation
<String
>((String
) null);
60 fLocation2
= new TmfLocation
<String
>(aString
);
61 fLocation3
= new TmfLocation
<Long
>(aLong
);
62 fLocation4
= new TmfLocation
<ITmfTimestamp
>(aTimestamp
);
66 protected void tearDown() throws Exception
{
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
74 public void testTmfLocation() {
75 assertNull("TmfLocation", fLocation0
.getLocation());
76 assertNull("TmfLocation", fLocation1
.getLocation());
77 assertEquals("TmfLocation", aString
, fLocation2
.getLocation());
78 assertEquals("TmfLocation", aLong
, fLocation3
.getLocation());
79 assertEquals("TmfLocation", aTimestamp
, fLocation4
.getLocation());
82 public void testTmfLocationCopy() {
83 TmfLocation
<Boolean
> location0
= new TmfLocation
<Boolean
>(fLocation0
);
84 TmfLocation
<String
> location1
= new TmfLocation
<String
>(fLocation1
);
85 TmfLocation
<String
> location2
= new TmfLocation
<String
>(fLocation2
);
86 TmfLocation
<Long
> location3
= new TmfLocation
<Long
>(fLocation3
);
87 TmfLocation
<ITmfTimestamp
> location4
= new TmfLocation
<ITmfTimestamp
>(fLocation4
);
89 assertNull("TmfLocation", location0
.getLocation());
90 assertNull("TmfLocation", location1
.getLocation());
91 assertEquals("TmfLocation", aString
, location2
.getLocation());
92 assertEquals("TmfLocation", aLong
, location3
.getLocation());
93 assertEquals("TmfLocation", aTimestamp
, location4
.getLocation());
96 // ------------------------------------------------------------------------
98 // ------------------------------------------------------------------------
100 public void testClone() {
102 TmfLocation
<String
> location1
= fLocation1
.clone();
103 TmfLocation
<String
> location2
= fLocation2
.clone();
104 TmfLocation
<Long
> location3
= fLocation3
.clone();
105 TmfLocation
<ITmfTimestamp
> location4
= fLocation4
.clone();
107 assertEquals("clone", fLocation1
, location1
);
108 assertEquals("clone", fLocation2
, location2
);
109 assertEquals("clone", fLocation3
, location3
);
110 assertEquals("clone", fLocation4
, location4
);
112 assertEquals("clone", fLocation1
.getLocation(), location1
.getLocation());
113 assertEquals("clone", fLocation2
.getLocation(), location2
.getLocation());
114 assertEquals("clone", fLocation3
.getLocation(), location3
.getLocation());
115 assertEquals("clone", fLocation4
.getLocation(), location4
.getLocation());
117 assertNull("clone", location1
.getLocation());
118 assertEquals("clone", aString
, location2
.getLocation());
119 assertEquals("clone", aLong
, location3
.getLocation());
120 assertEquals("clone", aTimestamp
, location4
.getLocation());
121 } catch (InternalError e
) {
126 public static class MyCloneableClass
implements Cloneable
, Comparable
<MyCloneableClass
> {
127 private String fName
;
129 public MyCloneableClass(String name
) {
134 public String
toString() {
139 public MyCloneableClass
clone() {
140 MyCloneableClass clone
= null;
142 clone
= (MyCloneableClass
) super.clone();
144 } catch (CloneNotSupportedException e
) {
150 public boolean equals(Object o
) {
151 return fName
.compareTo(((MyCloneableClass
) o
).fName
) == 0;
155 public int compareTo(MyCloneableClass o
) {
156 return fName
.compareTo(o
.fName
);
160 public void testCloneCloneable() {
162 MyCloneableClass myClass
= new MyCloneableClass("myCloneableClass");
163 TmfLocation
<MyCloneableClass
> location
= new TmfLocation
<MyCloneableClass
>(myClass
);
164 TmfLocation
<MyCloneableClass
> clone
= location
.clone();
166 assertEquals("clone", location
, clone
);
167 assertEquals("clone", location
.getLocation(), clone
.getLocation());
168 assertEquals("clone", myClass
, location
.getLocation());
169 } catch (InternalError e
) {
170 fail("clone a cloneable class");
174 public static class MyUnCloneableClass
implements Comparable
<MyUnCloneableClass
> {
175 private String fName
;
177 public MyUnCloneableClass(String name
) {
182 public String
toString() {
187 public Object
clone() throws CloneNotSupportedException
{
188 throw new CloneNotSupportedException();
192 public boolean equals(Object o
) {
193 return fName
.compareTo(((MyUnCloneableClass
) o
).fName
) == 0;
197 public int compareTo(MyUnCloneableClass o
) {
198 return fName
.compareTo(o
.fName
);
202 public void testCloneUncloneable() {
204 MyUnCloneableClass myClass
= new MyUnCloneableClass("myUncloneableClass");
205 TmfLocation
<MyUnCloneableClass
> myLocation
= new TmfLocation
<MyUnCloneableClass
>(myClass
);
207 fail("clone an uncloneable class");
208 } catch (InternalError e
) {
212 // ------------------------------------------------------------------------
214 // ------------------------------------------------------------------------
216 public void testHashCode() throws Exception
{
217 TmfLocation
<String
> location1
= new TmfLocation
<String
>((String
) null);
218 TmfLocation
<String
> location2
= new TmfLocation
<String
>(aString
);
219 TmfLocation
<Long
> location3
= new TmfLocation
<Long
>(aLong
);
221 assertTrue("hashCode", fLocation1
.hashCode() == location1
.hashCode());
222 assertTrue("hashCode", fLocation2
.hashCode() == location2
.hashCode());
223 assertTrue("hashCode", fLocation3
.hashCode() == location3
.hashCode());
225 assertTrue("hashCode", fLocation2
.hashCode() != location3
.hashCode());
226 assertTrue("hashCode", fLocation3
.hashCode() != location2
.hashCode());
229 // ------------------------------------------------------------------------
231 // ------------------------------------------------------------------------
233 private static class TmfLocation2
extends TmfLocation
<String
> {
234 public TmfLocation2(String location
) {
239 public void testEqualsWrongTypes() throws Exception
{
240 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
241 TmfLocation2 location2
= new TmfLocation2(aString
);
243 assertFalse("equals", location1
.equals(location2
));
244 assertFalse("equals", location2
.equals(location1
));
247 public void testEqualsWithNulls() throws Exception
{
248 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
249 TmfLocation
<String
> location2
= new TmfLocation
<String
>((String
) null);
251 assertFalse("equals", location1
.equals(location2
));
252 assertFalse("equals", location2
.equals(location1
));
255 public void testEqualsReflexivity() throws Exception
{
256 assertTrue("equals", fLocation2
.equals(fLocation2
));
257 assertTrue("equals", fLocation3
.equals(fLocation3
));
259 assertTrue("equals", !fLocation2
.equals(fLocation3
));
260 assertTrue("equals", !fLocation3
.equals(fLocation2
));
263 public void testEqualsSymmetry() throws Exception
{
264 TmfLocation
<String
> location2
= new TmfLocation
<String
>(aString
);
265 TmfLocation
<Long
> location3
= new TmfLocation
<Long
>(aLong
);
267 assertTrue("equals", location2
.equals(fLocation2
));
268 assertTrue("equals", fLocation2
.equals(location2
));
270 assertTrue("equals", location3
.equals(fLocation3
));
271 assertTrue("equals", fLocation3
.equals(location3
));
274 public void testEqualsTransivity() throws Exception
{
275 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
276 TmfLocation
<String
> location2
= new TmfLocation
<String
>(aString
);
277 TmfLocation
<String
> location3
= new TmfLocation
<String
>(aString
);
279 assertTrue("equals", location1
.equals(location2
));
280 assertTrue("equals", location2
.equals(location3
));
281 assertTrue("equals", location3
.equals(location1
));
284 public void testEqualsNull() throws Exception
{
285 assertTrue("equals", !fLocation2
.equals(null));
286 assertTrue("equals", !fLocation2
.equals(null));
289 // ------------------------------------------------------------------------
291 // ------------------------------------------------------------------------
293 public void testToString() {
294 String aString
= "some location";
296 TmfTimestamp aTimestamp
= new TmfTimestamp();
298 TmfLocation
<String
> location1
= new TmfLocation
<String
>(aString
);
299 TmfLocation
<Long
> location2
= new TmfLocation
<Long
>(aLong
);
300 TmfLocation
<ITmfTimestamp
> location3
= new TmfLocation
<ITmfTimestamp
>(aTimestamp
);
302 String expected1
= "TmfLocation [fLocation=" + aString
+ "]";
303 String expected2
= "TmfLocation [fLocation=" + aLong
+ "]";
304 String expected3
= "TmfLocation [fLocation=" + aTimestamp
+ "]";
306 assertEquals("toString", expected1
, location1
.toString());
307 assertEquals("toString", expected2
, location2
.toString());
308 assertEquals("toString", expected3
, location3
.toString());