001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.strategy;
019:
020: import java.util.HashMap;
021:
022: import junit.framework.Test;
023: import junit.framework.TestCase;
024: import junit.framework.TestSuite;
025:
026: import org.apache.commons.betwixt.ElementDescriptor;
027:
028: /**
029: * Tests the defaultPluralStemmer
030: *
031: * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
032: * @version $Id: TestDefaultPluralStemmer.java 438373 2006-08-30 05:17:21Z bayard $
033: */
034: public class TestDefaultPluralStemmer extends TestCase {
035:
036: public static Test suite() {
037: return new TestSuite(TestDefaultPluralStemmer.class);
038: }
039:
040: public TestDefaultPluralStemmer(String testName) {
041: super (testName);
042: }
043:
044: public void testNullMap() {
045: DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
046: try {
047: stemmer.findPluralDescriptor("test", null);
048: fail("Should throw a nullpointer exception, since the map in the stemmer cannot be null");
049: } catch (NullPointerException npe) {
050: }
051: }
052:
053: /**
054: * This is the first match when calling the defaultStemmer.
055: * It just adds an s to the the property and it should find it..
056: */
057: public void testFirstMatch() {
058:
059: ElementDescriptor des = new ElementDescriptor();
060: des.setQualifiedName("FooBars");
061: des.setPropertyType(java.util.List.class);
062: HashMap map = new HashMap();
063: map.put("FooBars", des);
064: DefaultPluralStemmer dps = new DefaultPluralStemmer();
065: ElementDescriptor result = dps.findPluralDescriptor("FooBar",
066: map);
067: assertEquals(des, result);
068: }
069:
070: /**
071: * Tests if the y is nicely replaces with ies and the correct
072: * ElementDescriptor is returned
073: */
074: public void testSecondMatch() {
075: ElementDescriptor des = new ElementDescriptor();
076: des.setQualifiedName("FooBary");
077: des.setPropertyType(java.util.List.class);
078: HashMap map = new HashMap();
079: map.put("FooBaries", des);
080: DefaultPluralStemmer dps = new DefaultPluralStemmer();
081: ElementDescriptor result = dps.findPluralDescriptor("FooBary",
082: map);
083: assertEquals(des, result);
084: }
085:
086: /**
087: * Tests if it actually skips the y if the length not greater than 1.
088: */
089: public void testSecondNonMatch() {
090: ElementDescriptor des = new ElementDescriptor();
091: des.setQualifiedName("y");
092: des.setPropertyType(java.util.List.class);
093: HashMap map = new HashMap();
094: map.put("yies", des);
095: DefaultPluralStemmer dps = new DefaultPluralStemmer();
096: ElementDescriptor result = dps.findPluralDescriptor("y", map);
097: assertNotNull(result);
098: }
099:
100: /**
101: * Uses the third if in pluralstemmer.
102: * It should return the specified y, without any changing.
103: */
104: public void testThirdMatch() {
105: ElementDescriptor des = new ElementDescriptor();
106: des.setQualifiedName("y");
107: des.setPropertyType(java.util.List.class);
108: HashMap map = new HashMap();
109: map.put("y", des);
110: DefaultPluralStemmer dps = new DefaultPluralStemmer();
111: ElementDescriptor result = dps.findPluralDescriptor("y", map);
112: assertEquals(des, result);
113: }
114:
115: /**
116: * Tests to see if you get warned when there are multiple matches
117: * found
118: */
119: public void testMultipleMatches() {
120: ElementDescriptor des = new ElementDescriptor();
121: des.setQualifiedName("y");
122: des.setPropertyType(java.util.List.class);
123: ElementDescriptor desyes = new ElementDescriptor();
124: desyes.setQualifiedName("yes");
125: desyes.setPropertyType(java.util.List.class);
126: ElementDescriptor desyesno = new ElementDescriptor();
127: desyesno.setQualifiedName("yesno");
128: desyesno.setPropertyType(java.util.List.class);
129: HashMap map = new HashMap();
130: map.put("y", des);
131: map.put("yes", desyes);
132: map.put("yesno", desyesno);
133: DefaultPluralStemmer dps = new DefaultPluralStemmer();
134: ElementDescriptor result = dps.findPluralDescriptor("y", map);
135: assertEquals(des, result);
136: result = dps.findPluralDescriptor("yes", map);
137: assertEquals(desyes, result);
138: result = dps.findPluralDescriptor("yesno", map);
139: assertEquals(desyesno, result);
140: }
141:
142: /**
143: * Test to find matched where plural ending is "es"
144: */
145: public void testESPluralEndingMatch() {
146: HashMap map = new HashMap();
147:
148: ElementDescriptor index = new ElementDescriptor("index",
149: "index", "");
150: map.put("index", index);
151: ElementDescriptor indexes = new ElementDescriptor("indexes",
152: "indexes", "");
153: map.put("indexes", indexes);
154:
155: ElementDescriptor patch = new ElementDescriptor("patch",
156: "patch", "");
157: map.put("patch", patch);
158: ElementDescriptor patches = new ElementDescriptor("patches",
159: "patches", "");
160: map.put("patches", patches);
161:
162: DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
163: ElementDescriptor result = stemmer.findPluralDescriptor(
164: "index", map);
165: assertEquals(indexes, result);
166:
167: result = stemmer.findPluralDescriptor("patches", map);
168: assertEquals(patches, result);
169: }
170:
171: /**
172: * Test if the closest match mechanisme is working
173: */
174: public void testClosestMatch() {
175: HashMap map = new HashMap();
176: ElementDescriptor yes1 = new ElementDescriptor("yes1", "yes1",
177: "");
178: map.put("yes1", yes1);
179: ElementDescriptor yes12 = new ElementDescriptor("yes12",
180: "yes12", "");
181: map.put("yes12", yes12);
182: ElementDescriptor yes123 = new ElementDescriptor("yes123",
183: "yes123", "");
184: map.put("yes123", yes123);
185: DefaultPluralStemmer stemmer = new DefaultPluralStemmer();
186: ElementDescriptor result = stemmer.findPluralDescriptor("yes",
187: map);
188: assertEquals(yes1, result);
189: }
190:
191: }
|