2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
package org.apache.solr.core;
20
import org.apache.solr.SolrTestCaseJ4;
21
import org.apache.solr.search.SolrIndexSearcher;
22
import org.apache.solr.util.RefCounted;
23
import org.apache.solr.common.params.EventParams;
24
import org.apache.lucene.store.Directory;
25
import org.junit.BeforeClass;
26
import org.junit.Test;
28
public class TestQuerySenderListener extends SolrTestCaseJ4 {
30
// number of instances configured in the solrconfig.xml
31
private static final int EXPECTED_MOCK_LISTENER_INSTANCES = 4;
33
private static int preInitMockListenerCount = 0;
36
public static void beforeClass() throws Exception {
37
// record current value prior to core initialization
38
// so we can verify the correct number of instances later
39
// NOTE: this won't work properly if concurrent tests run
41
preInitMockListenerCount = MockEventListener.getCreateCount();
43
initCore("solrconfig-querysender.xml","schema.xml");
46
public void testListenerCreationCounts() {
47
SolrCore core = h.getCore();
49
assertEquals("Unexpected number of listeners created",
50
EXPECTED_MOCK_LISTENER_INSTANCES,
51
MockEventListener.getCreateCount() - preInitMockListenerCount);
55
public void testRequestHandlerRegistry() {
56
// property values defined in build.xml
57
SolrCore core = h.getCore();
59
assertEquals( 2, core.firstSearcherListeners.size() );
60
assertEquals( 2, core.newSearcherListeners.size() );
64
public void testSearcherEvents() throws Exception {
65
SolrCore core = h.getCore();
66
SolrEventListener newSearcherListener = core.newSearcherListeners.get(0);
67
assertTrue("Not an instance of QuerySenderListener", newSearcherListener instanceof QuerySenderListener);
68
QuerySenderListener qsl = (QuerySenderListener) newSearcherListener;
70
RefCounted<SolrIndexSearcher> currentSearcherRef = core.getSearcher();
71
SolrIndexSearcher currentSearcher = currentSearcherRef.get();
72
qsl.newSearcher(currentSearcher, null);//test new Searcher
73
MockQuerySenderListenerReqHandler mock = (MockQuerySenderListenerReqHandler) core.getRequestHandler("mock");
74
assertNotNull("Mock is null", mock);
75
String evt = mock.req.getParams().get(EventParams.EVENT);
76
assertNotNull("Event is null", evt);
77
assertTrue(evt + " is not equal to " + EventParams.FIRST_SEARCHER, evt.equals(EventParams.FIRST_SEARCHER) == true);
78
Directory dir = currentSearcher.getReader().directory();
79
SolrIndexSearcher newSearcher = new SolrIndexSearcher(core, core.getSchema(), "testQuerySenderListener", dir, true, false);
81
qsl.newSearcher(newSearcher, currentSearcher);
82
evt = mock.req.getParams().get(EventParams.EVENT);
83
assertNotNull("Event is null", evt);
84
assertTrue(evt + " is not equal to " + EventParams.NEW_SEARCHER, evt.equals(EventParams.NEW_SEARCHER) == true);
86
currentSearcherRef.decref();