ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / TmfContextTest.java
CommitLineData
d18dd09b 1/*******************************************************************************
61759503 2 * Copyright (c) 2009, 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
ea271da6 13 * Patrick Tasse - Updated for removal of context clone
d18dd09b
ASL
14 *******************************************************************************/
15
6c13869b 16package org.eclipse.linuxtools.tmf.core.tests.trace;
d18dd09b 17
6e1886bc
AM
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
6e1886bc
AM
20import static org.junit.Assert.assertTrue;
21import static org.junit.Assert.fail;
d18dd09b 22
3bd46eef 23import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
cbdacf03 24import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
6c13869b 25import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
a3db8436
AM
26import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
27import org.eclipse.linuxtools.tmf.core.trace.location.TmfTimestampLocation;
6e1886bc 28import org.junit.Test;
d18dd09b
ASL
29
30/**
d18dd09b
ASL
31 * Test suite for the TmfContext class.
32 */
cad06250 33@SuppressWarnings("javadoc")
6e1886bc 34public class TmfContextTest {
d18dd09b 35
cbdacf03
FC
36 // ------------------------------------------------------------------------
37 // Variables
38 // ------------------------------------------------------------------------
39
6e1886bc
AM
40 private final Long aLong = 12345L;
41 private final TmfTimestamp aTimestamp = new TmfTimestamp();
d18dd09b 42
8eaa4a57
MAL
43 private final TmfLongLocation fLocation1 = new TmfLongLocation(aLong);
44 private final TmfTimestampLocation fLocation2 = new TmfTimestampLocation(aTimestamp);
d18dd09b 45
6e1886bc
AM
46 private final long fRank1 = 1;
47 private final long fRank2 = 2;
cbdacf03 48
6e1886bc
AM
49 private final TmfContext fContext1 = new TmfContext(fLocation1, fRank1);
50 private final TmfContext fContext2 = new TmfContext(fLocation2, fRank2);
d18dd09b
ASL
51
52 // ------------------------------------------------------------------------
53 // Constructors
54 // ------------------------------------------------------------------------
55
6e1886bc 56 @Test
cbdacf03
FC
57 public void testTmfContextDefault() {
58 final TmfContext context = new TmfContext();
59 assertEquals("getLocation", null, context.getLocation());
9b749023 60 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context.getRank());
cbdacf03
FC
61 }
62
6e1886bc 63 @Test
cbdacf03
FC
64 public void testTmfContextNoRank() {
65 final TmfContext context1 = new TmfContext(fLocation1);
66 final TmfContext context2 = new TmfContext(fLocation2);
cbdacf03
FC
67
68 assertEquals("getLocation", fLocation1, context1.getLocation());
69 assertEquals("getLocation", fLocation2, context2.getLocation());
cbdacf03 70
9b749023
AM
71 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context1.getRank());
72 assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context2.getRank());
cbdacf03
FC
73 }
74
6e1886bc 75 @Test
cbdacf03
FC
76 public void testTmfContext() {
77 assertEquals("getLocation", fLocation1, fContext1.getLocation());
78 assertEquals("getLocation", fLocation2, fContext2.getLocation());
cbdacf03
FC
79
80 assertEquals("getRank", fRank1, fContext1.getRank());
81 assertEquals("getRank", fRank2, fContext2.getRank());
cbdacf03
FC
82 }
83
6e1886bc 84 @Test
cbdacf03
FC
85 public void testTmfContextCopy() {
86 final TmfContext context1 = new TmfContext(fContext1);
87 final TmfContext context2 = new TmfContext(fContext2);
cbdacf03
FC
88
89 assertEquals("getLocation", fLocation1, context1.getLocation());
90 assertEquals("getLocation", fLocation2, context2.getLocation());
cbdacf03
FC
91
92 assertEquals("getRank", fRank1, context1.getRank());
93 assertEquals("getRank", fRank2, context2.getRank());
cbdacf03
FC
94 }
95
6e1886bc 96 @Test
5d837f9b
FC
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
cbdacf03
FC
110 // ------------------------------------------------------------------------
111 // equals
112 // ------------------------------------------------------------------------
113
6e1886bc 114 @Test
54a7a54c 115 public void testEqualsReflexivity() {
cbdacf03
FC
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
6e1886bc 123 @Test
54a7a54c 124 public void testEqualsSymmetry() {
cbdacf03
FC
125 final TmfContext context1 = new TmfContext(fContext1);
126 final TmfContext context2 = new TmfContext(fContext2);
d18dd09b 127
cbdacf03
FC
128 assertTrue("equals", context1.equals(fContext1));
129 assertTrue("equals", fContext1.equals(context1));
d18dd09b 130
cbdacf03
FC
131 assertTrue("equals", context2.equals(fContext2));
132 assertTrue("equals", fContext2.equals(context2));
133 }
d18dd09b 134
6e1886bc 135 @Test
54a7a54c 136 public void testEqualsTransivity() {
cbdacf03
FC
137 final TmfContext context1 = new TmfContext(fContext1);
138 final TmfContext context2 = new TmfContext(context1);
139 final TmfContext context3 = new TmfContext(context2);
d18dd09b 140
cbdacf03
FC
141 assertTrue("equals", context1.equals(context2));
142 assertTrue("equals", context2.equals(context3));
143 assertTrue("equals", context1.equals(context3));
144 }
d18dd09b 145
6e1886bc 146 @Test
54a7a54c 147 public void testEqualsNull() {
cbdacf03
FC
148 assertFalse("equals", fContext1.equals(null));
149 assertFalse("equals", fContext2.equals(null));
150 }
d18dd09b 151
0879b6b9 152 private static class MyContext extends TmfContext {
cbdacf03 153 }
d18dd09b 154
6e1886bc 155 @Test
54a7a54c 156 public void testNonEquals() {
d18dd09b 157
cbdacf03
FC
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 }
d18dd09b
ASL
183
184 // ------------------------------------------------------------------------
cbdacf03 185 // hashCode
d18dd09b
ASL
186 // ------------------------------------------------------------------------
187
6e1886bc 188 @Test
54a7a54c 189 public void testHashCode() {
cbdacf03
FC
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
d18dd09b
ASL
204 // ------------------------------------------------------------------------
205 // toString
206 // ------------------------------------------------------------------------
207
6e1886bc 208 @Test
cbdacf03 209 public void testToString() {
8eaa4a57
MAL
210 final String expected1 = "TmfContext [fLocation=" + fLocation1 + ", fRank=" + fRank1 + "]";
211 final String expected2 = "TmfContext [fLocation=" + fLocation2 + ", fRank=" + fRank2 + "]";
d18dd09b 212
cbdacf03
FC
213 assertEquals("toString", expected1, fContext1.toString());
214 assertEquals("toString", expected2, fContext2.toString());
cbdacf03 215 }
d18dd09b 216
d18dd09b
ASL
217 // ------------------------------------------------------------------------
218 // setLocation, setRank, updateRank
219 // ------------------------------------------------------------------------
220
6e1886bc 221 @Test
cbdacf03
FC
222 public void testSetLocation() {
223 final TmfContext context1 = new TmfContext(fContext1);
224 context1.setLocation(fContext2.getLocation());
d18dd09b 225
cbdacf03 226 assertEquals("getLocation", fLocation2, context1.getLocation());
8eaa4a57 227 assertEquals("getRank", fRank1, context1.getRank());
cbdacf03 228 }
d18dd09b 229
6e1886bc 230 @Test
cbdacf03
FC
231 public void testSetRank() {
232 final TmfContext context1 = new TmfContext(fContext1);
233 context1.setRank(fContext2.getRank());
d18dd09b 234
cbdacf03
FC
235 assertEquals("getLocation", fLocation1, context1.getLocation());
236 assertEquals("getRank", fRank2, context1.getRank());
237 }
d18dd09b 238
6e1886bc 239 @Test
cbdacf03
FC
240 public void testIncreaseRank() {
241 final TmfContext context1 = new TmfContext(fContext1);
d18dd09b 242
cbdacf03
FC
243 context1.increaseRank();
244 assertEquals("getRank", fRank1 + 1, context1.getRank());
245 context1.increaseRank();
246 assertEquals("getRank", fRank1 + 2, context1.getRank());
d18dd09b 247
cbdacf03
FC
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 }
d18dd09b
ASL
254
255}
This page took 0.066915 seconds and 5 git commands to generate.