Skip to main content

[jsonp~git:06ae40ea] Adding couple of tests that checks JsonGenerationException when multiple

  • From:
  • To:
  • Subject: [jsonp~git:06ae40ea] Adding couple of tests that checks JsonGenerationException when multiple
  • Date: Tue, 15 Oct 2013 00:01:56 +0000

Project:    jsonp
Repository: git
Revision:   06ae40eafa29bb5cb1594cfc7580d99e129dc2e3
Author:     jitu
Date:       2013-10-15 00:00:57 UTC
Link:       

Log Message:
------------
[JSONP-20] localizing some more messages

Adding couple of tests that checks JsonGenerationException when multiple
JSON texts are created from a single gnerator instance.



Revisions:
----------
a34985a7dce11e49bc846ff44c0ee02c26b8796e
06ae40eafa29bb5cb1594cfc7580d99e129dc2e3


Modified Paths:
---------------
impl/src/main/java/org/glassfish/json/JsonGeneratorImpl.java
impl/src/main/java/org/glassfish/json/JsonMessages.java
impl/src/main/java/org/glassfish/json/JsonStructureParser.java
impl/src/main/resources/org/glassfish/json/messages.properties
tests/src/test/java/org/glassfish/json/tests/JsonGeneratorTest.java


Diffs:
------
--- a/impl/src/main/java/org/glassfish/json/JsonGeneratorImpl.java
+++ b/impl/src/main/java/org/glassfish/json/JsonGeneratorImpl.java
@@ -141,10 +141,10 @@ class JsonGeneratorImpl implements JsonGenerator {
     @Override
     public JsonGenerator writeStartObject() {
         if (currentContext.scope == Scope.IN_OBJECT) {
-            throw new JsonGenerationException("writeStartObject() cannot be 
called in object context");
+            throw new 
JsonGenerationException(JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         if (currentContext.scope == Scope.IN_NONE && !currentContext.first) {
-            throw new JsonGenerationException("writeStartObject() cannot be 
called in no context more than once");
+            throw new 
JsonGenerationException(JsonMessages.GENERATOR_ILLEGAL_MULTIPLE_TEXT());
         }
         writeComma();
         writeChar('{');
@@ -157,7 +157,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator writeStartObject(String name) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeChar('{');
@@ -177,7 +177,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String name, String fieldValue) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeEscapedString(fieldValue);
@@ -188,7 +188,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String name, int value) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeInt(value);
@@ -199,7 +199,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String name, long value) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeString(String.valueOf(value));
@@ -210,7 +210,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String name, double value) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         if (Double.isInfinite(value) || Double.isNaN(value)) {
             throw new 
NumberFormatException(JsonMessages.GENERATOR_DOUBLE_INFINITE_NAN());
@@ -224,7 +224,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String name, BigInteger value) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeString(String.valueOf(value));
@@ -235,7 +235,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String name, BigDecimal value) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeString(String.valueOf(value));
@@ -246,7 +246,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String name, boolean value) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeString(value? "true" : "false");
@@ -257,7 +257,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator writeNull(String name) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeString("null");
@@ -268,7 +268,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(JsonValue value) {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         switch (value.getValueType()) {
             case ARRAY:
@@ -312,10 +312,10 @@ class JsonGeneratorImpl implements JsonGenerator {
     @Override
     public JsonGenerator writeStartArray() {
         if (currentContext.scope == Scope.IN_OBJECT) {
-            throw new JsonGenerationException("writeStartArray() cannot be 
called in object context");
+            throw new 
JsonGenerationException(JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         if (currentContext.scope == Scope.IN_NONE && !currentContext.first) {
-            throw new JsonGenerationException("writeStartArray() cannot be 
called in no context more than once");
+            throw new 
JsonGenerationException(JsonMessages.GENERATOR_ILLEGAL_MULTIPLE_TEXT());
         }
         writeComma();
         writeChar('[');
@@ -328,7 +328,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator writeStartArray(String name) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeName(name);
         writeChar('[');
@@ -341,7 +341,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String name, JsonValue value) {
         if (currentContext.scope != Scope.IN_OBJECT) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         switch (value.getValueType()) {
             case ARRAY:
@@ -384,7 +384,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(String value) {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeComma();
         writeEscapedString(value);
@@ -395,7 +395,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(int value) {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeComma();
         writeInt(value);
@@ -406,7 +406,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(long value) {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeValue(String.valueOf(value));
         return this;
@@ -416,7 +416,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(double value) {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         if (Double.isInfinite(value) || Double.isNaN(value)) {
             throw new 
NumberFormatException(JsonMessages.GENERATOR_DOUBLE_INFINITE_NAN());
@@ -429,7 +429,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(BigInteger value) {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeValue(value.toString());
         return this;
@@ -439,7 +439,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(BigDecimal value) {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeValue(value.toString());
         return this;
@@ -448,7 +448,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator write(boolean value) {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeComma();
         writeString(value ? "true" : "false");
@@ -458,7 +458,7 @@ class JsonGeneratorImpl implements JsonGenerator {
     public JsonGenerator writeNull() {
         if (currentContext.scope != Scope.IN_ARRAY) {
             throw new JsonGenerationException(
-                    JsonMessages.GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT());
+                    
JsonMessages.GENERATOR_ILLEGAL_METHOD(currentContext.scope));
         }
         writeComma();
         writeString("null");--- 
a/impl/src/main/java/org/glassfish/json/JsonMessages.java
+++ b/impl/src/main/java/org/glassfish/json/JsonMessages.java
@@ -41,6 +41,8 @@
 package org.glassfish.json;
 
 
+import javax.json.stream.JsonLocation;
+import javax.json.stream.JsonParser;
 import java.text.MessageFormat;
 import java.util.ResourceBundle;
 
@@ -54,7 +56,7 @@ final class JsonMessages {
             ResourceBundle.getBundle("org.glassfish.json.messages");
 
     // tokenizer messages
-    static String TOKENIZER_UNEXPECTED_CHAR(Object ch) {
+    static String TOKENIZER_UNEXPECTED_CHAR(int ch) {
         return localize("tokenizer.unexpected.char", ch);
     }
 
@@ -68,35 +70,35 @@ final class JsonMessages {
 
 
     // parser messages
-    static String PARSER_GETSTRING_ERR(Object arg0) {
-        return localize("parser.getString.err", arg0);
+    static String PARSER_GETSTRING_ERR(JsonParser.Event event) {
+        return localize("parser.getString.err", event);
     }
 
-    static String PARSER_ISINTEGRALNUMBER_ERR(Object arg0) {
-        return localize("parser.isIntegralNumber.err", arg0);
+    static String PARSER_ISINTEGRALNUMBER_ERR(JsonParser.Event event) {
+        return localize("parser.isIntegralNumber.err", event);
     }
 
-    static String PARSER_GETINT_ERR(Object arg0) {
-        return localize("parser.getInt.err", arg0);
+    static String PARSER_GETINT_ERR(JsonParser.Event event) {
+        return localize("parser.getInt.err", event);
     }
 
-    static String PARSER_GETLONG_ERR(Object arg0) {
-        return localize("parser.getLong.err", arg0);
+    static String PARSER_GETLONG_ERR(JsonParser.Event event) {
+        return localize("parser.getLong.err", event);
     }
 
-    static String PARSER_GETBIGDECIMAL_ERR(Object arg0) {
-        return localize("parser.getBigDecimal.err", arg0);
+    static String PARSER_GETBIGDECIMAL_ERR(JsonParser.Event event) {
+        return localize("parser.getBigDecimal.err", event);
     }
 
-    static String PARSER_EXPECTED_EOF(Object arg0) {
-        return localize("parser.expected.eof", arg0);
+    static String PARSER_EXPECTED_EOF(JsonTokenizer.JsonToken token) {
+        return localize("parser.expected.eof", token);
     }
 
     static String PARSER_TOKENIZER_CLOSE_IO() {
         return localize("parser.tokenizer.close.io");
     }
 
-    static String PARSER_INVALID_TOKEN(Object token, Object location, Object 
expectedTokens) {
+    static String PARSER_INVALID_TOKEN(JsonTokenizer.JsonToken token, 
JsonLocation location, String expectedTokens) {
         return localize("parser.invalid.token", token, location, 
expectedTokens);
     }
 
@@ -114,12 +116,8 @@ final class JsonMessages {
         return localize("generator.write.io.err");
     }
 
-    static String GENERATOR_METHOD_LEGAL_IN_OBJECT_CONTEXT() {
-        return localize("generator.method.legal.in.object.context");
-    }
-
-    static String GENERATOR_METHOD_LEGAL_IN_ARRAY_CONTEXT() {
-        return localize("generator.method.legal.in.array.context");
+    static String GENERATOR_ILLEGAL_METHOD(Object scope) {
+        return localize("generator.illegal.method", scope);
     }
 
     static String GENERATOR_DOUBLE_INFINITE_NAN() {
@@ -130,6 +128,11 @@ final class JsonMessages {
         return localize("generator.incomplete.json");
     }
 
+    static String GENERATOR_ILLEGAL_MULTIPLE_TEXT() {
+        return localize("generator.illegal.multiple.text");
+    }
+
+
 
     // writer messages
     static String WRITER_WRITE_ALREADY_CALLED() {--- 
a/impl/src/main/java/org/glassfish/json/JsonStructureParser.java
+++ b/impl/src/main/java/org/glassfish/json/JsonStructureParser.java
@@ -72,8 +72,7 @@ class JsonStructureParser implements JsonParser {
         } else if (state == Event.VALUE_STRING) {
             return ((JsonString)current.getJsonValue()).getString();
         }
-        throw new IllegalStateException("JsonParser#getString() can only be 
called in"
-                + " KEY_NAME or VALUE_STRING states, not in "+state);
+        throw new 
IllegalStateException(JsonMessages.PARSER_GETSTRING_ERR(state));
     }
 
     @Override
@@ -81,9 +80,7 @@ class JsonStructureParser implements JsonParser {
         if (state == Event.VALUE_NUMBER) {
             return ((JsonNumber)current.getJsonValue()).isIntegral();
         }
-        throw new IllegalStateException("JsonParser#isIntegralNumber() can 
only be called in"
-                + " VALUE_NUMBER state, not in "+state);
-
+        throw new 
IllegalStateException(JsonMessages.PARSER_ISINTEGRALNUMBER_ERR(state));
     }
 
     @Override
@@ -91,8 +88,7 @@ class JsonStructureParser implements JsonParser {
         if (state == Event.VALUE_NUMBER) {
             return ((JsonNumber)current.getJsonValue()).intValue();
         }
-        throw new IllegalStateException("JsonParser#getInt() can only be 
called in"
-                + " VALUE_NUMBER state, not in "+state);
+        throw new 
IllegalStateException(JsonMessages.PARSER_GETINT_ERR(state));
     }
 
     @Override
@@ -100,8 +96,7 @@ class JsonStructureParser implements JsonParser {
         if (state == Event.VALUE_NUMBER) {
             return ((JsonNumber)current.getJsonValue()).longValue();
         }
-        throw new IllegalStateException("JsonParser#getLong() can only be 
called in"
-                + " VALUE_NUMBER state, not in "+state);
+        throw new 
IllegalStateException(JsonMessages.PARSER_GETLONG_ERR(state));
     }
 
     @Override
@@ -109,8 +104,7 @@ class JsonStructureParser implements JsonParser {
         if (state == Event.VALUE_NUMBER) {
             return ((JsonNumber)current.getJsonValue()).bigDecimalValue();
         }
-        throw new IllegalStateException("JsonParser#getBigDecimal() can only 
be called in"
-                + " VALUE_NUMBER state, not in "+state);
+        throw new 
IllegalStateException(JsonMessages.PARSER_GETBIGDECIMAL_ERR(state));
     }
 
     @Override--- 
a/impl/src/main/resources/org/glassfish/json/messages.properties
+++ b/impl/src/main/resources/org/glassfish/json/messages.properties
@@ -15,10 +15,11 @@ parser.invalid.token=Invalid token={0} at {1}. Expected 
tokens are: {2}
 generator.flush.io.err=I/O error while flushing generated JSON
 generator.close.io.err=I/O error while closing JsonGenerator
 generator.write.io.err=I/O error while writing in JsonGenerator
-generator.method.legal.in.object.context=JSON generation via name/value 
method can only be called in object context
-generator.method.legal.in.array.context=JSON generation via value method can 
only be called in array context
+generator.illegal.method=Illegal method during JSON generation, \
+  not valid in current context {0}
 generator.double.infinite.nan=double value cannot be Infinite or NaN
 generator.incomplete.json=Generating incomplete JSON
+generator.illegal.multiple.text=Cannot generate more than one JSON text
 
 writer.write.already.called=write/writeObject/writeArray/close method is 
already called
 

--- a/tests/src/test/java/org/glassfish/json/tests/JsonGeneratorTest.java
+++ b/tests/src/test/java/org/glassfish/json/tests/JsonGeneratorTest.java
@@ -127,7 +127,6 @@ public class JsonGeneratorTest extends TestCase {
                 .writeEnd()
                 .writeEnd();
         generator.close();
-        System.out.println(sw.toString());
     }
 
     // tests JsonGenerator when JsonValue is used for generation
@@ -262,6 +261,7 @@ public class JsonGeneratorTest extends TestCase {
             generator.writeStartObject();
             fail("Expected JsonGenerationException, writeStartObject() 
cannot be called more than once");
         } catch (JsonGenerationException je) {
+            je.printStackTrace();
             // Expected exception
         }
     }
@@ -313,6 +313,30 @@ public class JsonGeneratorTest extends TestCase {
         }
     }
 
+    public void testGenerationException6() throws Exception {
+        StringWriter writer = new StringWriter();
+        JsonGenerator generator = Json.createGenerator(writer);
+        generator.writeStartObject().writeEnd();
+        try {
+            generator.writeStartObject();
+            fail("Expected JsonGenerationException, cannot generate one more 
JSON text");
+        } catch (JsonGenerationException je) {
+            // Expected exception
+        }
+    }
+
+    public void testGenerationException7() throws Exception {
+        StringWriter writer = new StringWriter();
+        JsonGenerator generator = Json.createGenerator(writer);
+        generator.writeStartArray().writeEnd();
+        try {
+            generator.writeStartArray();
+            fail("Expected JsonGenerationException, cannot generate one more 
JSON text");
+        } catch (JsonGenerationException je) {
+            // Expected exception
+        }
+    }
+
     public void testGeneratorArrayDouble() throws Exception {
         StringWriter writer = new StringWriter();
         JsonGenerator generator = Json.createGenerator(writer);





[jsonp~git:06ae40ea] Adding couple of tests that checks JsonGenerationException when multiple

jitu 10/15/2013
 
 
Close
loading
Please Confirm
Close