Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
cbdacf03 | 2 | * Copyright (c) 2009, 2010, 2012 Ericsson |
d18dd09b ASL |
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 | |
0316808c | 11 | * Francois Chouinard - Adapted for TMF Trace Model 1.0 |
d18dd09b ASL |
12 | *******************************************************************************/ |
13 | ||
6c13869b | 14 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
d18dd09b ASL |
15 | |
16 | import junit.framework.TestCase; | |
17 | ||
fcccd900 | 18 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b | 19 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
cbdacf03 | 20 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
6c13869b FC |
21 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
22 | import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; | |
d18dd09b ASL |
23 | |
24 | /** | |
d18dd09b ASL |
25 | * Test suite for the TmfContext class. |
26 | */ | |
3b38ea61 | 27 | @SuppressWarnings("nls") |
d18dd09b ASL |
28 | public class TmfContextTest extends TestCase { |
29 | ||
cbdacf03 FC |
30 | // ------------------------------------------------------------------------ |
31 | // Variables | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | final String aString = "some location"; | |
35 | final Long aLong = 12345L; | |
36 | final TmfTimestamp aTimestamp = new TmfTimestamp(); | |
d18dd09b | 37 | |
cbdacf03 FC |
38 | final TmfLocation<String> fLocation1 = new TmfLocation<String>(aString); |
39 | final TmfLocation<Long> fLocation2 = new TmfLocation<Long>(aLong); | |
40 | final TmfLocation<ITmfTimestamp> fLocation3 = new TmfLocation<ITmfTimestamp>(aTimestamp); | |
d18dd09b | 41 | |
cbdacf03 FC |
42 | final long fRank1 = 1; |
43 | final long fRank2 = 2; | |
44 | final long fRank3 = 3; | |
45 | ||
46 | final TmfContext fContext1 = new TmfContext(fLocation1, fRank1); | |
47 | final TmfContext fContext2 = new TmfContext(fLocation2, fRank2); | |
48 | final TmfContext fContext3 = new TmfContext(fLocation3, fRank3); | |
d18dd09b | 49 | |
d18dd09b ASL |
50 | // ------------------------------------------------------------------------ |
51 | // Housekeeping | |
52 | // ------------------------------------------------------------------------ | |
53 | ||
cbdacf03 FC |
54 | /** |
55 | * @param name the test name | |
56 | */ | |
57 | public TmfContextTest(final String name) { | |
58 | super(name); | |
59 | } | |
d18dd09b | 60 | |
cbdacf03 FC |
61 | @Override |
62 | protected void setUp() throws Exception { | |
63 | super.setUp(); | |
64 | } | |
d18dd09b | 65 | |
cbdacf03 FC |
66 | @Override |
67 | protected void tearDown() throws Exception { | |
68 | super.tearDown(); | |
69 | } | |
d18dd09b ASL |
70 | |
71 | // ------------------------------------------------------------------------ | |
72 | // Constructors | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
cbdacf03 FC |
75 | public void testTmfContextDefault() { |
76 | final TmfContext context = new TmfContext(); | |
77 | assertEquals("getLocation", null, context.getLocation()); | |
78 | assertEquals("getRank", TmfContext.UNKNOWN_RANK, context.getRank()); | |
79 | } | |
80 | ||
81 | public void testTmfContextNoRank() { | |
82 | final TmfContext context1 = new TmfContext(fLocation1); | |
83 | final TmfContext context2 = new TmfContext(fLocation2); | |
84 | final TmfContext context3 = new TmfContext(fLocation3); | |
85 | ||
86 | assertEquals("getLocation", fLocation1, context1.getLocation()); | |
87 | assertEquals("getLocation", fLocation2, context2.getLocation()); | |
88 | assertEquals("getLocation", fLocation3, context3.getLocation()); | |
89 | ||
90 | assertEquals("getRank", TmfContext.UNKNOWN_RANK, context1.getRank()); | |
91 | assertEquals("getRank", TmfContext.UNKNOWN_RANK, context2.getRank()); | |
92 | assertEquals("getRank", TmfContext.UNKNOWN_RANK, context3.getRank()); | |
93 | } | |
94 | ||
95 | public void testTmfContext() { | |
96 | assertEquals("getLocation", fLocation1, fContext1.getLocation()); | |
97 | assertEquals("getLocation", fLocation2, fContext2.getLocation()); | |
98 | assertEquals("getLocation", fLocation3, fContext3.getLocation()); | |
99 | ||
100 | assertEquals("getRank", fRank1, fContext1.getRank()); | |
101 | assertEquals("getRank", fRank2, fContext2.getRank()); | |
102 | assertEquals("getRank", fRank3, fContext3.getRank()); | |
103 | } | |
104 | ||
105 | public void testTmfContextCopy() { | |
106 | final TmfContext context1 = new TmfContext(fContext1); | |
107 | final TmfContext context2 = new TmfContext(fContext2); | |
108 | final TmfContext context3 = new TmfContext(fContext3); | |
109 | ||
110 | assertEquals("getLocation", fLocation1, context1.getLocation()); | |
111 | assertEquals("getLocation", fLocation2, context2.getLocation()); | |
112 | assertEquals("getLocation", fLocation3, context3.getLocation()); | |
113 | ||
114 | assertEquals("getRank", fRank1, context1.getRank()); | |
115 | assertEquals("getRank", fRank2, context2.getRank()); | |
116 | assertEquals("getRank", fRank3, context3.getRank()); | |
117 | } | |
118 | ||
5d837f9b FC |
119 | public void testTmfContextCopy2() { |
120 | try { | |
121 | new TmfContext((TmfContext) null); | |
122 | fail("Copy constructor: no exception"); | |
123 | } | |
124 | catch (final IllegalArgumentException e) { | |
125 | // pass | |
126 | } | |
127 | catch (final Exception e) { | |
128 | fail("Copy constructor: wrong exception"); | |
129 | } | |
130 | } | |
131 | ||
cbdacf03 FC |
132 | // ------------------------------------------------------------------------ |
133 | // equals | |
134 | // ------------------------------------------------------------------------ | |
135 | ||
136 | public void testEqualsReflexivity() throws Exception { | |
137 | assertTrue("equals", fContext1.equals(fContext1)); | |
138 | assertTrue("equals", fContext2.equals(fContext2)); | |
139 | ||
140 | assertFalse("equals", fContext1.equals(fContext2)); | |
141 | assertFalse("equals", fContext2.equals(fContext1)); | |
142 | } | |
143 | ||
144 | public void testEqualsSymmetry() throws Exception { | |
145 | final TmfContext context1 = new TmfContext(fContext1); | |
146 | final TmfContext context2 = new TmfContext(fContext2); | |
d18dd09b | 147 | |
cbdacf03 FC |
148 | assertTrue("equals", context1.equals(fContext1)); |
149 | assertTrue("equals", fContext1.equals(context1)); | |
d18dd09b | 150 | |
cbdacf03 FC |
151 | assertTrue("equals", context2.equals(fContext2)); |
152 | assertTrue("equals", fContext2.equals(context2)); | |
153 | } | |
d18dd09b | 154 | |
cbdacf03 FC |
155 | public void testEqualsTransivity() throws Exception { |
156 | final TmfContext context1 = new TmfContext(fContext1); | |
157 | final TmfContext context2 = new TmfContext(context1); | |
158 | final TmfContext context3 = new TmfContext(context2); | |
d18dd09b | 159 | |
cbdacf03 FC |
160 | assertTrue("equals", context1.equals(context2)); |
161 | assertTrue("equals", context2.equals(context3)); | |
162 | assertTrue("equals", context1.equals(context3)); | |
163 | } | |
d18dd09b | 164 | |
cbdacf03 FC |
165 | public void testEqualsNull() throws Exception { |
166 | assertFalse("equals", fContext1.equals(null)); | |
167 | assertFalse("equals", fContext2.equals(null)); | |
168 | } | |
d18dd09b | 169 | |
cbdacf03 FC |
170 | public class MyContext extends TmfContext { |
171 | } | |
d18dd09b | 172 | |
cbdacf03 | 173 | public void testNonEquals() throws Exception { |
d18dd09b | 174 | |
cbdacf03 FC |
175 | // Different classes |
176 | final MyContext myContext = new MyContext(); | |
177 | assertFalse("equals", fContext1.equals(myContext)); | |
178 | assertFalse("equals", myContext.equals(fContext1)); | |
179 | ||
180 | // Different locations | |
181 | TmfContext context1 = new TmfContext(fContext1); | |
182 | TmfContext context2 = new TmfContext(fContext1); | |
183 | context1.setLocation(null); | |
184 | context2.setLocation(null); | |
185 | ||
186 | assertFalse("equals", fContext1.equals(context1)); | |
187 | assertFalse("equals", context1.equals(fContext1)); | |
188 | assertTrue("equals", context1.equals(context2)); | |
189 | ||
190 | // Different ranks | |
191 | context1 = new TmfContext(fContext1); | |
192 | context2 = new TmfContext(fContext1); | |
193 | context1.setRank(fContext1.getRank() + 1); | |
194 | context2.setRank(fContext1.getRank() + 2); | |
195 | ||
196 | assertFalse("equals", fContext1.equals(context1)); | |
197 | assertFalse("equals", context1.equals(fContext1)); | |
198 | assertFalse("equals", context1.equals(context2)); | |
199 | } | |
d18dd09b ASL |
200 | |
201 | // ------------------------------------------------------------------------ | |
cbdacf03 | 202 | // hashCode |
d18dd09b ASL |
203 | // ------------------------------------------------------------------------ |
204 | ||
cbdacf03 FC |
205 | public void testHashCode() throws Exception { |
206 | final TmfContext context1 = new TmfContext(fContext1); | |
207 | final TmfContext context2 = new TmfContext(fContext2); | |
208 | ||
209 | assertEquals("hashCode", fContext1.hashCode(), context1.hashCode()); | |
210 | assertEquals("hashCode", fContext2.hashCode(), context2.hashCode()); | |
211 | ||
212 | assertFalse("hashCode", fContext1.hashCode() == context2.hashCode()); | |
213 | assertFalse("hashCode", fContext2.hashCode() == context1.hashCode()); | |
214 | ||
215 | final TmfContext nullContext1 = new TmfContext(); | |
216 | final TmfContext nullContext2 = new TmfContext(nullContext1); | |
217 | assertEquals("hashCode", nullContext1.hashCode(), nullContext2.hashCode()); | |
218 | } | |
219 | ||
d18dd09b ASL |
220 | // ------------------------------------------------------------------------ |
221 | // toString | |
222 | // ------------------------------------------------------------------------ | |
223 | ||
cbdacf03 FC |
224 | public void testToString() { |
225 | final String expected1 = "TmfContext [fLocation=" + fLocation1 + ", fRank=" + 1 + "]"; | |
226 | final String expected2 = "TmfContext [fLocation=" + fLocation2 + ", fRank=" + 2 + "]"; | |
227 | final String expected3 = "TmfContext [fLocation=" + fLocation3 + ", fRank=" + 3 + "]"; | |
d18dd09b | 228 | |
cbdacf03 FC |
229 | assertEquals("toString", expected1, fContext1.toString()); |
230 | assertEquals("toString", expected2, fContext2.toString()); | |
231 | assertEquals("toString", expected3, fContext3.toString()); | |
232 | } | |
d18dd09b ASL |
233 | |
234 | // ------------------------------------------------------------------------ | |
235 | // clone | |
236 | // ------------------------------------------------------------------------ | |
237 | ||
cbdacf03 FC |
238 | public void testClone() { |
239 | try { | |
240 | final TmfContext context1 = fContext1.clone(); | |
241 | final TmfContext context2 = fContext2.clone(); | |
242 | final TmfContext context3 = fContext3.clone(); | |
d18dd09b | 243 | |
cbdacf03 FC |
244 | assertEquals("clone", context1, fContext1); |
245 | assertEquals("clone", context2, fContext2); | |
246 | assertEquals("clone", context3, fContext3); | |
5d837f9b FC |
247 | |
248 | context1.setLocation(null); | |
249 | final TmfContext context4 = context1.clone(); | |
250 | assertEquals("clone", context1, context4); | |
251 | assertNull(context1.getLocation()); | |
252 | assertNull(context4.getLocation()); | |
253 | ||
cbdacf03 FC |
254 | } catch (final InternalError e) { |
255 | fail("clone()"); | |
256 | } | |
257 | } | |
d18dd09b ASL |
258 | |
259 | // ------------------------------------------------------------------------ | |
260 | // setLocation, setRank, updateRank | |
261 | // ------------------------------------------------------------------------ | |
262 | ||
cbdacf03 FC |
263 | public void testSetLocation() { |
264 | final TmfContext context1 = new TmfContext(fContext1); | |
265 | context1.setLocation(fContext2.getLocation()); | |
d18dd09b | 266 | |
cbdacf03 FC |
267 | assertEquals("getLocation", fLocation2, context1.getLocation()); |
268 | assertEquals("getRank", 1, context1.getRank()); | |
269 | } | |
d18dd09b | 270 | |
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 | |
cbdacf03 FC |
279 | public void testIncreaseRank() { |
280 | final TmfContext context1 = new TmfContext(fContext1); | |
d18dd09b | 281 | |
cbdacf03 FC |
282 | context1.increaseRank(); |
283 | assertEquals("getRank", fRank1 + 1, context1.getRank()); | |
284 | context1.increaseRank(); | |
285 | assertEquals("getRank", fRank1 + 2, context1.getRank()); | |
d18dd09b | 286 | |
cbdacf03 FC |
287 | context1.setRank(ITmfContext.UNKNOWN_RANK); |
288 | context1.increaseRank(); | |
289 | assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context1.getRank()); | |
290 | context1.increaseRank(); | |
291 | assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context1.getRank()); | |
292 | } | |
d18dd09b ASL |
293 | |
294 | } |