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: */package org.apache.solr.util;
017:
018: import junit.framework.TestCase;
019:
020: import java.util.HashMap;
021: import java.util.Map;
022:
023: import org.apache.solr.core.SolrException;
024: import org.apache.solr.request.SolrParams;
025: import org.apache.solr.request.MapSolrParams;
026: import org.apache.solr.request.DefaultSolrParams;
027:
028: /**
029: * @author ryan
030: */
031: public class SolrParamTest extends TestCase {
032: public void testGetParams() {
033: Map<String, String> pmap = new HashMap<String, String>();
034: pmap.put("str", "string");
035: pmap.put("bool", "true");
036: pmap.put("true-0", "true");
037: pmap.put("true-1", "yes");
038: pmap.put("true-2", "on");
039: pmap.put("false-0", "false");
040: pmap.put("false-1", "off");
041: pmap.put("false-2", "no");
042: pmap.put("int", "100");
043: pmap.put("float", "10.6");
044: pmap.put("f.fl.str", "string");
045: pmap.put("f.fl.bool", "true");
046: pmap.put("f.fl.int", "100");
047: pmap.put("f.fl.float", "10.6");
048: pmap.put("f.bad.bool", "notbool");
049: pmap.put("f.bad.int", "notint");
050: pmap.put("f.bad.float", "notfloat");
051: final SolrParams params = new MapSolrParams(pmap);
052:
053: // Test the string values we put in directly
054: assertEquals("string", params.get("str"));
055: assertEquals("true", params.get("bool"));
056: assertEquals("100", params.get("int"));
057: assertEquals("10.6", params.get("float"));
058: assertEquals("string", params.get("f.fl.str"));
059: assertEquals("true", params.get("f.fl.bool"));
060: assertEquals("100", params.get("f.fl.int"));
061: assertEquals("10.6", params.get("f.fl.float"));
062: assertEquals("notbool", params.get("f.bad.bool"));
063: assertEquals("notint", params.get("f.bad.int"));
064: assertEquals("notfloat", params.get("f.bad.float"));
065:
066: final String pstr = "string";
067: final Boolean pbool = Boolean.TRUE;
068: final Integer pint = new Integer(100);
069: final Float pfloat = new Float(10.6f);
070:
071: // Make sure they parse ok
072: assertEquals(pstr, params.get("str"));
073: assertEquals(pbool, params.getBool("bool"));
074: assertEquals(pint, params.getInt("int"));
075: assertEquals(pfloat, params.getFloat("float"));
076: assertEquals(pbool, params.getBool("f.fl.bool"));
077: assertEquals(pint, params.getInt("f.fl.int"));
078: assertEquals(pfloat, params.getFloat("f.fl.float"));
079: assertEquals(pstr, params.getFieldParam("fl", "str"));
080: assertEquals(pbool, params.getFieldBool("fl", "bool"));
081: assertEquals(pint, params.getFieldInt("fl", "int"));
082: assertEquals(pfloat, params.getFieldFloat("fl", "float"));
083:
084: // Test field defaulting (fall through to non-field-specific value)
085: assertEquals(pint, params.getFieldInt("fff", "int"));
086:
087: // test boolean parsing
088: for (int i = 0; i < 3; i++) {
089: // Must use Boolean rather than boolean reference value to prevent
090: // auto-unboxing ambiguity
091: assertEquals(Boolean.TRUE, params.getBool("true-" + i));
092: assertEquals(Boolean.FALSE, params.getBool("false-" + i));
093: }
094:
095: // Malformed params: These should throw a 400
096: assertEquals(400, getReturnCode(new Runnable() {
097: public void run() {
098: params.getInt("f.bad.int");
099: }
100: }));
101: assertEquals(400, getReturnCode(new Runnable() {
102: public void run() {
103: params.getBool("f.bad.bool");
104: }
105: }));
106: assertEquals(400, getReturnCode(new Runnable() {
107: public void run() {
108: params.getFloat("f.bad.float");
109: }
110: }));
111:
112: // Ask for params that arent there
113: assertNull(params.get("asagdsaga"));
114: assertNull(params.getBool("asagdsaga"));
115: assertNull(params.getInt("asagdsaga"));
116: assertNull(params.getFloat("asagdsaga"));
117:
118: // Get things with defaults
119: assertEquals(pstr, params.get("xxx", pstr));
120: assertEquals(pbool.booleanValue(), params.getBool("xxx", pbool));
121: assertEquals(pint.intValue(), params.getInt("xxx", pint));
122: assertEquals(pfloat.floatValue(), params
123: .getFloat("xxx", pfloat));
124: assertEquals(pbool.booleanValue(), params.getFieldBool("xxx",
125: "bool", pbool));
126: assertEquals(pint.intValue(), params.getFieldInt("xxx", "int",
127: pint));
128: assertEquals(pfloat.floatValue(), params.getFieldFloat("xxx",
129: "float", pfloat));
130: assertEquals(pstr, params.getFieldParam("xxx", "str", pstr));
131:
132: // Required params testing uses decorator
133: final SolrParams required = params.required();
134:
135: // Required params which are present should test same as above
136: assertEquals(pstr, required.get("str"));
137: assertEquals(pbool, required.getBool("bool"));
138: assertEquals(pint, required.getInt("int"));
139: assertEquals(pfloat, required.getFloat("float"));
140:
141: // field value present
142: assertEquals(pbool, required.getFieldBool("fl", "bool"));
143: // field defaulting (fall through to non-field-specific value)
144: //assertEquals( pint , required.getFieldInt( "fff", "int" ) );
145:
146: // Required params which are missing: These should throw a 400
147: assertEquals(400, getReturnCode(new Runnable() {
148: public void run() {
149: required.get("aaaa");
150: }
151: }));
152: assertEquals(400, getReturnCode(new Runnable() {
153: public void run() {
154: required.getInt("f.bad.int");
155: }
156: }));
157: assertEquals(400, getReturnCode(new Runnable() {
158: public void run() {
159: required.getBool("f.bad.bool");
160: }
161: }));
162: assertEquals(400, getReturnCode(new Runnable() {
163: public void run() {
164: required.getFloat("f.bad.float");
165: }
166: }));
167: assertEquals(400, getReturnCode(new Runnable() {
168: public void run() {
169: required.getInt("aaa");
170: }
171: }));
172: assertEquals(400, getReturnCode(new Runnable() {
173: public void run() {
174: required.getBool("aaa");
175: }
176: }));
177: assertEquals(400, getReturnCode(new Runnable() {
178: public void run() {
179: required.getFloat("aaa");
180: }
181: }));
182: assertEquals(400, getReturnCode(new Runnable() {
183: public void run() {
184: params.getFieldBool("bad", "bool");
185: }
186: }));
187: assertEquals(400, getReturnCode(new Runnable() {
188: public void run() {
189: params.getFieldInt("bad", "int");
190: }
191: }));
192:
193: // Fields with default use their parent value:
194: assertEquals(params.get("aaaa", "str"), required.get("aaaa",
195: "str"));
196: assertEquals(params.getInt("f.bad.nnnn", pint), required
197: .getInt("f.bad.nnnn", pint));
198:
199: // Check default SolrParams
200: Map<String, String> dmap = new HashMap<String, String>();
201: // these are not defined in params
202: dmap.put("dstr", "default");
203: dmap.put("dint", "123");
204: // these are defined in params
205: dmap.put("int", "456");
206: SolrParams defaults = new DefaultSolrParams(params,
207: new MapSolrParams(dmap));
208:
209: // in params, not in default
210: assertEquals(pstr, defaults.get("str"));
211: // in default, not in params
212: assertEquals("default", defaults.get("dstr"));
213: assertEquals(new Integer(123), defaults.getInt("dint"));
214: // in params, overriding defaults
215: assertEquals(pint, defaults.getInt("int"));
216: // in neither params nor defaults
217: assertNull(defaults.get("asagdsaga"));
218: }
219:
220: public static int getReturnCode(Runnable runnable) {
221: try {
222: runnable.run();
223: } catch (SolrException sx) {
224: return sx.code();
225: } catch (Exception ex) {
226: ex.printStackTrace();
227: return 500;
228: }
229: return 200;
230: }
231: }
|