1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.search.solr.internal; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.net.URL; |
24 |
|
import java.util.Date; |
25 |
|
|
26 |
|
import org.apache.commons.io.FileUtils; |
27 |
|
import org.apache.solr.core.CoreContainer; |
28 |
|
import org.apache.solr.core.SolrCore; |
29 |
|
import org.junit.Assert; |
30 |
|
import org.junit.Before; |
31 |
|
import org.junit.Rule; |
32 |
|
import org.junit.Test; |
33 |
|
import org.xwiki.component.manager.ComponentLookupException; |
34 |
|
import org.xwiki.component.phase.InitializationException; |
35 |
|
import org.xwiki.configuration.ConfigurationSource; |
36 |
|
import org.xwiki.environment.Environment; |
37 |
|
import org.xwiki.search.solr.internal.api.SolrInstance; |
38 |
|
import org.xwiki.test.annotation.AllComponents; |
39 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
40 |
|
|
41 |
|
import static org.mockito.ArgumentMatchers.any; |
42 |
|
import static org.mockito.ArgumentMatchers.anyString; |
43 |
|
import static org.mockito.ArgumentMatchers.eq; |
44 |
|
import static org.mockito.Mockito.when; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@version |
50 |
|
|
51 |
|
@AllComponents |
|
|
| 91.9% |
Uncovered Elements: 3 (37) |
Complexity: 7 |
Complexity Density: 0.23 |
|
52 |
|
public class EmbeddedSolrInstanceInitializationTest |
53 |
|
{ |
54 |
|
@Rule |
55 |
|
public final MockitoComponentManagerRule mocker = new MockitoComponentManagerRule(); |
56 |
|
|
57 |
|
protected File PERMANENT_DIRECTORY = new File("target", "data-" + new Date().getTime()); |
58 |
|
|
59 |
|
protected ConfigurationSource mockXWikiProperties; |
60 |
|
|
61 |
|
protected static String solrHomeProperty = String.format("%s.%s.%s", "solr", EmbeddedSolrInstance.TYPE, "home"); |
62 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
63 |
3 |
@Before... |
64 |
|
public void setup() throws Exception |
65 |
|
{ |
66 |
3 |
Environment mockEnvironment = this.mocker.registerMockComponent(Environment.class); |
67 |
|
|
68 |
3 |
when(mockEnvironment.getPermanentDirectory()).thenReturn(PERMANENT_DIRECTORY); |
69 |
|
|
70 |
3 |
FileUtils.deleteDirectory(PERMANENT_DIRECTORY); |
71 |
3 |
PERMANENT_DIRECTORY.mkdirs(); |
72 |
|
|
73 |
3 |
this.mockXWikiProperties = this.mocker.registerMockComponent(ConfigurationSource.class, "xwikiproperties"); |
74 |
|
} |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
76 |
1 |
@Test... |
77 |
|
public void testInitialization() throws Exception |
78 |
|
{ |
79 |
1 |
URL url = this.getClass().getClassLoader().getResource("solrhome"); |
80 |
1 |
when(this.mockXWikiProperties.getProperty(eq(solrHomeProperty), anyString())).thenReturn(url.getPath()); |
81 |
|
|
82 |
1 |
getInstanceAndAssertHomeDirectory(url.getPath()); |
83 |
|
} |
84 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
85 |
1 |
@Test... |
86 |
|
public void testInstantiationNewHome() throws Exception |
87 |
|
{ |
88 |
1 |
String newHome = new File(PERMANENT_DIRECTORY, "doesNotExist").getAbsolutePath(); |
89 |
1 |
when(this.mockXWikiProperties.getProperty(eq(solrHomeProperty), anyString())).thenReturn(newHome); |
90 |
|
|
91 |
1 |
getInstanceAndAssertHomeDirectory(newHome); |
92 |
|
} |
93 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
1PASS
|
|
94 |
1 |
@Test... |
95 |
|
public void testInstantiationInvalidHome() throws ComponentLookupException, Exception |
96 |
|
{ |
97 |
1 |
when(this.mockXWikiProperties.getProperty(eq(solrHomeProperty), anyString())).thenReturn(""); |
98 |
|
|
99 |
|
|
100 |
1 |
try { |
101 |
1 |
getInstanceAndAssertHomeDirectory(null); |
102 |
|
|
103 |
0 |
Assert.fail("Specify a valid directory. Empty values are not accepted."); |
104 |
|
} catch (ComponentLookupException e) { |
105 |
1 |
Assert.assertTrue(e.getCause() instanceof InitializationException); |
106 |
1 |
Assert.assertTrue(e.getCause().getCause() instanceof IllegalArgumentException); |
107 |
|
} |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
@param |
114 |
|
@throws |
115 |
|
@throws |
116 |
|
|
|
|
| 86.7% |
Uncovered Elements: 2 (15) |
Complexity: 2 |
Complexity Density: 0.15 |
|
117 |
3 |
private void getInstanceAndAssertHomeDirectory(String expected) throws ComponentLookupException, Exception... |
118 |
|
{ |
119 |
3 |
SolrInstance instance = mocker.getInstance(SolrInstance.class, "embedded"); |
120 |
2 |
Assert.assertNotNull(instance); |
121 |
|
|
122 |
2 |
EmbeddedSolrInstance implementation = ((EmbeddedSolrInstance) instance); |
123 |
2 |
CoreContainer container = implementation.getContainer(); |
124 |
|
|
125 |
2 |
if (expected == null) { |
126 |
0 |
expected = implementation.getDefaultHomeDirectory(); |
127 |
|
} |
128 |
|
|
129 |
2 |
Assert.assertEquals(expected, container.getSolrHome()); |
130 |
2 |
Assert.assertEquals(1, container.getCores().size()); |
131 |
2 |
SolrCore core = container.getCores().iterator().next(); |
132 |
2 |
File coreBaseDirectory = new File(container.getSolrHome(), core.getName()); |
133 |
2 |
File configDirectory = new File(coreBaseDirectory, DefaultSolrConfiguration.CONF_DIRECTORY); |
134 |
2 |
Assert.assertTrue(new File(configDirectory, core.getSchemaResource()).exists()); |
135 |
2 |
Assert.assertTrue(new File(configDirectory, core.getConfigResource()).exists()); |
136 |
|
} |
137 |
|
} |