Commit | Line | Data |
---|---|---|
d18dd09b ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 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 | *******************************************************************************/ | |
12 | ||
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.tests.trace; |
d18dd09b ASL |
14 | |
15 | import junit.framework.TestCase; | |
16 | ||
fcccd900 | 17 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
6c13869b FC |
18 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
19 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; | |
20 | import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; | |
d18dd09b ASL |
21 | |
22 | /** | |
23 | * <b><u>TmfContextTest</u></b> | |
24 | * <p> | |
25 | * Test suite for the TmfContext class. | |
26 | */ | |
3b38ea61 | 27 | @SuppressWarnings("nls") |
d18dd09b ASL |
28 | public class TmfContextTest extends TestCase { |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Variables | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | final String aString = "some location"; | |
35 | final Long aLong = 12345L; | |
36 | final TmfTimestamp aTimestamp = new TmfTimestamp(); | |
37 | ||
38 | final TmfLocation<String> fLocation1 = new TmfLocation<String>(aString); | |
39 | final TmfLocation<Long> fLocation2 = new TmfLocation<Long>(aLong); | |
fcccd900 | 40 | final TmfLocation<ITmfTimestamp> fLocation3 = new TmfLocation<ITmfTimestamp>(aTimestamp); |
d18dd09b ASL |
41 | |
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); | |
49 | ||
50 | // ------------------------------------------------------------------------ | |
51 | // Housekeeping | |
52 | // ------------------------------------------------------------------------ | |
53 | ||
54 | /** | |
55 | * @param name the test name | |
56 | */ | |
57 | public TmfContextTest(String name) { | |
58 | super(name); | |
59 | } | |
60 | ||
61 | @Override | |
62 | protected void setUp() throws Exception { | |
63 | super.setUp(); | |
64 | } | |
65 | ||
66 | @Override | |
67 | protected void tearDown() throws Exception { | |
68 | super.tearDown(); | |
69 | } | |
70 | ||
71 | // ------------------------------------------------------------------------ | |
72 | // Constructors | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
75 | public void testTmfContextDefault() { | |
76 | TmfContext context = new TmfContext(); | |
77 | assertEquals("getLocation", null, context.getLocation()); | |
550d787e | 78 | assertEquals("getRank", TmfContext.UNKNOWN_RANK, context.getRank()); |
d18dd09b ASL |
79 | } |
80 | ||
81 | public void testTmfContextNoRank() { | |
82 | TmfContext context1 = new TmfContext(fLocation1); | |
83 | TmfContext context2 = new TmfContext(fLocation2); | |
84 | 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 | ||
550d787e FC |
90 | assertEquals("getRank", TmfContext.UNKNOWN_RANK, context1.getRank()); |
91 | assertEquals("getRank", TmfContext.UNKNOWN_RANK, context2.getRank()); | |
92 | assertEquals("getRank", TmfContext.UNKNOWN_RANK, context3.getRank()); | |
d18dd09b ASL |
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 | TmfContext context1 = new TmfContext(fContext1); | |
107 | TmfContext context2 = new TmfContext(fContext2); | |
108 | 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 | ||
119 | // ------------------------------------------------------------------------ | |
120 | // equals | |
121 | // ------------------------------------------------------------------------ | |
122 | ||
123 | public void testEqualsReflexivity() throws Exception { | |
124 | assertTrue("equals", fContext1.equals(fContext1)); | |
125 | assertTrue("equals", fContext2.equals(fContext2)); | |
126 | ||
127 | assertTrue("equals", !fContext1.equals(fContext2)); | |
128 | assertTrue("equals", !fContext2.equals(fContext1)); | |
129 | } | |
130 | ||
131 | public void testEqualsSymmetry() throws Exception { | |
132 | TmfContext context1 = new TmfContext(fContext1); | |
133 | TmfContext context2 = new TmfContext(fContext2); | |
134 | ||
135 | assertTrue("equals", context1.equals(fContext1)); | |
136 | assertTrue("equals", fContext1.equals(context1)); | |
137 | ||
138 | assertTrue("equals", context2.equals(fContext2)); | |
139 | assertTrue("equals", fContext2.equals(context2)); | |
140 | } | |
141 | ||
142 | public void testEqualsTransivity() throws Exception { | |
143 | TmfContext context1 = new TmfContext(fContext1); | |
144 | TmfContext context2 = new TmfContext(context1); | |
145 | TmfContext context3 = new TmfContext(context2); | |
146 | ||
147 | assertTrue("equals", context1.equals(context2)); | |
148 | assertTrue("equals", context2.equals(context3)); | |
149 | assertTrue("equals", context1.equals(context3)); | |
150 | } | |
151 | ||
152 | public void testEqualsNull() throws Exception { | |
153 | assertTrue("equals", !fContext1.equals(null)); | |
154 | assertTrue("equals", !fContext2.equals(null)); | |
155 | } | |
156 | ||
157 | // ------------------------------------------------------------------------ | |
158 | // hashCode | |
159 | // ------------------------------------------------------------------------ | |
160 | ||
161 | public void testHashCode() throws Exception { | |
162 | TmfContext context1 = new TmfContext(fContext1); | |
163 | TmfContext context2 = new TmfContext(fContext2); | |
164 | ||
165 | assertTrue("hashCode", fContext1.hashCode() == context1.hashCode()); | |
166 | assertTrue("hashCode", fContext2.hashCode() == context2.hashCode()); | |
167 | ||
168 | assertTrue("hashCode", fContext1.hashCode() != context2.hashCode()); | |
169 | assertTrue("hashCode", fContext2.hashCode() != context1.hashCode()); | |
170 | } | |
171 | ||
172 | // ------------------------------------------------------------------------ | |
173 | // toString | |
174 | // ------------------------------------------------------------------------ | |
175 | ||
176 | public void testToString() { | |
177 | String expected1 = "[TmfContext(" + fLocation1 + "," + 1 + ")]"; | |
178 | String expected2 = "[TmfContext(" + fLocation2 + "," + 2 + ")]"; | |
179 | String expected3 = "[TmfContext(" + fLocation3 + "," + 3 + ")]"; | |
180 | ||
181 | assertEquals("toString", expected1, fContext1.toString()); | |
182 | assertEquals("toString", expected2, fContext2.toString()); | |
183 | assertEquals("toString", expected3, fContext3.toString()); | |
184 | } | |
185 | ||
186 | // ------------------------------------------------------------------------ | |
187 | // clone | |
188 | // ------------------------------------------------------------------------ | |
189 | ||
190 | public void testClone() { | |
191 | try { | |
192 | TmfContext context1 = fContext1.clone(); | |
193 | TmfContext context2 = fContext2.clone(); | |
194 | TmfContext context3 = fContext3.clone(); | |
195 | ||
196 | assertEquals("clone", context1, fContext1); | |
197 | assertEquals("clone", context2, fContext2); | |
198 | assertEquals("clone", context3, fContext3); | |
199 | } | |
200 | catch (InternalError e) { | |
201 | fail("clone()"); | |
202 | } | |
203 | } | |
204 | ||
205 | // ------------------------------------------------------------------------ | |
206 | // setLocation, setRank, updateRank | |
207 | // ------------------------------------------------------------------------ | |
208 | ||
209 | public void testSetLocation() { | |
210 | TmfContext context1 = new TmfContext(fContext1); | |
211 | context1.setLocation(fContext2.getLocation()); | |
212 | ||
213 | assertEquals("getLocation", fLocation2, context1.getLocation()); | |
214 | assertEquals("getRank", 1, context1.getRank()); | |
215 | } | |
216 | ||
217 | public void testSetRank() { | |
218 | TmfContext context1 = new TmfContext(fContext1); | |
219 | context1.setRank(fContext2.getRank()); | |
220 | ||
221 | assertEquals("getLocation", fLocation1, context1.getLocation()); | |
222 | assertEquals("getRank", fRank2, context1.getRank()); | |
223 | } | |
224 | ||
225 | public void testUpdatetRank() { | |
226 | TmfContext context1 = new TmfContext(fContext1); | |
227 | ||
228 | context1.updateRank(0); | |
229 | assertEquals("getRank", fRank1, context1.getRank()); | |
230 | ||
231 | context1.updateRank(-1); | |
232 | assertEquals("getRank", fRank1 - 1, context1.getRank()); | |
233 | ||
234 | context1.updateRank(2); | |
235 | assertEquals("getRank", fRank1 + 1, context1.getRank()); | |
236 | } | |
237 | ||
238 | } |