Apache Lucene open-source search software
tests: fix resource leaks + simplify
Yonik Seeley9355b6d473cc
diff --git a/solr/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java b/solr/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.javaindex a9ec8f5348a..b81a85cd12e 100644--- a/solr/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java+++ b/solr/src/test/org/apache/solr/handler/component/QueryElevationComponentTest.java@@ -101,29 +101,15 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {assertEquals( "xxxx", comp.getAnalyzedQuery( "XXXX" ) );assertEquals( "xxxxyyyy", comp.getAnalyzedQuery( "XXXX YYYY" ) );- }-- @Test- public void testEmptyQuery() throws Exception {- SolrCore core = h.getCore();-- //String query = "title:ipod";- Map<String,String> args = new HashMap<String, String>();- args.put( "q.alt", "*:*" );- args.put( "defType", "dismax");- args.put( CommonParams.QT, "/elevate" );- //args.put( CommonParams.FL, "id,title,score" );- SolrQueryRequest req = new LocalSolrQueryRequest( core, new MapSolrParams( args) );- assertQ("Make sure QEC handles null queries", req, "//*[@numFound='0']");+ assertQ("Make sure QEC handles null queries", req("qt","/elevate", "q.alt","*:*", "defType","dismax"),+ "//*[@numFound='0']");}@Testpublic void testSorting() throws IOException{- SolrCore core = h.getCore();-assertU(adoc("id", "a", "title", "ipod", "str_s", "a" ));assertU(adoc("id", "b", "title", "ipod ipod", "str_s", "b" ));assertU(adoc("id", "c", "title", "ipod ipod ipod", "str_s", "c" ));@@ -141,8 +127,10 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {args.put( CommonParams.FL, "id,score" );args.put( "indent", "true" );//args.put( CommonParams.FL, "id,title,score" );- SolrQueryRequest req = new LocalSolrQueryRequest( core, new MapSolrParams( args) );-+ SolrQueryRequest req = new LocalSolrQueryRequest( h.getCore(), new MapSolrParams( args) );+ IndexReader reader = req.getSearcher().getReader();+ QueryElevationComponent booster = (QueryElevationComponent)req.getCore().getSearchComponent( "elevate" );+assertQ("Make sure standard sort works as expected", req,"//*[@numFound='3']","//result/doc[1]/str[@name='id'][.='a']"@@ -151,11 +139,10 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {);// Explicitly set what gets boosted- IndexReader reader = core.getSearcher().get().getReader();- QueryElevationComponent booster = (QueryElevationComponent)core.getSearchComponent( "elevate" );booster.elevationCache.clear();booster.setTopQueryResults( reader, query, new String[] { "x", "y", "z" }, null );+assertQ("All six should make it", req,"//*[@numFound='6']","//result/doc[1]/str[@name='id'][.='x']"@@ -230,6 +217,8 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {,"//result/doc[3]/str[@name='id'][.='c']");++ req.close();}// write a test file to boost some docs@@ -253,31 +242,33 @@ public class QueryElevationComponentTest extends SolrTestCaseJ4 {@Testpublic void testElevationReloading() throws Exception{- SolrCore core = h.getCore();-String testfile = "data-elevation.xml";- File f = new File( core.getDataDir(), testfile );+ File f = new File( h.getCore().getDataDir(), testfile );writeFile( f, "aaa", "A" );- QueryElevationComponent comp = (QueryElevationComponent)core.getSearchComponent("elevate");+ QueryElevationComponent comp = (QueryElevationComponent)h.getCore().getSearchComponent("elevate");NamedList<String> args = new NamedList<String>();args.add( QueryElevationComponent.CONFIG_FILE, testfile );comp.init( args );- comp.inform( core );-- IndexReader reader = core.getSearcher().get().getReader();- Map<String, ElevationObj> map = comp.getElevationMap(reader, core);+ comp.inform( h.getCore() );++ SolrQueryRequest req = req();+ IndexReader reader = req.getSearcher().getReader();+ Map<String, ElevationObj> map = comp.getElevationMap(reader, h.getCore());assertTrue( map.get( "aaa" ).priority.containsKey( new BytesRef("A") ) );assertNull( map.get( "bbb" ) );+ req.close();// now change the filewriteFile( f, "bbb", "B" );assertU(adoc("id", "10000")); // will get same reader if no index changeassertU(commit());-- reader = core.getSearcher().get().getReader();- map = comp.getElevationMap(reader, core);++ req = req();+ reader = req.getSearcher().getReader();+ map = comp.getElevationMap(reader, h.getCore());assertNull( map.get( "aaa" ) );assertTrue( map.get( "bbb" ).priority.containsKey( new BytesRef("B") ) );+ req.close();}}