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