~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/solrj/src/test/org/apache/solr/common/params/SolrParamTest.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.solr.common.params;
19
 
 
20
 
import java.util.HashMap;
21
 
import java.util.Map;
22
 
 
23
 
import org.apache.lucene.util.LuceneTestCase;
24
 
import org.apache.solr.common.SolrException;
25
 
import org.apache.solr.common.params.DefaultSolrParams;
26
 
import org.apache.solr.common.params.MapSolrParams;
27
 
import org.apache.solr.common.params.SolrParams;
28
 
 
29
 
/**
30
 
 */
31
 
public class SolrParamTest extends LuceneTestCase 
32
 
{  
33
 
  public void testGetParams() {
34
 
    Map<String,String> pmap = new HashMap<String, String>();
35
 
    pmap.put( "str"        , "string"   );
36
 
    pmap.put( "bool"       , "true"     );
37
 
    pmap.put( "true-0"     , "true"     );
38
 
    pmap.put( "true-1"     , "yes"      );
39
 
    pmap.put( "true-2"     , "on"       );
40
 
    pmap.put( "false-0"    , "false"    );
41
 
    pmap.put( "false-1"    , "off"      );
42
 
    pmap.put( "false-2"    , "no"       );
43
 
    pmap.put( "int"        , "100"      );
44
 
    pmap.put( "float"      , "10.6"     );
45
 
    pmap.put( "f.fl.str"   , "string"   );
46
 
    pmap.put( "f.fl.bool"  , "true"     );
47
 
    pmap.put( "f.fl.int"   , "100"      );
48
 
    pmap.put( "f.fl.float" , "10.6"     );
49
 
    pmap.put( "f.bad.bool" , "notbool"  );
50
 
    pmap.put( "f.bad.int"  , "notint"   );
51
 
    pmap.put( "f.bad.float", "notfloat" );
52
 
    final SolrParams params = new MapSolrParams( pmap );
53
 
    
54
 
    // Test the string values we put in directly
55
 
    assertEquals(  "string"   , params.get( "str"       ) );
56
 
    assertEquals(  "true"     , params.get( "bool"      ) );
57
 
    assertEquals(  "100"      , params.get( "int"       ) );
58
 
    assertEquals(  "10.6"     , params.get( "float"     ) );
59
 
    assertEquals(  "string"   , params.get( "f.fl.str"    ) );
60
 
    assertEquals(  "true"     , params.get( "f.fl.bool"   ) );
61
 
    assertEquals(  "100"      , params.get( "f.fl.int"    ) );
62
 
    assertEquals(  "10.6"     , params.get( "f.fl.float"  ) );
63
 
    assertEquals(  "notbool"  , params.get( "f.bad.bool"  ) );
64
 
    assertEquals(  "notint"   , params.get( "f.bad.int"   ) );
65
 
    assertEquals(  "notfloat" , params.get( "f.bad.float" ) );
66
 
    
67
 
    final String  pstr = "string";
68
 
    final Boolean pbool = Boolean.TRUE;
69
 
    final Integer pint = new Integer( 100 );
70
 
    final Float   pfloat = new Float( 10.6f );
71
 
    
72
 
    // Make sure they parse ok
73
 
    assertEquals( pstr   , params.get(      "str"      ) );
74
 
    assertEquals( pbool  , params.getBool(  "bool"     ) );
75
 
    assertEquals( pint   , params.getInt(   "int"      ) );
76
 
    assertEquals( pfloat , params.getFloat( "float"    ) );
77
 
    assertEquals( pbool  , params.getBool(  "f.fl.bool"  ) );
78
 
    assertEquals( pint   , params.getInt(   "f.fl.int"   ) );
79
 
    assertEquals( pfloat , params.getFloat( "f.fl.float" ) );
80
 
    assertEquals( pstr   , params.getFieldParam( "fl", "str"  ) );
81
 
    assertEquals( pbool  , params.getFieldBool(  "fl", "bool" ) );
82
 
    assertEquals( pint   , params.getFieldInt(   "fl", "int"  ) );
83
 
    assertEquals( pfloat , params.getFieldFloat( "fl", "float" ) );
84
 
    
85
 
    // Test field defaulting (fall through to non-field-specific value)
86
 
    assertEquals( pint   , params.getFieldInt( "fff",  "int"      ) );
87
 
    
88
 
    // test boolean parsing
89
 
    for( int i=0; i<3; i++ ) {
90
 
      // Must use Boolean rather than boolean reference value to prevent
91
 
      // auto-unboxing ambiguity
92
 
      assertEquals( Boolean.TRUE,  params.getBool( "true-"+i  ) );
93
 
      assertEquals( Boolean.FALSE, params.getBool( "false-"+i ) );
94
 
    }
95
 
    
96
 
    // Malformed params: These should throw a 400
97
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getInt(   "f.bad.int" ); } } ) );
98
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getBool(  "f.bad.bool" ); } } ) );
99
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getFloat( "f.bad.float" ); } } ) );
100
 
    
101
 
    // Ask for params that arent there
