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.jcontainer.dna.ConfigurationException;
36  import org.objectledge.filesystem.FileSystem;
37  import org.objectledge.utils.LedgeTestCase;
38  import org.objectledge.xml.XMLGrammarCache;
39  import org.objectledge.xml.XMLValidator;
40  import org.xml.sax.SAXParseException;
41  
42  /***
43   * 
44   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
45   * @version $Id: SelectorTest.java,v 1.5 2005/11/16 23:38:15 zwierzem Exp $
46   */
47  public class SelectorTest extends LedgeTestCase
48  {
49      public Configuration getSelectorConfig(String name)
50          throws Exception
51      {
52          FileSystem fs = FileSystem.getStandardFileSystem("src/test/resources");
53          String configPath = "selector/"+name;
54          URL configUrl = fs.getResource(configPath);
55          URL schemaUrl = fs.getResource("org/objectledge/selector/Selector.rng");
56          if(configUrl == null || schemaUrl == null)
57          {
58              throw new Exception("fs config invalid, or files missing");
59          }
60          XMLValidator validator = new XMLValidator(new XMLGrammarCache());
61          try
62          {
63              validator.validate(configUrl, schemaUrl);
64          }
65          catch(SAXParseException e)
66          {
67              throw new Exception("parser error "+e.getMessage()+" in "+e.getSystemId()+" at line "+
68                  e.getLineNumber(), e);
69          }
70          return getConfig(fs, configPath);
71      }
72  
73      public void testSelector()
74          throws Exception
75      {
76          Configuration config = getSelectorConfig("Selector.xml");
77          Object[] objects = new Object[3];
78          objects[0] = new Integer(0);
79          objects[1] = new Integer(1);
80          objects[2] = new Integer(2);
81          Selector selector = new Selector(config, objects);
82          Map values = new HashMap();
83          Variables vars = new IntrospectionVariables(values);
84          values.put("number", new Integer(0));
85          Integer i = (Integer)selector.select(vars);
86          assertEquals(0, i.intValue());
87          values.put("number", new Integer(1));
88          i = (Integer)selector.select(vars);
89          assertEquals(1, i.intValue());
90          values.put("number", new Integer(99));
91          i = (Integer)selector.select(vars);
92          assertEquals(2, i.intValue());
93      }
94      
95      public void testFallthrough()
96          throws Exception
97      {
98          Configuration config = getSelectorConfig("Fallthrough.xml");
99          Object[] objects = new Object[2];
100         objects[0] = new Integer(0);
101         objects[1] = new Integer(1);
102         Selector selector = new Selector(config, objects);
103         Map values = new HashMap();
104         Variables vars = new IntrospectionVariables(values);
105         values.put("number", new Integer(0));
106         Integer i = (Integer)selector.select(vars);
107         assertEquals(0, i.intValue());
108         values.put("number", new Integer(1));
109         i = (Integer)selector.select(vars);
110         assertEquals(1, i.intValue());
111         values.put("number", new Integer(99));
112         i = (Integer)selector.select(vars);
113         assertNull(i);                
114     }
115     
116     public void testUnmatched()
117         throws Exception
118     {
119         Configuration config = getSelectorConfig("Selector.xml");
120         Object[] objects = new Object[2];
121         objects[0] = new Integer(0);
122         objects[1] = new Integer(1);
123         try
124         {
125             new Selector(config, objects);
126             fail("exception expected");
127         }
128         catch(Exception e)
129         {
130             assertEquals(ConfigurationException.class, e.getClass());
131         }
132     }
133 }