1   /*
2    * Copyright 2001-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.commons.betwixt.strategy;
18  
19  import java.util.HashMap;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  
25  import org.apache.commons.betwixt.ElementDescriptor;
26  
27  /***
28   * Tests the defaultPluralStemmer
29   * 
30   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
31   * @version $Id: TestDefaultPluralStemmer.java,v 1.6 2004/02/28 13:38:36 yoavs Exp $
32   */
33  public class TestDefaultPluralStemmer extends TestCase
34  {
35  
36      public static Test suite() {
37          return new TestSuite(TestDefaultPluralStemmer.class);
38      }
39  
40      public TestDefaultPluralStemmer(String testName)  {
41          super(testName);
42      }
43      
44      public void testNullMap() {
45          DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
46          try {
47              stemmer.findPluralDescriptor("test", null);
48              fail("Should throw a nullpointer exception, since the map in the stemmer cannot be null");
49          }catch(NullPointerException npe) {
50          }
51      }
52      
53      /***
54       * This is the first match when calling the defaultStemmer.
55       * It just adds an s to the the property and it should find it..
56       */
57      public void testFirstMatch() {
58          
59          ElementDescriptor des = new ElementDescriptor();
60          des.setQualifiedName("FooBars");
61          des.setPropertyType(java.util.List.class);
62          HashMap map = new HashMap();
63          map.put("FooBars", des);
64          DefaultPluralStemmer dps = new DefaultPluralStemmer();
65          ElementDescriptor result = dps.findPluralDescriptor("FooBar", map);
66          assertEquals(des, result);
67      }
68      /***
69       * Tests if the y is nicely replaces with ies and the correct
70       * ElementDescriptor is returned
71       */
72      public void testSecondMatch() {
73          ElementDescriptor des = new ElementDescriptor();
74          des.setQualifiedName("FooBary");
75          des.setPropertyType(java.util.List.class);
76          HashMap map = new HashMap();
77          map.put("FooBaries", des);
78          DefaultPluralStemmer dps = new DefaultPluralStemmer();
79          ElementDescriptor result = dps.findPluralDescriptor("FooBary", map);
80          assertEquals(des, result);
81      }
82      
83      /***
84       * Tests if it actually skips the y if the length not greater than 1.
85       */
86      public void testSecondNonMatch() {
87          ElementDescriptor des = new ElementDescriptor();
88          des.setQualifiedName("y");
89          des.setPropertyType(java.util.List.class);
90          HashMap map = new HashMap();
91          map.put("yies", des);
92          DefaultPluralStemmer dps = new DefaultPluralStemmer();
93          ElementDescriptor result = dps.findPluralDescriptor("y", map);
94          assertNotNull(result);
95      }
96      
97      /***
98       * Uses the third if in pluralstemmer.
99       * It should return the specified y, without any changing.
100      */
101     public void testThirdMatch() {
102         ElementDescriptor des = new ElementDescriptor();
103         des.setQualifiedName("y");
104         des.setPropertyType(java.util.List.class);
105         HashMap map = new HashMap();
106         map.put("y", des);
107         DefaultPluralStemmer dps = new DefaultPluralStemmer();
108         ElementDescriptor result = dps.findPluralDescriptor("y", map);
109         assertEquals(des, result);
110     }
111    
112     /***
113      * Tests to see if you get warned when there are multiple matches
114      * found
115      */
116     public void testMultipleMatches() {
117         ElementDescriptor des = new ElementDescriptor();
118         des.setQualifiedName("y");
119         des.setPropertyType(java.util.List.class);
120         ElementDescriptor desyes = new ElementDescriptor();
121         desyes.setQualifiedName("yes");
122         desyes.setPropertyType(java.util.List.class);
123         ElementDescriptor desyesno = new ElementDescriptor();
124         desyesno.setQualifiedName("yesno");
125         desyesno.setPropertyType(java.util.List.class);
126         HashMap map = new HashMap();
127         map.put("y", des);
128         map.put("yes", desyes);
129         map.put("yesno", desyesno);
130         DefaultPluralStemmer dps = new DefaultPluralStemmer();
131         ElementDescriptor result = dps.findPluralDescriptor("y", map);
132         assertEquals(des, result);
133         result = dps.findPluralDescriptor("yes", map);
134         assertEquals(desyes, result);
135         result = dps.findPluralDescriptor("yesno", map);
136         assertEquals(desyesno, result);
137     }
138    
139     /***
140      *  Test to find matched where plural ending is "es" 
141      */
142     public void testESPluralEndingMatch() {
143         HashMap map = new HashMap();
144 
145         ElementDescriptor index = new ElementDescriptor("index", "index","");
146         map.put("index", index);
147         ElementDescriptor indexes = new ElementDescriptor("indexes", "indexes","");
148         map.put("indexes", indexes);
149 
150         ElementDescriptor patch = new ElementDescriptor("patch", "patch","");
151         map.put("patch", patch);
152         ElementDescriptor patches = new ElementDescriptor("patches", "patches","");
153         map.put("patches", patches);
154 
155         DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
156         ElementDescriptor result = stemmer.findPluralDescriptor("index", map);
157         assertEquals(indexes, result);
158 
159         result = stemmer.findPluralDescriptor("patches", map);
160         assertEquals(patches, result);
161     }
162  
163     /***
164      *  Test if the closest match mechanisme is working
165      */
166     public void testClosestMatch() {
167         HashMap map = new HashMap();
168         ElementDescriptor yes1 = new ElementDescriptor("yes1", "yes1","");
169         map.put("yes1", yes1);
170         ElementDescriptor yes12 = new ElementDescriptor("yes12", "yes12","");
171         map.put("yes12", yes12);
172         ElementDescriptor yes123 = new ElementDescriptor("yes123", "yes123","");
173         map.put("yes123", yes123);
174         DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
175         ElementDescriptor result = stemmer.findPluralDescriptor("yes", map);
176         assertEquals(yes1, result);
177     }    
178     
179 }
180