Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfContextTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 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 * Patrick Tasse - Updated for removal of context clone
14 *******************************************************************************/
15
16 package org.eclipse.linuxtools.tmf.core.tests.trace;
17
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;
22
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;
30
31 /**
32 * Test suite for the TmfContext class.
33 */
34 @SuppressWarnings("javadoc")
35 public class TmfContextTest {
36
37 // ------------------------------------------------------------------------
38 // Variables
39 // ------------------------------------------------------------------------
40
41 private final String aString = "some location";
42 private final Long aLong = 12345L;
43 private final TmfTimestamp aTimestamp = new TmfTimestamp();
44
45 private final TmfStringLocation fLocation1 = new TmfStringLocation(aString);
46 private final TmfLongLocation fLocation2 = new TmfLongLocation(aLong);
47 private final TmfTimestampLocation fLocation3 = new TmfTimestampLocation(aTimestamp);
48
49 private final long fRank1 = 1;
50 private final long fRank2 = 2;
51 private final long fRank3 = 3;
52
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);
56
57 // ------------------------------------------------------------------------
58 // Constructors
59 // ------------------------------------------------------------------------
60
61 @Test
62 public void testTmfContextDefault() {
63 final TmfContext context = new TmfContext();
64 assertEquals("getLocation", null, context.getLocation());
65 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context.getRank());
66 }
67
68 @Test
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);
73
74 assertEquals("getLocation", fLocation1, context1.getLocation());
75 assertEquals("getLocation", fLocation2, context2.getLocation());
76 assertEquals("getLocation", fLocation3, context3.getLocation());
77
78 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context1.getRank());
79 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context2.getRank());
80 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context3.getRank());
81 }
82
83 @Test
84 public void testTmfContext() {
85 assertEquals("getLocation", fLocation1, fContext1.getLocation());
86 assertEquals("getLocation", fLocation2, fContext2.getLocation());
87 assertEquals("getLocation", fLocation3, fContext3.getLocation());
88
89 assertEquals("getRank", fRank1, fContext1.getRank());
90 assertEquals("getRank", fRank2, fContext2.getRank());
91 assertEquals("getRank", fRank3, fContext3.getRank());
92 }
93
94 @Test
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);
99
100 assertEquals("getLocation", fLocation1, context1.getLocation());
101 assertEquals("getLocation", fLocation2, context2.getLocation());
102 assertEquals("getLocation", fLocation3, context3.getLocation());
103
104 assertEquals("getRank", fRank1, context1.getRank());
105 assertEquals("getRank", fRank2, context2.getRank());
106 assertEquals("getRank", fRank3, context3.getRank());
107 }
108
109 @Test
110 public void testTmfContextCopy2() {
111 try {
112 new TmfContext((TmfContext) null);
113 fail("Copy constructor: no exception");
114 }
115 catch (final IllegalArgumentException e) {
116 // pass
117 }
118 catch (final Exception e) {
119 fail("Copy constructor: wrong exception");
120 }
121 }
122
123 // ------------------------------------------------------------------------
124 // equals
125 // ------------------------------------------------------------------------
126
127 @Test
128 public void testEqualsReflexivity() {
129 assertTrue("equals", fContext1.equals(fContext1));
130 assertTrue("equals", fContext2.equals(fContext2));
131
132 assertFalse("equals", fContext1.equals(fContext2));
133 assertFalse("equals", fContext2.equals(fContext1));
134 }
135
136 @Test
137 public void testEqualsSymmetry() {
138 final TmfContext context1 = new TmfContext(fContext1);
139 final TmfContext context2 = new TmfContext(fContext2);
140
141 assertTrue("equals", context1.equals(fContext1));
142 assertTrue("equals", fContext1.equals(context1));
143
144 assertTrue("equals", context2.equals(fContext2));
145 assertTrue("equals", fContext2.equals(context2));
146 }
147
148 @Test
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);
153
154 assertTrue("equals", context1.equals(context2));
155 assertTrue("equals", context2.equals(context3));
156 assertTrue("equals", context1.equals(context3));
157 }
158
159 @Test
160 public void testEqualsNull() {
161 assertFalse("equals", fContext1.equals(null));
162 assertFalse("equals", fContext2.equals(null));
163 }
164
165 private static class MyContext extends TmfContext {
166 }
167
168 @Test
169 public void testNonEquals() {
170
171 // Different classes
172 final MyContext myContext = new MyContext();
173 assertFalse("equals", fContext1.equals(myContext));
174 assertFalse("equals", myContext.equals(fContext1));
175
176 // Different locations
177 TmfContext context1 = new TmfContext(fContext1);
178 TmfContext context2 = new TmfContext(fContext1);
179 context1.setLocation(null);
180 context2.setLocation(null);
181
182 assertFalse("equals", fContext1.equals(context1));
183 assertFalse("equals", context1.equals(fContext1));
184 assertTrue("equals", context1.equals(context2));
185
186 // Different ranks
187 context1 = new TmfContext(fContext1);
188 context2 = new TmfContext(fContext1);
189 context1.setRank(fContext1.getRank() + 1);
190 context2.setRank(fContext1.getRank() + 2);
191
192 assertFalse("equals", fContext1.equals(context1));
193 assertFalse("equals", context1.equals(fContext1));
194 assertFalse("equals", context1.equals(context2));
195 }
196
197 // ------------------------------------------------------------------------
198 // hashCode
199 // ------------------------------------------------------------------------
200
201 @Test
202 public void testHashCode() {
203 final TmfContext context1 = new TmfContext(fContext1);
204 final TmfContext context2 = new TmfContext(fContext2);
205
206 assertEquals("hashCode", fContext1.hashCode(), context1.hashCode());
207 assertEquals("hashCode", fContext2.hashCode(), context2.hashCode());
208
209 assertFalse("hashCode", fContext1.hashCode() == context2.hashCode());
210 assertFalse("hashCode", fContext2.hashCode() == context1.hashCode());
211
212 final TmfContext nullContext1 = new TmfContext();
213 final TmfContext nullContext2 = new TmfContext(nullContext1);
214 assertEquals("hashCode", nullContext1.hashCode(), nullContext2.hashCode());
215 }
216
217 // ------------------------------------------------------------------------
218 // toString
219 // ------------------------------------------------------------------------
220
221 @Test
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 + "]";
226
227 assertEquals("toString", expected1, fContext1.toString());
228 assertEquals("toString", expected2, fContext2.toString());
229 assertEquals("toString", expected3, fContext3.toString());
230 }
231
232 // ------------------------------------------------------------------------
233 // setLocation, setRank, updateRank
234 // ------------------------------------------------------------------------
235
236 @Test
237 public void testSetLocation() {
238 final TmfContext context1 = new TmfContext(fContext1);
239 context1.setLocation(fContext2.getLocation());
240
241 assertEquals("getLocation", fLocation2, context1.getLocation());
242 assertEquals("getRank", 1, context1.getRank());
243 }
244
245 @Test
246 public void testSetRank() {
247 final TmfContext context1 = new TmfContext(fContext1);
248 context1.setRank(fContext2.getRank());
249
250 assertEquals("getLocation", fLocation1, context1.getLocation());
251 assertEquals("getRank", fRank2, context1.getRank());
252 }
253
254 @Test
255 public void testIncreaseRank() {
256 final TmfContext context1 = new TmfContext(fContext1);
257
258 context1.increaseRank();
259 assertEquals("getRank", fRank1 + 1, context1.getRank());
260 context1.increaseRank();
261 assertEquals("getRank", fRank1 + 2, context1.getRank());
262
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());
268 }
269
270 }
This page took 0.069926 seconds and 6 git commands to generate.