1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Alexandre Montplaisir - Port to JUnit4
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
16 import static org
.junit
.Assert
.assertEquals
;
17 import static org
.junit
.Assert
.assertFalse
;
18 import static org
.junit
.Assert
.assertNull
;
19 import static org
.junit
.Assert
.assertTrue
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTimestampLocation
;
25 import org
.junit
.Before
;
26 import org
.junit
.Test
;
29 * Test suite for the TmfLocation class.
31 @SuppressWarnings({"nls","javadoc"})
32 public class TmfLocationTest
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private String aString
= "some location";
39 private Long aLong
= 12345L;
40 private TmfTimestamp aTimestamp
= new TmfTimestamp();
42 private TmfStringLocation fLocation1
;
43 private TmfStringLocation fLocation2
;
44 private TmfLongLocation fLocation3
;
45 private TmfTimestampLocation fLocation4
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
53 fLocation1
= new TmfStringLocation((String
) null);
54 fLocation2
= new TmfStringLocation(aString
);
55 fLocation3
= new TmfLongLocation(aLong
);
56 fLocation4
= new TmfTimestampLocation(aTimestamp
);
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
64 public void testTmfLocation() {
65 assertNull("TmfLocation", fLocation1
.getLocationInfo());
66 assertEquals("TmfLocation", aString
, fLocation2
.getLocationInfo());
67 assertEquals("TmfLocation", aLong
, fLocation3
.getLocationInfo());
68 assertEquals("TmfLocation", aTimestamp
, fLocation4
.getLocationInfo());
72 public void testTmfLocationCopy() {
73 TmfStringLocation location1
= new TmfStringLocation(fLocation1
);
74 TmfStringLocation location2
= new TmfStringLocation(fLocation2
);
75 TmfLongLocation location3
= new TmfLongLocation(fLocation3
);
76 TmfTimestampLocation location4
= new TmfTimestampLocation(fLocation4
);
78 assertNull("TmfLocation", location1
.getLocationInfo());
79 assertEquals("TmfLocation", aString
, location2
.getLocationInfo());
80 assertEquals("TmfLocation", aLong
, location3
.getLocationInfo());
81 assertEquals("TmfLocation", aTimestamp
, location4
.getLocationInfo());
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
89 public void testHashCode() {
90 TmfStringLocation location1
= new TmfStringLocation((String
) null);
91 TmfStringLocation location2
= new TmfStringLocation(aString
);
92 TmfLongLocation location3
= new TmfLongLocation(aLong
);
94 assertTrue("hashCode", fLocation1
.hashCode() == location1
.hashCode());
95 assertTrue("hashCode", fLocation2
.hashCode() == location2
.hashCode());
96 assertTrue("hashCode", fLocation3
.hashCode() == location3
.hashCode());
98 assertTrue("hashCode", fLocation2
.hashCode() != location3
.hashCode());
99 assertTrue("hashCode", fLocation3
.hashCode() != location2
.hashCode());
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
106 private static class TmfLocation2
extends TmfStringLocation
{
107 public TmfLocation2(String location
) {
113 public void testEqualsWrongTypes() {
114 ITmfLocation location1
= new TmfStringLocation(aString
);
115 TmfLocation2 location2
= new TmfLocation2(aString
);
117 assertFalse("equals", location1
.equals(location2
));
118 assertFalse("equals", location2
.equals(location1
));
122 public void testEqualsWithNulls() {
123 TmfStringLocation location1
= new TmfStringLocation(aString
);
124 TmfStringLocation location2
= new TmfStringLocation((String
) null);
126 assertFalse("equals", location1
.equals(location2
));
127 assertFalse("equals", location2
.equals(location1
));
131 public void testEqualsReflexivity() {
132 assertTrue("equals", fLocation2
.equals(fLocation2
));
133 assertTrue("equals", fLocation3
.equals(fLocation3
));
135 assertTrue("equals", !fLocation2
.equals(fLocation3
));
136 assertTrue("equals", !fLocation3
.equals(fLocation2
));
140 public void testEqualsSymmetry() {
141 TmfStringLocation location2
= new TmfStringLocation(aString
);
142 TmfLongLocation location3
= new TmfLongLocation(aLong
);
144 assertTrue("equals", location2
.equals(fLocation2
));
145 assertTrue("equals", fLocation2
.equals(location2
));
147 assertTrue("equals", location3
.equals(fLocation3
));
148 assertTrue("equals", fLocation3
.equals(location3
));
152 public void testEqualsTransivity() {
153 TmfStringLocation location1
= new TmfStringLocation(aString
);
154 TmfStringLocation location2
= new TmfStringLocation(aString
);
155 TmfStringLocation location3
= new TmfStringLocation(aString
);
157 assertTrue("equals", location1
.equals(location2
));
158 assertTrue("equals", location2
.equals(location3
));
159 assertTrue("equals", location3
.equals(location1
));
163 public void testEqualsNull() {
164 assertTrue("equals", !fLocation2
.equals(null));
165 assertTrue("equals", !fLocation2
.equals(null));
168 // ------------------------------------------------------------------------
170 // ------------------------------------------------------------------------
173 public void testToString() {
174 String str
= "some location";
176 TmfTimestamp ts
= new TmfTimestamp();
178 TmfStringLocation location1
= new TmfStringLocation(str
);
179 TmfLongLocation location2
= new TmfLongLocation(lng
);
180 TmfTimestampLocation location3
= new TmfTimestampLocation(ts
);
182 String expected1
= "TmfStringLocation [fLocationInfo=" + str
+ "]";
183 String expected2
= "TmfLongLocation [fLocationInfo=" + lng
+ "]";
184 String expected3
= "TmfTimestampLocation [fLocationInfo=" + ts
+ "]";
186 assertEquals("toString", expected1
, location1
.toString());
187 assertEquals("toString", expected2
, location2
.toString());
188 assertEquals("toString", expected3
, location3
.toString());