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.client.solrj;
19
import org.apache.solr.SolrJettyTestBase;
20
import org.apache.solr.client.solrj.beans.Field;
21
import org.apache.solr.client.solrj.impl.BinaryRequestWriter;
22
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
23
import org.apache.solr.client.solrj.request.RequestWriter;
24
import org.apache.solr.client.solrj.response.QueryResponse;
25
import org.apache.solr.common.SolrInputDocument;
26
import org.apache.solr.util.ExternalPaths;
27
import org.junit.BeforeClass;
28
import org.junit.Test;
30
import java.io.IOException;
31
import java.util.Iterator;
37
* @version $Id: TestBatchUpdate.java 1146838 2011-07-14 18:40:52Z sarowe $
39
public class TestBatchUpdate extends SolrJettyTestBase {
42
public static void beforeTest() throws Exception {
43
createJetty(ExternalPaths.EXAMPLE_HOME, null, null);
46
static final int numdocs = 1000;
50
public void testWithXml() throws Exception {
51
CommonsHttpSolrServer commonsHttpSolrServer = (CommonsHttpSolrServer) getSolrServer();
52
commonsHttpSolrServer.setRequestWriter(new RequestWriter());
53
commonsHttpSolrServer.deleteByQuery( "*:*" ); // delete everything!
54
doIt(commonsHttpSolrServer);
58
public void testWithBinary()throws Exception{
59
CommonsHttpSolrServer commonsHttpSolrServer = (CommonsHttpSolrServer) getSolrServer();
60
commonsHttpSolrServer.setRequestWriter(new BinaryRequestWriter());
61
commonsHttpSolrServer.deleteByQuery( "*:*" ); // delete everything!
62
doIt(commonsHttpSolrServer);
66
public void testWithBinaryBean()throws Exception{
67
CommonsHttpSolrServer commonsHttpSolrServer = (CommonsHttpSolrServer) getSolrServer();
68
commonsHttpSolrServer.setRequestWriter(new BinaryRequestWriter());
69
commonsHttpSolrServer.deleteByQuery( "*:*" ); // delete everything!
70
final int[] counter = new int[1];
72
commonsHttpSolrServer.addBeans(new Iterator<Bean>() {
74
public boolean hasNext() {
75
return counter[0] < numdocs;
79
Bean bean = new Bean();
80
bean.id = "" + (++counter[0]);
85
public void remove() {
89
commonsHttpSolrServer.commit();
90
SolrQuery query = new SolrQuery("*:*");
91
QueryResponse response = commonsHttpSolrServer.query(query);
92
assertEquals(0, response.getStatus());
93
assertEquals(numdocs, response.getResults().getNumFound());
96
public static class Bean{
103
private void doIt(CommonsHttpSolrServer commonsHttpSolrServer) throws SolrServerException, IOException {
104
final int[] counter = new int[1];
106
commonsHttpSolrServer.add(new Iterator<SolrInputDocument>() {
108
public boolean hasNext() {
109
return counter[0] < numdocs;
112
public SolrInputDocument next() {
113
SolrInputDocument doc = new SolrInputDocument();
114
doc.addField("id", "" + (++counter[0]));
115
doc.addField("cat", "foocat");
119
public void remove() {
124
commonsHttpSolrServer.commit();
125
SolrQuery query = new SolrQuery("*:*");
126
QueryResponse response = commonsHttpSolrServer.query(query);
127
assertEquals(0, response.getStatus());
128
assertEquals(numdocs, response.getResults().getNumFound());