Commit | Line | Data |
---|---|---|
39f9eadb | 1 | /******************************************************************************* |
6e1886bc | 2 | * Copyright (c) 2012, 2013 Ericsson |
54a7a54c | 3 | * |
39f9eadb FC |
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 | |
54a7a54c | 8 | * |
39f9eadb FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
6e1886bc | 11 | * Alexandre Montplaisir - Port to JUnit4 |
39f9eadb FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.tmf.core.tests.event; | |
15 | ||
6e1886bc AM |
16 | import static org.junit.Assert.assertEquals; |
17 | import static org.junit.Assert.assertNotSame; | |
18 | import static org.junit.Assert.assertNull; | |
19 | import static org.junit.Assert.assertSame; | |
409bea20 | 20 | import static org.junit.Assert.assertTrue; |
39f9eadb | 21 | |
6e1886bc | 22 | import java.util.Arrays; |
409bea20 | 23 | import java.util.Set; |
39f9eadb FC |
24 | |
25 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; | |
26 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; | |
27 | import org.eclipse.linuxtools.tmf.core.event.TmfEventType; | |
28 | import org.eclipse.linuxtools.tmf.core.event.TmfEventTypeManager; | |
6e1886bc | 29 | import org.junit.Test; |
39f9eadb FC |
30 | |
31 | /** | |
39f9eadb | 32 | * Test suite for the TmfEventTypeManager class. |
39f9eadb | 33 | */ |
cad06250 | 34 | @SuppressWarnings("javadoc") |
6e1886bc | 35 | public class TmfEventTypeManagerTest { |
39f9eadb FC |
36 | |
37 | // ------------------------------------------------------------------------ | |
38 | // Variables | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | private static final TmfEventTypeManager fInstance = TmfEventTypeManager.getInstance(); | |
42 | ||
43 | private final String fContext1 = "JUnit context 1"; | |
44 | private final String fContext2 = "JUnit context 2"; | |
45 | ||
46 | private final String fTypeId1 = "Some type"; | |
47 | private final String fTypeId2 = "Some other type"; | |
48 | private final String fTypeId3 = "Yet another type"; | |
49 | private final String fTypeId4 = "A final type"; | |
50 | ||
51 | private final String fLabel0 = "label1"; | |
52 | private final String fLabel1 = "label2"; | |
b742c196 | 53 | private final String fLabel2 = "label3"; |
085d898f | 54 | |
39f9eadb FC |
55 | private final String[] fLabels0 = new String[] { }; |
56 | private final String[] fLabels1 = new String[] { fLabel0, fLabel1 }; | |
b742c196 | 57 | private final String[] fLabels2 = new String[] { fLabel1, fLabel0, fLabel2 }; |
39f9eadb FC |
58 | |
59 | private final TmfEventType fType0 = new TmfEventType(fContext1, fTypeId1, TmfEventField.makeRoot(fLabels0)); | |
60 | private final TmfEventType fType1 = new TmfEventType(fContext1, fTypeId2, TmfEventField.makeRoot(fLabels1)); | |
61 | private final TmfEventType fType2 = new TmfEventType(fContext2, fTypeId3, TmfEventField.makeRoot(fLabels2)); | |
62 | private final TmfEventType fType3 = new TmfEventType(fContext2, fTypeId4, TmfEventField.makeRoot(fLabels1)); | |
63 | ||
39f9eadb FC |
64 | // ------------------------------------------------------------------------ |
65 | // Getters | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
6e1886bc | 68 | @Test |
39f9eadb FC |
69 | public void testGetContexts() { |
70 | fInstance.clear(); | |
71 | fInstance.add(fContext1, fType0); | |
72 | fInstance.add(fContext1, fType1); | |
73 | fInstance.add(fContext2, fType2); | |
74 | fInstance.add(fContext2, fType3); | |
75 | ||
085d898f | 76 | final String[] contexts = fInstance.getContexts(); |
39f9eadb FC |
77 | Arrays.sort(contexts); |
78 | assertEquals("getContexts", 2, contexts.length); | |
79 | assertEquals("getContexts", fContext1, contexts[0]); | |
80 | assertEquals("getContexts", fContext2, contexts[1]); | |
81 | } | |
82 | ||
6e1886bc | 83 | @Test |
39f9eadb FC |
84 | public void testGetTypes() { |
85 | fInstance.clear(); | |
86 | fInstance.add(fContext1, fType0); | |
87 | fInstance.add(fContext1, fType1); | |
88 | fInstance.add(fContext2, fType2); | |
89 | fInstance.add(fContext2, fType3); | |
90 | ||
409bea20 GB |
91 | Set<ITmfEventType> types = fInstance.getTypes(fContext1); |
92 | assertEquals("getTypes", 2, types.size()); | |
93 | assertTrue(types.contains(fType1)); | |
94 | assertTrue(types.contains(fType0)); | |
39f9eadb FC |
95 | |
96 | types = fInstance.getTypes(fContext2); | |
409bea20 GB |
97 | assertEquals("getTypes", 2, types.size()); |
98 | assertTrue(types.contains(fType2)); | |
99 | assertTrue(types.contains(fType3)); | |
39f9eadb FC |
100 | } |
101 | ||
6e1886bc | 102 | @Test |
39f9eadb FC |
103 | public void testGetType() { |
104 | fInstance.clear(); | |
105 | fInstance.add(fContext1, fType0); | |
106 | fInstance.add(fContext1, fType1); | |
107 | fInstance.add(fContext2, fType2); | |
108 | fInstance.add(fContext2, fType3); | |
109 | ||
110 | ITmfEventType type = fInstance.getType(fContext1, fType0.getName()); | |
111 | assertSame("getType", fType0, type); | |
112 | type = fInstance.getType(fContext1, fType1.getName()); | |
113 | assertSame("getType", fType1, type); | |
114 | type = fInstance.getType(fContext1, fType2.getName()); | |
115 | assertNull("getType", type); | |
116 | type = fInstance.getType(fContext1, fType3.getName()); | |
117 | assertNull("getType", type); | |
118 | ||
119 | type = fInstance.getType(fContext2, fType2.getName()); | |
120 | assertSame("getType", fType2, type); | |
121 | type = fInstance.getType(fContext2, fType3.getName()); | |
122 | assertSame("getType", fType3, type); | |
123 | type = fInstance.getType(fContext2, fType0.getName()); | |
124 | assertNull("getType", type); | |
125 | type = fInstance.getType(fContext2, fType1.getName()); | |
126 | assertNull("getType", type); | |
127 | } | |
128 | ||
129 | // ------------------------------------------------------------------------ | |
130 | // Operations | |
131 | // ------------------------------------------------------------------------ | |
132 | ||
6e1886bc | 133 | @Test |
39f9eadb FC |
134 | public void testClear() { |
135 | fInstance.clear(); | |
136 | assertEquals("clear", 0, fInstance.getContexts().length); | |
409bea20 | 137 | assertEquals("clear", 0, fInstance.getTypes(null).size()); |
39f9eadb FC |
138 | assertNull("clear", fInstance.getType(null, null)); |
139 | assertEquals("clear", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString()); | |
140 | } | |
141 | ||
6e1886bc | 142 | @Test |
39f9eadb FC |
143 | public void testClearContext() { |
144 | fInstance.clear(); | |
145 | fInstance.add(fContext1, fType0); | |
146 | fInstance.add(fContext1, fType1); | |
147 | fInstance.add(fContext2, fType2); | |
148 | fInstance.add(fContext2, fType3); | |
149 | ||
150 | fInstance.clear(fContext1); | |
151 | ||
085d898f | 152 | final String[] contexts = fInstance.getContexts(); |
39f9eadb FC |
153 | assertEquals("clear context", 1, contexts.length); |
154 | assertEquals("clear context", fContext2, contexts[0]); | |
155 | ||
409bea20 GB |
156 | Set<ITmfEventType> types = fInstance.getTypes(fContext1); |
157 | assertEquals("clear context", 0, types.size()); | |
39f9eadb FC |
158 | |
159 | ITmfEventType type = fInstance.getType(fContext1, fType0.getName()); | |
160 | assertNull("clear context", type); | |
161 | type = fInstance.getType(fContext1, fType1.getName()); | |
162 | assertNull("clear context", type); | |
163 | ||
164 | types = fInstance.getTypes(fContext2); | |
409bea20 GB |
165 | assertEquals("clear context", 2, types.size()); |
166 | assertTrue(types.contains(fType2)); | |
167 | assertTrue(types.contains(fType3)); | |
39f9eadb FC |
168 | } |
169 | ||
6e1886bc | 170 | @Test |
39f9eadb FC |
171 | public void testBasicAdd() { |
172 | fInstance.clear(); | |
173 | fInstance.add(fContext1, fType0); | |
174 | ||
085d898f | 175 | final String[] contexts = fInstance.getContexts(); |
39f9eadb FC |
176 | assertEquals("add", 1, contexts.length); |
177 | assertEquals("add", fContext1, contexts[0]); | |
178 | ||
409bea20 GB |
179 | final Set<ITmfEventType> types = fInstance.getTypes(contexts[0]); |
180 | assertEquals("add", 1, types.size()); | |
181 | assertTrue(types.contains(fType0)); | |
39f9eadb FC |
182 | |
183 | ITmfEventType type = fInstance.getType(contexts[0], fType0.getName()); | |
184 | assertSame("add", fType0, type); | |
185 | ||
186 | type = fInstance.getType(contexts[0], fType1.getName()); | |
187 | assertNotSame("add", fType0, type); | |
188 | } | |
189 | ||
6e1886bc | 190 | @Test |
39f9eadb FC |
191 | public void testAdd() { |
192 | fInstance.clear(); | |
193 | fInstance.add(fContext1, fType0); | |
194 | fInstance.add(fContext1, fType1); | |
195 | fInstance.add(fContext2, fType2); | |
196 | fInstance.add(fContext2, fType3); | |
197 | ||
085d898f | 198 | final String[] contexts = fInstance.getContexts(); |
39f9eadb FC |
199 | Arrays.sort(contexts); |
200 | assertEquals("add", 2, contexts.length); | |
201 | assertEquals("add", fContext1, contexts[0]); | |
202 | assertEquals("add", fContext2, contexts[1]); | |
203 | ||
409bea20 GB |
204 | Set<ITmfEventType> types = fInstance.getTypes(fContext1); |
205 | assertEquals("add", 2, types.size()); | |
206 | assertTrue(types.contains(fType0)); | |
207 | assertTrue(types.contains(fType1)); | |
39f9eadb FC |
208 | |
209 | types = fInstance.getTypes(fContext2); | |
409bea20 GB |
210 | assertEquals("add", 2, types.size()); |
211 | assertTrue(types.contains(fType2)); | |
212 | assertTrue(types.contains(fType3)); | |
39f9eadb FC |
213 | |
214 | ITmfEventType type = fInstance.getType(fContext1, fType0.getName()); | |
215 | assertSame("add", fType0, type); | |
216 | type = fInstance.getType(fContext1, fType1.getName()); | |
217 | assertSame("add", fType1, type); | |
218 | type = fInstance.getType(fContext2, fType2.getName()); | |
219 | assertSame("add", fType2, type); | |
220 | type = fInstance.getType(fContext2, fType3.getName()); | |
221 | assertSame("add", fType3, type); | |
222 | ||
223 | type = fInstance.getType(fContext1, fType2.getName()); | |
224 | assertNull("add", type); | |
225 | type = fInstance.getType(fContext2, fType0.getName()); | |
226 | assertNull("add", type); | |
227 | } | |
228 | ||
229 | // ------------------------------------------------------------------------ | |
230 | // Object | |
231 | // ------------------------------------------------------------------------ | |
232 | ||
6e1886bc | 233 | @Test |
39f9eadb FC |
234 | public void testToString() { |
235 | fInstance.clear(); | |
236 | assertEquals("toString", "TmfEventTypeManager [fEventTypes={}]", fInstance.toString()); | |
237 | ||
238 | fInstance.add(fContext1, fType0); | |
239 | assertEquals("toString", "TmfEventTypeManager [fEventTypes={" + fContext1 + "={" + fTypeId1 + "=" + fType0 + "}}]", fInstance.toString()); | |
240 | } | |
241 | ||
242 | } |