2358768c924879cab01cf4904ccdfeef4413347a
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfContextTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012, 2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adapted for TMF Trace Model 1.0
12 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.core.tests.trace;
16
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertFalse;
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22
23 import org.eclipse.linuxtools.tmf.core.event.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;
29
30 /**
31 * Test suite for the TmfContext class.
32 */
33 @SuppressWarnings({"nls","javadoc"})
34 public class TmfContextTest {
35
36 // ------------------------------------------------------------------------
37 // Variables
38 // ------------------------------------------------------------------------
39
40 private final String aString = "some location";
41 private final Long aLong = 12345L;
42 private final TmfTimestamp aTimestamp = new TmfTimestamp();
43
44 private final TmfStringLocation fLocation1 = new TmfStringLocation(aString);
45 private final TmfLongLocation fLocation2 = new TmfLongLocation(aLong);
46 private final TmfTimestampLocation fLocation3 = new TmfTimestampLocation(aTimestamp);
47
48 private final long fRank1 = 1;
49 private final long fRank2 = 2;
50 private final long fRank3 = 3;
51
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);
55
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59
60 @Test
61 public void testTmfContextDefault() {
62 final TmfContext context = new TmfContext();
63 assertEquals("getLocation", null, context.getLocation());
64 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context.getRank());
65 }
66
67 @Test
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);
72
73 assertEquals("getLocation", fLocation1, context1.getLocation());
74 assertEquals("getLocation", fLocation2, context2.getLocation());
75 assertEquals("getLocation", fLocation3, context3.getLocation());
76
77 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context1.getRank());
78 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context2.getRank());
79 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context3.getRank());
80 }
81
82 @Test
83 public void testTmfContext() {
84 assertEquals("getLocation", fLocation1, fContext1.getLocation());
85 assertEquals("getLocation", fLocation2, fContext2.getLocation());
86 assertEquals("getLocation", fLocation3, fContext3.getLocation());
87
88 assertEquals("getRank", fRank1, fContext1.getRank());
89 assertEquals("getRank", fRank2, fContext2.getRank());
90 assertEquals("getRank", fRank3, fContext3.getRank());
91 }
92
93 @Test
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);
98
99 assertEquals("getLocation", fLocation1, context1.getLocation());
100 assertEquals("getLocation", fLocation2, context2.getLocation());
101 assertEquals("getLocation", fLocation3, context3.getLocation());
102
103 assertEquals("getRank", fRank1, context1.getRank());
104 assertEquals("getRank", fRank2, context2.getRank());
105 assertEquals("getRank", fRank3, context3.getRank());
106 }
107
108 @Test
109 public void testTmfContextCopy2() {
110 try {
111 new TmfContext((TmfContext) null);
112 fail("Copy constructor: no exception");
113 }
114 catch (final IllegalArgumentException e) {
115 // pass
116 }
117 catch (final Exception e) {
118 fail("Copy constructor: wrong exception");
119 }
120 }
121
122 // ------------------------------------------------------------------------
123 // equals
124 // ------------------------------------------------------------------------
125
126 @Test
127 public void testEqualsReflexivity() {
128 assertTrue("equals", fContext1.equals(fContext1));
129 assertTrue("equals", fContext2.equals(fContext2));
130
131 assertFalse("equals", fContext1.equals(fContext2));
132 assertFalse("equals", fContext2.equals(fContext1));
133 }
134
135 @Test
136 public void testEqualsSymmetry() {
137 final TmfContext context1 = new TmfContext(fContext1);
138 final TmfContext context2 = new TmfContext(fContext2);
139
140 assertTrue("equals", context1.equals(fContext1));
141 assertTrue("equals", fContext1.equals(context1));
142
143 assertTrue("equals", context2.equals(fContext2));
144 assertTrue("equals", fContext2.equals(context2));
145 }
146
147 @Test
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);
152
153 assertTrue("equals", context1.equals(context2));
154 assertTrue("equals", context2.equals(context3));
155 assertTrue("equals", context1.equals(context3));
156 }
157
158 @Test
159 public void testEqualsNull() {
160 assertFalse("equals", fContext1.equals(null));
161 assertFalse("equals", fContext2.equals(null));
162 }
163
164 private static class MyContext extends TmfContext {
165 }
166
167 @Test
168 public void testNonEquals() {
169
170 // Different classes
171 final MyContext myContext = new MyContext();
172 assertFalse("equals", fContext1.equals(myContext));
173 assertFalse("equals", myContext.equals(fContext1));
174
175 // Different locations
176 TmfContext context1 = new TmfContext(fContext1);
177 TmfContext context2 = new TmfContext(fContext1);
178 context1.setLocation(null);
179 context2.setLocation(null);
180
181 assertFalse("equals", fContext1.equals(context1));
182 assertFalse("equals", context1.equals(fContext1));
183 assertTrue("equals", context1.equals(context2));
184
185 // Different ranks
186 context1 = new TmfContext(fContext1);
187 context2 = new TmfContext(fContext1);
188 context1.setRank(fContext1.getRank() + 1);
189 context2.setRank(fContext1.getRank() + 2);
190
191 assertFalse("equals", fContext1.equals(context1));
192 assertFalse("equals", context1.equals(fContext1));
193 assertFalse("equals", context1.equals(context2));
194 }
195
196 // ------------------------------------------------------------------------
197 // hashCode
198 // ------------------------------------------------------------------------
199
200 @Test
201 public void testHashCode() {
202 final TmfContext context1 = new TmfContext(fContext1);
203 final TmfContext context2 = new TmfContext(fContext2);
204
205 assertEquals("hashCode", fContext1.hashCode(), context1.hashCode());
206 assertEquals("hashCode", fContext2.hashCode(), context2.hashCode());
207
208 assertFalse("hashCode", fContext1.hashCode() == context2.hashCode());
209 assertFalse("hashCode", fContext2.hashCode() == context1.hashCode());
210
211 final TmfContext nullContext1 = new TmfContext();
212 final TmfContext nullContext2 = new TmfContext(nullContext1);
213 assertEquals("hashCode", nullContext1.hashCode(), nullContext2.hashCode());
214 }
215
216 // ------------------------------------------------------------------------
217 // toString
218 // ------------------------------------------------------------------------
219
220 @Test
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 + "]";
225
226 assertEquals("toString", expected1, fContext1.toString());
227 assertEquals("toString", expected2, fContext2.toString());
228 assertEquals("toString", expected3, fContext3.toString());
229 }
230
231 // ------------------------------------------------------------------------
232 // clone
233 // ------------------------------------------------------------------------
234
235 @Test
236 public void testClone() {
237 try {
238 final TmfContext context1 = fContext1.clone();
239 final TmfContext context2 = fContext2.clone();
240 final TmfContext context3 = fContext3.clone();
241
242 assertEquals("clone", context1, fContext1);
243 assertEquals("clone", context2, fContext2);
244 assertEquals("clone", context3, fContext3);
245
246 context1.setLocation(null);
247 final TmfContext context4 = context1.clone();
248 assertEquals("clone", context1, context4);
249 assertNull(context1.getLocation());
250 assertNull(context4.getLocation());
251
252 } catch (final InternalError e) {
253 fail("clone()");
254 }
255 }
256
257 // ------------------------------------------------------------------------
258 // setLocation, setRank, updateRank
259 // ------------------------------------------------------------------------
260
261 @Test
262 public void testSetLocation() {
263 final TmfContext context1 = new TmfContext(fContext1);
264 context1.setLocation(fContext2.getLocation());
265
266 assertEquals("getLocation", fLocation2, context1.getLocation());
267 assertEquals("getRank", 1, context1.getRank());
268 }
269
270 @Test
271 public void testSetRank() {
272 final TmfContext context1 = new TmfContext(fContext1);
273 context1.setRank(fContext2.getRank());
274
275 assertEquals("getLocation", fLocation1, context1.getLocation());
276 assertEquals("getRank", fRank2, context1.getRank());
277 }
278
279 @Test
280 public void testIncreaseRank() {
281 final TmfContext context1 = new TmfContext(fContext1);
282
283 context1.increaseRank();
284 assertEquals("getRank", fRank1 + 1, context1.getRank());
285 context1.increaseRank();
286 assertEquals("getRank", fRank1 + 2, context1.getRank());
287
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());
293 }
294
295 }
This page took 0.0376 seconds and 5 git commands to generate.