1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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 fTypeId1
= "Some type";
42 private final String fTypeId2
= "Some other type";
44 private final String fLabel0
= "label1";
45 private final String fLabel1
= "label2";
46 private final String fLabel2
= "label3";
48 private final String
[] fLabels0
= new String
[] { };
49 private final String
[] fLabels1
= new String
[] { fLabel0
, fLabel1
};
50 private final String
[] fLabels2
= new String
[] { fLabel1
, fLabel0
, fLabel2
};
52 private final ITmfEventType fType0
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels0
));
53 private final ITmfEventType fType1
= new TmfEventType(fTypeId2
, TmfEventField
.makeRoot(fLabels1
));
54 private final ITmfEventType fType2
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels2
));
55 private final ITmfEventType fType3
= new TmfEventType(fTypeId2
, TmfEventField
.makeRoot(fLabels1
));
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 public void testDefaultConstructor() {
63 final ITmfEventType type
= new TmfEventType();
64 assertEquals("getName", ITmfEventType
.DEFAULT_TYPE_ID
, type
.getName());
65 assertNull("getRootField", type
.getRootField());
66 assertEquals("getFieldNames", 0, type
.getFieldNames().size());
70 public void testFullConstructor() {
71 final ITmfEventType type0
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels0
));
72 assertEquals("getName", fTypeId1
, type0
.getName());
73 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels0
), type0
.getRootField());
74 final Collection
<String
> labels0
= type0
.getFieldNames();
75 assertEquals("getFieldNames length", fLabels0
.length
, labels0
.size());
76 assertArrayEquals(fLabels0
, labels0
.toArray(new String
[labels0
.size()]));
78 final ITmfEventType type1
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
79 assertEquals("getName", fTypeId1
, type1
.getName());
80 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels1
), type1
.getRootField());
81 final Collection
<String
> labels1
= type1
.getFieldNames();
82 assertEquals("getFieldNames length", fLabels1
.length
, labels1
.size());
83 assertArrayEquals(fLabels1
, labels1
.toArray(new String
[labels1
.size()]));
85 final ITmfEventType type2
= new TmfEventType(fTypeId2
, TmfEventField
.makeRoot(fLabels2
));
86 assertEquals("getName", fTypeId2
, type2
.getName());
87 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels2
), type2
.getRootField());
88 final Collection
<String
> labels2
= type2
.getFieldNames();
89 assertEquals("getFieldNames length", fLabels2
.length
, labels2
.size());
90 assertArrayEquals(fLabels2
, labels2
.toArray(new String
[labels2
.size()]));
93 @Test(expected
= IllegalArgumentException
.class)
94 public void testConstructorCornerCases() {
95 new TmfEventType(null, null);
99 public void testCopyConstructor() {
100 final TmfEventType original
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
101 final TmfEventType copy
= new TmfEventType(original
);
103 assertEquals("getName", fTypeId1
, copy
.getName());
104 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels1
), copy
.getRootField());
105 final Collection
<String
> labels1
= copy
.getFieldNames();
106 assertEquals("getFieldNames length", fLabels1
.length
, labels1
.size());
107 assertArrayEquals(fLabels1
, labels1
.toArray(new String
[labels1
.size()]));
111 public void testCopyConstructorCornerCases() {
113 new TmfEventType(null);
114 fail("TmfEventType: null argument");
115 } catch (final IllegalArgumentException e
) {
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
124 public void testHashCode() {
125 final TmfEventType copy1
= new TmfEventType(fType0
);
127 assertTrue("hashCode", fType0
.hashCode() == copy1
.hashCode());
128 assertTrue("hashCode", fType0
.hashCode() != fType3
.hashCode());
131 // ------------------------------------------------------------------------
133 // ------------------------------------------------------------------------
136 public void testEqualsReflexivity() {
137 assertTrue("equals", fType0
.equals(fType0
));
138 assertTrue("equals", fType3
.equals(fType3
));
140 assertFalse("equals", fType0
.equals(fType3
));
141 assertFalse("equals", fType3
.equals(fType0
));
145 public void testEqualsSymmetry() {
146 final TmfEventType copy0
= new TmfEventType(fType0
);
147 assertTrue("equals", fType0
.equals(copy0
));
148 assertTrue("equals", copy0
.equals(fType0
));
150 final TmfEventType copy1
= new TmfEventType(fType1
);
151 assertTrue("equals", fType1
.equals(copy1
));
152 assertTrue("equals", copy1
.equals(fType1
));
154 final TmfEventType copy2
= new TmfEventType(fType2
);
155 assertTrue("equals", fType2
.equals(copy2
));
156 assertTrue("equals", copy2
.equals(fType2
));
160 public void testEqualsTransivity() {
161 TmfEventType copy1
= new TmfEventType(fType1
);
162 TmfEventType copy2
= new TmfEventType(copy1
);
163 assertTrue("equals", fType1
.equals(copy1
));
164 assertTrue("equals", copy1
.equals(copy2
));
165 assertTrue("equals", fType1
.equals(copy2
));
167 copy1
= new TmfEventType(fType2
);
168 copy2
= new TmfEventType(copy1
);
169 assertTrue("equals", fType2
.equals(copy1
));
170 assertTrue("equals", copy1
.equals(copy2
));
171 assertTrue("equals", fType2
.equals(copy2
));
173 copy1
= new TmfEventType(fType3
);
174 copy2
= new TmfEventType(copy1
);
175 assertTrue("equals", fType3
.equals(copy1
));
176 assertTrue("equals", copy1
.equals(copy2
));
177 assertTrue("equals", fType3
.equals(copy2
));
181 public void testEqualsNull() {
182 assertFalse("equals", fType0
.equals(null));
183 assertFalse("equals", fType3
.equals(null));
187 public void testNonEquals() {
188 assertFalse("equals", fType0
.equals(fType1
));
189 assertFalse("equals", fType1
.equals(fType2
));
190 assertFalse("equals", fType2
.equals(fType3
));
191 assertFalse("equals", fType3
.equals(fType0
));
195 public void testNonEqualsClasses() {
196 assertFalse("equals", fType1
.equals(fLabels1
));
199 // ------------------------------------------------------------------------
201 // ------------------------------------------------------------------------
204 public void testToString() {
205 final String expected1
= "TmfEventType [fTypeId=" + ITmfEventType
.DEFAULT_TYPE_ID
+ "]";
206 final TmfEventType type1
= new TmfEventType();
207 assertEquals("toString", expected1
, type1
.toString());
209 final String expected2
= "TmfEventType [fTypeId=" + fTypeId1
+ "]";
210 final TmfEventType type2
= new TmfEventType(fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
211 assertEquals("toString", expected2
, type2
.toString());