1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Francois Chouinard - Adjusted for new Event Model
12 * Alexandre Montplaisir - Port to JUnit4
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
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
;
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
;
29 * Test suite for the TmfEventType class.
31 @SuppressWarnings("javadoc")
32 public class TmfEventTypeTest
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private final String fContext1
= "JUnit context 1";
39 private final String fContext2
= "JUnit context 2";
41 private final String fTypeId1
= "Some type";
42 private final String fTypeId2
= "Some other type";
44 private final String fLabel0
= "label1";
45 private final String fLabel1
= "label2";
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
};
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
));
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
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));
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
]);
81 assertNull("getFieldName", type0
.getFieldName(labels0
.length
));
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
]);
92 assertNull("getFieldName", type1
.getFieldName(labels1
.length
));
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
]);
103 assertNull("getFieldName", type2
.getFieldName(labels2
.length
));
107 public void testConstructorCornerCases() {
109 new TmfEventType(null, fTypeId1
, null);
110 fail("TmfEventType: null context");
111 } catch (final IllegalArgumentException e
) {
115 new TmfEventType(fContext1
, null, null);
116 fail("TmfEventType: null type");
117 } catch (final IllegalArgumentException e
) {
122 public void testCopyConstructor() {
123 final TmfEventType original
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
124 final TmfEventType copy
= new TmfEventType(original
);
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
]);
134 assertNull("getFieldName", copy
.getFieldName(labels1
.length
));
138 public void testCopyConstructorCornerCases() {
140 new TmfEventType(null);
141 fail("TmfEventType: null argument");
142 } catch (final IllegalArgumentException e
) {
146 // ------------------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 public void testClone() {
152 final ITmfEventType clone
= fType1
.clone();
154 assertTrue("clone", fType1
.clone().equals(fType1
));
155 assertTrue("clone", clone
.clone().equals(clone
));
157 assertEquals("clone", clone
, fType1
);
158 assertEquals("clone", fType1
, clone
);
162 public void testClone2() {
163 final ITmfEventType type
= new TmfEventType();
164 final ITmfEventType clone
= type
.clone();
166 assertTrue("clone", type
.clone().equals(type
));
167 assertTrue("clone", clone
.clone().equals(clone
));
169 assertEquals("clone", clone
, type
);
170 assertEquals("clone", type
, clone
);
173 // ------------------------------------------------------------------------
175 // ------------------------------------------------------------------------
178 public void testHashCode() {
179 final TmfEventType copy1
= new TmfEventType(fType0
);
181 assertTrue("hashCode", fType0
.hashCode() == copy1
.hashCode());
182 assertTrue("hashCode", fType0
.hashCode() != fType3
.hashCode());
185 // ------------------------------------------------------------------------
187 // ------------------------------------------------------------------------
190 public void testEqualsReflexivity() {
191 assertTrue("equals", fType0
.equals(fType0
));
192 assertTrue("equals", fType3
.equals(fType3
));
194 assertFalse("equals", fType0
.equals(fType3
));
195 assertFalse("equals", fType3
.equals(fType0
));
199 public void testEqualsSymmetry() {
200 final TmfEventType copy0
= new TmfEventType(fType0
);
201 assertTrue("equals", fType0
.equals(copy0
));
202 assertTrue("equals", copy0
.equals(fType0
));
204 final TmfEventType copy1
= new TmfEventType(fType1
);
205 assertTrue("equals", fType1
.equals(copy1
));
206 assertTrue("equals", copy1
.equals(fType1
));
208 final TmfEventType copy2
= new TmfEventType(fType2
);
209 assertTrue("equals", fType2
.equals(copy2
));
210 assertTrue("equals", copy2
.equals(fType2
));
214 public void testEqualsTransivity() {
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
));
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
));
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
));
235 public void testEqualsNull() {
236 assertFalse("equals", fType0
.equals(null));
237 assertFalse("equals", fType3
.equals(null));
241 public void testNonEquals() {
242 assertFalse("equals", fType0
.equals(fType1
));
243 assertFalse("equals", fType1
.equals(fType2
));
244 assertFalse("equals", fType2
.equals(fType3
));
245 assertFalse("equals", fType3
.equals(fType0
));
249 public void testNonEqualsClasses() {
250 assertFalse("equals", fType1
.equals(fLabels1
));
253 // ------------------------------------------------------------------------
255 // ------------------------------------------------------------------------
258 public void testToString() {
259 final String expected1
= "TmfEventType [fContext=" + ITmfEventType
.DEFAULT_CONTEXT_ID
+
260 ", fTypeId=" + ITmfEventType
.DEFAULT_TYPE_ID
+ "]";
261 final TmfEventType type1
= new TmfEventType();
262 assertEquals("toString", expected1
, type1
.toString());
264 final String expected2
= "TmfEventType [fContext=" + fContext1
+ ", fTypeId=" + fTypeId1
+ "]";
265 final TmfEventType type2
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
266 assertEquals("toString", expected2
, type2
.toString());