Merge commit 'e1de8d2d152352eded708615a967021db7800709' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / event / TmfEventTypeTest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
14
15 package org.eclipse.linuxtools.tmf.core.tests.event;
16
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;
22
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
24 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
25 import org.eclipse.linuxtools.tmf.core.event.TmfEventType;
26 import org.junit.Test;
27
28 /**
29 * Test suite for the TmfEventType class.
30 */
31 @SuppressWarnings("javadoc")
32 public class TmfEventTypeTest {
33
34 // ------------------------------------------------------------------------
35 // Variables
36 // ------------------------------------------------------------------------
37
38 private final String fContext1 = "JUnit context 1";
39 private final String fContext2 = "JUnit context 2";
40
41 private final String fTypeId1 = "Some type";
42 private final String fTypeId2 = "Some other type";
43
44 private final String fLabel0 = "label1";
45 private final String fLabel1 = "label2";
46
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 };
50
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));
55
56 // ------------------------------------------------------------------------
57 // Constructors
58 // ------------------------------------------------------------------------
59
60 @Test
61 public void testDefaultConstructor() {
62 final ITmfEventType type = new TmfEventType();
63 assertEquals("getContext", ITmfEventType.DEFAULT_CONTEXT_ID, type.getContext());
64 assertEquals("getName", ITmfEventType.DEFAULT_TYPE_ID, type.getName());
65 assertNull("getRootField", type.getRootField());
66 assertEquals("getFieldNames", 0, type.getFieldNames().length);
67 assertNull("getFieldName", type.getFieldName(0));
68 }
69
70 @Test
71 public void testFullConstructor() {
72 final ITmfEventType type0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0));
73 assertEquals("getContext", fContext1, type0.getContext());
74 assertEquals("getName", fTypeId1, type0.getName());
75 assertEquals("getRootField", TmfEventField.makeRoot(fLabels0), type0.getRootField());
76 final String[] labels0 = type0.getFieldNames();
77 assertEquals("getFieldNames length", fLabels0.length, labels0.length);
78 for (int i = 0; i < labels0.length; i++) {
79 assertEquals("getFieldNames", fLabels0[i], labels0[i]);
80 }
81 assertNull("getFieldName", type0.getFieldName(labels0.length));
82
83 final ITmfEventType type1 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1));
84 assertEquals("getContext", fContext1, type1.getContext());
85 assertEquals("getName", fTypeId1, type1.getName());
86 assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), type1.getRootField());
87 final String[] labels1 = type1.getFieldNames();
88 assertEquals("getFieldNames length", fLabels1.length, labels1.length);
89 for (int i = 0; i < labels1.length; i++) {
90 assertEquals("getFieldNames", fLabels1[i], labels1[i]);
91 }
92 assertNull("getFieldName", type1.getFieldName(labels1.length));
93
94 final ITmfEventType type2 = new TmfEventType(fContext2, fTypeId2, TmfEventField.makeRoot(fLabels2));
95 assertEquals("getContext", fContext2, type2.getContext());
96 assertEquals("getName", fTypeId2, type2.getName());
97 assertEquals("getRootField", TmfEventField.makeRoot(fLabels2), type2.getRootField());
98 final String[] labels2 = type2.getFieldNames();
99 assertEquals("getFieldNames length", fLabels2.length, labels2.length);
100 for (int i = 0; i < labels2.length; i++) {
101 assertEquals("getFieldNames", fLabels2[i], labels2[i]);
102 }
103 assertNull("getFieldName", type2.getFieldName(labels2.length));
104 }
105
106 @Test
107 public void testConstructorCornerCases() {
108 try {
109 new TmfEventType(null, fTypeId1, null);
110 fail("TmfEventType: null context");
111 } catch (final IllegalArgumentException e) {
112 }
113
114 try {
115 new TmfEventType(fContext1, null, null);
116 fail("TmfEventType: null type");
117 } catch (final IllegalArgumentException e) {
118 }
119 }
120
121 @Test
122 public void testCopyConstructor() {
123 final TmfEventType original = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1));
124 final TmfEventType copy = new TmfEventType(original);
125
126 assertEquals("getContext", fContext1, copy.getContext());
127 assertEquals("getName", fTypeId1, copy.getName());
128 assertEquals("getRootField", TmfEventField.makeRoot(fLabels1), copy.getRootField());
129 final String[] labels1 = copy.getFieldNames();
130 assertEquals("getFieldNames length", fLabels1.length, labels1.length);
131 for (int i = 0; i < labels1.length; i++) {
132 assertEquals("getFieldNames", fLabels1[i], labels1[i]);
133 }
134 assertNull("getFieldName", copy.getFieldName(labels1.length));
135 }
136
137 @Test
138 public void testCopyConstructorCornerCases() {
139 try {
140 new TmfEventType(null);
141 fail("TmfEventType: null argument");
142 } catch (final IllegalArgumentException e) {
143 }
144 }
145
146 // ------------------------------------------------------------------------
147 // hashCode
148 // ------------------------------------------------------------------------
149
150 @Test
151 public void testHashCode() {
152 final TmfEventType copy1 = new TmfEventType(fType0);
153
154 assertTrue("hashCode", fType0.hashCode() == copy1.hashCode());
155 assertTrue("hashCode", fType0.hashCode() != fType3.hashCode());
156 }
157
158 // ------------------------------------------------------------------------
159 // equals
160 // ------------------------------------------------------------------------
161
162 @Test
163 public void testEqualsReflexivity() {
164 assertTrue("equals", fType0.equals(fType0));
165 assertTrue("equals", fType3.equals(fType3));
166
167 assertFalse("equals", fType0.equals(fType3));
168 assertFalse("equals", fType3.equals(fType0));
169 }
170
171 @Test
172 public void testEqualsSymmetry() {
173 final TmfEventType copy0 = new TmfEventType(fType0);
174 assertTrue("equals", fType0.equals(copy0));
175 assertTrue("equals", copy0.equals(fType0));
176
177 final TmfEventType copy1 = new TmfEventType(fType1);
178 assertTrue("equals", fType1.equals(copy1));
179 assertTrue("equals", copy1.equals(fType1));
180
181 final TmfEventType copy2 = new TmfEventType(fType2);
182 assertTrue("equals", fType2.equals(copy2));
183 assertTrue("equals", copy2.equals(fType2));
184 }
185
186 @Test
187 public void testEqualsTransivity() {
188 TmfEventType copy1 = new TmfEventType(fType1);
189 TmfEventType copy2 = new TmfEventType(copy1);
190 assertTrue("equals", fType1.equals(copy1));
191 assertTrue("equals", copy1.equals(copy2));
192 assertTrue("equals", fType1.equals(copy2));
193
194 copy1 = new TmfEventType(fType2);
195 copy2 = new TmfEventType(copy1);
196 assertTrue("equals", fType2.equals(copy1));
197 assertTrue("equals", copy1.equals(copy2));
198 assertTrue("equals", fType2.equals(copy2));
199
200 copy1 = new TmfEventType(fType3);
201 copy2 = new TmfEventType(copy1);
202 assertTrue("equals", fType3.equals(copy1));
203 assertTrue("equals", copy1.equals(copy2));
204 assertTrue("equals", fType3.equals(copy2));
205 }
206
207 @Test
208 public void testEqualsNull() {
209 assertFalse("equals", fType0.equals(null));
210 assertFalse("equals", fType3.equals(null));
211 }
212
213 @Test
214 public void testNonEquals() {
215 assertFalse("equals", fType0.equals(fType1));
216 assertFalse("equals", fType1.equals(fType2));
217 assertFalse("equals", fType2.equals(fType3));
218 assertFalse("equals", fType3.equals(fType0));
219 }
220
221 @Test
222 public void testNonEqualsClasses() {
223 assertFalse("equals", fType1.equals(fLabels1));
224 }
225
226 // ------------------------------------------------------------------------
227 // toString
228 // ------------------------------------------------------------------------
229
230 @Test
231 public void testToString() {
232 final String expected1 = "TmfEventType [fContext=" + ITmfEventType.DEFAULT_CONTEXT_ID +
233 ", fTypeId=" + ITmfEventType.DEFAULT_TYPE_ID + "]";
234 final TmfEventType type1 = new TmfEventType();
235 assertEquals("toString", expected1, type1.toString());
236
237 final String expected2 = "TmfEventType [fContext=" + fContext1 + ", fTypeId=" + fTypeId1 + "]";
238 final TmfEventType type2 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels1));
239 assertEquals("toString", expected2, type2.toString());
240 }
241
242 }
This page took 0.035898 seconds and 5 git commands to generate.