Created
October 27, 2014 08:06
-
-
Save hanrw/463a690931aceb20abad to your computer and use it in GitHub Desktop.
Solr test demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.solr.SolrTestCaseJ4; | |
import org.apache.solr.client.solrj.SolrServerException; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import java.io.IOException; | |
public class BasicConfigurationTest extends SolrTestCaseJ4 { | |
@BeforeClass | |
public static void init() throws Exception { | |
SolrTestCaseJ4.initCore("solrhome/conf/solrconfig.xml", "solrhome/conf/schema.xml", "solrhome/"); | |
} | |
@Test | |
public void noResultInEmptyIndex() throws SolrServerException { | |
assertQ("test query on empty index", | |
req("text that is not found") | |
, "//result[@numFound='0']" | |
); | |
} | |
@Test | |
public void pathIsMandatory() throws SolrServerException, IOException { | |
assertFailedU(adoc("title", "the title")); | |
} | |
@Test | |
public void simpleDocumentIsIndexedAndFound() throws SolrServerException, IOException { | |
assertU(adoc("path", "/tmp/foo", "content", "Some important content.")); | |
assertU(commit()); | |
assertQ("added document found", | |
req("important") | |
, "//result[@numFound='1']" | |
); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.solr.SolrTestCaseJ4; | |
import org.apache.solr.client.solrj.SolrQuery; | |
import org.apache.solr.client.solrj.SolrServerException; | |
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; | |
import org.apache.solr.client.solrj.response.QueryResponse; | |
import org.apache.solr.common.SolrInputDocument; | |
import org.apache.solr.common.params.SolrParams; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import java.io.IOException; | |
public class ServerBasedTalkTest extends SolrTestCaseJ4 { | |
private EmbeddedSolrServer server; | |
@BeforeClass | |
public static void init() throws Exception { | |
SolrTestCaseJ4.initCore("backend/src/test/resources/config/solr/solrconfig.xml", "backend/src/test/resources/config/solr/conf/schema.xml"); | |
} | |
@Before | |
public void setUp() { | |
server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName()); | |
} | |
@Test | |
public void queryOnEmptyIndexNoResults() throws SolrServerException { | |
QueryResponse response = server.query(new SolrQuery("text that is not found")); | |
assertTrue(response.getResults().isEmpty()); | |
} | |
@Test | |
public void singleDocumentIsFound() throws IOException, SolrServerException { | |
SolrInputDocument document = new SolrInputDocument(); | |
document.addField("path", "/tmp/foo"); | |
document.addField("content", "Mein Hut der hat 4 Ecken"); | |
server.add(document); | |
server.commit(); | |
SolrParams params = new SolrQuery("ecke"); | |
QueryResponse response = server.query(params); | |
assertEquals(1L, response.getResults().getNumFound()); | |
assertEquals("/tmp/foo", response.getResults().get(0).get("path")); | |
} | |
@After | |
public void tearDown() { | |
super.clearIndex(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; | |
import org.apache.solr.util.AbstractSolrTestCase; | |
import org.apache.solr.client.solrj.SolrQuery; | |
import org.apache.solr.client.solrj.SolrServer; | |
import org.apache.solr.client.solrj.SolrServerException; | |
import org.apache.solr.client.solrj.response.QueryResponse; | |
import org.apache.solr.common.SolrInputDocument; | |
import org.apache.solr.common.params.SolrParams; | |
import org.junit.Before; | |
import org.junit.Test; | |
import static org.junit.Assert.assertEquals; | |
public class SolrSearchConfigTest extends AbstractSolrTestCase { | |
private SolrServer server; | |
@Override | |
public String getSolrHome() { | |
return "backend/src/test/resources/config/solr/solr"; | |
} | |
@Before | |
@Override | |
public void setUp() throws Exception { | |
super.setUp(); | |
server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName()); | |
} | |
@Test | |
public void testThatNoResultsAreReturned() throws SolrServerException { | |
SolrParams params = new SolrQuery("text that is not found"); | |
QueryResponse response = server.query(params); | |
assertEquals(0L, response.getResults().getNumFound()); | |
} | |
@Test | |
public void testThatDocumentIsFound() throws SolrServerException, IOException { | |
SolrInputDocument document = new SolrInputDocument(); | |
document.addField("id", "1"); | |
document.addField("name", "my name"); | |
server.add(document); | |
server.commit(); | |
SolrParams params = new SolrQuery("name"); | |
QueryResponse response = server.query(params); | |
assertEquals(1L, response.getResults().getNumFound()); | |
assertEquals("1", response.getResults().get(0).get("id")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment