cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tylerho...@apache.org
Subject git commit: Follow up to 7111 to fix failing unit tests on trunk
Date Wed, 16 Jul 2014 16:27:35 GMT
Repository: cassandra
Updated Branches:
  refs/heads/trunk 82cfcab4c -> c7f3a238e


Follow up to 7111 to fix failing unit tests on trunk

Patch by Benjamin Lerer; reviewed by Tyler Hobbs for CASSANDRA-7111


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/c7f3a238
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/c7f3a238
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/c7f3a238

Branch: refs/heads/trunk
Commit: c7f3a238e7fcc09292c0316b4df3279a4d241b2d
Parents: 82cfcab
Author: Tyler Hobbs <tyler@datastax.com>
Authored: Wed Jul 16 11:26:35 2014 -0500
Committer: Tyler Hobbs <tyler@datastax.com>
Committed: Wed Jul 16 11:26:35 2014 -0500

----------------------------------------------------------------------
 .../apache/cassandra/cql3/ErrorCollector.java   | 37 ++++++++++++++++++--
 .../apache/cassandra/cql3/QueryProcessor.java   |  2 +-
 .../apache/cassandra/cql3/CqlParserTest.java    |  8 ++---
 .../cassandra/cql3/ErrorCollectorTest.java      | 24 +++++++++++++
 4 files changed, 63 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/c7f3a238/src/java/org/apache/cassandra/cql3/ErrorCollector.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/ErrorCollector.java b/src/java/org/apache/cassandra/cql3/ErrorCollector.java
index 41536f5..40949de 100644
--- a/src/java/org/apache/cassandra/cql3/ErrorCollector.java
+++ b/src/java/org/apache/cassandra/cql3/ErrorCollector.java
@@ -91,14 +91,14 @@ public final class ErrorCollector implements ErrorListener
     }
 
     /**
-     * Throws the last syntax error found by the lexer or the parser if it exists.
+     * Throws the first syntax error found by the lexer or the parser if it exists.
      *
      * @throws SyntaxException the syntax error.
      */
-    public void throwLastSyntaxError() throws SyntaxException
+    public void throwFirstSyntaxError() throws SyntaxException
     {
         if (!errorMsgs.isEmpty())
-            throw new SyntaxException(errorMsgs.getLast());
+            throw new SyntaxException(errorMsgs.getFirst());
     }
 
     /**
@@ -132,6 +132,9 @@ public final class ErrorCollector implements ErrorListener
                              Token to,
                              Token offending)
     {
+        if (!areTokensValid(from, to, offending))
+            return;
+
         String[] lines = query.split("\n");
 
         boolean includeQueryStart = (from.getLine() == 1) && (from.getCharPositionInLine()
== 0);
@@ -157,6 +160,34 @@ public final class ErrorCollector implements ErrorListener
     }
 
     /**
+     * Checks if the specified tokens are valid.
+     *
+     * @param token the tokens to check
+     * @return <code>true</code> if all the specified tokens are valid ones,
+     * <code>false</code> otherwise.
+     */
+    private static boolean areTokensValid(Token... tokens)
+    {
+        for (Token token : tokens)
+        {
+            if (!isTokenValid(token))
+                return false;
+        }
+        return true;
+    }
+
+    /**
+     * Checks that the specified token is valid.
+     *
+     * @param token the token to check
+     * @return <code>true</code> if it is considered as valid, <code>false</code>
otherwise.
+     */
+    private static boolean isTokenValid(Token token)
+    {
+        return token.getLine() > 0 && token.getCharPositionInLine() >= 0;
+    }
+
+    /**
      * Puts the specified token within square brackets.
      *
      * @param line the line containing the token

http://git-wip-us.apache.org/repos/asf/cassandra/blob/c7f3a238/src/java/org/apache/cassandra/cql3/QueryProcessor.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/QueryProcessor.java b/src/java/org/apache/cassandra/cql3/QueryProcessor.java
index bc64f80..999c849 100644
--- a/src/java/org/apache/cassandra/cql3/QueryProcessor.java
+++ b/src/java/org/apache/cassandra/cql3/QueryProcessor.java
@@ -455,7 +455,7 @@ public class QueryProcessor implements QueryHandler
 
             // The errorCollector has queue up any errors that the lexer and parser may have
encountered
             // along the way, if necessary, we turn the last error into exceptions here.
-            errorCollector.throwLastSyntaxError();
+            errorCollector.throwFirstSyntaxError();
 
             return statement;
         }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/c7f3a238/test/unit/org/apache/cassandra/cql3/CqlParserTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/CqlParserTest.java b/test/unit/org/apache/cassandra/cql3/CqlParserTest.java
index d122eb5..eaba9c7 100644
--- a/test/unit/org/apache/cassandra/cql3/CqlParserTest.java
+++ b/test/unit/org/apache/cassandra/cql3/CqlParserTest.java
@@ -36,7 +36,7 @@ public class CqlParserTest
         SyntaxErrorCounter firstCounter = new SyntaxErrorCounter();
         SyntaxErrorCounter secondCounter = new SyntaxErrorCounter();
 
-        CharStream stream = new ANTLRStringStream("SELECT * FORM test;");
+        CharStream stream = new ANTLRStringStream("SELECT * FORM users");
         CqlLexer lexer = new CqlLexer(stream);
 
         TokenStream tokenStream = new CommonTokenStream(lexer);
@@ -46,8 +46,8 @@ public class CqlParserTest
 
         parser.query();
 
-        assertEquals(1, firstCounter.count);
-        assertEquals(1, secondCounter.count);
+        assertTrue(firstCounter.count > 0);
+        assertTrue(secondCounter.count > 0);
     }
 
     @Test
@@ -67,7 +67,7 @@ public class CqlParserTest
 
         parser.query();
 
-        assertEquals(1, firstCounter.count);
+        assertTrue(firstCounter.count > 0);
         assertEquals(0, secondCounter.count);
     }
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/c7f3a238/test/unit/org/apache/cassandra/cql3/ErrorCollectorTest.java
----------------------------------------------------------------------
diff --git a/test/unit/org/apache/cassandra/cql3/ErrorCollectorTest.java b/test/unit/org/apache/cassandra/cql3/ErrorCollectorTest.java
index 4f5db34..4ecf460 100644
--- a/test/unit/org/apache/cassandra/cql3/ErrorCollectorTest.java
+++ b/test/unit/org/apache/cassandra/cql3/ErrorCollectorTest.java
@@ -107,6 +107,30 @@ public class ErrorCollectorTest
         assertEquals(expected, builder.toString());
     }
 
+    /**
+     * With ANTLR 3.5.2 it appears that some tokens can contains unexpected values: a line
= 0 
+     * and a charPositionInLine = -1.
+     */
+    @Test
+    public void testAppendSnippetWithInvalidToken()
+    {
+        String query = "select * fom users";
+
+        ErrorCollector collector = new ErrorCollector(query);
+
+        StringBuilder builder = new StringBuilder();
+
+        Token from = new MockToken(1, 5, "select");
+        Token to = new MockToken(0, -1, "");
+        Token offending = new MockToken(0, -1, "");
+
+        collector.appendSnippet(builder, from, to, offending);
+
+        String expected = "";
+
+        assertEquals(expected, builder.toString());
+    }
+
     private final static class MockToken implements Token
     {
         /**


Mime
View raw message