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.
17
package org.apache.solr.schema;
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;
32
import java.nio.ByteBuffer;
34
import java.io.FileOutputStream;
35
import java.util.List;
37
public class TestBinaryField extends LuceneTestCase {
38
CommonsHttpSolrServer server;
39
JettySolrRunner jetty;
42
static final String context = "/example";
45
public void setUp() throws Exception {
48
File home = new File(TEMP_DIR,
49
"solrtest-TestBinaryField-" + System.currentTimeMillis());
51
File homeDir = new File(home, "example");
52
File dataDir = new File(homeDir, "data");
53
File confDir = new File(homeDir, "conf");
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);
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);
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");
75
jetty = new JettySolrRunner(context, 0);
77
port = jetty.getLocalPort();
79
String url = "http://localhost:" + jetty.getLocalPort() + context;
80
server = new CommonsHttpSolrServer(url);
83
public void testSimple() throws Exception {
84
byte[] buf = new byte[10];
85
for (int i = 0; i < 10; i++) {
88
SolrInputDocument doc = null;
89
doc = new SolrInputDocument();
90
doc.addField("id", 1);
91
doc.addField("data", ByteBuffer.wrap(buf, 2, 5));
94
doc = new SolrInputDocument();
95
doc.addField("id", 2);
96
doc.addField("data", ByteBuffer.wrap(buf, 4, 3));
99
doc = new SolrInputDocument();
100
doc.addField("id", 3);
101
doc.addField("data", buf);
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");
115
assertEquals(5, data.length);
116
for (int i = 0; i < data.length; i++) {
118
assertEquals((byte)(i + 2), b);
121
} else if (id == 2) {
122
assertEquals(3, data.length);
123
for (int i = 0; i < data.length; i++) {
125
assertEquals((byte)(i + 4), b);
129
} else if (id == 3) {
130
assertEquals(10, data.length);
131
for (int i = 0; i < data.length; i++) {
133
assertEquals((byte)i, b);
139
for (Bean d : beans) {
141
byte[] data = d.data;
143
assertEquals(5, data.length);
144
for (int i = 0; i < data.length; i++) {
146
assertEquals((byte)(i + 2), b);
149
} else if (id == 2) {
150
assertEquals(3, data.length);
151
for (int i = 0; i < data.length; i++) {
153
assertEquals((byte)(i + 4), b);
157
} else if (id == 3) {
158
assertEquals(10, data.length);
159
for (int i = 0; i < data.length; i++) {
161
assertEquals((byte)i, b);
169
public static class Bean{
178
public void tearDown() throws Exception {