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