102
 
    assertNull( params.get( "asagdsaga" ) );
103
 
    assertNull( params.getBool( "asagdsaga" ) );
104
 
    assertNull( params.getInt( "asagdsaga" ) );
105
 
    assertNull( params.getFloat( "asagdsaga" ) );
106
 
    
107
 
    // Get things with defaults
108
 
    assertEquals( pstr                  , params.get(          "xxx", pstr   ) );
109
 
    assertEquals( pbool.booleanValue()  , params.getBool(      "xxx", pbool   ) );
110
 
    assertEquals( pint.intValue()       , params.getInt(       "xxx", pint   ) );
111
 
    assertEquals( pfloat.floatValue()   , params.getFloat(     "xxx", pfloat  ), 0.1);
112
 
    assertEquals( pbool.booleanValue()  , params.getFieldBool( "xxx", "bool", pbool ) );
113
 
    assertEquals( pint.intValue()       , params.getFieldInt(  "xxx", "int", pint  ) );
114
 
    assertEquals( pfloat.floatValue()   , params.getFieldFloat("xxx", "float", pfloat  ), 0.1);
115
 
    assertEquals( pstr                  , params.getFieldParam("xxx", "str", pstr  ) );
116
 
 
117
 
    // Required params testing uses decorator
118
 
    final SolrParams required = params.required();
119
 
    
120
 
    // Required params which are present should test same as above
121
 
    assertEquals( pstr   , required.get(      "str"      ) );
122
 
    assertEquals( pbool  , required.getBool(  "bool"     ) );
123
 
    assertEquals( pint   , required.getInt(   "int"      ) );
124
 
    assertEquals( pfloat , required.getFloat( "float"    ) );
125
 
    
126
 
    // field value present
127
 
    assertEquals( pbool  , required.getFieldBool(  "fl", "bool" ) );
128
 
    // field defaulting (fall through to non-field-specific value)
129
 
    assertEquals( pstr   , required.getFieldParams("fakefield", "str")[0] );
130
 
    assertEquals( pstr   , required.getFieldParam( "fakefield", "str"   ) );
131
 
    assertEquals( pbool  , required.getFieldBool(  "fakefield", "bool"  ) );
132
 
    assertEquals( pint   , required.getFieldInt(   "fakefield", "int"   ) );
133
 
    assertEquals( pfloat , required.getFieldFloat( "fakefield", "float" ) );
134
 
    
135
 
    // Required params which are missing: These should throw a 400
136
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.get( "aaaa" ); } } ) );
137
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getInt(   "f.bad.int" ); } } ) );
138
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getBool(  "f.bad.bool" ); } } ) );
139
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getFloat( "f.bad.float" ); } } ) );
140
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getInt(   "aaa" ); } } ) );
141
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getBool(  "aaa" ); } } ) );
142
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { required.getFloat( "aaa" ); } } ) );
143
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getFieldBool(  "bad", "bool" ); } } ) );
144
 
    assertEquals( 400, getReturnCode( new Runnable() { public void run() { params.getFieldInt(   "bad", "int"  ); } } ) );
145
 
 
146
 
    // Fields with default use their parent value:
147
 
    assertEquals(
148
 
        params.get(   "aaaa", "str" ), 
149
 
        required.get( "aaaa", "str" ) );
150
 
    assertEquals(
151
 
        params.getInt(   "f.bad.nnnn", pint ), 
152
 
        required.getInt( "f.bad.nnnn", pint ) );
153
 
    
154
 
    // Check default SolrParams
155
 
    Map<String,String> dmap = new HashMap<String, String>();
156
 
    // these are not defined in params
157
 
    dmap.put( "dstr"               , "default"   );
158
 
    dmap.put( "dint"               , "123"       );
159
 
    // these are defined in params
160
 
    dmap.put( "int"                , "456"       );
161
 
    SolrParams defaults = new DefaultSolrParams( params, new MapSolrParams( dmap ) );
162
 
  
163
 
    // in params, not in default
164
 
    assertEquals( pstr                  , defaults.get( "str"      ) );
165
 
    // in default, not in params
166
 
    assertEquals( "default"             , defaults.get( "dstr"      ) );
167
 
    assertEquals( new Integer(123)      , defaults.getInt(  "dint"     ) );
168
 
    // in params, overriding defaults
169
 
    assertEquals( pint                  , defaults.getInt(   "int"      ) );
170
 
    // in neither params nor defaults
171
 
    assertNull( defaults.get( "asagdsaga" ) );
172
 
  }
173
 
  
174
 
  public static int getReturnCode( Runnable runnable )
175
 
  {
176
 
    try {
177
 
      runnable.run();
178
 
    }
179
 
    catch( SolrException sx ) {
180
 
      return sx.code();
181
 
    }
182
 
    catch( Exception ex ) {
183
 
      ex.printStackTrace();
184
 
      return 500;
185
 
    }
186
 
    return 200;
187
 
  }
188
 
}