1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.tests
.event
;
16 import junit
.framework
.TestCase
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventType
;
23 * Test suite for the TmfEventType class.
25 @SuppressWarnings("nls")
26 public class TmfEventTypeTest
extends TestCase
{
28 // ------------------------------------------------------------------------
30 // ------------------------------------------------------------------------
32 private final String fContext1
= "JUnit context 1";
33 private final String fContext2
= "JUnit context 2";
35 private final String fTypeId1
= "Some type";
36 private final String fTypeId2
= "Some other type";
38 private final String fLabel0
= "label1";
39 private final String fLabel1
= "label2";
41 private final String
[] fLabels0
= new String
[] { };
42 private final String
[] fLabels1
= new String
[] { fLabel0
, fLabel1
};
43 private final String
[] fLabels2
= new String
[] { fLabel1
, fLabel0
, fLabel1
};
45 private final ITmfEventType fType0
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels0
));
46 private final ITmfEventType fType1
= new TmfEventType(fContext1
, fTypeId2
, TmfEventField
.makeRoot(fLabels1
));
47 private final ITmfEventType fType2
= new TmfEventType(fContext2
, fTypeId1
, TmfEventField
.makeRoot(fLabels2
));
48 private final ITmfEventType fType3
= new TmfEventType(fContext2
, fTypeId2
, TmfEventField
.makeRoot(fLabels1
));
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
55 * @param name the test name
57 public TmfEventTypeTest(final String name
) {
62 protected void setUp() throws Exception
{
67 protected void tearDown() throws Exception
{
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 public void testDefaultConstructor() {
76 final ITmfEventType type
= new TmfEventType();
77 assertEquals("getContext", TmfEventType
.DEFAULT_CONTEXT_ID
, type
.getContext());
78 assertEquals("getName", TmfEventType
.DEFAULT_TYPE_ID
, type
.getName());
79 assertNull("getRootField", type
.getRootField());
80 assertEquals("getFieldNames", 0, type
.getFieldNames().length
);
81 assertNull("getFieldName", type
.getFieldName(0));
84 public void testFullConstructor() {
85 final ITmfEventType type0
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels0
));
86 assertEquals("getContext", fContext1
, type0
.getContext());
87 assertEquals("getName", fTypeId1
, type0
.getName());
88 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels0
), type0
.getRootField());
89 final String
[] labels0
= type0
.getFieldNames();
90 assertEquals("getFieldNames length", fLabels0
.length
, labels0
.length
);
91 for (int i
= 0; i
< labels0
.length
; i
++)
92 assertEquals("getFieldNames", fLabels0
[i
], labels0
[i
]);
93 assertNull("getFieldName", type0
.getFieldName(labels0
.length
));
95 final ITmfEventType type1
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
96 assertEquals("getContext", fContext1
, type1
.getContext());
97 assertEquals("getName", fTypeId1
, type1
.getName());
98 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels1
), type1
.getRootField());
99 final String
[] labels1
= type1
.getFieldNames();
100 assertEquals("getFieldNames length", fLabels1
.length
, labels1
.length
);
101 for (int i
= 0; i
< labels1
.length
; i
++)
102 assertEquals("getFieldNames", fLabels1
[i
], labels1
[i
]);
103 assertNull("getFieldName", type1
.getFieldName(labels1
.length
));
105 final ITmfEventType type2
= new TmfEventType(fContext2
, fTypeId2
, TmfEventField
.makeRoot(fLabels2
));
106 assertEquals("getContext", fContext2
, type2
.getContext());
107 assertEquals("getName", fTypeId2
, type2
.getName());
108 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels2
), type2
.getRootField());
109 final String
[] labels2
= type2
.getFieldNames();
110 assertEquals("getFieldNames length", fLabels2
.length
, labels2
.length
);
111 for (int i
= 0; i
< labels2
.length
; i
++)
112 assertEquals("getFieldNames", fLabels2
[i
], labels2
[i
]);
113 assertNull("getFieldName", type2
.getFieldName(labels2
.length
));
116 public void testConstructorCornerCases() {
118 new TmfEventType(null, fTypeId1
, null);
119 fail("TmfEventType: null context");
120 } catch (final IllegalArgumentException e
) {
124 new TmfEventType(fContext1
, null, null);
125 fail("TmfEventType: null type");
126 } catch (final IllegalArgumentException e
) {
130 public void testCopyConstructor() {
131 final TmfEventType original
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
132 final TmfEventType copy
= new TmfEventType(original
);
134 assertEquals("getContext", fContext1
, copy
.getContext());
135 assertEquals("getName", fTypeId1
, copy
.getName());
136 assertEquals("getRootField", TmfEventField
.makeRoot(fLabels1
), copy
.getRootField());
137 final String
[] labels1
= copy
.getFieldNames();
138 assertEquals("getFieldNames length", fLabels1
.length
, labels1
.length
);
139 for (int i
= 0; i
< labels1
.length
; i
++)
140 assertEquals("getFieldNames", fLabels1
[i
], labels1
[i
]);
141 assertNull("getFieldName", copy
.getFieldName(labels1
.length
));
144 public void testCopyConstructorCornerCases() {
146 new TmfEventType(null);
147 fail("TmfEventType: null argument");
148 } catch (final IllegalArgumentException e
) {
152 // ------------------------------------------------------------------------
154 // ------------------------------------------------------------------------
156 public static class MyEventType
extends TmfEventType
{
159 public boolean equals(final Object other
) {
160 return super.equals(other
);
164 public MyEventType
clone() {
165 return (MyEventType
) super.clone();
169 public void testClone() throws Exception
{
170 final ITmfEventType clone
= fType1
.clone();
172 assertTrue("clone", fType1
.clone().equals(fType1
));
173 assertTrue("clone", clone
.clone().equals(clone
));
175 assertEquals("clone", clone
, fType1
);
176 assertEquals("clone", fType1
, clone
);
179 public void testClone2() throws Exception
{
180 final ITmfEventType type
= new TmfEventType();
181 final ITmfEventType clone
= type
.clone();
183 assertTrue("clone", type
.clone().equals(type
));
184 assertTrue("clone", clone
.clone().equals(clone
));
186 assertEquals("clone", clone
, type
);
187 assertEquals("clone", type
, clone
);
190 // ------------------------------------------------------------------------
192 // ------------------------------------------------------------------------
194 public void testHashCode() throws Exception
{
195 final TmfEventType copy1
= new TmfEventType(fType0
);
197 assertTrue("hashCode", fType0
.hashCode() == copy1
.hashCode());
198 assertTrue("hashCode", fType0
.hashCode() != fType3
.hashCode());
201 // ------------------------------------------------------------------------
203 // ------------------------------------------------------------------------
205 public void testEqualsReflexivity() throws Exception
{
206 assertTrue("equals", fType0
.equals(fType0
));
207 assertTrue("equals", fType3
.equals(fType3
));
209 assertFalse("equals", fType0
.equals(fType3
));
210 assertFalse("equals", fType3
.equals(fType0
));
213 public void testEqualsSymmetry() throws Exception
{
214 final TmfEventType copy0
= new TmfEventType(fType0
);
215 assertTrue("equals", fType0
.equals(copy0
));
216 assertTrue("equals", copy0
.equals(fType0
));
218 final TmfEventType copy1
= new TmfEventType(fType1
);
219 assertTrue("equals", fType1
.equals(copy1
));
220 assertTrue("equals", copy1
.equals(fType1
));
222 final TmfEventType copy2
= new TmfEventType(fType2
);
223 assertTrue("equals", fType2
.equals(copy2
));
224 assertTrue("equals", copy2
.equals(fType2
));
227 public void testEqualsTransivity() throws Exception
{
228 TmfEventType copy1
= new TmfEventType(fType1
);
229 TmfEventType copy2
= new TmfEventType(copy1
);
230 assertTrue("equals", fType1
.equals(copy1
));
231 assertTrue("equals", copy1
.equals(copy2
));
232 assertTrue("equals", fType1
.equals(copy2
));
234 copy1
= new TmfEventType(fType2
);
235 copy2
= new TmfEventType(copy1
);
236 assertTrue("equals", fType2
.equals(copy1
));
237 assertTrue("equals", copy1
.equals(copy2
));
238 assertTrue("equals", fType2
.equals(copy2
));
240 copy1
= new TmfEventType(fType3
);
241 copy2
= new TmfEventType(copy1
);
242 assertTrue("equals", fType3
.equals(copy1
));
243 assertTrue("equals", copy1
.equals(copy2
));
244 assertTrue("equals", fType3
.equals(copy2
));
247 public void testEqualsNull() throws Exception
{
248 assertFalse("equals", fType0
.equals(null));
249 assertFalse("equals", fType3
.equals(null));
252 public void testNonEquals() throws Exception
{
253 assertFalse("equals", fType0
.equals(fType1
));
254 assertFalse("equals", fType1
.equals(fType2
));
255 assertFalse("equals", fType2
.equals(fType3
));
256 assertFalse("equals", fType3
.equals(fType0
));
259 public void testNonEqualsClasses() throws Exception
{
260 assertFalse("equals", fType1
.equals(fLabels1
));
263 // ------------------------------------------------------------------------
265 // ------------------------------------------------------------------------
267 public void testToString() {
268 final String expected1
= "TmfEventType [fContext=" + TmfEventType
.DEFAULT_CONTEXT_ID
+
269 ", fTypeId=" + TmfEventType
.DEFAULT_TYPE_ID
+ "]";
270 final TmfEventType type1
= new TmfEventType();
271 assertEquals("toString", expected1
, type1
.toString());
273 final String expected2
= "TmfEventType [fContext=" + fContext1
+ ", fTypeId=" + fTypeId1
+ "]";
274 final TmfEventType type2
= new TmfEventType(fContext1
, fTypeId1
, TmfEventField
.makeRoot(fLabels1
));
275 assertEquals("toString", expected2
, type2
.toString());