Commit | Line | Data |
---|---|---|
63c43609 MR |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 École Polytechnique de Montréal | |
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 | * Guilliano Molaire - Initial API and implementation | |
11 | * Mathieu Rail - Initial API and implementation | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.core.tests.analysis; | |
15 | ||
16 | import java.util.ArrayList; | |
17 | import java.util.List; | |
18 | import java.util.Set; | |
19 | ||
20 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisRequirement; | |
21 | import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisRequirement.ValuePriorityLevel; | |
22 | ||
23 | import static org.junit.Assert.assertEquals; | |
24 | import static org.junit.Assert.assertFalse; | |
25 | import static org.junit.Assert.assertNull; | |
26 | import static org.junit.Assert.assertTrue; | |
27 | ||
28 | import org.junit.Test; | |
29 | ||
30 | /** | |
31 | * Test suite for the {@link TmfAnalysisRequirement} class. | |
32 | * | |
33 | * @author Guilliano Molaire | |
34 | * @author Mathieu Rail | |
35 | */ | |
36 | public class AnalysisRequirementTest { | |
37 | ||
38 | /* Requirements used in the tests */ | |
39 | private TmfAnalysisRequirement fRequirement; | |
40 | private TmfAnalysisRequirement fSubRequirement; | |
41 | ||
42 | /* Types of requirement type strings */ | |
43 | private static final String TYPE_A = "Test Type A"; | |
44 | private static final String TYPE_B = "Test Type B"; | |
45 | ||
46 | /* Requirement value name strings */ | |
47 | private static final String VALUE_A = "Test Value A"; | |
48 | private static final String VALUE_B = "Test Value B"; | |
49 | private static final String VALUE_C = "Test Value C"; | |
50 | private static final String VALUE_D = "Test Value D"; | |
51 | private static final String VALUE_E = "Test Value E"; | |
52 | private static final String VALUE_F = "Test Value F"; | |
53 | ||
54 | /* Requirement information strings */ | |
55 | private static final String INFO_A = "This is an information."; | |
56 | private static final String INFO_B = "This is another information."; | |
57 | private static final String INFO_C = "This is the last information."; | |
58 | ||
59 | /** | |
60 | * Test suite for the {@link TmfAnalysisRequirement#addInformation} and the | |
61 | * {@link TmfAnalysisRequirement#getInformation} methods. | |
62 | */ | |
63 | @Test | |
64 | public void testAddAndGetInformation() { | |
65 | fRequirement = new TmfAnalysisRequirement(TYPE_A); | |
66 | ||
67 | fRequirement.addInformation(INFO_A); | |
68 | fRequirement.addInformation(INFO_B); | |
69 | fRequirement.addInformation(INFO_B); | |
70 | ||
71 | Set<String> information = fRequirement.getInformation(); | |
72 | ||
73 | assertEquals(2, information.size()); | |
74 | ||
75 | assertTrue(information.contains(INFO_A)); | |
76 | assertTrue(information.contains(INFO_B)); | |
77 | } | |
78 | ||
79 | /** | |
80 | * Test suite for the {@link TmfAnalysisRequirement#addValues} and the | |
81 | * {@link TmfAnalysisRequirement#addValue} methods. | |
82 | */ | |
83 | @Test | |
84 | public void testAddValuesToRequirement() { | |
85 | fRequirement = new TmfAnalysisRequirement(TYPE_A); | |
86 | ||
87 | assertEquals(0, fRequirement.getValues().size()); | |
88 | ||
89 | List<String> values = new ArrayList<>(); | |
90 | values.add(VALUE_A); | |
91 | values.add(VALUE_B); | |
92 | values.add(VALUE_C); | |
93 | values.add(VALUE_C); | |
94 | ||
95 | /* | |
96 | * Add values to the requirement with the same level, Value C should | |
97 | * only exist once | |
98 | */ | |
99 | fRequirement.addValues(values, ValuePriorityLevel.MANDATORY); | |
100 | assertEquals(3, fRequirement.getValues().size()); | |
101 | ||
102 | /* | |
103 | * The new value should only exist once and its level should be | |
104 | * mandatory | |
105 | */ | |
106 | fRequirement.addValue(VALUE_D, ValuePriorityLevel.OPTIONAL); | |
107 | fRequirement.addValue(VALUE_D, ValuePriorityLevel.MANDATORY); | |
108 | ||
109 | assertEquals(4, fRequirement.getValues().size()); | |
110 | assertEquals(ValuePriorityLevel.MANDATORY, fRequirement.getValueLevel(VALUE_D)); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Test suite for the {@link TmfAnalysisRequirement#getValueLevel} method. | |
115 | */ | |
116 | @Test | |
117 | public void testGetValueLevel() { | |
118 | fRequirement = new TmfAnalysisRequirement(TYPE_A); | |
119 | fRequirement.addValue(VALUE_A, ValuePriorityLevel.OPTIONAL); | |
120 | ||
121 | /* Try to get the level of a value */ | |
122 | assertEquals(ValuePriorityLevel.OPTIONAL, fRequirement.getValueLevel(VALUE_A)); | |
123 | ||
124 | /* Try to get the level of a value that doesn't exist */ | |
125 | assertNull(fRequirement.getValueLevel(VALUE_B)); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Test suite for the {@link TmfAnalysisRequirement#merge} method with the | |
130 | * parameter value MANDATORY. | |
131 | */ | |
132 | @Test | |
133 | public void testMergeMandatory() { | |
134 | initMergeRequirements(TYPE_A, TYPE_A); | |
135 | ||
136 | assertTrue(fRequirement.merge(fSubRequirement, ValuePriorityLevel.MANDATORY)); | |
137 | ||
138 | assertEquals(fRequirement.getValues().size(), 6); | |
139 | ||
140 | assertEquals(ValuePriorityLevel.MANDATORY, fRequirement.getValueLevel(VALUE_A)); | |
141 | assertEquals(ValuePriorityLevel.MANDATORY, fRequirement.getValueLevel(VALUE_B)); | |
142 | ||
143 | assertEquals(ValuePriorityLevel.MANDATORY, fRequirement.getValueLevel(VALUE_C)); | |
144 | assertEquals(ValuePriorityLevel.OPTIONAL, fRequirement.getValueLevel(VALUE_D)); | |
145 | ||
146 | assertEquals(ValuePriorityLevel.MANDATORY, fRequirement.getValueLevel(VALUE_E)); | |
147 | assertEquals(ValuePriorityLevel.OPTIONAL, fRequirement.getValueLevel(VALUE_F)); | |
148 | ||
149 | Set<String> information = fRequirement.getInformation(); | |
150 | ||
151 | assertEquals(3, information.size()); | |
152 | ||
153 | assertTrue(information.contains(INFO_A)); | |
154 | assertTrue(information.contains(INFO_B)); | |
155 | assertTrue(information.contains(INFO_C)); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Test suite for the {@link TmfAnalysisRequirement#merge} method with the | |
160 | * parameter value OPTIONAL. | |
161 | */ | |
162 | @Test | |
163 | public void testMergeOptional() { | |
164 | initMergeRequirements(TYPE_A, TYPE_A); | |
165 | ||
166 | assertTrue(fRequirement.merge(fSubRequirement, ValuePriorityLevel.OPTIONAL)); | |
167 | ||
168 | assertEquals(6, fRequirement.getValues().size()); | |
169 | ||
170 | assertEquals(ValuePriorityLevel.MANDATORY, fRequirement.getValueLevel(VALUE_A)); | |
171 | assertEquals(ValuePriorityLevel.MANDATORY, fRequirement.getValueLevel(VALUE_B)); | |
172 | ||
173 | assertEquals(ValuePriorityLevel.OPTIONAL, fRequirement.getValueLevel(VALUE_C)); | |
174 | assertEquals(ValuePriorityLevel.OPTIONAL, fRequirement.getValueLevel(VALUE_D)); | |
175 | ||
176 | assertEquals(ValuePriorityLevel.OPTIONAL, fRequirement.getValueLevel(VALUE_E)); | |
177 | assertEquals(ValuePriorityLevel.OPTIONAL, fRequirement.getValueLevel(VALUE_F)); | |
178 | ||
179 | Set<String> information = fRequirement.getInformation(); | |
180 | ||
181 | assertEquals(3, information.size()); | |
182 | ||
183 | assertTrue(information.contains(INFO_A)); | |
184 | assertTrue(information.contains(INFO_B)); | |
185 | assertTrue(information.contains(INFO_C)); | |
186 | } | |
187 | ||
188 | /** | |
189 | * Test suite for the {@link TmfAnalysisRequirement#merge} method with | |
190 | * different requirement types. | |
191 | */ | |
192 | @Test | |
193 | public void testMergeDifferentTypes() { | |
194 | initMergeRequirements(TYPE_A, TYPE_B); | |
195 | ||
196 | assertFalse(fRequirement.merge(fSubRequirement, ValuePriorityLevel.OPTIONAL)); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Test suite for the {@link TmfAnalysisRequirement#isSameType} method. | |
201 | */ | |
202 | @Test | |
203 | public void testIsSameRequirementType() { | |
204 | fRequirement = new TmfAnalysisRequirement(TYPE_A); | |
205 | ||
206 | assertTrue(fRequirement.isSameType(new TmfAnalysisRequirement(TYPE_A))); | |
207 | assertFalse(fRequirement.isSameType(new TmfAnalysisRequirement(TYPE_B))); | |
208 | } | |
209 | ||
210 | /** | |
211 | * Initialize the requirement and sub-requirement for the merge tests. | |
212 | * | |
213 | * @param typeA | |
214 | * The type of the first requirement | |
215 | * @param typeB | |
216 | * The type of the second requirement | |
217 | */ | |
218 | private void initMergeRequirements(String typeA, String typeB) { | |
219 | fRequirement = new TmfAnalysisRequirement(typeA); | |
220 | fRequirement.addValue(VALUE_A, ValuePriorityLevel.MANDATORY); | |
221 | fRequirement.addValue(VALUE_B, ValuePriorityLevel.MANDATORY); | |
222 | ||
223 | fRequirement.addValue(VALUE_C, ValuePriorityLevel.OPTIONAL); | |
224 | fRequirement.addValue(VALUE_D, ValuePriorityLevel.OPTIONAL); | |
225 | ||
226 | fRequirement.addInformation(INFO_A); | |
227 | fRequirement.addInformation(INFO_B); | |
228 | ||
229 | /* This sub-requirement will be merged into requirement */ | |
230 | fSubRequirement = new TmfAnalysisRequirement(typeB); | |
231 | fSubRequirement.addValue(VALUE_A, ValuePriorityLevel.MANDATORY); | |
232 | fSubRequirement.addValue(VALUE_B, ValuePriorityLevel.OPTIONAL); | |
233 | ||
234 | fSubRequirement.addValue(VALUE_C, ValuePriorityLevel.MANDATORY); | |
235 | fSubRequirement.addValue(VALUE_D, ValuePriorityLevel.OPTIONAL); | |
236 | ||
237 | fSubRequirement.addValue(VALUE_E, ValuePriorityLevel.MANDATORY); | |
238 | fSubRequirement.addValue(VALUE_F, ValuePriorityLevel.OPTIONAL); | |
239 | ||
240 | fSubRequirement.addInformation(INFO_B); | |
241 | fSubRequirement.addInformation(INFO_C); | |
242 | } | |
243 | } |