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
19
20
21
22
23
24
25 public abstract class BaseTestCase {
26 protected final Logger LOG = LoggerFactory.getLogger(this.getClass());
27 protected String classesDir;
28 protected String solrHome;
29 protected DiscoveryEnvUtil discoveryEnvUtil = new DiscoveryEnvUtil();
30
31
32 public BaseTestCase() {
33 classesDir = getClass().getResource("/").getPath();
34 printLogInfo("classesDir = " + new File(classesDir).getAbsolutePath());
35 discoveryEnvUtil.initTestEnvironment();
36 discoveryEnvUtil.printEnvironment();
37
38
39
40
41
42
43
44
45
46
47
48
49 System.setProperty(SolrServerManager.SOLR_SERVER_EMBEDDED, "true");
50
51 }
52
53 protected void printLogInfo(String msg) {
54 System.out.println(msg);
55 LOG.info(msg);
56 }
57
58 protected void setUp() throws Exception {
59 }
60
61 protected String readFile(File file) throws IOException {
62 BufferedReader reader = new BufferedReader(new FileReader(file));
63 String line = null;
64 StringBuilder stringBuilder = new StringBuilder();
65 String ls = System.getProperty("line.separator");
66 while ((line = reader.readLine()) != null) {
67 stringBuilder.append(line);
68 stringBuilder.append(ls);
69 }
70 return stringBuilder.toString();
71 }
72
73 protected QueryResponse executeQuery(String args) throws Exception {
74 SolrServer solr = SolrServerManager.getInstance().getSolrServer();
75 SolrQuery query = new SolrQuery();
76 query.setQuery(args);
77 QueryResponse response = solr.query(query);
78 return response;
79 }
80
81 protected long getRecordCount() throws Exception {
82 QueryResponse response = executeQuery("*:*");
83 return response.getResults().getNumFound();
84 }
85
86
87 protected void cleanUpData() throws Exception {
88 SolrServer server = SolrServerManager.getInstance().getSolrServer();
89 server.deleteByQuery("*:*");
90 server.commit();
91 }
92
93 protected static String getMethodName(StackTraceElement e[]) {
94 boolean doNext = false;
95 String result = "";
96 for (StackTraceElement s : e) {
97 if (doNext) {
98 result = s.getMethodName();
99 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 }