Java tutorial
/* * Copyright 2012-2014 the original author or authors. * * Licensed 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 sample.solr; import org.apache.log4j.Logger; 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.SolrDocument; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.core.CoreContainer; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import sample.ui.SampleWebStaticApplication; import java.io.IOException; import static org.hamcrest.Matchers.containsString; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; /** * Basic integration tests for demo application. * * @author Dave Syer */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = SampleWebStaticApplication.class) @WebAppConfiguration @IntegrationTest("server.port=0") @DirtiesContext public class SampleSolrIntegrationTest { static final Logger LOG = Logger.getLogger(SampleSolrIntegrationTest.class); @Value("${local.server.port}") private int port = 0; private EmbeddedSolrServer server; private CoreContainer container; @Before public void setup() { this.container = new CoreContainer("testdata/solr"); this.container.load(); this.server = new EmbeddedSolrServer(container, "collection1"); try { server.deleteByQuery("*:*"); server.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } SolrInputDocument document = new SolrInputDocument(); document.addField("name", "bob"); document.addField("text", "Lorem ipsum"); document.addField("id", "" + System.currentTimeMillis()); try { server.add(document); server.commit(); } catch (SolrServerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testHome() throws Exception { ModifiableSolrParams solrParams = new ModifiableSolrParams(); solrParams.add(CommonParams.Q, "*:*"); QueryResponse queryResponse = server.query(solrParams); for (SolrDocument document : queryResponse.getResults()) { LOG.error("document" + document); } LOG.error("queryResponse" + queryResponse); } }