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
.tracecompass
.tmf
.core
.tests
.event
;
17 import static org
.junit
.Assert
.assertArrayEquals
;
18 import static org
.junit
.Assert
.assertEquals
;
19 import static org
.junit
.Assert
.assertFalse
;
20 import static org
.junit
.Assert
.assertNull
;
21 import static org
.junit
.Assert
.assertTrue
;
22 import static org
.junit
.Assert
.fail
;
24 import java
.util
.Collection
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventType
;
29 import org
.junit
.Test
;
32 * Test suite for the TmfEventType class.
34 @SuppressWarnings("javadoc")
35 public class TmfEventTypeTest
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 private final String fContext1
= "JUnit context 1";
42 private final String fContext2
= "JUnit context 2";
44 private final String fTypeId1
= "Some type";
45 private final String fTypeId2
= "Some other type";
47 private final String fLabel0
= "label1";
48 private final String fLabel1
= "label2";
49 private final String fLabel2
= "label3";
51 private final String
[] fLabels0
= new String
[] { };
52 private final String
[] fLabels1
= new String
[] { fLabel0
, fLabel1
};
53 private final String
[] fLabels2
= new String
[] { fLabel1
, fLabel0
, fLabel2
};
55 private final ITmfEventType fType0
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels0
));
56 private final ITmfEventType fType1
= new TmfEventType(fContext1
, fTypeId2
, TmfEventField
.makeRoot(fLabels1
));
57 private final ITmfEventType fType2
= new TmfEventType(fContext2
, fTypeId1
, TmfEventField
.makeRoot(fLabels2
));
58 private final ITmfEventType fType3
= new TmfEventType(fContext2
, fTypeId2
, TmfEventField
.makeRoot(fLabels1
));
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 public void testDefaultConstructor() {
66 final ITmfEventType type
= new TmfEventType();
67 assertEquals("getContext", ITmfEventType
.DEFAULT_CONTEXT_ID
, type
.getContext());
68 assertEquals("getName", ITmfEventType
.DEFAULT_TYPE_ID
, type
.getName());
69 assertNull("getRootField", type
.getRootField());
70 assertEquals("getFieldNames", 0, type
.getFieldNames().size());
74 public void testFullConstructor() {
75 final ITmfEventType type0
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels0
));
76 assertEquals("getContext", fContext1
, type0
.getContext());
77 assertEquals("getName", fTypeId1
, type0
.getName());
78 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels0
), type0
.getRootField());
79 final Collection
<String
> labels0
= type0
.getFieldNames();
80 assertEquals("getFieldNames length", fLabels0
.length
, labels0
.size());
81 assertArrayEquals(fLabels0
, labels0
.toArray(new String
[labels0
.size()]));
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 Collection
<String
> labels1
= type1
.getFieldNames();
88 assertEquals("getFieldNames length", fLabels1
.length
, labels1
.size());
89 assertArrayEquals(fLabels1
, labels1
.toArray(new String
[labels1
.size()]));
91 final ITmfEventType type2
= new TmfEventType(fContext2
, fTypeId2
, TmfEventField
.makeRoot(fLabels2
));
92 assertEquals("getContext", fContext2
, type2
.getContext());
93 assertEquals("getName", fTypeId2
, type2
.getName());
94 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels2
), type2
.getRootField());
95 final Collection
<String
> labels2
= type2
.getFieldNames();
96 assertEquals("getFieldNames length", fLabels2
.length
, labels2
.size());
97 assertArrayEquals(fLabels2
, labels2
.toArray(new String
[labels2
.size()]));
101 public void testConstructorCornerCases() {
103 new TmfEventType(null, fTypeId1
, null);
104 fail("TmfEventType: null context");
105 } catch (final IllegalArgumentException e
) {
109 new TmfEventType(fContext1
, null, null);
110 fail("TmfEventType: null type");
111 } catch (final IllegalArgumentException e
) {
116 public void testCopyConstructor() {
117 final TmfEventType original
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
118 final TmfEventType copy
= new TmfEventType(original
);
120 assertEquals("getContext", fContext1
, copy
.getContext());
121 assertEquals("getName", fTypeId1
, copy
.getName());
122 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels1
), copy
.getRootField());
123 final Collection
<String
> labels1
= copy
.getFieldNames();
124 assertEquals("getFieldNames length", fLabels1
.length
, labels1
.size());
125 assertArrayEquals(fLabels1
, labels1
.toArray(new String
[labels1
.size()]));
129 public void testCopyConstructorCornerCases() {
131 new TmfEventType(null);
132 fail("TmfEventType: null argument");
133 } catch (final IllegalArgumentException e
) {
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
142 public void testHashCode() {
143 final TmfEventType copy1
= new TmfEventType(fType0
);
145 assertTrue("hashCode", fType0
.hashCode() == copy1
.hashCode());
146 assertTrue("hashCode", fType0
.hashCode() != fType3
.hashCode());
149 // ------------------------------------------------------------------------
151 // ------------------------------------------------------------------------
154 public void testEqualsReflexivity() {
155 assertTrue("equals", fType0
.equals(fType0
));
156 assertTrue("equals", fType3
.equals(fType3
));
158 assertFalse("equals", fType0
.equals(fType3
));
159 assertFalse("equals", fType3
.equals(fType0
));
163 public void testEqualsSymmetry() {
164 final TmfEventType copy0
= new TmfEventType(fType0
);
165 assertTrue("equals", fType0
.equals(copy0
));
166 assertTrue("equals", copy0
.equals(fType0
));
168 final TmfEventType copy1
= new TmfEventType(fType1
);
169 assertTrue("equals", fType1
.equals(copy1
));
170 assertTrue("equals", copy1
.equals(fType1
));
172 final TmfEventType copy2
= new TmfEventType(fType2
);
173 assertTrue("equals", fType2
.equals(copy2
));
174 assertTrue("equals", copy2
.equals(fType2
));
178 public void testEqualsTransivity() {
179 TmfEventType copy1
= new TmfEventType(fType1
);
180 TmfEventType copy2
= new TmfEventType(copy1
);
181 assertTrue("equals", fType1
.equals(copy1
));
182 assertTrue("equals", copy1
.equals(copy2
));
183 assertTrue("equals", fType1
.equals(copy2
));
185 copy1
= new TmfEventType(fType2
);
186 copy2
= new TmfEventType(copy1
);
187 assertTrue("equals", fType2
.equals(copy1
));
188 assertTrue("equals", copy1
.equals(copy2
));
189 assertTrue("equals", fType2
.equals(copy2
));
191 copy1
= new TmfEventType(fType3
);
192 copy2
= new TmfEventType(copy1
);
193 assertTrue("equals", fType3
.equals(copy1
));
194 assertTrue("equals", copy1
.equals(copy2
));
195 assertTrue("equals", fType3
.equals(copy2
));
199 public void testEqualsNull() {
200 assertFalse("equals", fType0
.equals(null));
201 assertFalse("equals", fType3
.equals(null));
205 public void testNonEquals() {
206 assertFalse("equals", fType0
.equals(fType1
));
207 assertFalse("equals", fType1
.equals(fType2
));
208 assertFalse("equals", fType2
.equals(fType3
));
209 assertFalse("equals", fType3
.equals(fType0
));
213 public void testNonEqualsClasses() {
214 assertFalse("equals", fType1
.equals(fLabels1
));
217 // ------------------------------------------------------------------------
219 // ------------------------------------------------------------------------
222 public void testToString() {
223 final String expected1
= "TmfEventType [fContext=" + ITmfEventType
.DEFAULT_CONTEXT_ID
+
224 ", fTypeId=" + ITmfEventType
.DEFAULT_TYPE_ID
+ "]";
225 final TmfEventType type1
= new TmfEventType();
226 assertEquals("toString", expected1
, type1
.toString());
228 final String expected2
= "TmfEventType [fContext=" + fContext1
+ ", fTypeId=" + fTypeId1
+ "]";
229 final TmfEventType type2
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
230 assertEquals("toString", expected2
, type2
.toString());