mirror of
https://github.com/apache/lucene.git
synced 2025-02-28 13:29:26 +00:00
Adding simple tests to increase code coverage for all SolrInfoMBeans -- this scans the classpath and calls all the functions (not expecting much)
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@663687 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
eaaf6236da
commit
0bbb3180fd
@ -267,9 +267,11 @@ public class SearchHandler extends RequestHandlerBase implements SolrCoreAware
|
||||
public String getDescription() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Search using components: ");
|
||||
for(SearchComponent c : components){
|
||||
sb.append(c.getName());
|
||||
sb.append(",");
|
||||
if( components != null ) {
|
||||
for(SearchComponent c : components){
|
||||
sb.append(c.getName());
|
||||
sb.append(",");
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
100
src/test/org/apache/solr/SolrInfoMBeanTest.java
Normal file
100
src/test/org/apache/solr/SolrInfoMBeanTest.java
Normal file
@ -0,0 +1,100 @@
|
||||
package org.apache.solr;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
|
||||
import javax.management.Query;
|
||||
|
||||
import org.apache.lucene.search.function.FieldCacheSource;
|
||||
import org.apache.solr.common.util.NamedList;
|
||||
import org.apache.solr.core.SolrInfoMBean;
|
||||
import org.apache.solr.handler.StandardRequestHandler;
|
||||
import org.apache.solr.handler.admin.LukeRequestHandler;
|
||||
import org.apache.solr.handler.component.SearchComponent;
|
||||
import org.apache.solr.handler.component.SearchHandler;
|
||||
import org.apache.solr.highlight.DefaultSolrHighlighter;
|
||||
import org.apache.solr.search.LRUCache;
|
||||
import org.apache.solr.search.QueryUtils;
|
||||
import org.apache.solr.update.UpdateHandler;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* A simple test used to increase code coverage for some standard things...
|
||||
*/
|
||||
public class SolrInfoMBeanTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Gets a list of everything we can find in the classpath and makes sure it has
|
||||
* a name, description, etc...
|
||||
*/
|
||||
public void testCallMBeanInfo() throws Exception {
|
||||
List<Class> classes = new ArrayList<Class>();
|
||||
classes.addAll(getClassesForPackage(StandardRequestHandler.class.getPackage().getName()));
|
||||
classes.addAll(getClassesForPackage(SearchHandler.class.getPackage().getName()));
|
||||
classes.addAll(getClassesForPackage(SearchComponent.class.getPackage().getName()));
|
||||
classes.addAll(getClassesForPackage(LukeRequestHandler.class.getPackage().getName()));
|
||||
classes.addAll(getClassesForPackage(DefaultSolrHighlighter.class.getPackage().getName()));
|
||||
classes.addAll(getClassesForPackage(LRUCache.class.getPackage().getName()));
|
||||
// System.out.println(classes);
|
||||
|
||||
int checked = 0;
|
||||
for( Class clazz : classes ) {
|
||||
if( SolrInfoMBean.class.isAssignableFrom( clazz ) ) {
|
||||
try {
|
||||
SolrInfoMBean info = (SolrInfoMBean)clazz.newInstance();
|
||||
|
||||
//System.out.println( info.getClass() );
|
||||
assertNotNull( info.getName() );
|
||||
assertNotNull( info.getDescription() );
|
||||
assertNotNull( info.getSource() );
|
||||
assertNotNull( info.getSourceId() );
|
||||
assertNotNull( info.getVersion() );
|
||||
assertNotNull( info.getCategory() );
|
||||
|
||||
if( info instanceof LRUCache ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
assertNotNull( info.toString() );
|
||||
// increase code coverage...
|
||||
assertNotNull( info.getDocs() + "" );
|
||||
assertNotNull( info.getStatistics()+"" );
|
||||
checked++;
|
||||
}
|
||||
catch( InstantiationException ex ) {
|
||||
// expected...
|
||||
//System.out.println( "unable to initalize: "+clazz );
|
||||
}
|
||||
}
|
||||
}
|
||||
assertTrue( "there are at leaset 10 SolrInfoMBean that should be found in the classpath.", checked > 10 );
|
||||
}
|
||||
|
||||
private static List<Class> getClassesForPackage(String pckgname) throws Exception {
|
||||
ArrayList<File> directories = new ArrayList<File>();
|
||||
ClassLoader cld = Thread.currentThread().getContextClassLoader();
|
||||
String path = pckgname.replace('.', '/');
|
||||
Enumeration<URL> resources = cld.getResources(path);
|
||||
while (resources.hasMoreElements()) {
|
||||
directories.add(new File(URLDecoder.decode(resources.nextElement().getPath(), "UTF-8")));
|
||||
}
|
||||
|
||||
ArrayList<Class> classes = new ArrayList<Class>();
|
||||
for (File directory : directories) {
|
||||
if (directory.exists()) {
|
||||
String[] files = directory.list();
|
||||
for (String file : files) {
|
||||
if (file.endsWith(".class")) {
|
||||
classes.add(Class.forName(pckgname + '.' + file.substring(0, file.length() - 6)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return classes;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user