001package org.kuali.ole.docstore.discovery; 002 003import org.apache.solr.client.solrj.SolrQuery; 004import org.apache.solr.client.solrj.SolrServer; 005import org.apache.solr.client.solrj.response.QueryResponse; 006import org.kuali.ole.docstore.discovery.service.SolrServerManager; 007import org.kuali.ole.docstore.discovery.util.DiscoveryEnvUtil; 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011import java.io.BufferedReader; 012import java.io.File; 013import java.io.FileReader; 014import java.io.IOException; 015import java.lang.reflect.Method; 016 017/** 018 * This is the base class for all other test classes. 019 * It defines some values related to environment for the test cases. 020 * It also has some utility methods that can be used by subclasses. 021 * 022 * @author Tirumalesh 023 * Date: 22/11/11 024 */ 025public abstract class BaseTestCase { 026 protected final Logger LOG = LoggerFactory.getLogger(this.getClass()); 027 protected String classesDir; 028 protected String solrHome; 029 protected DiscoveryEnvUtil discoveryEnvUtil = new DiscoveryEnvUtil(); 030 031 032 public BaseTestCase() { 033 classesDir = getClass().getResource("/").getPath(); 034 printLogInfo("classesDir = " + new File(classesDir).getAbsolutePath()); 035 discoveryEnvUtil.initTestEnvironment(); 036 discoveryEnvUtil.printEnvironment(); 037 038 039 /* solrHome = classesDir + "/../../../ole-solr-config"; 040 printLogInfo("solrHome = " + new File(solrHome).getAbsolutePath()); 041 042 String logFilePath = classesDir + "log4j.properties"; 043 PropertyConfigurator.configure(logFilePath); 044 printLogInfo("log4j properties file = " + logFilePath); 045 046 System.setProperty("discovery.properties.file", DiscoveryConstants.TEST_DISCOVERY_PROP_FILE); 047 System.setProperty(SolrServerManager.SOLR_HOME, solrHome); 048*/ 049 System.setProperty(SolrServerManager.SOLR_SERVER_EMBEDDED, "true"); 050 051 } 052 053 protected void printLogInfo(String msg) { 054 System.out.println(msg); 055 LOG.info(msg); 056 } 057 058 protected void setUp() throws Exception { 059 } 060 061 protected String readFile(File file) throws IOException { 062 BufferedReader reader = new BufferedReader(new FileReader(file)); 063 String line = null; 064 StringBuilder stringBuilder = new StringBuilder(); 065 String ls = System.getProperty("line.separator"); 066 while ((line = reader.readLine()) != null) { 067 stringBuilder.append(line); 068 stringBuilder.append(ls); 069 } 070 return stringBuilder.toString(); 071 } 072 073 protected QueryResponse executeQuery(String args) throws Exception { 074 SolrServer solr = SolrServerManager.getInstance().getSolrServer(); 075 SolrQuery query = new SolrQuery(); 076 query.setQuery(args); 077 QueryResponse response = solr.query(query); 078 return response; 079 } 080 081 protected long getRecordCount() throws Exception { 082 QueryResponse response = executeQuery("*:*"); 083 return response.getResults().getNumFound(); 084 } 085 086 087 protected void cleanUpData() throws Exception { 088 SolrServer server = SolrServerManager.getInstance().getSolrServer(); 089 server.deleteByQuery("*:*"); 090 server.commit(); 091 } 092 093 protected static String getMethodName(StackTraceElement e[]) { 094 boolean doNext = false; 095 String result = ""; 096 for (StackTraceElement s : e) { 097 if (doNext) { 098 result = s.getMethodName(); 099 break; 100 } 101 doNext = s.getMethodName().equals("getStackTrace"); 102 } 103 return result; 104 } 105 106 protected static Method getCallingMethod() throws ClassNotFoundException { 107 final Thread t = Thread.currentThread(); 108 final StackTraceElement[] stackTrace = t.getStackTrace(); 109 final StackTraceElement ste = stackTrace[2]; 110 final String methodName = ste.getMethodName(); 111 final String className = ste.getClassName(); 112 Class<?> kls = Class.forName(className); 113 do { 114 for (final Method candidate : kls.getDeclaredMethods()) { 115 if (candidate.getName().equals(methodName)) { 116 return candidate; 117 } 118 } 119 kls = kls.getSuperclass(); 120 } while (kls != null); 121 return null; 122 } 123 124}