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