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