Example #1
0
 /**
  * Test query for stripping existing namespaces.
  *
  * @throws Exception exception
  */
 @Test
 public void stripNS() throws Exception {
   final IO io = IO.get("<a xmlns:a='a'><b><c/><c/><c/></b></a>");
   try (final QueryProcessor qp =
       new QueryProcessor("/*:a/*:b", context).context(new DBNode(io))) {
     final ANode sub = (ANode) qp.iter().next();
     DataBuilder.stripNS(sub, token("a"), context);
   }
 }
Example #2
0
  /** Tests the specified instance. */
  @Test
  public void test() {
    final StringBuilder sb = new StringBuilder();
    int fail = 0;

    for (final Object[] qu : queries) {
      final boolean correct = qu.length == 3;
      final String query = qu[correct ? 2 : 1].toString();
      final Value cmp = correct ? (Value) qu[1] : null;

      final QueryProcessor qp = new QueryProcessor(query, context);
      try {
        final Value val = qp.value();
        if (!correct || !new DeepCompare().equal(val, cmp)) {
          sb.append("[" + qu[0] + "] " + query);
          String s = correct && cmp.size() != 1 ? "#" + cmp.size() : "";
          sb.append("\n[E" + s + "] ");
          if (correct) {
            final String cp = cmp.toString();
            sb.append('\'');
            sb.append(cp.length() > 1000 ? cp.substring(0, 1000) + "..." : cp);
            sb.append('\'');
          } else {
            sb.append("error");
          }
          final TokenBuilder types = new TokenBuilder();
          for (final Item it : val) types.add(it.type.toString()).add(" ");
          s = val.size() == 1 ? "" : "#" + val.size();
          sb.append("\n[F" + s + "] '" + val + "', " + types + details() + '\n');
          ++fail;
        }
      } catch (final Exception ex) {
        final String msg = ex.getMessage();
        if (correct || msg == null || msg.contains("mailman")) {
          final String cp = correct && cmp.data() != null ? cmp.toString() : "()";
          sb.append(
              "["
                  + qu[0]
                  + "] "
                  + query
                  + "\n[E] "
                  + cp
                  + "\n[F] "
                  + (msg == null ? Util.className(ex) : msg.replaceAll("\r\n?|\n", " "))
                  + ' '
                  + details()
                  + '\n');
          ex.printStackTrace();
          ++fail;
        }
      } finally {
        qp.close();
      }
    }
    if (fail != 0) fail(fail + " Errors. [E] = expected, [F] = found:\n" + sb.toString().trim());
  }
Example #3
0
 /** Test query. Detects malformed namespace hierarchy. */
 @Test
 @Ignore
 public void xuty0004() {
   final String query =
       "declare variable $input-context external;"
           + "let $source as node()* := ("
           + "    <status>on leave</status>,"
           + "    <!-- for 6 months -->"
           + "  ),"
           + "  $target := $input-context/works[1]/employee[1]"
           + "return insert nodes $source into $target";
   try (final QueryProcessor qp = new QueryProcessor(query, context)) {
     qp.value();
   } catch (final QueryException ex) {
     assertEquals("XUTY0004", ex.error().code);
   }
   fail("should throw XUTY0004");
 }
Example #4
0
 /**
  * Parses a module.
  *
  * @param io input reference
  * @return query parser
  * @throws QueryException query exception
  */
 final QueryParser parseQuery(final IO io) throws QueryException {
   try (final QueryContext qctx = new QueryContext(qc)) {
     final String input = string(io.read());
     // parse query
     final QueryParser qp = new QueryParser(input, io.path(), qctx, null);
     module = QueryProcessor.isLibrary(input) ? qp.parseLibrary(true) : qp.parseMain();
     return qp;
   } catch (final IOException | QueryException ex) {
     throw IOERR_X.get(info, ex);
   }
 }