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 * Francois Chouinard - Adapted for TMF Trace Model 1.0
12 * Alexandre Montplaisir - Port to JUnit4
13 * Patrick Tasse - Updated for removal of context clone
14 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
;
18 import static org
.junit
.Assert
.assertEquals
;
19 import static org
.junit
.Assert
.assertFalse
;
20 import static org
.junit
.Assert
.assertTrue
;
21 import static org
.junit
.Assert
.fail
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.tests
.trace
.location
.TmfStringLocation
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfLongLocation
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.location
.TmfTimestampLocation
;
29 import org
.junit
.Test
;
32 * Test suite for the TmfContext class.
34 @SuppressWarnings("javadoc")
35 public class TmfContextTest
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private final String aString
= "some location";
42 private final Long aLong
= 12345L;
43 private final TmfTimestamp aTimestamp
= new TmfTimestamp();
45 private final TmfStringLocation fLocation1
= new TmfStringLocation(aString
);
46 private final TmfLongLocation fLocation2
= new TmfLongLocation(aLong
);
47 private final TmfTimestampLocation fLocation3
= new TmfTimestampLocation(aTimestamp
);
49 private final long fRank1
= 1;
50 private final long fRank2
= 2;
51 private final long fRank3
= 3;
53 private final TmfContext fContext1
= new TmfContext(fLocation1
, fRank1
);
54 private final TmfContext fContext2
= new TmfContext(fLocation2
, fRank2
);
55 private final TmfContext fContext3
= new TmfContext(fLocation3
, fRank3
);
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 public void testTmfContextDefault() {
63 final TmfContext context
= new TmfContext();
64 assertEquals("getLocation", null, context
.getLocation());
65 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
69 public void testTmfContextNoRank() {
70 final TmfContext context1
= new TmfContext(fLocation1
);
71 final TmfContext context2
= new TmfContext(fLocation2
);
72 final TmfContext context3
= new TmfContext(fLocation3
);
74 assertEquals("getLocation", fLocation1
, context1
.getLocation());
75 assertEquals("getLocation", fLocation2
, context2
.getLocation());
76 assertEquals("getLocation", fLocation3
, context3
.getLocation());
78 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());
79 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context2
.getRank());
80 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context3
.getRank());
84 public void testTmfContext() {
85 assertEquals("getLocation", fLocation1
, fContext1
.getLocation());
86 assertEquals("getLocation", fLocation2
, fContext2
.getLocation());
87 assertEquals("getLocation", fLocation3
, fContext3
.getLocation());
89 assertEquals("getRank", fRank1
, fContext1
.getRank());
90 assertEquals("getRank", fRank2
, fContext2
.getRank());
91 assertEquals("getRank", fRank3
, fContext3
.getRank());
95 public void testTmfContextCopy() {
96 final TmfContext context1
= new TmfContext(fContext1
);
97 final TmfContext context2
= new TmfContext(fContext2
);
98 final TmfContext context3
= new TmfContext(fContext3
);
100 assertEquals("getLocation", fLocation1
, context1
.getLocation());
101 assertEquals("getLocation", fLocation2
, context2
.getLocation());
102 assertEquals("getLocation", fLocation3
, context3
.getLocation());
104 assertEquals("getRank", fRank1
, context1
.getRank());
105 assertEquals("getRank", fRank2
, context2
.getRank());
106 assertEquals("getRank", fRank3
, context3
.getRank());
110 public void testTmfContextCopy2() {
112 new TmfContext((TmfContext
) null);
113 fail("Copy constructor: no exception");
115 catch (final IllegalArgumentException e
) {
118 catch (final Exception e
) {
119 fail("Copy constructor: wrong exception");
123 // ------------------------------------------------------------------------
125 // ------------------------------------------------------------------------
128 public void testEqualsReflexivity() {
129 assertTrue("equals", fContext1
.equals(fContext1
));
130 assertTrue("equals", fContext2
.equals(fContext2
));
132 assertFalse("equals", fContext1
.equals(fContext2
));
133 assertFalse("equals", fContext2
.equals(fContext1
));
137 public void testEqualsSymmetry() {
138 final TmfContext context1
= new TmfContext(fContext1
);
139 final TmfContext context2
= new TmfContext(fContext2
);
141 assertTrue("equals", context1
.equals(fContext1
));
142 assertTrue("equals", fContext1
.equals(context1
));
144 assertTrue("equals", context2
.equals(fContext2
));
145 assertTrue("equals", fContext2
.equals(context2
));
149 public void testEqualsTransivity() {
150 final TmfContext context1
= new TmfContext(fContext1
);
151 final TmfContext context2
= new TmfContext(context1
);
152 final TmfContext context3
= new TmfContext(context2
);
154 assertTrue("equals", context1
.equals(context2
));
155 assertTrue("equals", context2
.equals(context3
));
156 assertTrue("equals", context1
.equals(context3
));
160 public void testEqualsNull() {
161 assertFalse("equals", fContext1
.equals(null));
162 assertFalse("equals", fContext2
.equals(null));
165 private static class MyContext
extends TmfContext
{
169 public void testNonEquals() {
172 final MyContext myContext
= new MyContext();
173 assertFalse("equals", fContext1
.equals(myContext
));
174 assertFalse("equals", myContext
.equals(fContext1
));
176 // Different locations
177 TmfContext context1
= new TmfContext(fContext1
);
178 TmfContext context2
= new TmfContext(fContext1
);
179 context1
.setLocation(null);
180 context2
.setLocation(null);
182 assertFalse("equals", fContext1
.equals(context1
));
183 assertFalse("equals", context1
.equals(fContext1
));
184 assertTrue("equals", context1
.equals(context2
));
187 context1
= new TmfContext(fContext1
);
188 context2
= new TmfContext(fContext1
);
189 context1
.setRank(fContext1
.getRank() + 1);
190 context2
.setRank(fContext1
.getRank() + 2);
192 assertFalse("equals", fContext1
.equals(context1
));
193 assertFalse("equals", context1
.equals(fContext1
));
194 assertFalse("equals", context1
.equals(context2
));
197 // ------------------------------------------------------------------------
199 // ------------------------------------------------------------------------
202 public void testHashCode() {
203 final TmfContext context1
= new TmfContext(fContext1
);
204 final TmfContext context2
= new TmfContext(fContext2
);
206 assertEquals("hashCode", fContext1
.hashCode(), context1
.hashCode());
207 assertEquals("hashCode", fContext2
.hashCode(), context2
.hashCode());
209 assertFalse("hashCode", fContext1
.hashCode() == context2
.hashCode());
210 assertFalse("hashCode", fContext2
.hashCode() == context1
.hashCode());
212 final TmfContext nullContext1
= new TmfContext();
213 final TmfContext nullContext2
= new TmfContext(nullContext1
);
214 assertEquals("hashCode", nullContext1
.hashCode(), nullContext2
.hashCode());
217 // ------------------------------------------------------------------------
219 // ------------------------------------------------------------------------
222 public void testToString() {
223 final String expected1
= "TmfContext [fLocation=" + fLocation1
+ ", fRank=" + 1 + "]";
224 final String expected2
= "TmfContext [fLocation=" + fLocation2
+ ", fRank=" + 2 + "]";
225 final String expected3
= "TmfContext [fLocation=" + fLocation3
+ ", fRank=" + 3 + "]";
227 assertEquals("toString", expected1
, fContext1
.toString());
228 assertEquals("toString", expected2
, fContext2
.toString());
229 assertEquals("toString", expected3
, fContext3
.toString());
232 // ------------------------------------------------------------------------
233 // setLocation, setRank, updateRank
234 // ------------------------------------------------------------------------
237 public void testSetLocation() {
238 final TmfContext context1
= new TmfContext(fContext1
);
239 context1
.setLocation(fContext2
.getLocation());
241 assertEquals("getLocation", fLocation2
, context1
.getLocation());
242 assertEquals("getRank", 1, context1
.getRank());
246 public void testSetRank() {
247 final TmfContext context1
= new TmfContext(fContext1
);
248 context1
.setRank(fContext2
.getRank());
250 assertEquals("getLocation", fLocation1
, context1
.getLocation());
251 assertEquals("getRank", fRank2
, context1
.getRank());
255 public void testIncreaseRank() {
256 final TmfContext context1
= new TmfContext(fContext1
);
258 context1
.increaseRank();
259 assertEquals("getRank", fRank1
+ 1, context1
.getRank());
260 context1
.increaseRank();
261 assertEquals("getRank", fRank1
+ 2, context1
.getRank());
263 context1
.setRank(ITmfContext
.UNKNOWN_RANK
);
264 context1
.increaseRank();
265 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());
266 context1
.increaseRank();
267 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());