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