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:
Ryan McKinley 2008-06-05 18:07:35 +00:00
parent eaaf6236da
commit 0bbb3180fd
2 changed files with 105 additions and 3 deletions

View File

@ -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();
}

View 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;
}
}