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 testHashCode() {
152 final TmfEventType copy1
= new TmfEventType(fType0
);
154 assertTrue("hashCode", fType0
.hashCode() == copy1
.hashCode());
155 assertTrue("hashCode", fType0
.hashCode() != fType3
.hashCode());
158 // ------------------------------------------------------------------------
160 // ------------------------------------------------------------------------
163 public void testEqualsReflexivity() {
164 assertTrue("equals", fType0
.equals(fType0
));
165 assertTrue("equals", fType3
.equals(fType3
));
167 assertFalse("equals", fType0
.equals(fType3
));
168 assertFalse("equals", fType3
.equals(fType0
));
172 public void testEqualsSymmetry() {
173 final TmfEventType copy0
= new TmfEventType(fType0
);
174 assertTrue("equals", fType0
.equals(copy0
));
175 assertTrue("equals", copy0
.equals(fType0
));
177 final TmfEventType copy1
= new TmfEventType(fType1
);
178 assertTrue("equals", fType1
.equals(copy1
));
179 assertTrue("equals", copy1
.equals(fType1
));
181 final TmfEventType copy2
= new TmfEventType(fType2
);
182 assertTrue("equals", fType2
.equals(copy2
));
183 assertTrue("equals", copy2
.equals(fType2
));
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
));
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
));
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
));
208 public void testEqualsNull() {
209 assertFalse("equals", fType0
.equals(null));
210 assertFalse("equals", fType3
.equals(null));
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
));
222 public void testNonEqualsClasses() {
223 assertFalse("equals", fType1
.equals(fLabels1
));
226 // ------------------------------------------------------------------------
228 // ------------------------------------------------------------------------
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());
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());