1 //
2 // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
14 // nor the names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28 package org.objectledge.selector;
29
30 import java.net.URL;
31 import java.util.HashMap;
32 import java.util.Map;
33
34 import org.jcontainer.dna.Configuration;
35 import org.objectledge.filesystem.FileSystem;
36 import org.objectledge.utils.LedgeTestCase;
37 import org.objectledge.xml.XMLGrammarCache;
38 import org.objectledge.xml.XMLValidator;
39 import org.xml.sax.SAXParseException;
40
41 /***
42 *
43 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
44 * @version $Id: RuleTest.java,v 1.4 2005/11/16 23:38:15 zwierzem Exp $
45 */
46 public class RuleTest extends LedgeTestCase
47 {
48 public void testRules()
49 throws Exception
50 {
51 FileSystem fs = FileSystem.getStandardFileSystem("src/test/resources");
52 URL configUrl = fs.getResource("selector/RuleTest.xml");
53 URL schemaUrl = fs.getResource("org/objectledge/selector/RuleTest.rng");
54 if(configUrl == null || schemaUrl == null)
55 {
56 throw new Exception("fs config invalid, or files missing");
57 }
58 XMLValidator validator = new XMLValidator(new XMLGrammarCache());
59 try
60 {
61 validator.validate(configUrl, schemaUrl);
62 }
63 catch(SAXParseException e)
64 {
65 throw new Exception("parser error "+e.getMessage()+" in "+e.getSystemId()+" at line "+
66 e.getLineNumber(), e);
67 }
68 Configuration config = getConfig(fs, "selector/RuleTest.xml");
69
70 Configuration[] tests = config.getChildren("test");
71 Rule[] rules = new Rule[tests.length];
72 String[] expected = new String[tests.length];
73 for(int i=0; i<tests.length; i++)
74 {
75 rules[i] = new Rule(tests[i].getChild("rule"));
76 expected[i] = tests[i].getAttribute("result");
77 }
78
79 Map values = new HashMap();
80 values.put("string", "string");
81 values.put("string2", "string");
82 values.put("one", new Integer(1));
83 Variables vars = new MapVariables(values);
84
85 for(int i=0; i<tests.length; i++)
86 {
87 Rule rule = rules[i];
88 try
89 {
90 boolean result = rule.evaluate(vars);
91 if(expected[i].equals("true"))
92 {
93 assertTrue("rule #"+i, result);
94 }
95 else if(expected[i].equals("false"))
96 {
97 assertFalse("rule #"+i, result);
98 }
99 else if(expected[i].equals("exception"))
100 {
101 fail("rule #"+i+": exeption expected");
102 }
103 }
104 catch(Exception e)
105 {
106 if(!expected[i].equals("exception"))
107 {
108 throw e;
109 }
110 }
111 }
112 }
113 }