1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.search.solr.internal

File EmbeddedSolrInstanceInitializationTest.java

 

Code metrics

2
30
5
1
137
83
7
0.23
6
5
1.4

Classes

Class Line # Actions
EmbeddedSolrInstanceInitializationTest 52 30 0% 7 3
0.918918991.9%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Test the initialization of the Solr instance.
48    *
49    * @version $Id: 317d04d657944d53391b04c08d91ec338fce6c9a $
50    */
51    @AllComponents
 
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   
 
63  3 toggle @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   
 
76  1 toggle @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   
 
85  1 toggle @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   
 
94  1 toggle @Test
95    public void testInstantiationInvalidHome() throws ComponentLookupException, Exception
96    {
97  1 when(this.mockXWikiProperties.getProperty(eq(solrHomeProperty), anyString())).thenReturn("");
98   
99    // Not actually expecting anything. This will throw an exception.
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    * TODO DOCUMENT ME!
112    *
113    * @param expected
114    * @throws ComponentLookupException
115    * @throws Exception
116    */
 
117  3 toggle 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    }