3 package com.cloudera.impala.testutil;
4 import java.text.SimpleDateFormat;
5 import java.util.ArrayList;
6 import java.util.Calendar;
7 import java.util.Collections;
8 import java.util.Scanner;
10 import java.io.StringWriter;
11 import java.io.StringReader;
13 import javax.json.Json;
14 import javax.json.stream.JsonGenerator;
15 import javax.json.JsonReader;
16 import javax.json.JsonObject;
17 import javax.json.JsonWriter;
18 import javax.json.JsonWriterFactory;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 import com.cloudera.impala.thrift.TClientRequest;
25 import com.cloudera.impala.thrift.TNetworkAddress;
26 import com.cloudera.impala.thrift.TQueryCtx;
27 import com.cloudera.impala.thrift.TQueryOptions;
28 import com.cloudera.impala.thrift.TSessionState;
29 import com.cloudera.impala.thrift.TSessionType;
30 import com.cloudera.impala.thrift.TUniqueId;
32 import com.google.common.collect.Maps;
35 private final static Logger
LOG = LoggerFactory.getLogger(TestUtils.class);
62 ArrayList<String> actual, ArrayList<String> expected,
boolean orderMatters) {
64 Collections.sort(actual);
65 Collections.sort(expected);
68 int maxLen = Math.min(actual.size(), expected.size());
69 for (
int i = 0; i < maxLen; ++i) {
70 String expectedStr = expected.get(i).trim();
71 String actualStr = actual.get(i);
73 boolean containsPrefix =
false;
74 for (
int prefixIdx = 0; prefixIdx < expectedFilePrefix_.length; ++prefixIdx) {
78 actualStr = actualStr.replaceFirst(expectedFilePrefix_[prefixIdx],
"");
85 boolean ignoreAfter =
false;
86 for (
int j = 0; j < ignoreContentAfter_.length; ++j) {
93 if (!actualStr.matches(regex)) {
102 Scanner e =
new Scanner(expectedStr);
103 Scanner a =
new Scanner(actualStr);
104 while (a.hasNext() && e.hasNext()) {
105 if (containsPrefix) {
106 if (!a.next().contains(e.next())) {
111 if (!a.next().equals(e.next())) {
117 if (mismatch != -1) {
122 if (e.hasNext() && !a.hasNext()) {
126 }
else if (a.hasNext() != e.hasNext()) {
131 if (mismatch == -1 && actual.size() < expected.size()) {
133 StringBuilder output =
134 new StringBuilder(
"actual result is missing lines:\n");
135 for (
int i = 0; i < actual.size(); ++i) {
136 output.append(actual.get(i)).append(
"\n");
138 output.append(
"missing:\n");
139 for (
int i = actual.size(); i < expected.size(); ++i) {
140 output.append(expected.get(i)).append(
"\n");
142 return output.toString();
145 if (mismatch != -1) {
147 StringBuilder output =
148 new StringBuilder(
"actual result doesn't match expected result:\n");
149 for (
int i = 0; i <= mismatch; ++i) {
150 output.append(actual.get(i)).append(
"\n");
153 for (
int i = 0; i < actual.get(mismatch).length(); ++i) {
157 for (
int i = mismatch + 1; i < actual.size(); ++i) {
158 output.append(actual.get(i)).append(
"\n");
160 output.append(
"\nexpected:\n");
161 for (String str : expected) {
162 output.append(str).append(
"\n");
164 return output.toString();
167 if (actual.size() > expected.size()) {
169 StringBuilder output =
170 new StringBuilder(
"actual result contains extra output:\n");
171 for (String str : actual) {
172 output.append(str).append(
"\n");
174 output.append(
"\nexpected:\n");
175 for (String str : expected) {
176 output.append(str).append(
"\n");
178 return output.toString();
205 TQueryCtx queryCtx =
new TQueryCtx();
206 queryCtx.setRequest(
new TClientRequest(
"FeTests",
new TQueryOptions()));
207 queryCtx.setQuery_id(
new TUniqueId());
208 queryCtx.setSession(
new TSessionState(
new TUniqueId(), TSessionType.BEESWAX,
209 defaultDb, user,
new TNetworkAddress(
"localhost", 0)));
210 SimpleDateFormat formatter =
new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss.SSSSSSSSS");
211 queryCtx.setNow_string(formatter.format(Calendar.getInstance().getTime()));
212 queryCtx.setPid(1000);
220 StringWriter sw =
new StringWriter();
221 JsonWriter jsonWriter = null;
223 JsonReader jr = Json.createReader(
new StringReader(json));
224 JsonObject jobj = jr.readObject();
225 Map<String, Object> properties = Maps.newHashMap();
226 properties.put(JsonGenerator.PRETTY_PRINTING,
true);
227 JsonWriterFactory writerFactory = Json.createWriterFactory(properties);
228 jsonWriter = writerFactory.createWriter(sw);
229 jsonWriter.writeObject(jobj);
230 }
catch (Exception e) {
231 LOG.error(String.format(
"Error pretty printing JSON string %s: %s", json,
235 if (jsonWriter != null) jsonWriter.close();
237 return sw.toString();
static final String[] ignoreContentAfter_
static final String HDFS_FILE_PATH_FILTER
static String applyHdfsFilePathFilter(String hdfsPath)
static final String regexAgainstActual_
static TQueryCtx createQueryContext()
static TQueryCtx createQueryContext(String defaultDb, String user)
static final String HDFS_HOST_PORT_FILTER
static String compareOutput(ArrayList< String > actual, ArrayList< String > expected, boolean orderMatters)
static final String[] expectedFilePrefix_
static String prettyPrintJson(String json)
static final String DEFAULT_DB