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
.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("javadoc")
34 public class TmfContextTest
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 private final Long aLong
= 12345L;
41 private final TmfTimestamp aTimestamp
= new TmfTimestamp();
43 private final TmfLongLocation fLocation1
= new TmfLongLocation(aLong
);
44 private final TmfTimestampLocation fLocation2
= new TmfTimestampLocation(aTimestamp
);
46 private final long fRank1
= 1;
47 private final long fRank2
= 2;
49 private final TmfContext fContext1
= new TmfContext(fLocation1
, fRank1
);
50 private final TmfContext fContext2
= new TmfContext(fLocation2
, fRank2
);
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 public void testTmfContextDefault() {
58 final TmfContext context
= new TmfContext();
59 assertEquals("getLocation", null, context
.getLocation());
60 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context
.getRank());
64 public void testTmfContextNoRank() {
65 final TmfContext context1
= new TmfContext(fLocation1
);
66 final TmfContext context2
= new TmfContext(fLocation2
);
68 assertEquals("getLocation", fLocation1
, context1
.getLocation());
69 assertEquals("getLocation", fLocation2
, context2
.getLocation());
71 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());
72 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context2
.getRank());
76 public void testTmfContext() {
77 assertEquals("getLocation", fLocation1
, fContext1
.getLocation());
78 assertEquals("getLocation", fLocation2
, fContext2
.getLocation());
80 assertEquals("getRank", fRank1
, fContext1
.getRank());
81 assertEquals("getRank", fRank2
, fContext2
.getRank());
85 public void testTmfContextCopy() {
86 final TmfContext context1
= new TmfContext(fContext1
);
87 final TmfContext context2
= new TmfContext(fContext2
);
89 assertEquals("getLocation", fLocation1
, context1
.getLocation());
90 assertEquals("getLocation", fLocation2
, context2
.getLocation());
92 assertEquals("getRank", fRank1
, context1
.getRank());
93 assertEquals("getRank", fRank2
, context2
.getRank());
97 public void testTmfContextCopy2() {
99 new TmfContext((TmfContext
) null);
100 fail("Copy constructor: no exception");
102 catch (final IllegalArgumentException e
) {
105 catch (final Exception e
) {
106 fail("Copy constructor: wrong exception");
110 // ------------------------------------------------------------------------
112 // ------------------------------------------------------------------------
115 public void testEqualsReflexivity() {
116 assertTrue("equals", fContext1
.equals(fContext1
));
117 assertTrue("equals", fContext2
.equals(fContext2
));
119 assertFalse("equals", fContext1
.equals(fContext2
));
120 assertFalse("equals", fContext2
.equals(fContext1
));
124 public void testEqualsSymmetry() {
125 final TmfContext context1
= new TmfContext(fContext1
);
126 final TmfContext context2
= new TmfContext(fContext2
);
128 assertTrue("equals", context1
.equals(fContext1
));
129 assertTrue("equals", fContext1
.equals(context1
));
131 assertTrue("equals", context2
.equals(fContext2
));
132 assertTrue("equals", fContext2
.equals(context2
));
136 public void testEqualsTransivity() {
137 final TmfContext context1
= new TmfContext(fContext1
);
138 final TmfContext context2
= new TmfContext(context1
);
139 final TmfContext context3
= new TmfContext(context2
);
141 assertTrue("equals", context1
.equals(context2
));
142 assertTrue("equals", context2
.equals(context3
));
143 assertTrue("equals", context1
.equals(context3
));
147 public void testEqualsNull() {
148 assertFalse("equals", fContext1
.equals(null));
149 assertFalse("equals", fContext2
.equals(null));
152 private static class MyContext
extends TmfContext
{
156 public void testNonEquals() {
159 final MyContext myContext
= new MyContext();
160 assertFalse("equals", fContext1
.equals(myContext
));
161 assertFalse("equals", myContext
.equals(fContext1
));
163 // Different locations
164 TmfContext context1
= new TmfContext(fContext1
);
165 TmfContext context2
= new TmfContext(fContext1
);
166 context1
.setLocation(null);
167 context2
.setLocation(null);
169 assertFalse("equals", fContext1
.equals(context1
));
170 assertFalse("equals", context1
.equals(fContext1
));
171 assertTrue("equals", context1
.equals(context2
));
174 context1
= new TmfContext(fContext1
);
175 context2
= new TmfContext(fContext1
);
176 context1
.setRank(fContext1
.getRank() + 1);
177 context2
.setRank(fContext1
.getRank() + 2);
179 assertFalse("equals", fContext1
.equals(context1
));
180 assertFalse("equals", context1
.equals(fContext1
));
181 assertFalse("equals", context1
.equals(context2
));
184 // ------------------------------------------------------------------------
186 // ------------------------------------------------------------------------
189 public void testHashCode() {
190 final TmfContext context1
= new TmfContext(fContext1
);
191 final TmfContext context2
= new TmfContext(fContext2
);
193 assertEquals("hashCode", fContext1
.hashCode(), context1
.hashCode());
194 assertEquals("hashCode", fContext2
.hashCode(), context2
.hashCode());
196 assertFalse("hashCode", fContext1
.hashCode() == context2
.hashCode());
197 assertFalse("hashCode", fContext2
.hashCode() == context1
.hashCode());
199 final TmfContext nullContext1
= new TmfContext();
200 final TmfContext nullContext2
= new TmfContext(nullContext1
);
201 assertEquals("hashCode", nullContext1
.hashCode(), nullContext2
.hashCode());
204 // ------------------------------------------------------------------------
206 // ------------------------------------------------------------------------
209 public void testToString() {
210 final String expected1
= "TmfContext [fLocation=" + fLocation1
+ ", fRank=" + fRank1
+ "]";
211 final String expected2
= "TmfContext [fLocation=" + fLocation2
+ ", fRank=" + fRank2
+ "]";
213 assertEquals("toString", expected1
, fContext1
.toString());
214 assertEquals("toString", expected2
, fContext2
.toString());
217 // ------------------------------------------------------------------------
218 // setLocation, setRank, updateRank
219 // ------------------------------------------------------------------------
222 public void testSetLocation() {
223 final TmfContext context1
= new TmfContext(fContext1
);
224 context1
.setLocation(fContext2
.getLocation());
226 assertEquals("getLocation", fLocation2
, context1
.getLocation());
227 assertEquals("getRank", fRank1
, context1
.getRank());
231 public void testSetRank() {
232 final TmfContext context1
= new TmfContext(fContext1
);
233 context1
.setRank(fContext2
.getRank());
235 assertEquals("getLocation", fLocation1
, context1
.getLocation());
236 assertEquals("getRank", fRank2
, context1
.getRank());
240 public void testIncreaseRank() {
241 final TmfContext context1
= new TmfContext(fContext1
);
243 context1
.increaseRank();
244 assertEquals("getRank", fRank1
+ 1, context1
.getRank());
245 context1
.increaseRank();
246 assertEquals("getRank", fRank1
+ 2, context1
.getRank());
248 context1
.setRank(ITmfContext
.UNKNOWN_RANK
);
249 context1
.increaseRank();
250 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());
251 context1
.increaseRank();
252 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());