KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > css > engine > value > PropertyManagerTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.batik.css.engine.value;
18
19 import java.util.StringTokenizer JavaDoc;
20
21 import org.w3c.css.sac.LexicalUnit;
22
23 import org.apache.batik.css.engine.value.svg.MarkerManager;
24 import org.apache.batik.css.engine.value.svg.OpacityManager;
25 import org.apache.batik.css.engine.value.svg.SVGColorManager;
26 import org.apache.batik.css.engine.value.svg.SVGPaintManager;
27 import org.apache.batik.css.engine.value.svg.SpacingManager;
28 import org.apache.batik.css.parser.Parser;
29 import org.apache.batik.test.AbstractTest;
30 import org.apache.batik.test.DefaultTestReport;
31 import org.apache.batik.test.TestReport;
32 import org.apache.batik.util.CSSConstants;
33
34 /**
35  * The class to test the CSS properties's manager.
36  *
37  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
38  * @version $Id: PropertyManagerTest.java,v 1.4 2005/04/01 02:28:16 deweese Exp $
39  */

40 public class PropertyManagerTest extends AbstractTest {
41
42     /**
43      * The error code for the 'is inherited' test.
44      */

45     public static final String JavaDoc ERROR_IS_INHERITED =
46         "PropertyManagerTest.error.inherited";
47
48     /**
49      * The error code if the property does not support the 'inherit' value.
50      */

51     public static final String JavaDoc ERROR_INHERIT_VALUE =
52         "PropertyManagerTest.error.inherit.value";
53
54     /**
55      * The error code for the 'default value' test.
56      */

57     public static final String JavaDoc ERROR_INVALID_DEFAULT_VALUE =
58         "PropertyManagerTest.error.invalid.default.value";
59
60     /**
61      * The error code for an invalid property value.
62      */

63     public static final String JavaDoc ERROR_INVALID_VALUE =
64         "PropertyManagerTest.error.invalid.value";
65
66     /**
67      * The error code if an exception occured while creating the manager.
68      */

69     public static final String JavaDoc ERROR_INSTANTIATION =
70         "PropertyManagerTest.error.instantiation";
71
72     /**
73      * The class of the manager.
74      */

75     protected String JavaDoc managerClassName;
76
77     /**
78      * This flag bit indicates whether or not the property is inherited.
79      */

80     protected Boolean JavaDoc isInherited;
81
82     /**
83      * The candidate values of the property.
84      */

85     protected String JavaDoc [] identValues;
86
87     /**
88      * The candidate default value of the property.
89      */

90     protected String JavaDoc defaultValue;
91
92     /**
93      * Constructs a new test for the specified manager classname.
94      *
95      * @param managerClassName the classname of the manager to test
96      * @param isInherited the expected flag to see if the property is inherited
97      * @param defaultValue the default value
98      * @param identValueList the list of possible identifiers
99      */

100     public PropertyManagerTest(String JavaDoc managerClassName,
101                                Boolean JavaDoc isInherited,
102                                String JavaDoc defaultValue,
103                                String JavaDoc identValueList) {
104         this.managerClassName = managerClassName;
105         this.isInherited = isInherited;
106         this.defaultValue = defaultValue;
107         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(identValueList, "|");
108         int nbIdentValue = tokens.countTokens();
109         if (nbIdentValue > 0) {
110             identValues = new String JavaDoc[nbIdentValue];
111             for (int i=0; tokens.hasMoreTokens(); ++i) {
112                 identValues[i] = tokens.nextToken().trim();
113             }
114         }
115     }
116
117     /**
118      * Creates the value manager.
119      */

120     protected ValueManager createValueManager() throws Exception JavaDoc {
121         return (ValueManager)Class.forName(managerClassName).newInstance();
122     }
123
124     /**
125      * Runs this test. This method will only throw exceptions if some aspect of
126      * the test's internal operation fails.
127      */

128     public TestReport runImpl() throws Exception JavaDoc {
129         DefaultTestReport report = new DefaultTestReport(this);
130
131         ValueManager manager;
132         try {
133             manager = createValueManager();
134         } catch (Exception JavaDoc ex) {
135             report.setErrorCode(ERROR_INSTANTIATION);
136             report.setPassed(false);
137             report.addDescriptionEntry(ERROR_INSTANTIATION, ex.getMessage());
138             return report;
139         }
140
141         // test default value if any
142
if (!defaultValue.equals("__USER_AGENT__")) {
143             String JavaDoc s = manager.getDefaultValue().getCssText();
144             if (!defaultValue.equalsIgnoreCase(s)) {
145                 report.setErrorCode(ERROR_INVALID_DEFAULT_VALUE);
146                 report.setPassed(false);
147                 report.addDescriptionEntry(ERROR_INVALID_DEFAULT_VALUE,
148                                            "should be: "+defaultValue);
149             }
150         }
151
152         // test if the property is inherited or not
153
if (isInherited.booleanValue() != manager.isInheritedProperty()) {
154             report.setErrorCode(ERROR_IS_INHERITED);
155             report.setPassed(false);
156             report.addDescriptionEntry(ERROR_IS_INHERITED, "");
157         }
158
159         Parser cssParser = new Parser();
160         // see if the property supports the value 'inherit'
161
try {
162             LexicalUnit lu = cssParser.parsePropertyValue("inherit");
163             Value v = manager.createValue(lu, null);
164             String JavaDoc s = v.getCssText();
165             if (!"inherit".equalsIgnoreCase(s)) {
166                 report.setErrorCode(ERROR_INHERIT_VALUE);
167                 report.setPassed(false);
168                 report.addDescriptionEntry(ERROR_INHERIT_VALUE, "inherit");
169             }
170         } catch (Exception JavaDoc ex) {
171             report.setErrorCode(ERROR_INHERIT_VALUE);
172             report.setPassed(false);
173             report.addDescriptionEntry(ERROR_INHERIT_VALUE, ex.getMessage());
174         }
175
176         // test all possible identifiers
177
if (identValues != null) {
178             try {
179                 for (int i=0; i < identValues.length; ++i) {
180                     LexicalUnit lu = cssParser.parsePropertyValue(identValues[i]);
181                     Value v = manager.createValue(lu, null);
182                     String JavaDoc s = v.getCssText();
183                     if (!identValues[i].equalsIgnoreCase(s)) {
184                         report.setErrorCode(ERROR_INVALID_VALUE);
185                         report.setPassed(false);
186                         report.addDescriptionEntry(ERROR_INVALID_VALUE,
187                                                    identValues[i]+"/"+s);
188                     }
189                 }
190             } catch (Exception JavaDoc ex) {
191                 report.setErrorCode(ERROR_INVALID_VALUE);
192                 report.setPassed(false);
193                 report.addDescriptionEntry(ERROR_INVALID_VALUE,
194                                            ex.getMessage());
195             }
196         }
197         return report;
198     }
199
200     /**
201      * Manager for 'fill'.
202      */

203     public static class FillManager extends SVGPaintManager {
204         public FillManager() {
205             super(CSSConstants.CSS_FILL_PROPERTY);
206         }
207     }
208
209     /**
210      * Manager for 'fill-opacity'.
211      */

212     public static class FillOpacityManager extends OpacityManager {
213         public FillOpacityManager() {
214             super(CSSConstants.CSS_FILL_OPACITY_PROPERTY, true);
215         }
216     }
217
218     /**
219      * Manager for 'flood-color'.
220      */

221     public static class FloodColorManager extends SVGColorManager {
222         public FloodColorManager() {
223             super(CSSConstants.CSS_FLOOD_COLOR_PROPERTY);
224         }
225     }
226
227     /**
228      * Manager for 'flood-opacity'.
229      */

230     public static class FloodOpacityManager extends OpacityManager {
231         public FloodOpacityManager() {
232             super(CSSConstants.CSS_FLOOD_OPACITY_PROPERTY, false);
233         }
234     }
235
236     /**
237      * Manager for 'letter-spacing'.
238      */

239     public static class LetterSpacingManager extends SpacingManager {
240         public LetterSpacingManager() {
241             super(CSSConstants.CSS_LETTER_SPACING_PROPERTY);
242         }
243     }
244
245     /**
246      * Manager for 'lighting-color'.
247      */

248     public static class LightingColorManager extends SVGColorManager {
249         public LightingColorManager() {
250             super(CSSConstants.CSS_LIGHTING_COLOR_PROPERTY, ValueConstants.WHITE_RGB_VALUE);
251         }
252     }
253
254     /**
255      * Manager for 'marker-end'.
256      */

257     public static class MarkerEndManager extends MarkerManager {
258         public MarkerEndManager() {
259             super(CSSConstants.CSS_MARKER_END_PROPERTY);
260         }
261     }
262
263     /**
264      * Manager for 'marker-mid'.
265      */

266     public static class MarkerMidManager extends MarkerManager {
267         public MarkerMidManager() {
268             super(CSSConstants.CSS_MARKER_MID_PROPERTY);
269         }
270     }
271
272     /**
273      * Manager for 'marker-start'.
274      */

275     public static class MarkerStartManager extends MarkerManager {
276         public MarkerStartManager() {
277             super(CSSConstants.CSS_MARKER_START_PROPERTY);
278         }
279     }
280
281     /**
282      * Manager for 'opacity'.
283      */

284     public static class DefaultOpacityManager extends OpacityManager {
285         public DefaultOpacityManager() {
286             super(CSSConstants.CSS_OPACITY_PROPERTY, false);
287         }
288     }
289
290     /**
291      * Manager for 'stop-color'.
292      */

293     public static class StopColorManager extends SVGColorManager {
294         public StopColorManager() {
295             super(CSSConstants.CSS_STOP_COLOR_PROPERTY);
296         }
297     }
298
299     /**
300      * Manager for 'stop-opacity'.
301      */

302     public static class StopOpacityManager extends OpacityManager {
303         public StopOpacityManager() {
304             super(CSSConstants.CSS_STOP_OPACITY_PROPERTY, false);
305         }
306     }
307
308     /**
309      * Manager for 'stroke'.
310      */

311     public static class StrokeManager extends SVGPaintManager {
312         public StrokeManager() {
313             super(CSSConstants.CSS_STROKE_PROPERTY, ValueConstants.NONE_VALUE);
314         }
315     }
316
317     /**
318      * Manager for 'stroke-opacity'.
319      */

320     public static class StrokeOpacityManager extends OpacityManager {
321         public StrokeOpacityManager() {
322             super(CSSConstants.CSS_STROKE_OPACITY_PROPERTY, true);
323         }
324     }
325
326     /**
327      * Manager for 'word-spacing'.
328      */

329     public static class WordSpacingManager extends SpacingManager {
330         public WordSpacingManager() {
331             super(CSSConstants.CSS_WORD_SPACING_PROPERTY);
332         }
333     }
334 }
335
Popular Tags