Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
6e1886bc | 2 | * Copyright (c) 2009, 2012, 2013 Ericsson |
9b749023 | 3 | * |
cbbcc354 | 4 | * All rights reserved. This program and the accompanying materials are made |
5 | * available under the terms of the Eclipse Public License v1.0 which | |
d18dd09b ASL |
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 | |
75d42a16 | 11 | * Francois Chouinard - Adjusted for new Event Model |
6e1886bc | 12 | * Alexandre Montplaisir - Port to JUnit4 |
d18dd09b ASL |
13 | *******************************************************************************/ |
14 | ||
6c13869b | 15 | package org.eclipse.linuxtools.tmf.core.tests.event; |
d18dd09b | 16 | |
6e1886bc AM |
17 | import static org.junit.Assert.assertEquals; |
18 | import static org.junit.Assert.assertFalse; | |
19 | import static org.junit.Assert.assertNull; | |
20 | import static org.junit.Assert.assertTrue; | |
21 | import static org.junit.Assert.fail; | |
cbd4ad82 | 22 | |
9ee9135e | 23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; |
4c564a2d | 24 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
6c13869b | 25 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
6e1886bc | 26 | import org.junit.Test; |
d18dd09b ASL |
27 | |
28 | /** | |
75d42a16 | 29 | * Test suite for the TmfEventType class. |
d18dd09b | 30 | */ |
6e1886bc AM |
31 | @SuppressWarnings({"nls", "javadoc"}) |
32 | public class TmfEventTypeTest { | |
d18dd09b | 33 | |
cbd4ad82 | 34 | // ------------------------------------------------------------------------ |
cbbcc354 | 35 | // Variables |
cbd4ad82 FC |
36 | // ------------------------------------------------------------------------ |
37 | ||
9ee9135e FC |
38 | private final String fContext1 = "JUnit context 1"; |
39 | private final String fContext2 = "JUnit context 2"; | |
40 | ||
39f9eadb | 41 | private final String fTypeId1 = "Some type"; |
cbbcc354 | 42 | private final String fTypeId2 = "Some other type"; |
9ee9135e | 43 | |
cbbcc354 | 44 | private final String fLabel0 = "label1"; |
45 | private final String fLabel1 = "label2"; | |
085d898f | 46 | |
39f9eadb FC |
47 | private final String[] fLabels0 = new String[] { }; |
48 | private final String[] fLabels1 = new String[] { fLabel0, fLabel1 }; | |
49 | private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel1 }; | |
d18dd09b | 50 | |
de126dbb FC |
51 | private final ITmfEventType fType0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0)); |
52 | private final ITmfEventType fType1 = new TmfEventType(fContext1, fTypeId2, TmfEventField.makeRoot(fLabels1)); | |
53 | private final ITmfEventType fType2 = new TmfEventType(fContext2, fTypeId1, TmfEventField.makeRoot(fLabels2)); | |
54 | private final ITmfEventType fType3 = new TmfEventType(fContext2, fTypeId2, TmfEventField.makeRoot(fLabels1)); | |
cbd4ad82 | 55 | |
cbbcc354 | 56 | // ------------------------------------------------------------------------ |
57 | // Constructors | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
6e1886bc | 60 | @Test |
9ee9135e | 61 | public void testDefaultConstructor() { |
085d898f | 62 | final ITmfEventType type = new TmfEventType(); |
9b749023 AM |
63 | assertEquals("getContext", ITmfEventType.DEFAULT_CONTEXT_ID, type.getContext()); |
64 | assertEquals("getName", ITmfEventType.DEFAULT_TYPE_ID, type.getName()); | |
9ee9135e | 65 | assertNull("getRootField", type.getRootField()); |
8b63ffb9 | 66 | assertEquals("getFieldNames", 0, type.getFieldNames().length); |
9ee9135e FC |
67 | assertNull("getFieldName", type.getFieldName(0)); |
68 | } | |
69 | ||
6e1886bc | 70 | @Test |
9ee9135e | 71 | public void testFullConstructor() { |
085d898f | 72 | final ITmfEventType type0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0)); |
9ee9135e FC |
73 | assertEquals("getContext", fContext1, type0.getContext()); |
74 | assertEquals("getName", fTypeId1, type0.getName()); | |
75 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels0), type0.getRootField()); | |
085d898f | 76 | final String[] labels0 = type0.getFieldNames(); |
9ee9135e | 77 | assertEquals("getFieldNames length", fLabels0.length, labels0.length); |
9b749023 | 78 | for (int i = 0; i < labels0.length; i++) { |
9ee9135e | 79 | assertEquals("getFieldNames", fLabels0[i], labels0[i]); |
9b749023 | 80 | } |
9ee9135e FC |
81 | assertNull("getFieldName", type0.getFieldName(labels0.length)); |
82 | ||
085d898f | 83 | final ITmfEventType type1 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1)); |
9ee9135e FC |
84 | assertEquals("getContext", fContext1, type1.getContext()); |
85 | assertEquals("getName", fTypeId1, type1.getName()); | |
86 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), type1.getRootField()); | |
085d898f | 87 | final String[] labels1 = type1.getFieldNames(); |
9ee9135e | 88 | assertEquals("getFieldNames length", fLabels1.length, labels1.length); |
9b749023 | 89 | for (int i = 0; i < labels1.length; i++) { |
9ee9135e | 90 | assertEquals("getFieldNames", fLabels1[i], labels1[i]); |
9b749023 | 91 | } |
9ee9135e FC |
92 | assertNull("getFieldName", type1.getFieldName(labels1.length)); |
93 | ||
085d898f | 94 | final ITmfEventType type2 = new TmfEventType(fContext2, fTypeId2, TmfEventField.makeRoot(fLabels2)); |
9ee9135e FC |
95 | assertEquals("getContext", fContext2, type2.getContext()); |
96 | assertEquals("getName", fTypeId2, type2.getName()); | |
97 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels2), type2.getRootField()); | |
085d898f | 98 | final String[] labels2 = type2.getFieldNames(); |
9ee9135e | 99 | assertEquals("getFieldNames length", fLabels2.length, labels2.length); |
9b749023 | 100 | for (int i = 0; i < labels2.length; i++) { |
9ee9135e | 101 | assertEquals("getFieldNames", fLabels2[i], labels2[i]); |
9b749023 | 102 | } |
9ee9135e FC |
103 | assertNull("getFieldName", type2.getFieldName(labels2.length)); |
104 | } | |
105 | ||
6e1886bc | 106 | @Test |
9ee9135e | 107 | public void testConstructorCornerCases() { |
cbbcc354 | 108 | try { |
9ee9135e FC |
109 | new TmfEventType(null, fTypeId1, null); |
110 | fail("TmfEventType: null context"); | |
085d898f | 111 | } catch (final IllegalArgumentException e) { |
cbbcc354 | 112 | } |
cbbcc354 | 113 | |
cbbcc354 | 114 | try { |
9ee9135e FC |
115 | new TmfEventType(fContext1, null, null); |
116 | fail("TmfEventType: null type"); | |
085d898f | 117 | } catch (final IllegalArgumentException e) { |
cbbcc354 | 118 | } |
119 | } | |
120 | ||
6e1886bc | 121 | @Test |
9ee9135e | 122 | public void testCopyConstructor() { |
085d898f FC |
123 | final TmfEventType original = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1)); |
124 | final TmfEventType copy = new TmfEventType(original); | |
cbbcc354 | 125 | |
9ee9135e FC |
126 | assertEquals("getContext", fContext1, copy.getContext()); |
127 | assertEquals("getName", fTypeId1, copy.getName()); | |
128 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), copy.getRootField()); | |
085d898f | 129 | final String[] labels1 = copy.getFieldNames(); |
9ee9135e | 130 | assertEquals("getFieldNames length", fLabels1.length, labels1.length); |
9b749023 | 131 | for (int i = 0; i < labels1.length; i++) { |
9ee9135e | 132 | assertEquals("getFieldNames", fLabels1[i], labels1[i]); |
9b749023 | 133 | } |
9ee9135e | 134 | assertNull("getFieldName", copy.getFieldName(labels1.length)); |
cbbcc354 | 135 | } |
136 | ||
6e1886bc | 137 | @Test |
9ee9135e | 138 | public void testCopyConstructorCornerCases() { |
cbbcc354 | 139 | try { |
9ee9135e | 140 | new TmfEventType(null); |
75d42a16 | 141 | fail("TmfEventType: null argument"); |
085d898f | 142 | } catch (final IllegalArgumentException e) { |
cbbcc354 | 143 | } |
144 | } | |
145 | ||
9ee9135e FC |
146 | // ------------------------------------------------------------------------ |
147 | // clone | |
148 | // ------------------------------------------------------------------------ | |
149 | ||
6e1886bc | 150 | @Test |
54a7a54c | 151 | public void testClone() { |
085d898f | 152 | final ITmfEventType clone = fType1.clone(); |
de126dbb FC |
153 | |
154 | assertTrue("clone", fType1.clone().equals(fType1)); | |
155 | assertTrue("clone", clone.clone().equals(clone)); | |
156 | ||
157 | assertEquals("clone", clone, fType1); | |
9ee9135e FC |
158 | assertEquals("clone", fType1, clone); |
159 | } | |
160 | ||
6e1886bc | 161 | @Test |
54a7a54c | 162 | public void testClone2() { |
085d898f FC |
163 | final ITmfEventType type = new TmfEventType(); |
164 | final ITmfEventType clone = type.clone(); | |
de126dbb FC |
165 | |
166 | assertTrue("clone", type.clone().equals(type)); | |
167 | assertTrue("clone", clone.clone().equals(clone)); | |
168 | ||
9ee9135e | 169 | assertEquals("clone", clone, type); |
de126dbb | 170 | assertEquals("clone", type, clone); |
9ee9135e FC |
171 | } |
172 | ||
173 | // ------------------------------------------------------------------------ | |
174 | // hashCode | |
175 | // ------------------------------------------------------------------------ | |
176 | ||
6e1886bc | 177 | @Test |
54a7a54c | 178 | public void testHashCode() { |
085d898f | 179 | final TmfEventType copy1 = new TmfEventType(fType0); |
9ee9135e FC |
180 | |
181 | assertTrue("hashCode", fType0.hashCode() == copy1.hashCode()); | |
182 | assertTrue("hashCode", fType0.hashCode() != fType3.hashCode()); | |
183 | } | |
184 | ||
cbbcc354 | 185 | // ------------------------------------------------------------------------ |
186 | // equals | |
187 | // ------------------------------------------------------------------------ | |
188 | ||
6e1886bc | 189 | @Test |
54a7a54c | 190 | public void testEqualsReflexivity() { |
cbbcc354 | 191 | assertTrue("equals", fType0.equals(fType0)); |
192 | assertTrue("equals", fType3.equals(fType3)); | |
193 | ||
39f9eadb FC |
194 | assertFalse("equals", fType0.equals(fType3)); |
195 | assertFalse("equals", fType3.equals(fType0)); | |
cbbcc354 | 196 | } |
197 | ||
6e1886bc | 198 | @Test |
54a7a54c | 199 | public void testEqualsSymmetry() { |
085d898f | 200 | final TmfEventType copy0 = new TmfEventType(fType0); |
9ee9135e FC |
201 | assertTrue("equals", fType0.equals(copy0)); |
202 | assertTrue("equals", copy0.equals(fType0)); | |
085d898f FC |
203 | |
204 | final TmfEventType copy1 = new TmfEventType(fType1); | |
9ee9135e FC |
205 | assertTrue("equals", fType1.equals(copy1)); |
206 | assertTrue("equals", copy1.equals(fType1)); | |
085d898f FC |
207 | |
208 | final TmfEventType copy2 = new TmfEventType(fType2); | |
9ee9135e FC |
209 | assertTrue("equals", fType2.equals(copy2)); |
210 | assertTrue("equals", copy2.equals(fType2)); | |
cbbcc354 | 211 | } |
d18dd09b | 212 | |
6e1886bc | 213 | @Test |
54a7a54c | 214 | public void testEqualsTransivity() { |
9ee9135e FC |
215 | TmfEventType copy1 = new TmfEventType(fType1); |
216 | TmfEventType copy2 = new TmfEventType(copy1); | |
217 | assertTrue("equals", fType1.equals(copy1)); | |
218 | assertTrue("equals", copy1.equals(copy2)); | |
219 | assertTrue("equals", fType1.equals(copy2)); | |
220 | ||
221 | copy1 = new TmfEventType(fType2); | |
222 | copy2 = new TmfEventType(copy1); | |
223 | assertTrue("equals", fType2.equals(copy1)); | |
224 | assertTrue("equals", copy1.equals(copy2)); | |
225 | assertTrue("equals", fType2.equals(copy2)); | |
226 | ||
227 | copy1 = new TmfEventType(fType3); | |
228 | copy2 = new TmfEventType(copy1); | |
229 | assertTrue("equals", fType3.equals(copy1)); | |
230 | assertTrue("equals", copy1.equals(copy2)); | |
231 | assertTrue("equals", fType3.equals(copy2)); | |
cbbcc354 | 232 | } |
233 | ||
6e1886bc | 234 | @Test |
54a7a54c | 235 | public void testEqualsNull() { |
9ee9135e FC |
236 | assertFalse("equals", fType0.equals(null)); |
237 | assertFalse("equals", fType3.equals(null)); | |
cbbcc354 | 238 | } |
239 | ||
6e1886bc | 240 | @Test |
54a7a54c | 241 | public void testNonEquals() { |
9ee9135e FC |
242 | assertFalse("equals", fType0.equals(fType1)); |
243 | assertFalse("equals", fType1.equals(fType2)); | |
244 | assertFalse("equals", fType2.equals(fType3)); | |
245 | assertFalse("equals", fType3.equals(fType0)); | |
cbbcc354 | 246 | } |
085d898f | 247 | |
6e1886bc | 248 | @Test |
54a7a54c | 249 | public void testNonEqualsClasses() { |
9ee9135e FC |
250 | assertFalse("equals", fType1.equals(fLabels1)); |
251 | } | |
085d898f | 252 | |
cbd4ad82 | 253 | // ------------------------------------------------------------------------ |
cbbcc354 | 254 | // toString |
cbd4ad82 | 255 | // ------------------------------------------------------------------------ |
d18dd09b | 256 | |
6e1886bc | 257 | @Test |
cbbcc354 | 258 | public void testToString() { |
9b749023 AM |
259 | final String expected1 = "TmfEventType [fContext=" + ITmfEventType.DEFAULT_CONTEXT_ID + |
260 | ", fTypeId=" + ITmfEventType.DEFAULT_TYPE_ID + "]"; | |
085d898f | 261 | final TmfEventType type1 = new TmfEventType(); |
cbbcc354 | 262 | assertEquals("toString", expected1, type1.toString()); |
263 | ||
085d898f FC |
264 | final String expected2 = "TmfEventType [fContext=" + fContext1 + ", fTypeId=" + fTypeId1 + "]"; |
265 | final TmfEventType type2 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1)); | |
cbbcc354 | 266 | assertEquals("toString", expected2, type2.toString()); |
267 | } | |
d18dd09b ASL |
268 | |
269 | } |