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