1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */ 
17   
18  package org.apache.commons.betwixt.strategy;
19  
20  import java.util.HashMap;
21  
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  
26  import org.apache.commons.betwixt.ElementDescriptor;
27  
28  /**
29   * Tests the defaultPluralStemmer
30   * 
31   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
32   * @version $Id: TestDefaultPluralStemmer.java 438373 2006-08-30 05:17:21Z bayard $
33   */
34  public class TestDefaultPluralStemmer extends TestCase
35  {
36  
37      public static Test suite() {
38          return new TestSuite(TestDefaultPluralStemmer.class);
39      }
40  
41      public TestDefaultPluralStemmer(String testName)  {
42          super(testName);
43      }
44      
45      public void testNullMap() {
46          DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
47          try {
48              stemmer.findPluralDescriptor("test", null);
49              fail("Should throw a nullpointer exception, since the map in the stemmer cannot be null");
50          }catch(NullPointerException npe) {
51          }
52      }
53      
54      /**
55       * This is the first match when calling the defaultStemmer.
56       * It just adds an s to the the property and it should find it..
57       */
58      public void testFirstMatch() {
59          
60          ElementDescriptor des = new ElementDescriptor();
61          des.setQualifiedName("FooBars");
62          des.setPropertyType(java.util.List.class);
63          HashMap map = new HashMap();
64          map.put("FooBars", des);
65          DefaultPluralStemmer dps = new DefaultPluralStemmer();
66          ElementDescriptor result = dps.findPluralDescriptor("FooBar", map);
67          assertEquals(des, result);
68      }
69      /**
70       * Tests if the y is nicely replaces with ies and the correct
71       * ElementDescriptor is returned
72       */
73      public void testSecondMatch() {
74          ElementDescriptor des = new ElementDescriptor();
75          des.setQualifiedName("FooBary");
76          des.setPropertyType(java.util.List.class);
77          HashMap map = new HashMap();
78          map.put("FooBaries", des);
79          DefaultPluralStemmer dps = new DefaultPluralStemmer();
80          ElementDescriptor result = dps.findPluralDescriptor("FooBary", map);
81          assertEquals(des, result);
82      }
83      
84      /**
85       * Tests if it actually skips the y if the length not greater than 1.
86       */
87      public void testSecondNonMatch() {
88          ElementDescriptor des = new ElementDescriptor();
89          des.setQualifiedName("y");
90          des.setPropertyType(java.util.List.class);
91          HashMap map = new HashMap();
92          map.put("yies", des);
93          DefaultPluralStemmer dps = new DefaultPluralStemmer();
94          ElementDescriptor result = dps.findPluralDescriptor("y", map);
95          assertNotNull(result);
96      }
97      
98      /**
99       * Uses the third if in pluralstemmer.
100      * It should return the specified y, without any changing.
101      */
102     public void testThirdMatch() {
103         ElementDescriptor des = new ElementDescriptor();
104         des.setQualifiedName("y");
105         des.setPropertyType(java.util.List.class);
106         HashMap map = new HashMap();
107         map.put("y", des);
108         DefaultPluralStemmer dps = new DefaultPluralStemmer();
109         ElementDescriptor result = dps.findPluralDescriptor("y", map);
110         assertEquals(des, result);
111     }
112    
113     /**
114      * Tests to see if you get warned when there are multiple matches
115      * found
116      */
117     public void testMultipleMatches() {
118         ElementDescriptor des = new ElementDescriptor();
119         des.setQualifiedName("y");
120         des.setPropertyType(java.util.List.class);
121         ElementDescriptor desyes = new ElementDescriptor();
122         desyes.setQualifiedName("yes");
123         desyes.setPropertyType(java.util.List.class);
124         ElementDescriptor desyesno = new ElementDescriptor();
125         desyesno.setQualifiedName("yesno");
126         desyesno.setPropertyType(java.util.List.class);
127         HashMap map = new HashMap();
128         map.put("y", des);
129         map.put("yes", desyes);
130         map.put("yesno", desyesno);
131         DefaultPluralStemmer dps = new DefaultPluralStemmer();
132         ElementDescriptor result = dps.findPluralDescriptor("y", map);
133         assertEquals(des, result);
134         result = dps.findPluralDescriptor("yes", map);
135         assertEquals(desyes, result);
136         result = dps.findPluralDescriptor("yesno", map);
137         assertEquals(desyesno, result);
138     }
139    
140     /**
141      *  Test to find matched where plural ending is "es" 
142      */
143     public void testESPluralEndingMatch() {
144         HashMap map = new HashMap();
145 
146         ElementDescriptor index = new ElementDescriptor("index", "index","");
147         map.put("index", index);
148         ElementDescriptor indexes = new ElementDescriptor("indexes", "indexes","");
149         map.put("indexes", indexes);
150 
151         ElementDescriptor patch = new ElementDescriptor("patch", "patch","");
152         map.put("patch", patch);
153         ElementDescriptor patches = new ElementDescriptor("patches", "patches","");
154         map.put("patches", patches);
155 
156         DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
157         ElementDescriptor result = stemmer.findPluralDescriptor("index", map);
158         assertEquals(indexes, result);
159 
160         result = stemmer.findPluralDescriptor("patches", map);
161         assertEquals(patches, result);
162     }
163  
164     /**
165      *  Test if the closest match mechanisme is working
166      */
167     public void testClosestMatch() {
168         HashMap map = new HashMap();
169         ElementDescriptor yes1 = new ElementDescriptor("yes1", "yes1","");
170         map.put("yes1", yes1);
171         ElementDescriptor yes12 = new ElementDescriptor("yes12", "yes12","");
172         map.put("yes12", yes12);
173         ElementDescriptor yes123 = new ElementDescriptor("yes123", "yes123","");
174         map.put("yes123", yes123);
175         DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
176         ElementDescriptor result = stemmer.findPluralDescriptor("yes", map);
177         assertEquals(yes1, result);
178     }    
179     
180 }
181