Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
6e1886bc | 2 | * Copyright (c) 2009, 2010, 2012, 2013 Ericsson |
9b749023 | 3 | * |
d18dd09b ASL |
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 | |
9b749023 | 8 | * |
d18dd09b ASL |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
0316808c | 11 | * Francois Chouinard - Adapted for TMF Trace Model 1.0 |
6e1886bc | 12 | * Alexandre Montplaisir - Port to JUnit4 |
d18dd09b ASL |
13 | *******************************************************************************/ |
14 | ||
6c13869b | 15 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
d18dd09b | 16 | |
6e1886bc AM |
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; | |
d18dd09b | 22 | |
6c13869b | 23 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
cbdacf03 | 24 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
6c13869b | 25 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
cb8c854e FC |
26 | import org.eclipse.linuxtools.tmf.core.trace.TmfLongLocation; |
27 | import org.eclipse.linuxtools.tmf.core.trace.TmfTimestampLocation; | |
6e1886bc | 28 | import org.junit.Test; |
d18dd09b ASL |
29 | |
30 | /** | |
d18dd09b ASL |
31 | * Test suite for the TmfContext class. |
32 | */ | |
54a7a54c | 33 | @SuppressWarnings({"nls","javadoc"}) |
6e1886bc | 34 | public class TmfContextTest { |
d18dd09b | 35 | |
cbdacf03 FC |
36 | // ------------------------------------------------------------------------ |
37 | // Variables | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
6e1886bc AM |
40 | private final String aString = "some location"; |
41 | private final Long aLong = 12345L; | |
42 | private final TmfTimestamp aTimestamp = new TmfTimestamp(); | |
d18dd09b | 43 | |
6e1886bc AM |
44 | private final TmfStringLocation fLocation1 = new TmfStringLocation(aString); |
45 | private final TmfLongLocation fLocation2 = new TmfLongLocation(aLong); | |
46 | private final TmfTimestampLocation fLocation3 = new TmfTimestampLocation(aTimestamp); | |
d18dd09b | 47 | |
6e1886bc AM |
48 | private final long fRank1 = 1; |
49 | private final long fRank2 = 2; | |
50 | private final long fRank3 = 3; | |
cbdacf03 | 51 | |
6e1886bc AM |
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); | |
d18dd09b ASL |
55 | |
56 | // ------------------------------------------------------------------------ | |
57 | // Constructors | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
6e1886bc | 60 | @Test |
cbdacf03 FC |
61 | public void testTmfContextDefault() { |
62 | final TmfContext context = new TmfContext(); | |
63 | assertEquals("getLocation", null, context.getLocation()); | |
9b749023 | 64 | assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context.getRank()); |
cbdacf03 FC |
65 | } |
66 | ||
6e1886bc | 67 | @Test |
cbdacf03 FC |
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 | ||
9b749023 AM |
77 | assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context1.getRank()); |
78 | assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context2.getRank()); | |
79 | assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context3.getRank()); | |
cbdacf03 FC |
80 | } |
81 | ||
6e1886bc | 82 | @Test |
cbdacf03 FC |
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 | ||
6e1886bc | 93 | @Test |
cbdacf03 FC |
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 | ||
6e1886bc | 108 | @Test |
5d837f9b FC |
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 | ||
cbdacf03 FC |
122 | // ------------------------------------------------------------------------ |
123 | // equals | |
124 | // ------------------------------------------------------------------------ | |
125 | ||
6e1886bc | 126 | @Test |
54a7a54c | 127 | public void testEqualsReflexivity() { |
cbdacf03 FC |
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 | ||
6e1886bc | 135 | @Test |
54a7a54c | 136 | public void testEqualsSymmetry() { |
cbdacf03 FC |
137 | final TmfContext context1 = new TmfContext(fContext1); |
138 | final TmfContext context2 = new TmfContext(fContext2); | |
d18dd09b | 139 | |
cbdacf03 FC |
140 | assertTrue("equals", context1.equals(fContext1)); |
141 | assertTrue("equals", fContext1.equals(context1)); | |
d18dd09b | 142 | |
cbdacf03 FC |
143 | assertTrue("equals", context2.equals(fContext2)); |
144 | assertTrue("equals", fContext2.equals(context2)); | |
145 | } | |
d18dd09b | 146 | |
6e1886bc | 147 | @Test |
54a7a54c | 148 | public void testEqualsTransivity() { |
cbdacf03 FC |
149 | final TmfContext context1 = new TmfContext(fContext1); |
150 | final TmfContext context2 = new TmfContext(context1); | |
151 | final TmfContext context3 = new TmfContext(context2); | |
d18dd09b | 152 | |
cbdacf03 FC |
153 | assertTrue("equals", context1.equals(context2)); |
154 | assertTrue("equals", context2.equals(context3)); | |
155 | assertTrue("equals", context1.equals(context3)); | |
156 | } | |
d18dd09b | 157 | |
6e1886bc | 158 | @Test |
54a7a54c | 159 | public void testEqualsNull() { |
cbdacf03 FC |
160 | assertFalse("equals", fContext1.equals(null)); |
161 | assertFalse("equals", fContext2.equals(null)); | |
162 | } | |
d18dd09b | 163 | |
0879b6b9 | 164 | private static class MyContext extends TmfContext { |
cbdacf03 | 165 | } |
d18dd09b | 166 | |
6e1886bc | 167 | @Test |
54a7a54c | 168 | public void testNonEquals() { |
d18dd09b | 169 | |
cbdacf03 FC |
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 | } | |
d18dd09b ASL |
195 | |
196 | // ------------------------------------------------------------------------ | |
cbdacf03 | 197 | // hashCode |
d18dd09b ASL |
198 | // ------------------------------------------------------------------------ |
199 | ||
6e1886bc | 200 | @Test |
54a7a54c | 201 | public void testHashCode() { |
cbdacf03 FC |
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 | ||
d18dd09b ASL |
216 | // ------------------------------------------------------------------------ |
217 | // toString | |
218 | // ------------------------------------------------------------------------ | |
219 | ||
6e1886bc | 220 | @Test |
cbdacf03 FC |
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 + "]"; | |
d18dd09b | 225 | |
cbdacf03 FC |
226 | assertEquals("toString", expected1, fContext1.toString()); |
227 | assertEquals("toString", expected2, fContext2.toString()); | |
228 | assertEquals("toString", expected3, fContext3.toString()); | |
229 | } | |
d18dd09b ASL |
230 | |
231 | // ------------------------------------------------------------------------ | |
232 | // clone | |
233 | // ------------------------------------------------------------------------ | |
234 | ||
6e1886bc | 235 | @Test |
cbdacf03 FC |
236 | public void testClone() { |
237 | try { | |
238 | final TmfContext context1 = fContext1.clone(); | |
239 | final TmfContext context2 = fContext2.clone(); | |
240 | final TmfContext context3 = fContext3.clone(); | |
d18dd09b | 241 | |
cbdacf03 FC |
242 | assertEquals("clone", context1, fContext1); |
243 | assertEquals("clone", context2, fContext2); | |
244 | assertEquals("clone", context3, fContext3); | |
5d837f9b FC |
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 | ||
cbdacf03 FC |
252 | } catch (final InternalError e) { |
253 | fail("clone()"); | |
254 | } | |
255 | } | |
d18dd09b ASL |
256 | |
257 | // ------------------------------------------------------------------------ | |
258 | // setLocation, setRank, updateRank | |
259 | // ------------------------------------------------------------------------ | |
260 | ||
6e1886bc | 261 | @Test |
cbdacf03 FC |
262 | public void testSetLocation() { |
263 | final TmfContext context1 = new TmfContext(fContext1); | |
264 | context1.setLocation(fContext2.getLocation()); | |
d18dd09b | 265 | |
cbdacf03 FC |
266 | assertEquals("getLocation", fLocation2, context1.getLocation()); |
267 | assertEquals("getRank", 1, context1.getRank()); | |
268 | } | |
d18dd09b | 269 | |
6e1886bc | 270 | @Test |
cbdacf03 FC |
271 | public void testSetRank() { |
272 | final TmfContext context1 = new TmfContext(fContext1); | |
273 | context1.setRank(fContext2.getRank()); | |
d18dd09b | 274 | |
cbdacf03 FC |
275 | assertEquals("getLocation", fLocation1, context1.getLocation()); |
276 | assertEquals("getRank", fRank2, context1.getRank()); | |
277 | } | |
d18dd09b | 278 | |
6e1886bc | 279 | @Test |
cbdacf03 FC |
280 | public void testIncreaseRank() { |
281 | final TmfContext context1 = new TmfContext(fContext1); | |
d18dd09b | 282 | |
cbdacf03 FC |
283 | context1.increaseRank(); |
284 | assertEquals("getRank", fRank1 + 1, context1.getRank()); | |
285 | context1.increaseRank(); | |
286 | assertEquals("getRank", fRank1 + 2, context1.getRank()); | |
d18dd09b | 287 | |
cbdacf03 FC |
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 | } | |
d18dd09b ASL |
294 | |
295 | } |