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
.ITmfContext
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
24 * <b><u>TmfContextTest</u></b>
26 * Test suite for the TmfContext class.
28 @SuppressWarnings("nls")
29 public class TmfContextTest
extends TestCase
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 final String aString
= "some location";
36 final Long aLong
= 12345L;
37 final TmfTimestamp aTimestamp
= new TmfTimestamp();
39 final TmfLocation
<String
> fLocation1
= new TmfLocation
<String
>(aString
);
40 final TmfLocation
<Long
> fLocation2
= new TmfLocation
<Long
>(aLong
);
41 final TmfLocation
<ITmfTimestamp
> fLocation3
= new TmfLocation
<ITmfTimestamp
>(aTimestamp
);
43 final long fRank1
= 1;
44 final long fRank2
= 2;
45 final long fRank3
= 3;
47 final TmfContext fContext1
= new TmfContext(fLocation1
, fRank1
);
48 final TmfContext fContext2
= new TmfContext(fLocation2
, fRank2
);
49 final TmfContext fContext3
= new TmfContext(fLocation3
, fRank3
);
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
56 * @param name the test name
58 public TmfContextTest(final String name
) {
63 protected void setUp() throws Exception
{
68 protected void tearDown() throws Exception
{
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
76 public void testTmfContextDefault() {
77 final TmfContext context
= new TmfContext();
78 assertEquals("getLocation", null, context
.getLocation());
79 assertEquals("getRank", TmfContext
.UNKNOWN_RANK
, context
.getRank());
82 public void testTmfContextNoRank() {
83 final TmfContext context1
= new TmfContext(fLocation1
);
84 final TmfContext context2
= new TmfContext(fLocation2
);
85 final TmfContext context3
= new TmfContext(fLocation3
);
87 assertEquals("getLocation", fLocation1
, context1
.getLocation());
88 assertEquals("getLocation", fLocation2
, context2
.getLocation());
89 assertEquals("getLocation", fLocation3
, context3
.getLocation());
91 assertEquals("getRank", TmfContext
.UNKNOWN_RANK
, context1
.getRank());
92 assertEquals("getRank", TmfContext
.UNKNOWN_RANK
, context2
.getRank());
93 assertEquals("getRank", TmfContext
.UNKNOWN_RANK
, context3
.getRank());
96 public void testTmfContext() {
97 assertEquals("getLocation", fLocation1
, fContext1
.getLocation());
98 assertEquals("getLocation", fLocation2
, fContext2
.getLocation());
99 assertEquals("getLocation", fLocation3
, fContext3
.getLocation());
101 assertEquals("getRank", fRank1
, fContext1
.getRank());
102 assertEquals("getRank", fRank2
, fContext2
.getRank());
103 assertEquals("getRank", fRank3
, fContext3
.getRank());
106 public void testTmfContextCopy() {
107 final TmfContext context1
= new TmfContext(fContext1
);
108 final TmfContext context2
= new TmfContext(fContext2
);
109 final TmfContext context3
= new TmfContext(fContext3
);
111 assertEquals("getLocation", fLocation1
, context1
.getLocation());
112 assertEquals("getLocation", fLocation2
, context2
.getLocation());
113 assertEquals("getLocation", fLocation3
, context3
.getLocation());
115 assertEquals("getRank", fRank1
, context1
.getRank());
116 assertEquals("getRank", fRank2
, context2
.getRank());
117 assertEquals("getRank", fRank3
, context3
.getRank());
120 public void testTmfContextCopy2() {
122 new TmfContext((TmfContext
) null);
123 fail("Copy constructor: no exception");
125 catch (final IllegalArgumentException e
) {
128 catch (final Exception e
) {
129 fail("Copy constructor: wrong exception");
133 // ------------------------------------------------------------------------
135 // ------------------------------------------------------------------------
137 public void testEqualsReflexivity() throws Exception
{
138 assertTrue("equals", fContext1
.equals(fContext1
));
139 assertTrue("equals", fContext2
.equals(fContext2
));
141 assertFalse("equals", fContext1
.equals(fContext2
));
142 assertFalse("equals", fContext2
.equals(fContext1
));
145 public void testEqualsSymmetry() throws Exception
{
146 final TmfContext context1
= new TmfContext(fContext1
);
147 final TmfContext context2
= new TmfContext(fContext2
);
149 assertTrue("equals", context1
.equals(fContext1
));
150 assertTrue("equals", fContext1
.equals(context1
));
152 assertTrue("equals", context2
.equals(fContext2
));
153 assertTrue("equals", fContext2
.equals(context2
));
156 public void testEqualsTransivity() throws Exception
{
157 final TmfContext context1
= new TmfContext(fContext1
);
158 final TmfContext context2
= new TmfContext(context1
);
159 final TmfContext context3
= new TmfContext(context2
);
161 assertTrue("equals", context1
.equals(context2
));
162 assertTrue("equals", context2
.equals(context3
));
163 assertTrue("equals", context1
.equals(context3
));
166 public void testEqualsNull() throws Exception
{
167 assertFalse("equals", fContext1
.equals(null));
168 assertFalse("equals", fContext2
.equals(null));
171 public class MyContext
extends TmfContext
{
174 public void testNonEquals() throws Exception
{
177 final MyContext myContext
= new MyContext();
178 assertFalse("equals", fContext1
.equals(myContext
));
179 assertFalse("equals", myContext
.equals(fContext1
));
181 // Different locations
182 TmfContext context1
= new TmfContext(fContext1
);
183 TmfContext context2
= new TmfContext(fContext1
);
184 context1
.setLocation(null);
185 context2
.setLocation(null);
187 assertFalse("equals", fContext1
.equals(context1
));
188 assertFalse("equals", context1
.equals(fContext1
));
189 assertTrue("equals", context1
.equals(context2
));
192 context1
= new TmfContext(fContext1
);
193 context2
= new TmfContext(fContext1
);
194 context1
.setRank(fContext1
.getRank() + 1);
195 context2
.setRank(fContext1
.getRank() + 2);
197 assertFalse("equals", fContext1
.equals(context1
));
198 assertFalse("equals", context1
.equals(fContext1
));
199 assertFalse("equals", context1
.equals(context2
));
202 // ------------------------------------------------------------------------
204 // ------------------------------------------------------------------------
206 public void testHashCode() throws Exception
{
207 final TmfContext context1
= new TmfContext(fContext1
);
208 final TmfContext context2
= new TmfContext(fContext2
);
210 assertEquals("hashCode", fContext1
.hashCode(), context1
.hashCode());
211 assertEquals("hashCode", fContext2
.hashCode(), context2
.hashCode());
213 assertFalse("hashCode", fContext1
.hashCode() == context2
.hashCode());
214 assertFalse("hashCode", fContext2
.hashCode() == context1
.hashCode());
216 final TmfContext nullContext1
= new TmfContext();
217 final TmfContext nullContext2
= new TmfContext(nullContext1
);
218 assertEquals("hashCode", nullContext1
.hashCode(), nullContext2
.hashCode());
221 // ------------------------------------------------------------------------
223 // ------------------------------------------------------------------------
225 public void testToString() {
226 final String expected1
= "TmfContext [fLocation=" + fLocation1
+ ", fRank=" + 1 + "]";
227 final String expected2
= "TmfContext [fLocation=" + fLocation2
+ ", fRank=" + 2 + "]";
228 final String expected3
= "TmfContext [fLocation=" + fLocation3
+ ", fRank=" + 3 + "]";
230 assertEquals("toString", expected1
, fContext1
.toString());
231 assertEquals("toString", expected2
, fContext2
.toString());
232 assertEquals("toString", expected3
, fContext3
.toString());
235 // ------------------------------------------------------------------------
237 // ------------------------------------------------------------------------
239 public void testClone() {
241 final TmfContext context1
= fContext1
.clone();
242 final TmfContext context2
= fContext2
.clone();
243 final TmfContext context3
= fContext3
.clone();
245 assertEquals("clone", context1
, fContext1
);
246 assertEquals("clone", context2
, fContext2
);
247 assertEquals("clone", context3
, fContext3
);
249 context1
.setLocation(null);
250 final TmfContext context4
= context1
.clone();
251 assertEquals("clone", context1
, context4
);
252 assertNull(context1
.getLocation());
253 assertNull(context4
.getLocation());
255 } catch (final InternalError e
) {
260 // ------------------------------------------------------------------------
261 // setLocation, setRank, updateRank
262 // ------------------------------------------------------------------------
264 public void testSetLocation() {
265 final TmfContext context1
= new TmfContext(fContext1
);
266 context1
.setLocation(fContext2
.getLocation());
268 assertEquals("getLocation", fLocation2
, context1
.getLocation());
269 assertEquals("getRank", 1, context1
.getRank());
272 public void testSetRank() {
273 final TmfContext context1
= new TmfContext(fContext1
);
274 context1
.setRank(fContext2
.getRank());
276 assertEquals("getLocation", fLocation1
, context1
.getLocation());
277 assertEquals("getRank", fRank2
, context1
.getRank());
280 public void testIncreaseRank() {
281 final TmfContext context1
= new TmfContext(fContext1
);
283 context1
.increaseRank();
284 assertEquals("getRank", fRank1
+ 1, context1
.getRank());
285 context1
.increaseRank();
286 assertEquals("getRank", fRank1
+ 2, context1
.getRank());
288 context1
.setRank(ITmfContext
.UNKNOWN_RANK
);
289 context1
.increaseRank();
290 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());
291 context1
.increaseRank();
292 assertEquals("getRank", ITmfContext
.UNKNOWN_RANK
, context1
.getRank());