View Javadoc

1   package org.kuali.ole.docstore.discovery;
2   
3   import org.apache.solr.client.solrj.SolrQuery;
4   import org.apache.solr.client.solrj.SolrServer;
5   import org.apache.solr.client.solrj.response.QueryResponse;
6   import org.kuali.ole.docstore.discovery.service.SolrServerManager;
7   import org.kuali.ole.docstore.discovery.util.DiscoveryEnvUtil;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  
11  import java.io.BufferedReader;
12  import java.io.File;
13  import java.io.FileReader;
14  import java.io.IOException;
15  import java.lang.reflect.Method;
16  
17  /**
18   * This is the base class for all other test classes.
19   * It defines some values related to environment for the test cases.
20   * It also has some utility methods that can be used by subclasses.
21   * @author Tirumalesh
22   * Date: 22/11/11
23   */
24  public abstract class BaseTestCase {
25      protected final Logger LOG = LoggerFactory.getLogger(this.getClass());
26      protected String classesDir;
27      protected String solrHome;
28      protected DiscoveryEnvUtil discoveryEnvUtil = new DiscoveryEnvUtil();
29  
30  
31      public BaseTestCase() {
32          classesDir = getClass().getResource("/").getPath();
33          printLogInfo("classesDir = " + new File(classesDir).getAbsolutePath());
34          discoveryEnvUtil.initTestEnvironment();
35          discoveryEnvUtil.printEnvironment();
36  
37  
38   /*       solrHome = classesDir + "/../../../ole-solr-config";
39          printLogInfo("solrHome = " + new File(solrHome).getAbsolutePath());
40  
41          String logFilePath = classesDir + "log4j.properties";
42          PropertyConfigurator.configure(logFilePath);
43          printLogInfo("log4j properties file = " + logFilePath);
44  
45          System.setProperty("discovery.properties.file", DiscoveryConstants.TEST_DISCOVERY_PROP_FILE);
46          System.setProperty(SolrServerManager.SOLR_HOME, solrHome);
47  */        System.setProperty(SolrServerManager.SOLR_SERVER_EMBEDDED, "true");
48  
49      }
50  
51      protected void printLogInfo(String msg) {
52          System.out.println(msg);
53          LOG.info(msg);
54      }
55  
56      protected void setUp() throws Exception {
57      }
58  
59      protected String readFile(File file) throws IOException {
60          BufferedReader reader = new BufferedReader(new FileReader(file));
61          String line = null;
62          StringBuilder stringBuilder = new StringBuilder();
63          String ls = System.getProperty("line.separator");
64          while ((line = reader.readLine()) != null) {
65              stringBuilder.append(line);
66              stringBuilder.append(ls);
67          }
68          return stringBuilder.toString();
69      }
70  
71      protected QueryResponse executeQuery(String args) throws Exception {
72          SolrServer solr = SolrServerManager.getInstance().getSolrServer();
73          SolrQuery query = new SolrQuery();
74          query.setQuery(args);
75          QueryResponse response = solr.query(query);
76          return response;
77      }
78  
79      protected long getRecordCount() throws Exception {
80          QueryResponse response = executeQuery("*:*");
81          return response.getResults().getNumFound();
82      }
83  
84  
85      
86      protected void cleanUpData() throws Exception {
87          SolrServer server = SolrServerManager.getInstance().getSolrServer();
88          server.deleteByQuery("*:*");
89          server.commit();
90      }
91  
92      protected static String getMethodName(StackTraceElement e[]) {
93          boolean doNext = false;
94          String result = "";
95          for (StackTraceElement s : e) {
96              if (doNext) {
97                  result = s.getMethodName();
98                  break;
99              }
100             doNext = s.getMethodName().equals("getStackTrace");
101         }
102         return result;
103     }
104 
105     protected static Method getCallingMethod() throws ClassNotFoundException {
106         final Thread t = Thread.currentThread();
107         final StackTraceElement[] stackTrace = t.getStackTrace();
108         final StackTraceElement ste = stackTrace[2];
109         final String methodName = ste.getMethodName();
110         final String className = ste.getClassName();
111         Class<?> kls = Class.forName(className);
112         do {
113             for (final Method candidate : kls.getDeclaredMethods()) {
114                 if (candidate.getName().equals(methodName)) {
115                     return candidate;
116                 }
117             }
118             kls = kls.getSuperclass();
119         } while (kls != null);
120         return null;
121     }
122 
123 }