NIHVIVO-336 A basic setup for reading files from /WEB-INF/classes/smokeTests and running smoketests for the classes in those files is in place.

This commit is contained in:
deepakkoni 2011-07-27 19:23:25 +00:00
parent 436d868fb9
commit 137357c754
5 changed files with 110 additions and 36 deletions

View file

@ -2,16 +2,28 @@
package edu.cornell.mannlib.vitro.webapp.controller.freemarker; package edu.cornell.mannlib.vitro.webapp.controller.freemarker;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.ServletContext;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.ResponseValues;
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues; import edu.cornell.mannlib.vitro.webapp.controller.freemarker.responsevalues.TemplateResponseValues;
import edu.cornell.mannlib.vitro.webapp.utils.smoketest.SolrContextChecker; import edu.cornell.mannlib.vitro.webapp.utils.smoketest.SmokeTest;
import edu.cornell.mannlib.vitro.webapp.utils.smoketest.TestResult; import edu.cornell.mannlib.vitro.webapp.utils.smoketest.TestResult;
/** /**
@ -23,39 +35,90 @@ import edu.cornell.mannlib.vitro.webapp.utils.smoketest.TestResult;
public class SmokeTestController extends FreemarkerHttpServlet { public class SmokeTestController extends FreemarkerHttpServlet {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(SmokeTestController.class.getName());
private static final String TEMPLATE_NAME = "smokeTest.ftl"; private static final String TEMPLATE_NAME = "smokeTest.ftl";
private static final String FILE_PATH = "/WEB-INF/classes/smokeTests";
private static final String PACKAGE_CONTAINING_SMOKETEST_CLASSES = "edu.cornell.mannlib.vitro.webapp.utils.smoketest.";
private List<String> listOfSmokeTestClasses = new ArrayList<String>();
private static final Log log = LogFactory.getLog(SmokeTestController.class.getName());
@Override @Override
protected ResponseValues processRequest(VitroRequest vreq){ protected ResponseValues processRequest(VitroRequest vreq){
SolrContextChecker solrContextChecker = new SolrContextChecker(); List<TestResult> results = new ArrayList<TestResult>();
TestResult result = solrContextChecker.test(vreq); ServletContext context = vreq.getSession().getServletContext();
readSmokeTestFilesFromPath(context);
Map<String, Object> body = new HashMap<String, Object>(); for(String className : listOfSmokeTestClasses){
try {
body.put("SolrContextChecker", result); Class thisClass = Class.forName(PACKAGE_CONTAINING_SMOKETEST_CLASSES +""+ className);
SmokeTest smokeTestsRunner = (SmokeTest)thisClass.newInstance();
//Deepak, results.add(smokeTestsRunner.test(vreq));
//This controller will need to load a list of tests.
//The list should be in files in a directory. } catch (ClassNotFoundException e) {
//ex. log.error("Class not found "+ e);
// vivo/WEB-INF/classes/resources/smokeTests/vitroTests.txt } catch(IllegalAccessException e){
// vivo/WEB-INF/classes/resources/smokeTests/vivoTests.txt log.error("Illegal access of the class " + e);
// vivo/WEB-INF/classes/resources/smokeTests/otherTests.txt } catch(InstantiationException e){
// log.error("Error instantiating class " + e);
// This controller should: }
// 1) look for that directory, }
// 2) get a list of all files in that directory
// 3) For each file:
// 4) For each line in the file: Map<String, Object> body = new HashMap<String, Object>();
// 5) Assume that the line has a fully qualified java class name body.put("results", results);
// and name and make a new instance of that class.
// If that class can be created and it is a SmokeTest, run
// the test and save the results in a list.
// 6) put the result list in the template variable map
return new TemplateResponseValues(TEMPLATE_NAME, body); return new TemplateResponseValues(TEMPLATE_NAME, body);
} }
private void readSmokeTestFilesFromPath(ServletContext context) {
log.debug("Reading smoketest files from "+ FILE_PATH );
Set<String> paths = context.getResourcePaths(FILE_PATH);
if(paths != null){
for(String p : paths){
readSmokeTestClassesFromFile(p, context);
}
}
}
private void readSmokeTestClassesFromFile(String p, ServletContext context) {
//check that this is a file and not a directory.
File f = new File(context.getRealPath(p));
if(f.exists() && f.isFile()){
InputStream fileStream = context.getResourceAsStream(p);
listOfSmokeTestClasses.addAll(getContentsFromFileStream(fileStream));
} else {
if(!f.exists()){
log.debug("File for path " + p + " does not exist");
}else if(f.isDirectory()){
log.debug("Path " + p + " corresponds to a directory and not file. File was not read.");
}
}
}
private List<String> getContentsFromFileStream(InputStream fileStream) {
List<String> classesList = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(fileStream));
String text = "";
try {
while((text = reader.readLine()) != null){
//ignore comments in the file.
if(text.startsWith("#") || StringUtils.isEmpty(text) || StringUtils.isBlank(text)){
continue;
}
classesList.add(text);
}
} catch (IOException e) {
log.error("Error reading file " + e);
}
return classesList;
}
} }

View file

@ -8,7 +8,9 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
* Any class that wishes to output status to smoketest.ftl * Any class that wishes to output status to smoketest.ftl
* implements this interface. * implements this interface.
*/ */
public interface SmokeTestsRunner { public interface SmokeTest {
public TestResult test(VitroRequest vreq); public TestResult test(VitroRequest vreq);
public String getName();
} }

View file

@ -16,7 +16,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
* TODO: This is just an initial test implementation and will continue * TODO: This is just an initial test implementation and will continue
* to change. * to change.
*/ */
public class SolrContextChecker implements SmokeTestsRunner { public class SolrContextChecker implements SmokeTest {
@Override @Override
public TestResult test(VitroRequest vreq) { public TestResult test(VitroRequest vreq) {
@ -38,4 +38,9 @@ public class SolrContextChecker implements SmokeTestsRunner {
return testResult; return testResult;
} }
@Override
public String getName(){
return SolrContextChecker.class.getName();
}
} }

View file

@ -11,11 +11,11 @@ package edu.cornell.mannlib.vitro.webapp.utils.smoketest;
public class TestResult { public class TestResult {
private String result = ""; private String result = "";
private boolean message = false; private boolean success = false;
public TestResult(String result, boolean message) { public TestResult(String result, boolean message) {
this.result = result; this.result = result;
this.message = message; this.success = message;
} }
public String getResult(){ public String getResult(){
@ -23,7 +23,7 @@ public class TestResult {
} }
public boolean getMessage(){ public boolean getMessage(){
return message; return success;
} }

View file

@ -4,6 +4,10 @@
<#-- TODO: This is an initial implementation and will continue to evolve. --> <#-- TODO: This is an initial implementation and will continue to evolve. -->
<#if SolrContextChecker??> <#if results??>
<h2>${SolrContextChecker.result}</h2> <#list results as x>
<#if x??>
<h2>${x.result}</h2>
</#if>
</#list>
</#if> </#if>