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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/core/ResourceLoaderTest.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.core;
19
 
 
20
 
import junit.framework.Assert;
21
 
 
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;
31
 
 
32
 
import java.io.File;
33
 
import java.io.InputStream;
34
 
import java.nio.charset.CharacterCodingException;
35
 
import java.util.Arrays;
36
 
import java.util.List;
37
 
 
38
 
public class ResourceLoaderTest extends LuceneTestCase 
39
 
{
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);
44
 
 
45
 
    loader = new SolrResourceLoader("solr");
46
 
    instDir = loader.getInstanceDir();
47
 
    assertTrue(instDir + " is not equal to " + "solr/", instDir.equals("solr" + File.separator) == true);
48
 
  }
49
 
 
50
 
  public void testAwareCompatibility() 
51
 
  {
52
 
    SolrResourceLoader loader = new SolrResourceLoader( "." );
53
 
    
54
 
    Class clazz = ResourceLoaderAware.class;
55
 
    // Check ResourceLoaderAware valid objects
56
 
    loader.assertAwareCompatibility( clazz, new NGramFilterFactory() );
57
 
    loader.assertAwareCompatibility( clazz, new KeywordTokenizerFactory() );
58
 
    
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()
65
 
    };
66
 
    for( Object obj : invalid ) {
67
 
      try {
68
 
        loader.assertAwareCompatibility( clazz, obj );
69
 
        Assert.fail( "Should be invalid class: "+obj + " FOR " + clazz );
70
 
      }
71
 
      catch( SolrException ex ) { } // OK
72
 
    }
73
 
    
74
 
 
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() );
80
 
    
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()
86
 
    };
87
 
    for( Object obj : invalid ) {
88
 
      try {
89
 
        loader.assertAwareCompatibility( clazz, obj );
90
 
        Assert.fail( "Should be invalid class: "+obj + " FOR " + clazz );
91
 
      }
92
 
      catch( SolrException ex ) { } // OK
93
 
    }
94
 
  }
95
 
  
96
 
  public void testBOMMarkers() throws Exception {
97
 
    final String fileWithBom = "stopwithbom.txt";
98
 
    SolrResourceLoader loader = new SolrResourceLoader(null);
99
 
 
100
 
    // preliminary sanity check
101
 
    InputStream bomStream = loader.openResource(fileWithBom);
102
 
    try {
103
 
      final byte[] bomExpected = new byte[] { -17, -69, -65 };
104
 
      final byte[] firstBytes = new byte[3];
105
 
      
106
 
      assertEquals("Should have been able to read 3 bytes from bomStream",
107
 
                   3, bomStream.read(firstBytes));
108
 
 
109
 
      assertTrue("This test only works if " + fileWithBom + 
110
 
                 " contains a BOM -- it appears someone removed it.", 
111
 
                 Arrays.equals(bomExpected, firstBytes));
112
 
    } finally {
113
 
      try { bomStream.close(); } catch (Exception e) { /* IGNORE */ }
114
 
    }
115
 
 
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));
120
 
  }
121
 
  
122
 
  public void testWrongEncoding() throws Exception {
123
 
    String wrongEncoding = "stopwordsWrongEncoding.txt";
124
 
    SolrResourceLoader loader = new SolrResourceLoader(null);
125
 
    // ensure we get our exception
126
 
    try {
127
 
      List<String> lines = loader.getLines(wrongEncoding);
128
 
      fail();
129
 
    } catch (SolrException expected) {
130
 
      assertTrue(expected.getCause() instanceof CharacterCodingException);
131
 
    }
132
 
  }
133
 
}