Java Code Examples for org.apache.calcite.util.Util#reader()

The following examples show how to use org.apache.calcite.util.Util#reader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SplunkConnectionImpl.java    From calcite with Apache License 2.0 5 votes vote down vote up
private void connect() {
  BufferedReader rd = null;

  try {
    String loginUrl =
        String.format(Locale.ROOT,
            "%s://%s:%d/services/auth/login",
            url.getProtocol(),
            url.getHost(),
            url.getPort());

    StringBuilder data = new StringBuilder();
    appendURLEncodedArgs(
        data, "username", username, "password", password);

    rd = Util.reader(post(loginUrl, data, requestHeaders));

    String line;
    StringBuilder reply = new StringBuilder();
    while ((line = rd.readLine()) != null) {
      reply.append(line);
      reply.append("\n");
    }

    Matcher m = SESSION_KEY.matcher(reply);
    if (m.find()) {
      sessionKey = m.group(1);
      requestHeaders.put("Authorization", "Splunk " + sessionKey);
    }
  } catch (Exception e) {
    e.printStackTrace();
  } finally {
    close(rd);
  }
}
 
Example 2
Source File: DocumentationTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Tests that every function in {@link SqlStdOperatorTable} is documented in
 * reference.md. */
@Test void testAllFunctionsAreDocumented() throws IOException {
  final FileFixture f = new FileFixture();
  final Map<String, PatternOp> map = new TreeMap<>();
  addOperators(map, "", SqlStdOperatorTable.instance().getOperatorList());
  for (SqlLibrary library : SqlLibrary.values()) {
    switch (library) {
    case STANDARD:
    case SPATIAL:
      continue;
    }
    addOperators(map, "\\| [^|]*" + library.abbrev + "[^|]* ",
        SqlLibraryOperatorTableFactory.INSTANCE
            .getOperatorTable(EnumSet.of(library)).getOperatorList());
  }
  final Set<String> regexSeen = new HashSet<>();
  try (LineNumberReader r = new LineNumberReader(Util.reader(f.inFile))) {
    for (;;) {
      final String line = r.readLine();
      if (line == null) {
        break;
      }
      for (Map.Entry<String, PatternOp> entry : map.entrySet()) {
        if (entry.getValue().pattern.matcher(line).matches()) {
          regexSeen.add(entry.getKey()); // function is documented
        }
      }
    }
  }
  final Set<String> regexNotSeen = new TreeSet<>(map.keySet());
  regexNotSeen.removeAll(regexSeen);
  assertThat("some functions are not documented: " + map.entrySet().stream()
          .filter(e -> regexNotSeen.contains(e.getKey()))
          .map(e -> e.getValue().opName + "(" + e.getKey() + ")")
          .collect(Collectors.joining(", ")),
      regexNotSeen.isEmpty(), is(true));
}
 
Example 3
Source File: DiffTestCase.java    From calcite with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of the lines in a given file.
 *
 * @param file File
 * @return List of lines
 */
private static List<String> fileLines(File file) {
  List<String> lines = new ArrayList<>();
  try (LineNumberReader r = new LineNumberReader(Util.reader(file))) {
    String line;
    while ((line = r.readLine()) != null) {
      lines.add(line);
    }
    return lines;
  } catch (IOException e) {
    e.printStackTrace();
    throw TestUtil.rethrow(e);
  }
}
 
Example 4
Source File: DocumentationTest.java    From calcite with Apache License 2.0 4 votes vote down vote up
/** Generates a copy of {@code reference.md} with the current set of key
 * words. Fails if the copy is different from the original. */
@Test void testGenerateKeyWords() throws IOException {
  final FileFixture f = new FileFixture();
  f.outFile.getParentFile().mkdirs();
  try (BufferedReader r = Util.reader(f.inFile);
       FileOutputStream fos = new FileOutputStream(f.outFile);
       PrintWriter w = Util.printWriter(f.outFile)) {
    String line;
    int stage = 0;
    while ((line = r.readLine()) != null) {
      if (line.equals("{% comment %} end {% endcomment %}")) {
        ++stage;
      }
      if (stage != 1) {
        w.println(line);
      }
      if (line.equals("{% comment %} start {% endcomment %}")) {
        ++stage;
        SqlAbstractParserImpl.Metadata metadata =
            new SqlParserTest().getSqlParser("").getMetadata();
        int z = 0;
        for (String s : metadata.getTokens()) {
          if (z++ > 0) {
            w.println(",");
          }
          if (metadata.isKeyword(s)) {
            w.print(metadata.isReservedWord(s) ? ("**" + s + "**") : s);
          }
        }
        w.println(".");
      }
    }
    w.flush();
    fos.flush();
    fos.getFD().sync();
  }
  String diff = DiffTestCase.diff(f.outFile, f.inFile);
  if (!diff.isEmpty()) {
    throw new AssertionError("Mismatch between " + f.outFile
        + " and " + f.inFile + ":\n"
        + diff);
  }
}
 
Example 5
Source File: DiffTestCase.java    From calcite with Apache License 2.0 4 votes vote down vote up
/**
 * Compares a log file with its reference log.
 *
 * <p>Usually, the log file and the reference log are in the same directory,
 * one ending with '.log' and the other with '.ref'.
 *
 * <p>If the files are identical, removes logFile.
 *
 * @param logFile Log file
 * @param refFile Reference log
 */
protected void diffFile(File logFile, File refFile) throws IOException {
  int n = 0;
  BufferedReader logReader = null;
  BufferedReader refReader = null;
  try {
    // NOTE: Use of diff.mask is deprecated, use diff_mask.
    String diffMask = System.getProperty("diff.mask", null);
    if (diffMask != null) {
      addDiffMask(diffMask);
    }

    diffMask = System.getProperty("diff_mask", null);
    if (diffMask != null) {
      addDiffMask(diffMask);
    }

    logReader = Util.reader(logFile);
    refReader = Util.reader(refFile);
    LineNumberReader logLineReader = new LineNumberReader(logReader);
    LineNumberReader refLineReader = new LineNumberReader(refReader);
    for (;;) {
      String logLine = logLineReader.readLine();
      String refLine = refLineReader.readLine();
      while ((logLine != null) && matchIgnorePatterns(logLine)) {
        // System.out.println("logMatch Line:" + logLine);
        logLine = logLineReader.readLine();
      }
      while ((refLine != null) && matchIgnorePatterns(refLine)) {
        // System.out.println("refMatch Line:" + logLine);
        refLine = refLineReader.readLine();
      }
      if ((logLine == null) || (refLine == null)) {
        if (logLine != null) {
          diffFail(
              logFile,
              logLineReader.getLineNumber());
        }
        if (refLine != null) {
          diffFail(
              logFile,
              refLineReader.getLineNumber());
        }
        break;
      }
      logLine = applyDiffMask(logLine);
      refLine = applyDiffMask(refLine);
      if (!logLine.equals(refLine)) {
        diffFail(
            logFile,
            logLineReader.getLineNumber());
      }
    }
  } finally {
    if (logReader != null) {
      logReader.close();
    }
    if (refReader != null) {
      refReader.close();
    }
  }

  // no diffs detected, so delete redundant .log file
  logFile.delete();
}