diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SmokeTestController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SmokeTestController.java index 6c51ed9c8..c6c05cf33 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SmokeTestController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/freemarker/SmokeTestController.java @@ -2,16 +2,28 @@ 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.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.LogFactory; 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.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; /** @@ -23,39 +35,90 @@ import edu.cornell.mannlib.vitro.webapp.utils.smoketest.TestResult; public class SmokeTestController extends FreemarkerHttpServlet { 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 listOfSmokeTestClasses = new ArrayList(); + + private static final Log log = LogFactory.getLog(SmokeTestController.class.getName()); + @Override protected ResponseValues processRequest(VitroRequest vreq){ - - SolrContextChecker solrContextChecker = new SolrContextChecker(); - TestResult result = solrContextChecker.test(vreq); - Map body = new HashMap(); - - body.put("SolrContextChecker", result); + List results = new ArrayList(); + ServletContext context = vreq.getSession().getServletContext(); + readSmokeTestFilesFromPath(context); - //Deepak, - //This controller will need to load a list of tests. - //The list should be in files in a directory. - //ex. - // vivo/WEB-INF/classes/resources/smokeTests/vitroTests.txt - // vivo/WEB-INF/classes/resources/smokeTests/vivoTests.txt - // vivo/WEB-INF/classes/resources/smokeTests/otherTests.txt - // - // 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: - // 5) Assume that the line has a fully qualified java class name - // 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 - + for(String className : listOfSmokeTestClasses){ + try { + + Class thisClass = Class.forName(PACKAGE_CONTAINING_SMOKETEST_CLASSES +""+ className); + SmokeTest smokeTestsRunner = (SmokeTest)thisClass.newInstance(); + + results.add(smokeTestsRunner.test(vreq)); + + } catch (ClassNotFoundException e) { + log.error("Class not found "+ e); + } catch(IllegalAccessException e){ + log.error("Illegal access of the class " + e); + } catch(InstantiationException e){ + log.error("Error instantiating class " + e); + } + } + + + Map body = new HashMap(); + body.put("results", results); + return new TemplateResponseValues(TEMPLATE_NAME, body); } + + private void readSmokeTestFilesFromPath(ServletContext context) { + + log.debug("Reading smoketest files from "+ FILE_PATH ); + Set 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 getContentsFromFileStream(InputStream fileStream) { + + List classesList = new ArrayList(); + 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; + + } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SmokeTestsRunner.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SmokeTest.java similarity index 86% rename from webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SmokeTestsRunner.java rename to webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SmokeTest.java index 002e3bfd5..c23ba50bc 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SmokeTestsRunner.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SmokeTest.java @@ -8,7 +8,9 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; * Any class that wishes to output status to smoketest.ftl * implements this interface. */ -public interface SmokeTestsRunner { +public interface SmokeTest { public TestResult test(VitroRequest vreq); + + public String getName(); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SolrContextChecker.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SolrContextChecker.java index 00e485288..2c1fb6662 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SolrContextChecker.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/SolrContextChecker.java @@ -16,7 +16,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest; * TODO: This is just an initial test implementation and will continue * to change. */ -public class SolrContextChecker implements SmokeTestsRunner { +public class SolrContextChecker implements SmokeTest { @Override public TestResult test(VitroRequest vreq) { @@ -37,5 +37,10 @@ public class SolrContextChecker implements SmokeTestsRunner { return testResult; } + + @Override + public String getName(){ + return SolrContextChecker.class.getName(); + } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/TestResult.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/TestResult.java index 5a27c307c..c91a6279a 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/TestResult.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/utils/smoketest/TestResult.java @@ -11,11 +11,11 @@ package edu.cornell.mannlib.vitro.webapp.utils.smoketest; public class TestResult { private String result = ""; - private boolean message = false; + private boolean success = false; public TestResult(String result, boolean message) { this.result = result; - this.message = message; + this.success = message; } public String getResult(){ @@ -23,7 +23,7 @@ public class TestResult { } public boolean getMessage(){ - return message; + return success; } diff --git a/webapp/web/templates/freemarker/body/smokeTest.ftl b/webapp/web/templates/freemarker/body/smokeTest.ftl index 4661f86b6..6970ff9ad 100644 --- a/webapp/web/templates/freemarker/body/smokeTest.ftl +++ b/webapp/web/templates/freemarker/body/smokeTest.ftl @@ -4,6 +4,10 @@ <#-- TODO: This is an initial implementation and will continue to evolve. --> -<#if SolrContextChecker??> -

${SolrContextChecker.result}

+<#if results??> + <#list results as x> + <#if x??> +

${x.result}

+ + \ No newline at end of file