Commit | Line | Data |
---|---|---|
d18dd09b | 1 | /******************************************************************************* |
61759503 | 2 | * Copyright (c) 2009, 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 | |
b742c196 | 17 | import static org.junit.Assert.assertArrayEquals; |
6e1886bc AM |
18 | import static org.junit.Assert.assertEquals; |
19 | import static org.junit.Assert.assertFalse; | |
20 | import static org.junit.Assert.assertNull; | |
21 | import static org.junit.Assert.assertTrue; | |
22 | import static org.junit.Assert.fail; | |
cbd4ad82 | 23 | |
b742c196 AM |
24 | import java.util.Collection; |
25 | ||
9ee9135e | 26 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; |
4c564a2d | 27 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
6c13869b | 28 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; |
6e1886bc | 29 | import org.junit.Test; |
d18dd09b ASL |
30 | |
31 | /** | |
75d42a16 | 32 | * Test suite for the TmfEventType class. |
d18dd09b | 33 | */ |
cad06250 | 34 | @SuppressWarnings("javadoc") |
6e1886bc | 35 | public class TmfEventTypeTest { |
d18dd09b | 36 | |
cbd4ad82 | 37 | // ------------------------------------------------------------------------ |
cbbcc354 | 38 | // Variables |
cbd4ad82 FC |
39 | // ------------------------------------------------------------------------ |
40 | ||
9ee9135e FC |
41 | private final String fContext1 = "JUnit context 1"; |
42 | private final String fContext2 = "JUnit context 2"; | |
43 | ||
39f9eadb | 44 | private final String fTypeId1 = "Some type"; |
cbbcc354 | 45 | private final String fTypeId2 = "Some other type"; |
9ee9135e | 46 | |
cbbcc354 | 47 | private final String fLabel0 = "label1"; |
48 | private final String fLabel1 = "label2"; | |
b742c196 | 49 | private final String fLabel2 = "label3"; |
085d898f | 50 | |
39f9eadb FC |
51 | private final String[] fLabels0 = new String[] { }; |
52 | private final String[] fLabels1 = new String[] { fLabel0, fLabel1 }; | |
b742c196 | 53 | private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel2 }; |
d18dd09b | 54 | |
de126dbb FC |
55 | private final ITmfEventType fType0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0)); |
56 | private final ITmfEventType fType1 = new TmfEventType(fContext1, fTypeId2, TmfEventField.makeRoot(fLabels1)); | |
57 | private final ITmfEventType fType2 = new TmfEventType(fContext2, fTypeId1, TmfEventField.makeRoot(fLabels2)); | |
58 | private final ITmfEventType fType3 = new TmfEventType(fContext2, fTypeId2, TmfEventField.makeRoot(fLabels1)); | |
cbd4ad82 | 59 | |
cbbcc354 | 60 | // ------------------------------------------------------------------------ |
61 | // Constructors | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
6e1886bc | 64 | @Test |
9ee9135e | 65 | public void testDefaultConstructor() { |
085d898f | 66 | final ITmfEventType type = new TmfEventType(); |
9b749023 AM |
67 | assertEquals("getContext", ITmfEventType.DEFAULT_CONTEXT_ID, type.getContext()); |
68 | assertEquals("getName", ITmfEventType.DEFAULT_TYPE_ID, type.getName()); | |
9ee9135e | 69 | assertNull("getRootField", type.getRootField()); |
b742c196 | 70 | assertEquals("getFieldNames", 0, type.getFieldNames().size()); |
9ee9135e FC |
71 | } |
72 | ||
6e1886bc | 73 | @Test |
9ee9135e | 74 | public void testFullConstructor() { |
085d898f | 75 | final ITmfEventType type0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0)); |
9ee9135e FC |
76 | assertEquals("getContext", fContext1, type0.getContext()); |
77 | assertEquals("getName", fTypeId1, type0.getName()); | |
78 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels0), type0.getRootField()); | |
b742c196 AM |
79 | final Collection<String> labels0 = type0.getFieldNames(); |
80 | assertEquals("getFieldNames length", fLabels0.length, labels0.size()); | |
81 | assertArrayEquals(fLabels0, labels0.toArray(new String[labels0.size()])); | |
9ee9135e | 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()); | |
b742c196 AM |
87 | final Collection<String> labels1 = type1.getFieldNames(); |
88 | assertEquals("getFieldNames length", fLabels1.length, labels1.size()); | |
89 | assertArrayEquals(fLabels1, labels1.toArray(new String[labels1.size()])); | |
9ee9135e | 90 | |
085d898f | 91 | final ITmfEventType type2 = new TmfEventType(fContext2, fTypeId2, TmfEventField.makeRoot(fLabels2)); |
9ee9135e FC |
92 | assertEquals("getContext", fContext2, type2.getContext()); |
93 | assertEquals("getName", fTypeId2, type2.getName()); | |
94 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels2), type2.getRootField()); | |
b742c196 AM |
95 | final Collection<String> labels2 = type2.getFieldNames(); |
96 | assertEquals("getFieldNames length", fLabels2.length, labels2.size()); | |
97 | assertArrayEquals(fLabels2, labels2.toArray(new String[labels2.size()])); | |
9ee9135e FC |
98 | } |
99 | ||
6e1886bc | 100 | @Test |
9ee9135e | 101 | public void testConstructorCornerCases() { |
cbbcc354 | 102 | try { |
9ee9135e FC |
103 | new TmfEventType(null, fTypeId1, null); |
104 | fail("TmfEventType: null context"); | |
085d898f | 105 | } catch (final IllegalArgumentException e) { |
cbbcc354 | 106 | } |
cbbcc354 | 107 | |
cbbcc354 | 108 | try { |
9ee9135e FC |
109 | new TmfEventType(fContext1, null, null); |
110 | fail("TmfEventType: null type"); | |
085d898f | 111 | } catch (final IllegalArgumentException e) { |
cbbcc354 | 112 | } |
113 | } | |
114 | ||
6e1886bc | 115 | @Test |
9ee9135e | 116 | public void testCopyConstructor() { |
085d898f FC |
117 | final TmfEventType original = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1)); |
118 | final TmfEventType copy = new TmfEventType(original); | |
cbbcc354 | 119 | |
9ee9135e FC |
120 | assertEquals("getContext", fContext1, copy.getContext()); |
121 | assertEquals("getName", fTypeId1, copy.getName()); | |
122 | assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), copy.getRootField()); | |
b742c196 AM |
123 | final Collection<String> labels1 = copy.getFieldNames(); |
124 | assertEquals("getFieldNames length", fLabels1.length, labels1.size()); | |
125 | assertArrayEquals(fLabels1, labels1.toArray(new String[labels1.size()])); | |
cbbcc354 | 126 | } |
127 | ||
6e1886bc | 128 | @Test |
9ee9135e | 129 | public void testCopyConstructorCornerCases() { |
cbbcc354 | 130 | try { |
9ee9135e | 131 | new TmfEventType(null); |
75d42a16 | 132 | fail("TmfEventType: null argument"); |
085d898f | 133 | } catch (final IllegalArgumentException e) { |
cbbcc354 | 134 | } |
135 | } | |
136 | ||
9ee9135e FC |
137 | // ------------------------------------------------------------------------ |
138 | // hashCode | |
139 | // ------------------------------------------------------------------------ | |
140 | ||
6e1886bc | 141 | @Test |
54a7a54c | 142 | public void testHashCode() { |
085d898f | 143 | final TmfEventType copy1 = new TmfEventType(fType0); |
9ee9135e FC |
144 | |
145 | assertTrue("hashCode", fType0.hashCode() == copy1.hashCode()); | |
146 | assertTrue("hashCode", fType0.hashCode() != fType3.hashCode()); | |
147 | } | |
148 | ||
cbbcc354 | 149 | // ------------------------------------------------------------------------ |
150 | // equals | |
151 | // ------------------------------------------------------------------------ | |
152 | ||
6e1886bc | 153 | @Test |
54a7a54c | 154 | public void testEqualsReflexivity() { |
cbbcc354 | 155 | assertTrue("equals", fType0.equals(fType0)); |
156 | assertTrue("equals", fType3.equals(fType3)); | |
157 | ||
39f9eadb FC |
158 | assertFalse("equals", fType0.equals(fType3)); |
159 | assertFalse("equals", fType3.equals(fType0)); | |
cbbcc354 | 160 | } |
161 | ||
6e1886bc | 162 | @Test |
54a7a54c | 163 | public void testEqualsSymmetry() { |
085d898f | 164 | final TmfEventType copy0 = new TmfEventType(fType0); |
9ee9135e FC |
165 | assertTrue("equals", fType0.equals(copy0)); |
166 | assertTrue("equals", copy0.equals(fType0)); | |
085d898f FC |
167 | |
168 | final TmfEventType copy1 = new TmfEventType(fType1); | |
9ee9135e FC |
169 | assertTrue("equals", fType1.equals(copy1)); |
170 | assertTrue("equals", copy1.equals(fType1)); | |
085d898f FC |
171 | |
172 | final TmfEventType copy2 = new TmfEventType(fType2); | |
9ee9135e FC |
173 | assertTrue("equals", fType2.equals(copy2)); |
174 | assertTrue("equals", copy2.equals(fType2)); | |
cbbcc354 | 175 | } |
d18dd09b | 176 | |
6e1886bc | 177 | @Test |
54a7a54c | 178 | public void testEqualsTransivity() { |
9ee9135e FC |
179 | TmfEventType copy1 = new TmfEventType(fType1); |
180 | TmfEventType copy2 = new TmfEventType(copy1); | |
181 | assertTrue("equals", fType1.equals(copy1)); | |
182 | assertTrue("equals", copy1.equals(copy2)); | |
183 | assertTrue("equals", fType1.equals(copy2)); | |
184 | ||
185 | copy1 = new TmfEventType(fType2); | |
186 | copy2 = new TmfEventType(copy1); | |
187 | assertTrue("equals", fType2.equals(copy1)); | |
188 | assertTrue("equals", copy1.equals(copy2)); | |
189 | assertTrue("equals", fType2.equals(copy2)); | |
190 | ||
191 | copy1 = new TmfEventType(fType3); | |
192 | copy2 = new TmfEventType(copy1); | |
193 | assertTrue("equals", fType3.equals(copy1)); | |
194 | assertTrue("equals", copy1.equals(copy2)); | |
195 | assertTrue("equals", fType3.equals(copy2)); | |
cbbcc354 | 196 | } |
197 | ||
6e1886bc | 198 | @Test |
54a7a54c | 199 | public void testEqualsNull() { |
9ee9135e FC |
200 | assertFalse("equals", fType0.equals(null)); |
201 | assertFalse("equals", fType3.equals(null)); | |
cbbcc354 | 202 | } |
203 | ||
6e1886bc | 204 | @Test |
54a7a54c | 205 | public void testNonEquals() { |
9ee9135e FC |
206 | assertFalse("equals", fType0.equals(fType1)); |
207 | assertFalse("equals", fType1.equals(fType2)); | |
208 | assertFalse("equals", fType2.equals(fType3)); | |
209 | assertFalse("equals", fType3.equals(fType0)); | |
cbbcc354 | 210 | } |
085d898f | 211 | |
6e1886bc | 212 | @Test |
54a7a54c | 213 | public void testNonEqualsClasses() { |
9ee9135e FC |
214 | assertFalse("equals", fType1.equals(fLabels1)); |
215 | } | |
085d898f | 216 | |
cbd4ad82 | 217 | // ------------------------------------------------------------------------ |
cbbcc354 | 218 | // toString |
cbd4ad82 | 219 | // ------------------------------------------------------------------------ |
d18dd09b | 220 | |
6e1886bc | 221 | @Test |
cbbcc354 | 222 | public void testToString() { |
9b749023 AM |
223 | final String expected1 = "TmfEventType [fContext=" + ITmfEventType.DEFAULT_CONTEXT_ID + |
224 | ", fTypeId=" + ITmfEventType.DEFAULT_TYPE_ID + "]"; | |
085d898f | 225 | final TmfEventType type1 = new TmfEventType(); |
cbbcc354 | 226 | assertEquals("toString", expected1, type1.toString()); |
227 | ||
085d898f FC |
228 | final String expected2 = "TmfEventType [fContext=" + fContext1 + ", fTypeId=" + fTypeId1 + "]"; |
229 | final TmfEventType type2 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1)); | |
cbbcc354 | 230 | assertEquals("toString", expected2, type2.toString()); |
231 | } | |
d18dd09b ASL |
232 | |
233 | } |