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

« back to all changes in this revision

Viewing changes to solr/core/src/test/org/apache/solr/schema/TestBinaryField.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
 
package org.apache.solr.schema;
18
 
 
19
 
import org.apache.lucene.util.LuceneTestCase;
20
 
import org.apache.solr.client.solrj.SolrQuery;
21
 
import org.apache.solr.client.solrj.beans.Field;
22
 
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
23
 
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
24
 
import org.apache.solr.client.solrj.response.QueryResponse;
25
 
import org.apache.solr.common.SolrDocument;
26
 
import org.apache.solr.common.SolrDocumentList;
27
 
import org.apache.solr.common.SolrInputDocument;
28
 
import org.apache.solr.core.SolrResourceLoader;
29
 
import org.apache.commons.io.FileUtils;
30
 
import org.apache.commons.io.IOUtils;
31
 
 
32
 
import java.nio.ByteBuffer;
33
 
import java.io.File;
34
 
import java.io.FileOutputStream;
35
 
import java.util.List;
36
 
 
37
 
public class TestBinaryField extends LuceneTestCase {
38
 
  CommonsHttpSolrServer server;
39
 
  JettySolrRunner jetty;
40
 
 
41
 
  int port = 0;
42
 
  static final String context = "/example";
43
 
 
44
 
  @Override
45
 
  public void setUp() throws Exception {
46
 
    super.setUp();
47
 
 
48
 
    File home = new File(TEMP_DIR,
49
 
        "solrtest-TestBinaryField-" + System.currentTimeMillis());
50
 
 
51
 
    File homeDir = new File(home, "example");
52
 
    File dataDir = new File(homeDir, "data");
53
 
    File confDir = new File(homeDir, "conf");
54
 
 
55
 
    homeDir.mkdirs();
56
 
    dataDir.mkdirs();
57
 
    confDir.mkdirs();
58
 
 
59
 
    SolrResourceLoader loader = new SolrResourceLoader(null, null);
60
 
    File f = new File(confDir, "solrconfig.xml");
61
 
    String fname = "solr/conf/solrconfig-slave1.xml";
62
 
    FileOutputStream out = new FileOutputStream(f);
63
 
    IOUtils.copy(loader.openResource(fname), out);
64
 
    out.close();
65
 
    f = new File(confDir, "schema.xml");
66
 
    fname = "solr/conf/schema-binaryfield.xml";
67
 
    out = new FileOutputStream(f);
68
 
    IOUtils.copy(loader.openResource(fname), out);
69
 
    out.close();
70
 
    System.setProperty("solr.solr.home", homeDir.getAbsolutePath());
71
 
    System.setProperty("solr.data.dir", dataDir.getAbsolutePath());
72
 
    System.setProperty("solr.test.sys.prop1", "propone");
73
 
    System.setProperty("solr.test.sys.prop2", "proptwo");
74
 
 
75
 
    jetty = new JettySolrRunner(context, 0);
76
 
    jetty.start();
77
 
    port = jetty.getLocalPort();
78
 
 
79
 
    String url = "http://localhost:" + jetty.getLocalPort() + context;
80
 
    server = new CommonsHttpSolrServer(url);
81
 
  }
82
 
 
83
 
  public void testSimple() throws Exception {
84
 
    byte[] buf = new byte[10];
85
 
    for (int i = 0; i < 10; i++) {
86
 
      buf[i] = (byte) i;
87
 
    }
88
 
    SolrInputDocument doc = null;
89
 
    doc = new SolrInputDocument();
90
 
    doc.addField("id", 1);
91
 
    doc.addField("data", ByteBuffer.wrap(buf, 2, 5));
92
 
    server.add(doc);
93
 
 
94
 
    doc = new SolrInputDocument();
95
 
    doc.addField("id", 2);
96
 
    doc.addField("data", ByteBuffer.wrap(buf, 4, 3));
97
 
    server.add(doc);
98
 
 
99
 
    doc = new SolrInputDocument();
100
 
    doc.addField("id", 3);
101
 
    doc.addField("data", buf);
102
 
    server.add(doc);
103
 
 
104
 
    server.commit();
105
 
 
106
 
    QueryResponse resp = server.query(new SolrQuery("*:*"));
107
 
    SolrDocumentList res = resp.getResults();
108
 
    List<Bean> beans = resp.getBeans(Bean.class);
109
 
    assertEquals(3, res.size());
110
 
    assertEquals(3, beans.size());
111
 
    for (SolrDocument d : res) {
112
 
      Integer id = (Integer) d.getFieldValue("id");
113
 
      byte[] data = (byte[]) d.getFieldValue("data");
114
 
      if (id == 1) {
115
 
        assertEquals(5, data.length);
116
 
        for (int i = 0; i < data.length; i++) {
117
 
          byte b = data[i];
118
 
          assertEquals((byte)(i + 2), b);
119
 
        }
120
 
 
121
 
      } else if (id == 2) {
122
 
        assertEquals(3, data.length);
123
 
        for (int i = 0; i < data.length; i++) {
124
 
          byte b = data[i];
125
 
          assertEquals((byte)(i + 4), b);
126
 
        }
127
 
 
128
 
 
129
 
      } else if (id == 3) {
130
 
        assertEquals(10, data.length);
131
 
        for (int i = 0; i < data.length; i++) {
132
 
          byte b = data[i];
133
 
          assertEquals((byte)i, b);
134
 
        }
135
 
 
136
 
      }
137
 
 
138
 
    }
139
 
    for (Bean d : beans) {
140
 
      Integer id = d.id;
141
 
      byte[] data = d.data;
142
 
      if (id == 1) {
143
 
        assertEquals(5, data.length);
144
 
        for (int i = 0; i < data.length; i++) {
145
 
          byte b = data[i];
146
 
          assertEquals((byte)(i + 2), b);
147
 
        }
148
 
 
149
 
      } else if (id == 2) {
150
 
        assertEquals(3, data.length);
151
 
        for (int i = 0; i < data.length; i++) {
152
 
          byte b = data[i];
153
 
          assertEquals((byte)(i + 4), b);
154
 
        }
155
 
 
156
 
 
157
 
      } else if (id == 3) {
158
 
        assertEquals(10, data.length);
159
 
        for (int i = 0; i < data.length; i++) {
160
 
          byte b = data[i];
161
 
          assertEquals((byte)i, b);
162
 
        }
163
 
 
164
 
      }
165
 
 
166
 
    }
167
 
 
168
 
  }
169
 
  public static class Bean{
170
 
    @Field
171
 
    int id;
172
 
    @Field
173
 
    byte [] data;
174
 
  }
175
 
 
176
 
 
177
 
  @Override
178
 
  public void tearDown() throws Exception {
179
 
    jetty.stop();
180
 
    super.tearDown();
181
 
  }
182
 
}