1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.usability.result;
|
---|
16 |
|
---|
17 | import java.io.File;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.net.URISyntaxException;
|
---|
20 | import java.util.EnumMap;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.Map.Entry;
|
---|
23 |
|
---|
24 | import jodd.props.Props;
|
---|
25 |
|
---|
26 | import com.google.common.base.Optional;
|
---|
27 | import com.google.common.base.Predicate;
|
---|
28 | import com.google.common.collect.Iterables;
|
---|
29 | import com.google.common.collect.Maps;
|
---|
30 |
|
---|
31 | import de.ugoe.cs.util.console.Console;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * <p>
|
---|
35 | * Helper class, which creates a {@link UsabilityProblemDescription} for a usability rule.
|
---|
36 | * </p>
|
---|
37 | *
|
---|
38 | * @author Alexander Deicke
|
---|
39 | */
|
---|
40 | public class UsabilityProblemDescriptionResolver {
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * <p>
|
---|
44 | * .properties file, which contains all details concerning a usability defect.
|
---|
45 | * </p>
|
---|
46 | */
|
---|
47 | private final String defectDescriptionFile = "defects.props";
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * <p>
|
---|
51 | * Creates a defect description for a {@link UsabilityRule}.
|
---|
52 | * </p>
|
---|
53 | *
|
---|
54 | * @param name
|
---|
55 | * of usability rule
|
---|
56 | * @return defect description for usability rule
|
---|
57 | */
|
---|
58 | public UsabilityProblemDescription descriptionFor(String usabilityRuleName) {
|
---|
59 | Props allProperties = initProperties();
|
---|
60 | Map<String, String> usabilityRuleProperties =
|
---|
61 | allUsabilityRuleProperties(allProperties, usabilityRuleName);
|
---|
62 | return createUsabilityDefect(usabilityRuleProperties);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * <p>
|
---|
67 | * Initializes the properties, which are used to create the defect description.
|
---|
68 | * </p>
|
---|
69 | *
|
---|
70 | * @return properties needed to create defect description
|
---|
71 | */
|
---|
72 | private Props initProperties() {
|
---|
73 | Optional<File> defectDescriptionFile = getDefectDescriptionFile();
|
---|
74 | Props props = new Props();
|
---|
75 | props.setEscapeNewLineValue("\n");
|
---|
76 | props.setValueTrimLeft(true);
|
---|
77 | if (defectDescriptionFile.isPresent()) {
|
---|
78 | loadProperties(defectDescriptionFile, props);
|
---|
79 | }
|
---|
80 | return props;
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * <p>
|
---|
85 | * Loads the .properties file from the system.
|
---|
86 | * </p>
|
---|
87 | *
|
---|
88 | * @return iff present, {@link File} object of the .properties file
|
---|
89 | */
|
---|
90 | private Optional<File> getDefectDescriptionFile() {
|
---|
91 | try {
|
---|
92 | return Optional.fromNullable(new File(ClassLoader
|
---|
93 | .getSystemResource(defectDescriptionFile).toURI()));
|
---|
94 | }
|
---|
95 | catch (URISyntaxException e) {
|
---|
96 | Console.printerr("Error while loading defect description file.");
|
---|
97 | Console.logException(e);
|
---|
98 | return Optional.absent();
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | *
|
---|
104 | * <p>
|
---|
105 | * Loads the values from the .properties.
|
---|
106 | * </p>
|
---|
107 | *
|
---|
108 | * @param defectDescriptionFile
|
---|
109 | * .properties file
|
---|
110 | * @param props
|
---|
111 | * object, which stores the loaded values
|
---|
112 | */
|
---|
113 | private void loadProperties(Optional<File> defectDescriptionFile, Props props) {
|
---|
114 | try {
|
---|
115 | props.load(defectDescriptionFile.get());
|
---|
116 | }
|
---|
117 | catch (IOException e) {
|
---|
118 | Console.logException(e);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * <p>
|
---|
124 | * Returns all existing properties for a given usability rule.
|
---|
125 | * </p>
|
---|
126 | *
|
---|
127 | * @param allProperties
|
---|
128 | * all properties available
|
---|
129 | * @param usabilityRuleName
|
---|
130 | * name of usability rule
|
---|
131 | * @return all properties of certain usability rule
|
---|
132 | */
|
---|
133 | private Map<String, String> allUsabilityRuleProperties(Props allProperties,
|
---|
134 | String usabilityRuleName)
|
---|
135 | {
|
---|
136 | Map<String, String> usabilityRuleProperties = Maps.newHashMap();
|
---|
137 | allProperties.extractSubProps(usabilityRuleProperties, usabilityRuleName + ".*");
|
---|
138 | return usabilityRuleProperties;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * <p>
|
---|
143 | * Creates the usability defect.
|
---|
144 | * </p>
|
---|
145 | *
|
---|
146 | * @param usabilityRuleProperties
|
---|
147 | * all properties needed for creation.
|
---|
148 | * @return defect description for a usability rule
|
---|
149 | */
|
---|
150 | private UsabilityProblemDescription createUsabilityDefect(Map<String, String> usabilityRuleProperties)
|
---|
151 | {
|
---|
152 | String description =
|
---|
153 | Iterables.getOnlyElement(Maps
|
---|
154 | .filterKeys(usabilityRuleProperties, descriptionProperty()).values());
|
---|
155 | EnumMap<UsabilityProblemSeverityLevel, Double> severity =
|
---|
156 | getSeverityMap(usabilityRuleProperties);
|
---|
157 | return new UsabilityProblemDescription(description, severity);
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * <p>
|
---|
162 | * Gets the description property.
|
---|
163 | * </p>
|
---|
164 | *
|
---|
165 | * @return description property
|
---|
166 | */
|
---|
167 | private Predicate<String> descriptionProperty() {
|
---|
168 | return new Predicate<String>() {
|
---|
169 |
|
---|
170 | public boolean apply(String key) {
|
---|
171 | return key.endsWith("description");
|
---|
172 | }
|
---|
173 |
|
---|
174 | };
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * <p>
|
---|
179 | * Creates severity level map for defect description, by matching all entried from .properties
|
---|
180 | * file to corresponding {@link UsabilityProblemSeverityLevel}.
|
---|
181 | * </p>
|
---|
182 | *
|
---|
183 | * @param usabilityRuleProperties
|
---|
184 | * all properties of certain usability rule
|
---|
185 | * @return assignment of {@link UsabilityProblemSeverityLevel} and corresponding threshold
|
---|
186 | */
|
---|
187 | private EnumMap<UsabilityProblemSeverityLevel, Double> getSeverityMap(Map<String, String> usabilityRuleProperties)
|
---|
188 | {
|
---|
189 | EnumMap<UsabilityProblemSeverityLevel, Double> severityMap =
|
---|
190 | Maps.newEnumMap(UsabilityProblemSeverityLevel.class);
|
---|
191 | Map<String, String> allSeverityProperties =
|
---|
192 | Maps.filterEntries(usabilityRuleProperties, allSeverityProperties());
|
---|
193 | for (Entry<String, String> severityProperty : allSeverityProperties.entrySet()) {
|
---|
194 | UsabilityProblemSeverityLevel severityLevel =
|
---|
195 | getSeverityLevel(severityProperty.getKey());
|
---|
196 | Double rule = Double.valueOf(severityProperty.getValue());
|
---|
197 | severityMap.put(severityLevel, rule);
|
---|
198 | }
|
---|
199 | return severityMap;
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * <p>
|
---|
204 | * Matches severity level from .properties file against {@link UsabilityProblemSeverityLevel}.
|
---|
205 | * </p>
|
---|
206 | *
|
---|
207 | * @param severityProperty
|
---|
208 | * severity level from .properties file
|
---|
209 | * @return matching {@link UsabilityProblemSeverityLevel}
|
---|
210 | */
|
---|
211 | private UsabilityProblemSeverityLevel getSeverityLevel(String severityProperty) {
|
---|
212 | int startSeverityLevel = severityProperty.lastIndexOf(".") + 1;
|
---|
213 | String severityLevelIdentifier =
|
---|
214 | severityProperty.substring(startSeverityLevel).toUpperCase();
|
---|
215 | return UsabilityProblemSeverityLevel.valueOf(severityLevelIdentifier);
|
---|
216 | }
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * <p>
|
---|
220 | * Gets the severity level properties.
|
---|
221 | * </p>
|
---|
222 | *
|
---|
223 | * @return severity level
|
---|
224 | */
|
---|
225 | private Predicate<Entry<String, String>> allSeverityProperties() {
|
---|
226 | return new Predicate<Map.Entry<String, String>>() {
|
---|
227 |
|
---|
228 | public boolean apply(Map.Entry<String, String> entry) {
|
---|
229 | return entry.getKey().contains("severity");
|
---|
230 | }
|
---|
231 |
|
---|
232 | };
|
---|
233 | }
|
---|
234 |
|
---|
235 | }
|
---|