Author: jens
Date: Thu Dec 23 14:58:19 2010
New Revision: 1052295
URL: http://svn.apache.org/viewvc?rev=1052295&view=rev
Log:
add a little bit of unit test code
Added:
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/opencmis/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/opencmis/orderly/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/opencmis/orderly/validate/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/opencmis/orderly/validate/Orderly.testsuite
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/opencmis/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/opencmis/orderly/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/opencmis/orderly/validate/
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/opencmis/orderly/validate/ParserTest.java
Modified:
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/pom.xml
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g
Modified:
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/pom.xml?rev=1052295&r1=1052294&r2=1052295&view=diff
==============================================================================
--- incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/pom.xml
(original)
+++ incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/pom.xml
Thu Dec 23 14:58:19 2010
@@ -110,6 +110,20 @@
</execution>
</executions>
</plugin>
+<!-- Enable this again if you want to run gunit from maven(seems not to work
well with this grammar)
+ <plugin>
+ <groupId>org.antlr</groupId>
+ <artifactId>maven-gunit-plugin</artifactId>
+ <executions>
+ <execution>
+ <!- - default phase: test - - >
+ <goals>
+ <goal>gunit</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+-->
</plugins>
</build>
Modified:
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g
URL:
http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g?rev=1052295&r1=1052294&r2=1052295&view=diff
==============================================================================
---
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g
(original)
+++
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/main/antlr3/org/apache/chemistry/opencmis/orderly/Orderly.g
Thu Dec 23 14:58:19 2010
@@ -15,7 +15,7 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
- *
+ *
*/
grammar Orderly;
@@ -85,6 +85,7 @@ package org.apache.chemistry.opencmis.or
@main {
}
+orderly_schema2 : orderly_schema EOF ;
orderly_schema returns [Object result]
: e=unnamed_entries? ';'? {$result = $e.result; }
Added:
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/opencmis/orderly/validate/Orderly.testsuite
URL:
http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/opencmis/orderly/validate/Orderly.testsuite?rev=1052295&view=auto
==============================================================================
---
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/opencmis/orderly/validate/Orderly.testsuite
(added)
+++
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/gunit/org/apache/chemistry/opencmis/orderly/validate/Orderly.testsuite
Thu Dec 23 14:58:19 2010
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ * Authors:
+ * Jens H�bel, Apache Software Foundation
+ */
+gunit Orderly;
+
+...@header{
+package org.apache.chemistry.opencmis.orderly;
+}
+
+STRING:
+<<"abc"
+>> OK
+"123" FAIL
+
Added:
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/opencmis/orderly/validate/ParserTest.java
URL:
http://svn.apache.org/viewvc/incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/opencmis/orderly/validate/ParserTest.java?rev=1052295&view=auto
==============================================================================
---
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/opencmis/orderly/validate/ParserTest.java
(added)
+++
incubator/chemistry/playground/chemistry-opencmis-orderlyvalidator/src/test/java/org/apache/chemistry/opencmis/orderly/validate/ParserTest.java
Thu Dec 23 14:58:19 2010
@@ -0,0 +1,257 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.chemistry.opencmis.orderly.validate;
+
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.List;
+
+import org.antlr.runtime.ANTLRInputStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.TokenSource;
+import org.antlr.runtime.TokenStream;
+import org.apache.chemistry.opencmis.orderly.OrderlyLexer;
+import org.apache.chemistry.opencmis.orderly.OrderlyParser;
+import
org.apache.chemistry.opencmis.orderly.OrderlyParser.orderly_schema_return;
+import org.codehaus.jackson.JsonFactory;
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.JsonParser;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.junit.Test;
+
+import eu.vahlas.json.schema.impl.JacksonSchema;
+
+public class ParserTest {
+
+ private static Object parseOrderly(String orderlyDocument) throws
Exception {
+ CharStream input = new ANTLRInputStream(new
ByteArrayInputStream(orderlyDocument.getBytes("UTF-8")) );
+ TokenSource lexer = new OrderlyLexer(input);
+ TokenStream tokens = new CommonTokenStream(lexer);
+ OrderlyParser parser = new OrderlyParser(tokens);
+
+ orderly_schema_return result = parser.orderly_schema();
+ if (parser.hasErrors())
+ throw new RuntimeException(parser.getErrorMessages());
+
+// CommonTree parserTree = (CommonTree) result.getTree();
+
+ // Extract hashmap from parsed tree
+ Object parsedMap = result.result;
+ return parsedMap;
+ }
+
+ private static void validate(String jsonDocument, String orderlyDocument)
throws Exception {
+
+ Object parsedMap = parseOrderly(orderlyDocument);
+ ObjectMapper mapper = new ObjectMapper();
+
+ // Allows to retrieve a JSONSchema object on various sources
+ // supported by the ObjectMapper provided
+ JsonFactory factory = new JsonFactory();
+
+ byte[] jsonSchemaAsBytes = mapper.writeValueAsBytes(parsedMap);
+
+ JsonParser jsonParser = factory.createJsonParser(jsonSchemaAsBytes);
+ JsonNode schemaNode = mapper.readTree(jsonParser);
+
+ JacksonSchema schema = new JacksonSchema(mapper, schemaNode);
+
+ // Validates a JSON Instance object stored in a file
+ InputStream instanceIS = new
ByteArrayInputStream(jsonDocument.getBytes("UTF-8"));
+ List<String> errors = schema.validate(instanceIS);
+
+ // Display the eventual errors
+ if (errors.size() > 0) {
+ for (String s : errors) {
+ System.err.println(s);
+ }
+ System.exit(1);
+ } else
+ System.out.println("Validation passed.");
+ }
+
+
+ @Test
+ public void testParseSimple() {
+ String orderlyDoc =
+ "object { " +
+ "string name; " +
+ "string description?; " +
+ "string homepage /^http:/; " +
+ "integer {1500,3000} invented; " +
+ "}*;";
+
+ try {
+ parseOrderly(orderlyDoc);
+ } catch (Exception e) {
+ fail("Error parsing orderly doc " + orderlyDoc + ": " + e);
+ }
+ }
+
+ @Test
+ public void testParseSimple2() {
+ String orderlyDoc = "any";
+
+ try {
+ parseOrderly(orderlyDoc);
+ } catch (Exception e) {
+ fail("Error parsing orderly doc " + orderlyDoc + ": " + e);
+ }
+ }
+
+ @Test
+ public void testParseFail() {
+ String orderlyDoc =
+ "object { " +
+ "djklh name; " +
+ "string description?; " +
+ "string homepage /^http:/; " +
+ "integer {1500,3000} invented; " +
+ "}*;";
+
+ try {
+ parseOrderly(orderlyDoc);
+ fail("Erroneous orderly file should fail while parsing.");
+ } catch (Exception e) {
+ System.out.println("Exception while parsing" + e);
+ }
+ }
+
+ @Test
+ public void testValidate() {
+ String orderlyDoc =
+ "object { " +
+ "string name; " +
+ "string description?; " +
+ "string homepage; " +
+ "integer {1500,3000} invented; " +
+ "}*;";
+
+ String jsonDoc =
+ "{" +
+ "\"name\": \"orderly\"," +
+ "\"description\": \"A schema language for JSON\"," +
+ "\"homepage\": \"http://orderly-json.org\"," +
+ "\"invented\": 2009" +
+ "}";
+
+ try {
+ validate(jsonDoc, orderlyDoc);
+ } catch (Exception e) {
+ fail("Exception while parsing" + e);
+ }
+ }
+
+ @Test
+ public void testValidateArray() {
+ String orderlyDoc =
+ "object {" +
+ "integer num;" +
+ "array {" +
+ "string;" +
+ "} name; " +
+ "}*;";
+
+ String jsonDoc =
+ "{" +
+ "\"num\": -1234," +
+ "\"name\": [" +
+ "\"aaa\"," +
+ "\"bbb\"," +
+ "\"ccc\"" +
+ "]}";
+
+ try {
+ validate(jsonDoc, orderlyDoc);
+ } catch (Exception e) {
+ fail("Exception while parsing" + e);
+ }
+ }
+
+ @Test
+ public void testValidateDirectArray() {
+ String orderlyDoc =
+ "array {" +
+ "string; " +
+ "}";
+
+ String jsonDoc =
+ "[" +
+ "\"aaa\"," +
+ "\"bbb\"," +
+ "\"ccc\"" +
+ "]";
+
+ try {
+ validate(jsonDoc, orderlyDoc);
+ } catch (Exception e) {
+ fail("Exception while parsing" + e);
+ }
+ }
+
+ @Test
+ public void testValidateUnion() {
+ String orderlyDoc =
+ "object {" +
+ "integer num;" +
+ "union {" +
+ "string;" +
+ "integer;" +
+ "} val; " +
+ "}*;";
+
+ String jsonDoc1 =
+ "{" +
+ "\"num\": -1234," +
+ "\"val\": \"aaa\"" +
+ "}";
+ String jsonDoc2 =
+ "{" +
+ "\"num\": -1234," +
+ "\"val\": 4321" +
+ "}";
+
+ try {
+ validate(jsonDoc1, orderlyDoc);
+ validate(jsonDoc2, orderlyDoc);
+ } catch (Exception e) {
+ fail("Exception while parsing" + e);
+ }
+ }
+
+ @Test
+ public void testValidateExtra() {
+ String orderlyDoc =
+ "string `{\"description\": \"The name of the service\"}`";
+ String jsonDoc =
+ "{" +
+ "\"description\" : \"The name of the service\"" +
+ "}";
+
+ try {
+ parseOrderly(orderlyDoc);
+// validate(jsonDoc, orderlyDoc);
+ } catch (Exception e) {
+ fail("Exception while parsing/validating " + e);
+ }
+ }
+}