Apache Lucene open-source search software
SOLR-5361: Hit Schemaless example with some tests.
Mark Robert Millerb01114939ace
diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.javaindex d8b1f9853c9..3a1cc4bc58d 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTests.java@@ -73,7 +73,7 @@ import org.slf4j.LoggerFactory;** @since solr 1.3*/-abstract public class SolrExampleTests extends SolrJettyTestBase+abstract public class SolrExampleTests extends SolrExampleTestsBase{private static Logger log = LoggerFactory.getLogger(SolrExampleTests.class);@@ -395,130 +395,6 @@ abstract public class SolrExampleTests extends SolrJettyTestBase}}- /**- * query the example- */- @Test- public void testCommitWithinOnAdd() throws Exception- {- // make sure it is empty...- SolrServer server = getSolrServer();- server.deleteByQuery( "*:*" );// delete everything!- server.commit();- QueryResponse rsp = server.query( new SolrQuery( "*:*") );- Assert.assertEquals( 0, rsp.getResults().getNumFound() );-- // Now try a timed commit...- SolrInputDocument doc3 = new SolrInputDocument();- doc3.addField( "id", "id3", 1.0f );- doc3.addField( "name", "doc3", 1.0f );- doc3.addField( "price", 10 );- UpdateRequest up = new UpdateRequest();- up.add( doc3 );- up.setCommitWithin( 500 ); // a smaller commitWithin caused failures on the following assert- up.process( server );-- rsp = server.query( new SolrQuery( "*:*") );- Assert.assertEquals( 0, rsp.getResults().getNumFound() );-- // TODO: not a great way to test this - timing is easily out- // of whack due to parallel tests and various computer specs/load- Thread.sleep( 1000 ); // wait 1 sec-- // now check that it comes out...- rsp = server.query( new SolrQuery( "id:id3") );-- int cnt = 0;- while (rsp.getResults().getNumFound() == 0) {- // wait and try again for slower/busier machines- // and/or parallel test effects.-- if (cnt++ == 10) {- break;- }-- Thread.sleep( 2000 ); // wait 2 seconds...-- rsp = server.query( new SolrQuery( "id:id3") );- }-- Assert.assertEquals( 1, rsp.getResults().getNumFound() );-- // Now test the new convenience parameter on the add() for commitWithin- SolrInputDocument doc4 = new SolrInputDocument();- doc4.addField( "id", "id4", 1.0f );- doc4.addField( "name", "doc4", 1.0f );- doc4.addField( "price", 10 );- server.add(doc4, 500);-- Thread.sleep( 1000 ); // wait 1 sec-- // now check that it comes out...- rsp = server.query( new SolrQuery( "id:id4") );-- cnt = 0;- while (rsp.getResults().getNumFound() == 0) {- // wait and try again for slower/busier machines- // and/or parallel test effects.-- if (cnt++ == 10) {- break;- }-- Thread.sleep( 2000 ); // wait 2 seconds...-- rsp = server.query( new SolrQuery( "id:id3") );- }-- Assert.assertEquals( 1, rsp.getResults().getNumFound() );- }-- @Test- public void testCommitWithinOnDelete() throws Exception- {- // make sure it is empty...- SolrServer server = getSolrServer();- server.deleteByQuery( "*:*" );// delete everything!- server.commit();- QueryResponse rsp = server.query( new SolrQuery( "*:*") );- Assert.assertEquals( 0, rsp.getResults().getNumFound() );-- // Now add one document...- SolrInputDocument doc3 = new SolrInputDocument();- doc3.addField( "id", "id3", 1.0f );- doc3.addField( "name", "doc3", 1.0f );- doc3.addField( "price", 10 );- server.add(doc3);- server.commit();-- // now check that it comes out...- rsp = server.query( new SolrQuery( "id:id3") );- Assert.assertEquals( 1, rsp.getResults().getNumFound() );-- // now test commitWithin on a delete- UpdateRequest up = new UpdateRequest();- up.setCommitWithin(1000);- up.deleteById("id3");- up.process( server );-- // the document should still be there- rsp = server.query( new SolrQuery( "id:id3") );- Assert.assertEquals( 1, rsp.getResults().getNumFound() );-- // check if the doc has been deleted every 250 ms for 30 seconds- long timeout = System.currentTimeMillis() + 30000;- do {- Thread.sleep( 250 ); // wait 250 ms-- rsp = server.query( new SolrQuery( "id:id3") );- if(rsp.getResults().getNumFound()==0) {- return;- }- } while(System.currentTimeMillis()<timeout);-- Assert.fail("commitWithin failed to commit");- }-@Testpublic void testErrorHandling() throws Exception{@@ -708,64 +584,6 @@ abstract public class SolrExampleTests extends SolrJettyTestBasersp = server.query( new SolrQuery( "*:*") );Assert.assertEquals( 5 , rsp.getResults().getNumFound() );}--- protected void assertNumFound( String query, int num ) throws SolrServerException, IOException- {- QueryResponse rsp = getSolrServer().query( new SolrQuery( query ) );- if( num != rsp.getResults().getNumFound() ) {- fail( "expected: "+num +" but had: "+rsp.getResults().getNumFound() + " :: " + rsp.getResults() );- }- }-- @Test- public void testAddDelete() throws Exception- {- SolrServer server = getSolrServer();-- // Empty the database...- server.deleteByQuery( "*:*" );// delete everything!-- SolrInputDocument[] doc = new SolrInputDocument[3];- for( int i=0; i<3; i++ ) {- doc[i] = new SolrInputDocument();- doc[i].setField( "id", i + " & 222", 1.0f );- }- String id = (String) doc[0].getField( "id" ).getFirstValue();-- server.add( doc[0] );- server.commit();- assertNumFound( "*:*", 1 ); // make sure it got in-- // make sure it got in there- server.deleteById( id );- server.commit();- assertNumFound( "*:*", 0 ); // make sure it got out-- // add it back- server.add( doc[0] );- server.commit();- assertNumFound( "*:*", 1 ); // make sure it got in- server.deleteByQuery( "id:\""+ClientUtils.escapeQueryChars(id)+"\"" );- server.commit();- assertNumFound( "*:*", 0 ); // make sure it got out-- // Add two documents- for( SolrInputDocument d : doc ) {- server.add( d );- }- server.commit();- assertNumFound( "*:*", 3 ); // make sure it got in-- // should be able to handle multiple delete commands in a single go- List<String> ids = new ArrayList<String>();- for( SolrInputDocument d : doc ) {- ids.add(d.getFieldValue("id").toString());- }- server.deleteById(ids);- server.commit();- assertNumFound( "*:*", 0 ); // make sure it got out- }@Testpublic void testLukeHandler() throws Exception@@ -1224,54 +1042,6 @@ abstract public class SolrExampleTests extends SolrJettyTestBasereturn doc;}- @Test- public void testStreamingRequest() throws Exception {- SolrServer server = getSolrServer();- // Empty the database...- server.deleteByQuery( "*:*" );// delete everything!- server.commit();- assertNumFound( "*:*", 0 ); // make sure it got in-- // Add some docs to the index- UpdateRequest req = new UpdateRequest();- for( int i=0; i<10; i++ ) {- SolrInputDocument doc = new SolrInputDocument();- doc.addField("id", "" + i );- doc.addField("cat", "foocat");- req.add( doc );- }- req.setAction(ACTION.COMMIT, true, true );- req.process( server );-- // Make sure it ran OK- SolrQuery query = new SolrQuery("*:*");- query.set( CommonParams.FL, "id,score,_docid_" );- QueryResponse response = server.query(query);- assertEquals(0, response.getStatus());- assertEquals(10, response.getResults().getNumFound());-- // Now make sure each document gets output- final AtomicInteger cnt = new AtomicInteger( 0 );- server.queryAndStreamResponse(query, new StreamingResponseCallback() {-- @Override- public void streamDocListInfo(long numFound, long start, Float maxScore) {- assertEquals(10, numFound );- }-- @Override- public void streamSolrDocument(SolrDocument doc) {- cnt.incrementAndGet();-- // Make sure the transformer works for streaming- Float score = (Float)doc.get( "score" );- assertEquals( "should have score", new Float(1.0), score );- }-- });- assertEquals(10, cnt.get() );- }-@Testpublic void testChineseDefaults() throws Exception {SolrServer server = getSolrServer();diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTestsBase.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTestsBase.javanew file mode 100644index 00000000000..72947d4add0--- /dev/null+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrExampleTestsBase.java@@ -0,0 +1,270 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.client.solrj;++import java.io.IOException;+import java.util.ArrayList;+import java.util.List;+import java.util.concurrent.atomic.AtomicInteger;++import junit.framework.Assert;++import org.apache.solr.SolrJettyTestBase;+import org.apache.solr.client.solrj.request.UpdateRequest;+import org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION;+import org.apache.solr.client.solrj.response.QueryResponse;+import org.apache.solr.client.solrj.util.ClientUtils;+import org.apache.solr.common.SolrDocument;+import org.apache.solr.common.SolrInputDocument;+import org.apache.solr.common.params.CommonParams;+import org.junit.Test;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++abstract public class SolrExampleTestsBase extends SolrJettyTestBase {+ private static Logger log = LoggerFactory+ .getLogger(SolrExampleTestsBase.class);++ /**+ * query the example+ */+ @Test+ public void testCommitWithinOnAdd() throws Exception {+ // make sure it is empty...+ SolrServer server = getSolrServer();+ server.deleteByQuery("*:*");// delete everything!+ server.commit();+ QueryResponse rsp = server.query(new SolrQuery("*:*"));+ Assert.assertEquals(0, rsp.getResults().getNumFound());++ // Now try a timed commit...+ SolrInputDocument doc3 = new SolrInputDocument();+ doc3.addField("id", "id3", 1.0f);+ doc3.addField("name", "doc3", 1.0f);+ doc3.addField("price", 10);+ UpdateRequest up = new UpdateRequest();+ up.add(doc3);+ up.setCommitWithin(500); // a smaller commitWithin caused failures on the+ // following assert+ up.process(server);++ rsp = server.query(new SolrQuery("*:*"));+ Assert.assertEquals(0, rsp.getResults().getNumFound());++ // TODO: not a great way to test this - timing is easily out+ // of whack due to parallel tests and various computer specs/load+ Thread.sleep(1000); // wait 1 sec++ // now check that it comes out...+ rsp = server.query(new SolrQuery("id:id3"));++ int cnt = 0;+ while (rsp.getResults().getNumFound() == 0) {+ // wait and try again for slower/busier machines+ // and/or parallel test effects.++ if (cnt++ == 10) {+ break;+ }++ Thread.sleep(2000); // wait 2 seconds...++ rsp = server.query(new SolrQuery("id:id3"));+ }++ Assert.assertEquals(1, rsp.getResults().getNumFound());++ // Now test the new convenience parameter on the add() for commitWithin+ SolrInputDocument doc4 = new SolrInputDocument();+ doc4.addField("id", "id4", 1.0f);+ doc4.addField("name", "doc4", 1.0f);+ doc4.addField("price", 10);+ server.add(doc4, 500);++ Thread.sleep(1000); // wait 1 sec++ // now check that it comes out...+ rsp = server.query(new SolrQuery("id:id4"));++ cnt = 0;+ while (rsp.getResults().getNumFound() == 0) {+ // wait and try again for slower/busier machines+ // and/or parallel test effects.++ if (cnt++ == 10) {+ break;+ }++ Thread.sleep(2000); // wait 2 seconds...++ rsp = server.query(new SolrQuery("id:id3"));+ }++ Assert.assertEquals(1, rsp.getResults().getNumFound());+ }++ @Test+ public void testCommitWithinOnDelete() throws Exception {+ // make sure it is empty...+ SolrServer server = getSolrServer();+ server.deleteByQuery("*:*");// delete everything!+ server.commit();+ QueryResponse rsp = server.query(new SolrQuery("*:*"));+ Assert.assertEquals(0, rsp.getResults().getNumFound());++ // Now add one document...+ SolrInputDocument doc3 = new SolrInputDocument();+ doc3.addField("id", "id3", 1.0f);+ doc3.addField("name", "doc3", 1.0f);+ doc3.addField("price", 10);+ server.add(doc3);+ server.commit();++ // now check that it comes out...+ rsp = server.query(new SolrQuery("id:id3"));+ Assert.assertEquals(1, rsp.getResults().getNumFound());++ // now test commitWithin on a delete+ UpdateRequest up = new UpdateRequest();+ up.setCommitWithin(1000);+ up.deleteById("id3");+ up.process(server);++ // the document should still be there+ rsp = server.query(new SolrQuery("id:id3"));+ Assert.assertEquals(1, rsp.getResults().getNumFound());++ // check if the doc has been deleted every 250 ms for 30 seconds+ long timeout = System.currentTimeMillis() + 30000;+ do {+ Thread.sleep(250); // wait 250 ms++ rsp = server.query(new SolrQuery("id:id3"));+ if (rsp.getResults().getNumFound() == 0) {+ return;+ }+ } while (System.currentTimeMillis() < timeout);++ Assert.fail("commitWithin failed to commit");+ }++ @Test+ public void testAddDelete() throws Exception {+ SolrServer server = getSolrServer();++ // Empty the database...+ server.deleteByQuery("*:*");// delete everything!++ SolrInputDocument[] doc = new SolrInputDocument[3];+ for (int i = 0; i < 3; i++) {+ doc[i] = new SolrInputDocument();+ doc[i].setField("id", i + " & 222", 1.0f);+ }+ String id = (String) doc[0].getField("id").getFirstValue();++ server.add(doc[0]);+ server.commit();+ assertNumFound("*:*", 1); // make sure it got in++ // make sure it got in there+ server.deleteById(id);+ server.commit();+ assertNumFound("*:*", 0); // make sure it got out++ // add it back+ server.add(doc[0]);+ server.commit();+ assertNumFound("*:*", 1); // make sure it got in+ server.deleteByQuery("id:\"" + ClientUtils.escapeQueryChars(id) + "\"");+ server.commit();+ assertNumFound("*:*", 0); // make sure it got out++ // Add two documents+ for (SolrInputDocument d : doc) {+ server.add(d);+ }+ server.commit();+ assertNumFound("*:*", 3); // make sure it got in++ // should be able to handle multiple delete commands in a single go+ List<String> ids = new ArrayList<String>();+ for (SolrInputDocument d : doc) {+ ids.add(d.getFieldValue("id").toString());+ }+ server.deleteById(ids);+ server.commit();+ assertNumFound("*:*", 0); // make sure it got out+ }++ @Test+ public void testStreamingRequest() throws Exception {+ SolrServer server = getSolrServer();+ // Empty the database...+ server.deleteByQuery("*:*");// delete everything!+ server.commit();+ assertNumFound("*:*", 0); // make sure it got in++ // Add some docs to the index+ UpdateRequest req = new UpdateRequest();+ for (int i = 0; i < 10; i++) {+ SolrInputDocument doc = new SolrInputDocument();+ doc.addField("id", "" + i);+ doc.addField("cat", "foocat");+ req.add(doc);+ }+ req.setAction(ACTION.COMMIT, true, true);+ req.process(server);++ // Make sure it ran OK+ SolrQuery query = new SolrQuery("*:*");+ query.set(CommonParams.FL, "id,score,_docid_");+ QueryResponse response = server.query(query);+ assertEquals(0, response.getStatus());+ assertEquals(10, response.getResults().getNumFound());++ // Now make sure each document gets output+ final AtomicInteger cnt = new AtomicInteger(0);+ server.queryAndStreamResponse(query, new StreamingResponseCallback() {++ @Override+ public void streamDocListInfo(long numFound, long start, Float maxScore) {+ assertEquals(10, numFound);+ }++ @Override+ public void streamSolrDocument(SolrDocument doc) {+ cnt.incrementAndGet();++ // Make sure the transformer works for streaming+ Float score = (Float) doc.get("score");+ assertEquals("should have score", new Float(1.0), score);+ }++ });+ assertEquals(10, cnt.get());+ }++ protected void assertNumFound(String query, int num)+ throws SolrServerException, IOException {+ QueryResponse rsp = getSolrServer().query(new SolrQuery(query));+ if (num != rsp.getResults().getNumFound()) {+ fail("expected: " + num + " but had: " + rsp.getResults().getNumFound()+ + " :: " + rsp.getResults());+ }+ }+}diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/SolrSchemalessExampleTests.java b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrSchemalessExampleTests.javanew file mode 100644index 00000000000..b674285ce35--- /dev/null+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/SolrSchemalessExampleTests.java@@ -0,0 +1,58 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++package org.apache.solr.client.solrj;++import org.apache.solr.client.solrj.impl.BinaryRequestWriter;+import org.apache.solr.client.solrj.impl.BinaryResponseParser;+import org.apache.solr.client.solrj.impl.HttpSolrServer;+import org.apache.solr.util.ExternalPaths;+import org.junit.BeforeClass;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class SolrSchemalessExampleTests extends SolrExampleTestsBase {+ private static Logger log = LoggerFactory+ .getLogger(SolrSchemalessExampleTests.class);++ @BeforeClass+ public static void beforeTest() throws Exception {+ createJetty(ExternalPaths.EXAMPLE_SCHEMALESS_HOME, null, null);+ }++ @Override+ public SolrServer createNewSolrServer() {+ try {+ // setup the server...+ String url = jetty.getBaseUrl().toString() + "/collection1";+ HttpSolrServer s = new HttpSolrServer(url);+ s.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);+ s.setDefaultMaxConnectionsPerHost(100);+ s.setMaxTotalConnections(100);+ s.setUseMultiPartPost(random().nextBoolean());++ if (random().nextBoolean()) {+ s.setParser(new BinaryResponseParser());+ s.setRequestWriter(new BinaryRequestWriter());+ }++ return s;+ } catch (Exception ex) {+ throw new RuntimeException(ex);+ }+ }+}diff --git a/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java b/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.javaindex d5f2fdad268..996ed5505a8 100644--- a/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java+++ b/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java@@ -38,11 +38,6 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4{private static Logger log = LoggerFactory.getLogger(SolrJettyTestBase.class);- // Try not introduce a dependency on the example schema or config unless you need to.- // using configs in the test directory allows more flexibility to change "example"- // without breaking configs.- public String getSolrHome() { return ExternalPaths.EXAMPLE_HOME; }-private static File TEST_KEYSTORE;static {TEST_KEYSTORE = (null == ExternalPaths.SOURCE_HOME)diff --git a/solr/test-framework/src/java/org/apache/solr/util/ExternalPaths.java b/solr/test-framework/src/java/org/apache/solr/util/ExternalPaths.javaindex f12e3a404a7..67597748417 100644--- a/solr/test-framework/src/java/org/apache/solr/util/ExternalPaths.java+++ b/solr/test-framework/src/java/org/apache/solr/util/ExternalPaths.java@@ -43,6 +43,8 @@ public class ExternalPaths {/* @see #SOURCE_HOME */public static String EXAMPLE_HOME = new File(SOURCE_HOME, "example/solr").getAbsolutePath();/* @see #SOURCE_HOME */+ public static String EXAMPLE_SCHEMALESS_HOME = new File(SOURCE_HOME, "example/example-schemaless/solr").getAbsolutePath();+ /* @see #SOURCE_HOME */public static String EXAMPLE_MULTICORE_HOME = new File(SOURCE_HOME, "example/multicore").getAbsolutePath();/* @see #SOURCE_HOME */public static String EXAMPLE_SCHEMA=EXAMPLE_HOME+"/collection1/conf/schema.xml";