1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012, 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
.timestamp
.TmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLongLocation
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTimestampLocation
;
28 import org
.junit
.Test
;
31 * Test suite for the TmfContext class.
33 @SuppressWarnings({"nls","javadoc"})
34 public class TmfContextTest
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 private final String aString
= "some location";
41 private final Long aLong
= 12345L;
42 private final TmfTimestamp aTimestamp
= new TmfTimestamp();
44 private final TmfStringLocation fLocation1
= new TmfStringLocation(aString
);
45 private final TmfLongLocation fLocation2
= new TmfLongLocation(aLong
);
46 private final TmfTimestampLocation fLocation3
= new TmfTimestampLocation(aTimestamp
);
48 private final long fRank1
= 1;
49 private final long fRank2
= 2;
50 private final long fRank3
= 3;
52 private final TmfContext fContext1
= new TmfContext(fLocation1
, fRank1
);
53 private final TmfContext fContext2
= new TmfContext(fLocation2
, fRank2
);
54 private final TmfContext fContext3
= new TmfContext(fLocation3
, fRank3
);
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
61 public void testTmfContextDefault() {
62 final TmfContext context
= new TmfContext();
63 assertEquals("getLocation", null, context
.getLocation());
64 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
68 public void testTmfContextNoRank() {
69 final TmfContext context1
= new TmfContext(fLocation1
);
70 final TmfContext context2
= new TmfContext(fLocation2
);
71 final TmfContext context3
= new TmfContext(fLocation3
);
73 assertEquals("getLocation", fLocation1
, context1
.getLocation());
74 assertEquals("getLocation", fLocation2
, context2
.getLocation());
75 assertEquals("getLocation", fLocation3
, context3
.getLocation());
77 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());
78 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context2
.getRank());
79 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context3
.getRank());
83 public void testTmfContext() {
84 assertEquals("getLocation", fLocation1
, fContext1
.getLocation());
85 assertEquals("getLocation", fLocation2
, fContext2
.getLocation());
86 assertEquals("getLocation", fLocation3
, fContext3
.getLocation());
88 assertEquals("getRank", fRank1
, fContext1
.getRank());
89 assertEquals("getRank", fRank2
, fContext2
.getRank());
90 assertEquals("getRank", fRank3
, fContext3
.getRank());
94 public void testTmfContextCopy() {
95 final TmfContext context1
= new TmfContext(fContext1
);
96 final TmfContext context2
= new TmfContext(fContext2
);
97 final TmfContext context3
= new TmfContext(fContext3
);
99 assertEquals("getLocation", fLocation1
, context1
.getLocation());
100 assertEquals("getLocation", fLocation2
, context2
.getLocation());
101 assertEquals("getLocation", fLocation3
, context3
.getLocation());
103 assertEquals("getRank", fRank1
, context1
.getRank());
104 assertEquals("getRank", fRank2
, context2
.getRank());
105 assertEquals("getRank", fRank3
, context3
.getRank());
109 public void testTmfContextCopy2() {
111 new TmfContext((TmfContext
) null);
112 fail("Copy constructor: no exception");
114 catch (final IllegalArgumentException e
) {
117 catch (final Exception e
) {
118 fail("Copy constructor: wrong exception");
122 // ------------------------------------------------------------------------
124 // ------------------------------------------------------------------------
127 public void testEqualsReflexivity() {
128 assertTrue("equals", fContext1
.equals(fContext1
));
129 assertTrue("equals", fContext2
.equals(fContext2
));
131 assertFalse("equals", fContext1
.equals(fContext2
));
132 assertFalse("equals", fContext2
.equals(fContext1
));
136 public void testEqualsSymmetry() {
137 final TmfContext context1
= new TmfContext(fContext1
);
138 final TmfContext context2
= new TmfContext(fContext2
);
140 assertTrue("equals", context1
.equals(fContext1
));
141 assertTrue("equals", fContext1
.equals(context1
));
143 assertTrue("equals", context2
.equals(fContext2
));
144 assertTrue("equals", fContext2
.equals(context2
));
148 public void testEqualsTransivity() {
149 final TmfContext context1
= new TmfContext(fContext1
);
150 final TmfContext context2
= new TmfContext(context1
);
151 final TmfContext context3
= new TmfContext(context2
);
153 assertTrue("equals", context1
.equals(context2
));
154 assertTrue("equals", context2
.equals(context3
));
155 assertTrue("equals", context1
.equals(context3
));
159 public void testEqualsNull() {
160 assertFalse("equals", fContext1
.equals(null));
161 assertFalse("equals", fContext2
.equals(null));
164 private static class MyContext
extends TmfContext
{
168 public void testNonEquals() {
171 final MyContext myContext
= new MyContext();
172 assertFalse("equals", fContext1
.equals(myContext
));
173 assertFalse("equals", myContext
.equals(fContext1
));
175 // Different locations
176 TmfContext context1
= new TmfContext(fContext1
);
177 TmfContext context2
= new TmfContext(fContext1
);
178 context1
.setLocation(null);
179 context2
.setLocation(null);
181 assertFalse("equals", fContext1
.equals(context1
));
182 assertFalse("equals", context1
.equals(fContext1
));
183 assertTrue("equals", context1
.equals(context2
));
186 context1
= new TmfContext(fContext1
);
187 context2
= new TmfContext(fContext1
);
188 context1
.setRank(fContext1
.getRank() + 1);
189 context2
.setRank(fContext1
.getRank() + 2);
191 assertFalse("equals", fContext1
.equals(context1
));
192 assertFalse("equals", context1
.equals(fContext1
));
193 assertFalse("equals", context1
.equals(context2
));
196 // ------------------------------------------------------------------------
198 // ------------------------------------------------------------------------
201 public void testHashCode() {
202 final TmfContext context1
= new TmfContext(fContext1
);
203 final TmfContext context2
= new TmfContext(fContext2
);
205 assertEquals("hashCode", fContext1
.hashCode(), context1
.hashCode());
206 assertEquals("hashCode", fContext2
.hashCode(), context2
.hashCode());
208 assertFalse("hashCode", fContext1
.hashCode() == context2
.hashCode());
209 assertFalse("hashCode", fContext2
.hashCode() == context1
.hashCode());
211 final TmfContext nullContext1
= new TmfContext();
212 final TmfContext nullContext2
= new TmfContext(nullContext1
);
213 assertEquals("hashCode", nullContext1
.hashCode(), nullContext2
.hashCode());
216 // ------------------------------------------------------------------------
218 // ------------------------------------------------------------------------
221 public void testToString() {
222 final String expected1
= "TmfContext [fLocation=" + fLocation1
+ ", fRank=" + 1 + "]";
223 final String expected2
= "TmfContext [fLocation=" + fLocation2
+ ", fRank=" + 2 + "]";
224 final String expected3
= "TmfContext [fLocation=" + fLocation3
+ ", fRank=" + 3 + "]";
226 assertEquals("toString", expected1
, fContext1
.toString());
227 assertEquals("toString", expected2
, fContext2
.toString());
228 assertEquals("toString", expected3
, fContext3
.toString());
231 // ------------------------------------------------------------------------
232 // setLocation, setRank, updateRank
233 // ------------------------------------------------------------------------
236 public void testSetLocation() {
237 final TmfContext context1
= new TmfContext(fContext1
);
238 context1
.setLocation(fContext2
.getLocation());
240 assertEquals("getLocation", fLocation2
, context1
.getLocation());
241 assertEquals("getRank", 1, context1
.getRank());
245 public void testSetRank() {
246 final TmfContext context1
= new TmfContext(fContext1
);
247 context1
.setRank(fContext2
.getRank());
249 assertEquals("getLocation", fLocation1
, context1
.getLocation());
250 assertEquals("getRank", fRank2
, context1
.getRank());
254 public void testIncreaseRank() {
255 final TmfContext context1
= new TmfContext(fContext1
);
257 context1
.increaseRank();
258 assertEquals("getRank", fRank1
+ 1, context1
.getRank());
259 context1
.increaseRank();
260 assertEquals("getRank", fRank1
+ 2, context1
.getRank());
262 context1
.setRank(ITmfContext
.UNKNOWN_RANK
);
263 context1
.increaseRank();
264 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());
265 context1
.increaseRank();
266 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());