Revision 7590
Added by ben leinfelder over 11 years ago
metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/SolrIndexIT.java | ||
---|---|---|
13 | 13 |
|
14 | 14 |
|
15 | 15 |
|
16 |
import org.apache.commons.io.FileUtils; |
|
17 | 16 |
import org.apache.solr.client.solrj.SolrServer; |
18 | 17 |
import org.apache.solr.client.solrj.SolrServerException; |
19 | 18 |
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; |
20 | 19 |
import org.apache.solr.client.solrj.response.QueryResponse; |
21 |
import org.apache.solr.common.SolrDocument; |
|
22 |
import org.apache.solr.common.SolrDocumentList; |
|
23 | 20 |
import org.apache.solr.common.params.SolrParams; |
24 | 21 |
import org.apache.solr.core.CoreContainer; |
25 | 22 |
import org.apache.solr.servlet.SolrRequestParsers; |
23 |
import org.dataone.configuration.Settings; |
|
26 | 24 |
import org.dataone.service.types.v1.SystemMetadata; |
27 | 25 |
import org.dataone.service.util.TypeMarshaller; |
26 |
import org.junit.Before; |
|
28 | 27 |
import org.junit.Test; |
29 | 28 |
import org.xml.sax.SAXException; |
30 | 29 |
|
... | ... | |
36 | 35 |
private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml"; |
37 | 36 |
private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098"; |
38 | 37 |
private static final String newId = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39099"; |
39 |
private static final String SOLRHOME = "solr-home"; |
|
40 |
private static final String SOLRTESTHOMEPATH = "target/"+SOLRHOME; |
|
38 |
private static String SOLRTESTHOMEPATH = null; |
|
41 | 39 |
|
40 |
/** |
|
41 |
* |
|
42 |
*/ |
|
43 |
@Before |
|
44 |
public void setUp() { |
|
45 |
SOLRTESTHOMEPATH = Settings.getConfiguration().getString("solr.homeDir"); |
|
46 |
} |
|
42 | 47 |
|
43 | 48 |
/** |
44 | 49 |
* Test building index for an insert. |
45 | 50 |
*/ |
46 | 51 |
@Test |
47 | 52 |
public void testInsert() throws Exception { |
53 |
try { |
|
48 | 54 |
SolrIndex solrIndex = generateSolrIndex(); |
55 |
|
|
49 | 56 |
//InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH)); |
50 | 57 |
SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH); |
51 | 58 |
InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH)); |
52 | 59 |
solrIndex.insert(id, systemMetadata, emlInputStream); |
53 | 60 |
String result = doQuery(solrIndex.getSolrServer()); |
54 | 61 |
assertTrue(result.contains("version1")); |
62 |
} catch (Exception e) { |
|
63 |
e.printStackTrace(); |
|
64 |
} |
|
55 | 65 |
} |
56 | 66 |
|
57 | 67 |
/** |
... | ... | |
128 | 138 |
* Generate a test solr server |
129 | 139 |
*/ |
130 | 140 |
private SolrServer generateSolrServer() throws IOException, ParserConfigurationException, SAXException { |
131 |
createSolrHome(); |
|
132 |
System.setProperty(SolrIndex.SOLRHOME, SOLRTESTHOMEPATH); |
|
133 |
//System.out.println("====="+"/Users/tao/projects/metacat-index/"+SOLRTESTHOMEPATH); |
|
134 |
CoreContainer.Initializer init = new CoreContainer.Initializer(); |
|
135 |
CoreContainer c = init.initialize(); |
|
141 |
//createSolrHome(); |
|
142 |
File configFile = new File(SOLRTESTHOMEPATH, "solr.xml"); |
|
143 |
CoreContainer c = new CoreContainer(SOLRTESTHOMEPATH, configFile); |
|
144 |
c.load(SOLRTESTHOMEPATH, configFile); |
|
136 | 145 |
SolrServer solrServer = new EmbeddedSolrServer(c, "collection1"); |
137 | 146 |
return solrServer; |
138 | 147 |
} |
139 | 148 |
|
140 |
/* |
|
141 |
* Create a solr home directory |
|
142 |
*/ |
|
143 |
private void createSolrHome() throws IOException { |
|
144 |
File solrHomeTestDir = new File(SOLRTESTHOMEPATH); |
|
145 |
solrHomeTestDir.mkdir(); |
|
146 |
File solrData = new File(SOLRTESTHOMEPATH+"/data"); |
|
147 |
solrData.mkdir(); |
|
148 |
File solr_home_source_dir = new File("src/main/resources/"+SOLRHOME); |
|
149 |
FileUtils.copyDirectory(solr_home_source_dir, solrHomeTestDir); |
|
150 |
} |
|
151 | 149 |
} |
152 | 150 |
|
metacat-index/src/test/resources/org/dataone/configuration/test.properties | ||
---|---|---|
1 |
dataone.hazelcast.configFilePath=../lib/hazelcast.xml |
|
1 |
dataone.hazelcast.configFilePath=../lib/hazelcast.xml |
|
2 |
solr.homeDir=target/classes/solr-home |
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SolrIndex.java | ||
---|---|---|
27 | 27 |
package edu.ucsb.nceas.metacat.index; |
28 | 28 |
|
29 | 29 |
import java.io.ByteArrayInputStream; |
30 |
import java.io.File; |
|
30 | 31 |
import java.io.IOException; |
31 | 32 |
import java.io.InputStream; |
32 | 33 |
import java.util.ArrayList; |
... | ... | |
76 | 77 |
*/ |
77 | 78 |
public class SolrIndex { |
78 | 79 |
|
79 |
|
|
80 |
|
|
81 |
public static final String SOLRHOME = "solr.solr.home"; |
|
82 | 80 |
public static final String SOLRHOMEPROPERTYNAME = "solr.homeDir"; |
83 | 81 |
|
84 |
private static final String SOLRINDEXWEBCONTEXT = "metacat-index"; |
|
85 |
private static final String SOLRSERVERNAME = "metacat-core"; |
|
86 | 82 |
//private static final String DEFAULTSOLRHOMEPATH = "/Users/tao/Downloads/apache-solr-3.4.0/example/solr"; |
87 | 83 |
|
88 | 84 |
private List<IDocumentSubprocessor> subprocessors = null; |
... | ... | |
126 | 122 |
String solrHomeDir = null; |
127 | 123 |
solrHomeDir = Settings.getConfiguration().getString(SOLRHOMEPROPERTYNAME); |
128 | 124 |
log.info("========================= the solr home from the metacat.properties is "+solrHomeDir); |
129 |
if(solrHomeDir == null || solrHomeDir.trim().equals("")) { |
|
130 |
String deployDir = Settings.getConfiguration().getString("application.deployDir"); |
|
131 |
if(deployDir == null || deployDir.trim().equals("")) { |
|
132 |
solrHomeDir = SOLRINDEXWEBCONTEXT+"/WEB-INF/classes/solr-home"; |
|
133 |
} else { |
|
134 |
solrHomeDir = deployDir + "/" +SOLRINDEXWEBCONTEXT+"/WEB-INF/classes/solr-home"; |
|
135 |
} |
|
136 |
|
|
137 |
} |
|
138 |
log.info("==========================================final solr home is "+solrHomeDir); |
|
139 |
System.setProperty(SOLRHOME, solrHomeDir); |
|
140 |
CoreContainer.Initializer init = new CoreContainer.Initializer(); |
|
141 |
CoreContainer c = init.initialize(); |
|
125 |
File configFile = new File(solrHomeDir, "solr.xml"); |
|
126 |
CoreContainer c = new CoreContainer(solrHomeDir, configFile); |
|
127 |
c.load(solrHomeDir, configFile); |
|
142 | 128 |
solrServer = new EmbeddedSolrServer(c, "collection1"); |
143 | 129 |
} |
144 | 130 |
|
Also available in: Unified diff
simplify testing with embedded solr server locations -- no need to copy solr-home or set system properties. Still more to do (factory method) but this is getting cleaner.