3 package com.cloudera.impala.testutil;
5 import static org.junit.Assert.fail;
7 import java.io.BufferedReader;
9 import java.io.FileReader;
10 import java.io.IOException;
11 import java.util.ArrayList;
12 import java.util.EnumMap;
13 import java.util.List;
14 import java.util.Scanner;
16 import org.apache.log4j.Logger;
18 import com.google.common.base.Joiner;
19 import com.google.common.collect.Lists;
20 import com.google.common.collect.Maps;
52 private static final Logger
LOG = Logger.getLogger(TestCase.class);
73 return "---- " + this.toString();
84 Maps.newEnumMap(Section.class);
98 expectedResultSections.put(section, contents);
127 ArrayList<String> ret = expectedResultSections.get(section);
129 return Lists.newArrayList();
130 }
else if (withComments && dbSuffix == null) {
134 ArrayList<String> retList = Lists.newArrayList();
135 for (String s : ret) {
136 if (!(s.startsWith(
"#") || s.startsWith(
"//"))) {
137 if (dbSuffix != null) {
138 retList.add(s.replaceAll(
"\\$DATABASE", dbSuffix));
142 }
else if (withComments) {
160 String delimiter, String dbSuffix) {
162 if (sectionList == null) {
166 return Joiner.on(delimiter).join(sectionList);
186 private final List<TestCase>
testCases = Lists.newArrayList();
220 private void open(String table) {
222 String testFileBaseDir =
223 new File(System.getenv(
"IMPALA_HOME"),
"testdata/workloads").getPath();
224 String fullPath =
new File(testFileBaseDir,
fileName).getPath();
225 reader =
new BufferedReader(
new FileReader(fullPath));
227 }
catch (Exception e) {
228 fail(e.getMessage());
237 ArrayList<String> sectionContents = Lists.newArrayList();
239 int sectionCount = 0;
241 while (
scanner.hasNextLine()) {
242 String line = scanner.nextLine();
244 if (line.startsWith(
"====") && sectionCount > 0) {
245 currentTestCase.addSection(currentSection, sectionContents);
246 if(!currentTestCase.isValid()) {
247 throw new IllegalStateException(
"Invalid test case" +
248 " at line " + currentTestCase.startLineNum +
" detected.");
250 return currentTestCase;
252 if (line.startsWith(
"----")) {
255 currentTestCase.addSection(currentSection, sectionContents);
256 boolean found =
false;
257 line = line.trim().toLowerCase();
260 if (!line.endsWith(
"----")) {
262 if (line.matches(
"----\\s+" + s.toString().toLowerCase() +
"\\b.*")) {
272 throw new IllegalStateException(
"Unknown section name: " + line);
278 throw new IllegalStateException(
"Unexpected number of untitled sections: "
281 currentSection = defaultSectionOrder.get(sectionCount);
282 LOG.warn(
"No section header found. Guessing: " + currentSection);
285 sectionContents = Lists.newArrayList();
287 sectionContents.add(line);
291 if(!currentTestCase.isValid()) {
292 throw new IllegalStateException(
"Invalid test case" +
293 " at line " + currentTestCase.startLineNum +
" detected.");
296 return currentTestCase;
310 while (
scanner.hasNextLine()) {
322 }
catch (IOException e) {
323 fail(e.getMessage());
boolean hasSetupSection()
ArrayList< String > getSectionContents(Section section)
String getSectionAsString(Section section, boolean withComments, String delimiter, String dbSuffix)
ArrayList< String > getSectionContents(Section section, boolean withComments)
static final ArrayList< Section > defaultSectionOrder
String getSectionAsString(Section section, boolean withComments, String delimiter)
final List< TestCase > testCases
void parseFile(String table)
final EnumMap< Section, ArrayList< String > > expectedResultSections
TestFileParser(String fileName)
void addSection(Section section, ArrayList< String > contents)
TestCase parseOneTestCase()
List< TestCase > getTestCases()
ArrayList< String > getSectionContents(Section section, boolean withComments, String dbSuffix)