1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
.text
;
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
;
20 import java
.util
.regex
.Pattern
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
23 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfContext
;
24 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
25 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.TmfLongLocation
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.text
.TextTraceContext
;
27 import org
.junit
.Test
;
30 * Test suite for the {@link TextTraceContext} class.
32 @SuppressWarnings("javadoc")
33 public class TextTraceContextTest
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private final Long aLong1
= 12345L;
40 private final Long aLong2
= 12346L;
41 private final Long aLong3
= 12347L;
43 private final TmfLongLocation fLocation1
= new TmfLongLocation(aLong1
);
44 private final TmfLongLocation fLocation2
= new TmfLongLocation(aLong2
);
46 private final long fRank1
= 1;
47 private final long fRank2
= 2;
49 private final TextTraceContext fContext1
= new TextTraceContext(fLocation1
, fRank1
);
50 private final TextTraceContext fContext2
= new TextTraceContext(fLocation1
, fRank1
);
52 private final Pattern pattern1
= Pattern
.compile("\\s*.*");
53 private final Pattern pattern2
= Pattern
.compile("\\s*.*");
55 private final String line1
= "line1";
56 private final String line2
= "line2";
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 public TextTraceContextTest () {
63 fContext1
.firstLine
= line1
;
64 fContext1
.firstLineMatcher
= pattern1
.matcher(line1
);
65 fContext1
.nextLineLocation
= aLong2
;
67 fContext2
.firstLine
= line2
;
68 fContext2
.firstLineMatcher
= pattern2
.matcher(line2
);
69 fContext2
.nextLineLocation
= aLong3
;
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
);
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 public void testEqualsReflexivity() {
88 assertTrue("equals", fContext1
.equals(fContext1
));
89 assertTrue("equals", fContext2
.equals(fContext2
));
91 assertFalse("equals", fContext1
.equals(fContext2
));
92 assertFalse("equals", fContext2
.equals(fContext1
));
96 public void testEqualsSymmetry() {
97 final TextTraceContext context1
= new TextTraceContext(fContext1
);
98 final TextTraceContext context2
= new TextTraceContext(fContext2
);
100 assertTrue("equals", context1
.equals(fContext1
));
101 assertTrue("equals", fContext1
.equals(context1
));
103 assertTrue("equals", context2
.equals(fContext2
));
104 assertTrue("equals", fContext2
.equals(context2
));
108 public void testEqualsTransivity() {
110 final TextTraceContext context1
= new TextTraceContext(fContext1
);
111 final TextTraceContext context2
= new TextTraceContext(fContext1
);
112 final TextTraceContext context3
= new TextTraceContext(fContext1
);
114 assertTrue("equals", context1
.equals(context2
));
115 assertTrue("equals", context2
.equals(context3
));
116 assertTrue("equals", context1
.equals(context3
));
120 public void testEqualsNull() {
121 assertFalse("equals", fContext1
.equals(null));
122 assertFalse("equals", fContext2
.equals(null));
125 private static class MyContext
extends TextTraceContext
{
127 public MyContext(ITmfLocation location
, long rank
) {
128 super(location
, rank
);
133 public void testNonEquals() {
136 final MyContext myContext
= new MyContext(fLocation1
, fRank1
);
137 assertFalse("equals", fContext1
.equals(myContext
));
138 assertFalse("equals", myContext
.equals(fContext1
));
140 // Different locations
141 TextTraceContext context1
= new TextTraceContext(fLocation1
, fRank1
);
142 TextTraceContext context2
= new TextTraceContext(fLocation2
, fRank1
);
143 assertFalse("equals", context1
.equals(context2
));
146 context1
= new TextTraceContext(fLocation1
, fRank1
);
147 context2
= new TextTraceContext(fLocation1
, fRank2
);
148 assertFalse("equals", context1
.equals(context2
));
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
));
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
));
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
));
179 // ------------------------------------------------------------------------
181 // ------------------------------------------------------------------------
184 public void testHashCode() {
185 final TextTraceContext context1
= new TextTraceContext(fContext1
);
186 final TextTraceContext context2
= new TextTraceContext(fContext2
);
188 assertEquals("hashCode", fContext1
.hashCode(), context1
.hashCode());
189 assertEquals("hashCode", fContext2
.hashCode(), context2
.hashCode());
191 assertFalse("hashCode", fContext1
.hashCode() == context2
.hashCode());
192 assertFalse("hashCode", fContext2
.hashCode() == context1
.hashCode());
194 final TmfContext nullContext1
= new TmfContext();
195 final TmfContext nullContext2
= new TmfContext(nullContext1
);
196 assertEquals("hashCode", nullContext1
.hashCode(), nullContext2
.hashCode());
199 // ------------------------------------------------------------------------
200 // setLocation, setRank, updateRank
201 // ------------------------------------------------------------------------
204 public void testSetLocation() {
205 final TextTraceContext context1
= new TextTraceContext(fLocation1
, fRank1
);
206 context1
.setLocation(fLocation2
);
208 assertEquals("getLocation", fLocation2
, context1
.getLocation());
209 assertEquals("getRank", fRank1
, context1
.getRank());
213 public void testSetRank() {
214 final TextTraceContext context1
= new TextTraceContext(fContext1
);
215 context1
.setRank(fContext2
.getRank());
217 assertEquals("getLocation", fContext1
.getLocation(), context1
.getLocation());
218 assertEquals("getRank", fContext2
.getRank(), context1
.getRank());
222 public void testIncreaseRank() {
223 final TextTraceContext context1
= new TextTraceContext(fLocation1
, fRank1
);
225 context1
.increaseRank();
226 assertEquals("getRank", fRank1
+ 1, context1
.getRank());
227 context1
.increaseRank();
228 assertEquals("getRank", fRank1
+ 2, context1
.getRank());
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());