Commit | Line | Data |
---|---|---|
eadf9801 BH |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 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 | * Bernd Hufmann - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.tests.trace.text; | |
14 | ||
15 | import static org.junit.Assert.assertEquals; | |
16 | import static org.junit.Assert.assertFalse; | |
17 | import static org.junit.Assert.assertNull; | |
18 | import static org.junit.Assert.assertTrue; | |
19 | ||
20 | import java.util.regex.Pattern; | |
21 | ||
22 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
23 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; | |
24 | import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation; | |
25 | import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation; | |
26 | import org.eclipse.linuxtools.tmf.core.trace.text.TextTraceContext; | |
27 | import org.junit.Test; | |
28 | ||
29 | /** | |
30 | * Test suite for the {@link TextTraceContext} class. | |
31 | */ | |
32 | @SuppressWarnings("javadoc") | |
33 | public class TextTraceContextTest { | |
34 | ||
35 | // ------------------------------------------------------------------------ | |
36 | // Variables | |
37 | // ------------------------------------------------------------------------ | |
38 | ||
39 | private final Long aLong1 = 12345L; | |
40 | private final Long aLong2 = 12346L; | |
41 | private final Long aLong3 = 12347L; | |
42 | ||
43 | private final TmfLongLocation fLocation1 = new TmfLongLocation(aLong1); | |
44 | private final TmfLongLocation fLocation2 = new TmfLongLocation(aLong2); | |
45 | ||
46 | private final long fRank1 = 1; | |
47 | private final long fRank2 = 2; | |
48 | ||
49 | private final TextTraceContext fContext1 = new TextTraceContext(fLocation1, fRank1); | |
50 | private final TextTraceContext fContext2 = new TextTraceContext(fLocation1, fRank1); | |
51 | ||
52 | private final Pattern pattern1 = Pattern.compile("\\s*.*"); | |
53 | private final Pattern pattern2 = Pattern.compile("\\s*.*"); | |
54 | ||
55 | private final String line1 = "line1"; | |
56 | private final String line2 = "line2"; | |
57 | ||
58 | // ------------------------------------------------------------------------ | |
59 | // Constructors | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
62 | public TextTraceContextTest () { | |
63 | fContext1.firstLine = line1; | |
64 | fContext1.firstLineMatcher = pattern1.matcher(line1); | |
65 | fContext1.nextLineLocation = aLong2; | |
66 | ||
67 | fContext2.firstLine = line2; | |
68 | fContext2.firstLineMatcher = pattern2.matcher(line2); | |
69 | fContext2.nextLineLocation = aLong3; | |
70 | } | |
71 | ||
72 | @Test | |
73 | public void testTmfContextDefault() { | |
74 | final TextTraceContext context = new TextTraceContext(fLocation1, fRank1); | |
75 | assertEquals("getLocation", fLocation1, context.getLocation()); | |
76 | assertEquals("getRank", fRank1, context.getRank()); | |
77 | assertNull(context.firstLine); | |
78 | assertNull(context.firstLineMatcher); | |
79 | assertEquals(0, context.nextLineLocation); | |
80 | } | |
81 | ||
82 | // ------------------------------------------------------------------------ | |
83 | // equals | |
84 | // ------------------------------------------------------------------------ | |
85 | ||
86 | @Test | |
87 | public void testEqualsReflexivity() { | |
88 | assertTrue("equals", fContext1.equals(fContext1)); | |
89 | assertTrue("equals", fContext2.equals(fContext2)); | |
90 | ||
91 | assertFalse("equals", fContext1.equals(fContext2)); | |
92 | assertFalse("equals", fContext2.equals(fContext1)); | |
93 | } | |
94 | ||
95 | @Test | |
96 | public void testEqualsSymmetry() { | |
97 | final TextTraceContext context1 = new TextTraceContext(fContext1); | |
98 | final TextTraceContext context2 = new TextTraceContext(fContext2); | |
99 | ||
100 | assertTrue("equals", context1.equals(fContext1)); | |
101 | assertTrue("equals", fContext1.equals(context1)); | |
102 | ||
103 | assertTrue("equals", context2.equals(fContext2)); | |
104 | assertTrue("equals", fContext2.equals(context2)); | |
105 | } | |
106 | ||
107 | @Test | |
108 | public void testEqualsTransivity() { | |
109 | ||
110 | final TextTraceContext context1 = new TextTraceContext(fContext1); | |
111 | final TextTraceContext context2 = new TextTraceContext(fContext1); | |
112 | final TextTraceContext context3 = new TextTraceContext(fContext1); | |
113 | ||
114 | assertTrue("equals", context1.equals(context2)); | |
115 | assertTrue("equals", context2.equals(context3)); | |
116 | assertTrue("equals", context1.equals(context3)); | |
117 | } | |
118 | ||
119 | @Test | |
120 | public void testEqualsNull() { | |
121 | assertFalse("equals", fContext1.equals(null)); | |
122 | assertFalse("equals", fContext2.equals(null)); | |
123 | } | |
124 | ||
125 | private static class MyContext extends TextTraceContext { | |
126 | ||
127 | public MyContext(ITmfLocation location, long rank) { | |
128 | super(location, rank); | |
129 | } | |
130 | } | |
131 | ||
132 | @Test | |
133 | public void testNonEquals() { | |
134 | ||
135 | // Different classes | |
136 | final MyContext myContext = new MyContext(fLocation1, fRank1); | |
137 | assertFalse("equals", fContext1.equals(myContext)); | |
138 | assertFalse("equals", myContext.equals(fContext1)); | |
139 | ||
140 | // Different locations | |
141 | TextTraceContext context1 = new TextTraceContext(fLocation1, fRank1); | |
142 | TextTraceContext context2 = new TextTraceContext(fLocation2, fRank1); | |
143 | assertFalse("equals", context1.equals(context2)); | |
144 | ||
145 | // Different ranks | |
146 | context1 = new TextTraceContext(fLocation1, fRank1); | |
147 | context2 = new TextTraceContext(fLocation1, fRank2); | |
148 | assertFalse("equals", context1.equals(context2)); | |
149 | ||
150 | // Different firstLine | |
151 | context1 = new TextTraceContext(fLocation1, fRank1); | |
152 | context1.firstLine = line1; | |
153 | context2 = new TextTraceContext(fLocation1, fRank1); | |
154 | context2.firstLine = line2; | |
155 | assertFalse("equals", context1.equals(context2)); | |
156 | ||
157 | // Different firstLineMatcher | |
158 | context1 = new TextTraceContext(fLocation1, fRank1); | |
159 | context1.firstLine = line1; | |
160 | context1.firstLineMatcher = fContext1.firstLineMatcher; | |
161 | context2 = new TextTraceContext(fLocation1, fRank1); | |
162 | context2.firstLine = line1; | |
163 | context2.firstLineMatcher = fContext2.firstLineMatcher; | |
164 | assertFalse("equals", context1.equals(context2)); | |
165 | ||
166 | // Different nextLineLocation | |
167 | context1 = new TextTraceContext(fLocation1, fRank1); | |
168 | context1.firstLine = line1; | |
169 | context1.firstLineMatcher = fContext1.firstLineMatcher; | |
170 | context1.nextLineLocation = aLong2; | |
171 | context2 = new TextTraceContext(fLocation1, fRank1); | |
172 | context2.firstLine = line1; | |
173 | context2.firstLineMatcher = fContext1.firstLineMatcher; | |
174 | context2.nextLineLocation = aLong3; | |
175 | assertFalse("equals", context1.equals(context2)); | |
176 | ||
177 | } | |
178 | ||
179 | // ------------------------------------------------------------------------ | |
180 | // hashCode | |
181 | // ------------------------------------------------------------------------ | |
182 | ||
183 | @Test | |
184 | public void testHashCode() { | |
185 | final TextTraceContext context1 = new TextTraceContext(fContext1); | |
186 | final TextTraceContext context2 = new TextTraceContext(fContext2); | |
187 | ||
188 | assertEquals("hashCode", fContext1.hashCode(), context1.hashCode()); | |
189 | assertEquals("hashCode", fContext2.hashCode(), context2.hashCode()); | |
190 | ||
191 | assertFalse("hashCode", fContext1.hashCode() == context2.hashCode()); | |
192 | assertFalse("hashCode", fContext2.hashCode() == context1.hashCode()); | |
193 | ||
194 | final TmfContext nullContext1 = new TmfContext(); | |
195 | final TmfContext nullContext2 = new TmfContext(nullContext1); | |
196 | assertEquals("hashCode", nullContext1.hashCode(), nullContext2.hashCode()); | |
197 | } | |
198 | ||
199 | // ------------------------------------------------------------------------ | |
200 | // setLocation, setRank, updateRank | |
201 | // ------------------------------------------------------------------------ | |
202 | ||
203 | @Test | |
204 | public void testSetLocation() { | |
205 | final TextTraceContext context1 = new TextTraceContext(fLocation1, fRank1); | |
206 | context1.setLocation(fLocation2); | |
207 | ||
208 | assertEquals("getLocation", fLocation2, context1.getLocation()); | |
209 | assertEquals("getRank", fRank1, context1.getRank()); | |
210 | } | |
211 | ||
212 | @Test | |
213 | public void testSetRank() { | |
214 | final TextTraceContext context1 = new TextTraceContext(fContext1); | |
215 | context1.setRank(fContext2.getRank()); | |
216 | ||
217 | assertEquals("getLocation", fContext1.getLocation(), context1.getLocation()); | |
218 | assertEquals("getRank", fContext2.getRank(), context1.getRank()); | |
219 | } | |
220 | ||
221 | @Test | |
222 | public void testIncreaseRank() { | |
223 | final TextTraceContext context1 = new TextTraceContext(fLocation1, fRank1); | |
224 | ||
225 | context1.increaseRank(); | |
226 | assertEquals("getRank", fRank1 + 1, context1.getRank()); | |
227 | context1.increaseRank(); | |
228 | assertEquals("getRank", fRank1 + 2, context1.getRank()); | |
229 | ||
230 | context1.setRank(ITmfContext.UNKNOWN_RANK); | |
231 | context1.increaseRank(); | |
232 | assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context1.getRank()); | |
233 | context1.increaseRank(); | |
234 | assertEquals("getRank", ITmfContext.UNKNOWN_RANK, context1.getRank()); | |
235 | } | |
236 | ||
237 | } | |
238 |