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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.solr.core;
20
import junit.framework.Assert;
22
import org.apache.lucene.util.LuceneTestCase;
23
import org.apache.solr.analysis.KeywordTokenizerFactory;
24
import org.apache.solr.analysis.NGramFilterFactory;
25
import org.apache.solr.common.SolrException;
26
import org.apache.solr.handler.admin.LukeRequestHandler;
27
import org.apache.solr.handler.component.FacetComponent;
28
import org.apache.solr.response.JSONResponseWriter;
29
import org.apache.solr.util.plugin.ResourceLoaderAware;
30
import org.apache.solr.util.plugin.SolrCoreAware;
33
import java.io.InputStream;
34
import java.nio.charset.CharacterCodingException;
35
import java.util.Arrays;
36
import java.util.List;
38
public class ResourceLoaderTest extends LuceneTestCase
40
public void testInstanceDir() throws Exception {
41
SolrResourceLoader loader = new SolrResourceLoader(null);
42
String instDir = loader.getInstanceDir();
43
assertTrue(instDir + " is not equal to " + "solr/", instDir.equals("solr/") == true);
45
loader = new SolrResourceLoader("solr");
46
instDir = loader.getInstanceDir();
47
assertTrue(instDir + " is not equal to " + "solr/", instDir.equals("solr" + File.separator) == true);
50
public void testAwareCompatibility()
52
SolrResourceLoader loader = new SolrResourceLoader( "." );
54
Class clazz = ResourceLoaderAware.class;
55
// Check ResourceLoaderAware valid objects
56
loader.assertAwareCompatibility( clazz, new NGramFilterFactory() );
57
loader.assertAwareCompatibility( clazz, new KeywordTokenizerFactory() );
59
// Make sure it throws an error for invalid objects
60
Object[] invalid = new Object[] {
61
// new NGramTokenFilter( null ),
62
"hello", new Float( 12.3f ),
63
new LukeRequestHandler(),
64
new JSONResponseWriter()
66
for( Object obj : invalid ) {
68
loader.assertAwareCompatibility( clazz, obj );
69
Assert.fail( "Should be invalid class: "+obj + " FOR " + clazz );
71
catch( SolrException ex ) { } // OK
75
clazz = SolrCoreAware.class;
76
// Check ResourceLoaderAware valid objects
77
loader.assertAwareCompatibility( clazz, new LukeRequestHandler() );
78
loader.assertAwareCompatibility( clazz, new FacetComponent() );
79
loader.assertAwareCompatibility( clazz, new JSONResponseWriter() );
81
// Make sure it throws an error for invalid objects
82
invalid = new Object[] {
83
new NGramFilterFactory(),
84
"hello", new Float( 12.3f ),
85
new KeywordTokenizerFactory()
87
for( Object obj : invalid ) {
89
loader.assertAwareCompatibility( clazz, obj );
90
Assert.fail( "Should be invalid class: "+obj + " FOR " + clazz );
92
catch( SolrException ex ) { } // OK
96
public void testBOMMarkers() throws Exception {
97
final String fileWithBom = "stopwithbom.txt";
98
SolrResourceLoader loader = new SolrResourceLoader(null);
100
// preliminary sanity check
101
InputStream bomStream = loader.openResource(fileWithBom);
103
final byte[] bomExpected = new byte[] { -17, -69, -65 };
104
final byte[] firstBytes = new byte[3];
106
assertEquals("Should have been able to read 3 bytes from bomStream",
107
3, bomStream.read(firstBytes));
109
assertTrue("This test only works if " + fileWithBom +
110
" contains a BOM -- it appears someone removed it.",
111
Arrays.equals(bomExpected, firstBytes));
113
try { bomStream.close(); } catch (Exception e) { /* IGNORE */ }
116
// now make sure getLines skips the BOM...
117
List<String> lines = loader.getLines(fileWithBom);
118
assertEquals(1, lines.size());
119
assertEquals("BOMsAreEvil", lines.get(0));
122
public void testWrongEncoding() throws Exception {
123
String wrongEncoding = "stopwordsWrongEncoding.txt";
124
SolrResourceLoader loader = new SolrResourceLoader(null);
125
// ensure we get our exception
127
List<String> lines = loader.getLines(wrongEncoding);
129
} catch (SolrException expected) {
130
assertTrue(expected.getCause() instanceof CharacterCodingException);