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 | * TODO comment
|
---|
36 | * </p>
|
---|
37 | *
|
---|
38 | * @author Alexander Deicke
|
---|
39 | */
|
---|
40 | public class DefectDescriptionResolver {
|
---|
41 |
|
---|
42 | private final String defectDescriptionFile = "defects.props";
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * <p>
|
---|
46 | * TODO: comment
|
---|
47 | * </p>
|
---|
48 | *
|
---|
49 | * @param name
|
---|
50 | * @return
|
---|
51 | */
|
---|
52 | public UsabilityDefect descriptionFor(String usabilityRuleName) {
|
---|
53 | Props allProperties = initProperties();
|
---|
54 | Map<String, String> usabilityRuleProperties =
|
---|
55 | allUsabilityRuleProperties(allProperties, usabilityRuleName);
|
---|
56 | return createUsabilityDefect(usabilityRuleProperties);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * <p>
|
---|
61 | * TODO: comment
|
---|
62 | * </p>
|
---|
63 | *
|
---|
64 | * @return
|
---|
65 | */
|
---|
66 | private Props initProperties() {
|
---|
67 | Optional<File> defectDescriptionFile = getDefectDescriptionFile();
|
---|
68 | Props props = new Props();
|
---|
69 | props.setEscapeNewLineValue("\n");
|
---|
70 | props.setValueTrimLeft(true);
|
---|
71 | if (defectDescriptionFile.isPresent()) {
|
---|
72 | loadProperties(defectDescriptionFile, props);
|
---|
73 | }
|
---|
74 | return props;
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * <p>
|
---|
79 | * TODO: comment
|
---|
80 | * </p>
|
---|
81 | *
|
---|
82 | * @return
|
---|
83 | */
|
---|
84 | private Optional<File> getDefectDescriptionFile() {
|
---|
85 | try {
|
---|
86 | return Optional.fromNullable(new File(ClassLoader
|
---|
87 | .getSystemResource(defectDescriptionFile).toURI()));
|
---|
88 | }
|
---|
89 | catch (URISyntaxException e) {
|
---|
90 | Console.printerr("Error while loading defect description file.");
|
---|
91 | Console.logException(e);
|
---|
92 | return Optional.absent();
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | private void loadProperties(Optional<File> defectDescriptionFile, Props props) {
|
---|
97 | try {
|
---|
98 | props.load(defectDescriptionFile.get());
|
---|
99 | }
|
---|
100 | catch (IOException e) {
|
---|
101 | Console.logException(e);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * <p>
|
---|
107 | * TODO: comment
|
---|
108 | * </p>
|
---|
109 | *
|
---|
110 | * @return
|
---|
111 | */
|
---|
112 | private Map<String, String> allUsabilityRuleProperties(Props allProperties,
|
---|
113 | String usabilityRuleName)
|
---|
114 | {
|
---|
115 | Map<String, String> usabilityRuleProperties = Maps.newHashMap();
|
---|
116 | allProperties.extractSubProps(usabilityRuleProperties, usabilityRuleName + ".*");
|
---|
117 | return usabilityRuleProperties;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * <p>
|
---|
122 | * TODO: comment
|
---|
123 | * </p>
|
---|
124 | *
|
---|
125 | * @param usabilityRuleProperties
|
---|
126 | * @return
|
---|
127 | */
|
---|
128 | private UsabilityDefect createUsabilityDefect(Map<String, String> usabilityRuleProperties) {
|
---|
129 | String description =
|
---|
130 | Iterables.getOnlyElement(Maps.filterKeys(usabilityRuleProperties, descriptionProperty()).values());
|
---|
131 | EnumMap<UsabilityDefectSeverityLevel, Double> severity =
|
---|
132 | getSeverityMap(usabilityRuleProperties);
|
---|
133 | return new UsabilityDefect(description, severity);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * <p>
|
---|
138 | * TODO: comment
|
---|
139 | * </p>
|
---|
140 | *
|
---|
141 | * @return
|
---|
142 | */
|
---|
143 | private Predicate<String> descriptionProperty() {
|
---|
144 | return new Predicate<String>() {
|
---|
145 |
|
---|
146 | public boolean apply(String key) {
|
---|
147 | return key.endsWith("description");
|
---|
148 | }
|
---|
149 |
|
---|
150 | };
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * <p>
|
---|
155 | * TODO: comment
|
---|
156 | * </p>
|
---|
157 | *
|
---|
158 | * @param usabilityRuleProperties
|
---|
159 | * @return
|
---|
160 | */
|
---|
161 | private EnumMap<UsabilityDefectSeverityLevel, Double> getSeverityMap(Map<String, String> usabilityRuleProperties)
|
---|
162 | {
|
---|
163 | EnumMap<UsabilityDefectSeverityLevel, Double> severityMap =
|
---|
164 | Maps.newEnumMap(UsabilityDefectSeverityLevel.class);
|
---|
165 | Map<String, String> allSeverityProperties =
|
---|
166 | Maps.filterEntries(usabilityRuleProperties, allSeverityProperties());
|
---|
167 | for (Entry<String, String> severityProperty : allSeverityProperties.entrySet()) {
|
---|
168 | UsabilityDefectSeverityLevel severityLevel =
|
---|
169 | getSeverityLevel(severityProperty.getKey());
|
---|
170 | Double rule = Double.valueOf(severityProperty.getValue());
|
---|
171 | severityMap.put(severityLevel, rule);
|
---|
172 | }
|
---|
173 | return severityMap;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * <p>
|
---|
178 | * TODO: comment
|
---|
179 | * </p>
|
---|
180 | *
|
---|
181 | * @param key
|
---|
182 | * @return
|
---|
183 | */
|
---|
184 | private UsabilityDefectSeverityLevel getSeverityLevel(String severityProperty) {
|
---|
185 | int startSeverityLevel = severityProperty.lastIndexOf(".") + 1;
|
---|
186 | String severitLevelIdentifier =
|
---|
187 | severityProperty.substring(startSeverityLevel).toUpperCase();
|
---|
188 | return UsabilityDefectSeverityLevel.valueOf(severitLevelIdentifier);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * <p>
|
---|
193 | * TODO: comment
|
---|
194 | * </p>
|
---|
195 | *
|
---|
196 | * @return
|
---|
197 | */
|
---|
198 | private Predicate<Entry<String, String>> allSeverityProperties() {
|
---|
199 | return new Predicate<Map.Entry<String, String>>() {
|
---|
200 |
|
---|
201 | public boolean apply(Map.Entry<String, String> entry) {
|
---|
202 | return entry.getKey().contains("severity");
|
---|
203 | }
|
---|
204 |
|
---|
205 | };
|
---|
206 | }
|
---|
207 |
|
---|
208 | }
|
---|