Commit | Line | Data |
---|---|---|
d18dd09b ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made 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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.tests.event; | |
14 | ||
cbd4ad82 FC |
15 | import junit.framework.TestCase; |
16 | ||
d18dd09b ASL |
17 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
18 | import org.eclipse.linuxtools.tmf.event.TmfEventContent; | |
19 | import org.eclipse.linuxtools.tmf.event.TmfEventField; | |
20 | import org.eclipse.linuxtools.tmf.event.TmfEventReference; | |
21 | import org.eclipse.linuxtools.tmf.event.TmfEventSource; | |
22 | import org.eclipse.linuxtools.tmf.event.TmfEventType; | |
23 | import org.eclipse.linuxtools.tmf.event.TmfEventTypeStub; | |
24 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
25 | ||
26 | /** | |
27 | * <b><u>TmfEventFieldTest</u></b> | |
28 | * <p> | |
cbd4ad82 | 29 | * Test suite for the TmfEventField class. |
d18dd09b ASL |
30 | */ |
31 | public class TmfEventFieldTest extends TestCase { | |
32 | ||
cbd4ad82 FC |
33 | // ------------------------------------------------------------------------ |
34 | // Variables | |
35 | // ------------------------------------------------------------------------ | |
36 | ||
d18dd09b ASL |
37 | private final TmfEventContent fContent; |
38 | private final String fFieldId = "Field"; | |
39 | private final Object fValue1 = new String("Value"); | |
40 | private final Object fValue2 = new Integer(10); | |
41 | ||
cbd4ad82 FC |
42 | private TmfEventField fField0; |
43 | private TmfEventField fField1; | |
44 | private TmfEventField fField2; | |
45 | private TmfEventField fField3; | |
46 | ||
47 | // ------------------------------------------------------------------------ | |
d18dd09b | 48 | // Housekeeping |
cbd4ad82 | 49 | // ------------------------------------------------------------------------ |
d18dd09b | 50 | |
cbd4ad82 FC |
51 | /** |
52 | * @param name the test name | |
53 | */ | |
d18dd09b ASL |
54 | public TmfEventFieldTest(String name) { |
55 | super(name); | |
56 | TmfTimestamp fTimestamp = new TmfTimestamp(); | |
57 | TmfEventSource fEventSource = new TmfEventSource(); | |
58 | TmfEventType fEventType = new TmfEventTypeStub(); | |
59 | TmfEventReference fReference = new TmfEventReference(); | |
60 | TmfEvent fEvent = new TmfEvent(fTimestamp, fEventSource, fEventType, fReference); | |
61 | ||
62 | fContent = new TmfEventContent(fEvent, "Some content"); | |
cbd4ad82 FC |
63 | |
64 | fField0 = new TmfEventField(fContent, fFieldId, fValue1); | |
65 | fField1 = new TmfEventField(fContent, fFieldId, fValue1); | |
66 | fField2 = new TmfEventField(fContent, fFieldId, fValue1); | |
67 | fField3 = new TmfEventField(fContent, fFieldId, fValue2); | |
d18dd09b ASL |
68 | } |
69 | ||
70 | @Override | |
71 | protected void setUp() throws Exception { | |
72 | super.setUp(); | |
73 | } | |
74 | ||
75 | @Override | |
76 | protected void tearDown() throws Exception { | |
77 | super.tearDown(); | |
78 | } | |
79 | ||
cbd4ad82 | 80 | // ------------------------------------------------------------------------ |
d18dd09b | 81 | // Constructors |
cbd4ad82 | 82 | // ------------------------------------------------------------------------ |
d18dd09b ASL |
83 | |
84 | public void testTmfEventField() { | |
cbd4ad82 FC |
85 | assertSame("getParent", fContent, fField0.getParent()); |
86 | assertSame("getId", fFieldId, fField0.getId()); | |
87 | assertSame("getValue", fValue1, fField0.getValue()); | |
88 | } | |
89 | ||
90 | public void testTmfEventFieldBadArg() { | |
91 | try { | |
92 | new TmfEventField(fContent, null, fValue1); | |
93 | fail("null copy"); | |
94 | } | |
95 | catch (IllegalArgumentException e) { | |
96 | // Success | |
97 | } | |
d18dd09b ASL |
98 | } |
99 | ||
100 | public void testTmfEventFieldCopy() { | |
101 | TmfEventField original = new TmfEventField(fContent, fFieldId, fValue1); | |
102 | TmfEventField field = new TmfEventField(original); | |
103 | assertSame("getParent", fContent, field.getParent()); | |
104 | assertSame("getId", fFieldId, field.getId()); | |
105 | assertSame("getValue", fValue1, field.getValue()); | |
106 | } | |
107 | ||
cbd4ad82 FC |
108 | public void testTmfEventFieldCopy2() { |
109 | try { | |
110 | new TmfEventField(null); | |
111 | fail("null copy"); | |
112 | } | |
113 | catch (IllegalArgumentException e) { | |
114 | // Success | |
115 | } | |
116 | } | |
117 | ||
118 | // ------------------------------------------------------------------------ | |
119 | // Modifiers | |
120 | // ------------------------------------------------------------------------ | |
121 | ||
122 | private class MyField extends TmfEventField { | |
123 | public MyField(TmfEventContent parent, String id, Object value) { | |
124 | super(parent, id, value); | |
125 | } | |
126 | public MyField(TmfEventField field) { | |
127 | super(field); | |
128 | } | |
129 | @Override | |
130 | public void setValue(Object value) { | |
131 | super.setValue(value); | |
132 | } | |
133 | } | |
134 | ||
135 | public void testSetValue() { | |
d18dd09b | 136 | TmfEventField original = new TmfEventField(fContent, fFieldId, fValue1); |
cbd4ad82 FC |
137 | TmfEventField field = new TmfEventField(original); |
138 | ||
139 | MyField myField = new MyField(field); | |
140 | assertSame("getValue", fValue1, myField.getValue()); | |
141 | ||
142 | myField.setValue(fValue2); | |
143 | assertSame("getValue", fValue2, myField.getValue()); | |
144 | } | |
145 | ||
146 | // ------------------------------------------------------------------------ | |
147 | // equals | |
148 | // ------------------------------------------------------------------------ | |
149 | ||
150 | public void testEqualsReflexivity() throws Exception { | |
151 | assertTrue("equals", fField0.equals(fField0)); | |
152 | assertTrue("equals", fField3.equals(fField3)); | |
153 | ||
154 | assertTrue("equals", !fField0.equals(fField3)); | |
155 | assertTrue("equals", !fField3.equals(fField0)); | |
156 | } | |
157 | ||
158 | public void testEqualsSymmetry() throws Exception { | |
159 | assertTrue("equals", fField0.equals(fField1)); | |
160 | assertTrue("equals", fField1.equals(fField0)); | |
161 | ||
162 | assertTrue("equals", !fField0.equals(fField3)); | |
163 | assertTrue("equals", !fField3.equals(fField0)); | |
164 | } | |
165 | ||
166 | public void testEqualsTransivity() throws Exception { | |
167 | assertTrue("equals", fField0.equals(fField1)); | |
168 | assertTrue("equals", fField1.equals(fField2)); | |
169 | assertTrue("equals", fField0.equals(fField2)); | |
d18dd09b | 170 | } |
cbd4ad82 | 171 | |
cbd4ad82 FC |
172 | public void testEqualsNull() throws Exception { |
173 | assertTrue("equals", !fField0.equals(null)); | |
174 | assertTrue("equals", !fField3.equals(null)); | |
175 | } | |
176 | ||
2fb2eb37 FC |
177 | // ------------------------------------------------------------------------ |
178 | // hashCode | |
179 | // ------------------------------------------------------------------------ | |
180 | ||
181 | public void testHashCode() throws Exception { | |
182 | assertTrue("hashCode", fField0.hashCode() == fField1.hashCode()); | |
183 | assertTrue("hashCode", fField0.hashCode() != fField3.hashCode()); | |
184 | } | |
185 | ||
cbd4ad82 FC |
186 | // ------------------------------------------------------------------------ |
187 | // toString | |
188 | // ------------------------------------------------------------------------ | |
d18dd09b ASL |
189 | |
190 | public void testToString() { | |
191 | String expected1 = "[TmfEventField(" + fFieldId + ":" + fValue1.toString() + ")]"; | |
192 | TmfEventField field = new TmfEventField(fContent, fFieldId, fValue1); | |
193 | assertEquals("toString", expected1, field.toString()); | |
194 | ||
195 | String expected2 = "[TmfEventField(" + fFieldId + ":" + fValue2.toString() + ")]"; | |
196 | field = new TmfEventField(fContent, fFieldId, fValue2); | |
197 | assertEquals("toString", expected2, field.toString()); | |
198 | } | |
199 | ||
200 | } |