gson-comments/gson/src/test/java/com/google/gson/JsonObjectTest.java

96 lines
2.9 KiB
Java
Raw Normal View History

/*
* Copyright (C) 2008 Google Inc.
*
* Licensed 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 com.google.gson;
import junit.framework.TestCase;
/**
* Unit test for the {@link JsonObject} class.
*
* @author Joel Leitch
*/
public class JsonObjectTest extends TestCase {
public void testAddingAndRemovingObjectProperties() throws Exception {
JsonObject jsonObj = new JsonObject();
String propertyName = "property";
assertFalse(jsonObj.has(propertyName));
assertNull(jsonObj.get(propertyName));
JsonPrimitive value = new JsonPrimitive("blah");
jsonObj.add(propertyName, value);
assertEquals(value, jsonObj.get(propertyName));
JsonElement removedElement = jsonObj.remove(propertyName);
assertEquals(value, removedElement);
assertFalse(jsonObj.has(propertyName));
}
public void testAddingNullProperties() throws Exception {
String propertyName = "property";
JsonObject jsonObj = new JsonObject();
jsonObj.add(propertyName, null);
assertTrue(jsonObj.has(propertyName));
JsonElement jsonElement = jsonObj.get(propertyName);
assertNotNull(jsonElement);
assertTrue(jsonElement.isJsonNull());
}
public void testAddingBooleanProperties() throws Exception {
String propertyName = "property";
JsonObject jsonObj = new JsonObject();
jsonObj.addProperty(propertyName, true);
assertTrue(jsonObj.has(propertyName));
JsonElement jsonElement = jsonObj.get(propertyName);
assertNotNull(jsonElement);
assertTrue(jsonElement.getAsBoolean());
}
public void testAddingStringProperties() throws Exception {
String propertyName = "property";
String value = "blah";
JsonObject jsonObj = new JsonObject();
jsonObj.addProperty(propertyName, value);
assertTrue(jsonObj.has(propertyName));
JsonElement jsonElement = jsonObj.get(propertyName);
assertNotNull(jsonElement);
assertEquals(value, jsonElement.getAsString());
}
public void testAddingCharacterProperties() throws Exception {
String propertyName = "property";
char value = 'a';
JsonObject jsonObj = new JsonObject();
jsonObj.addProperty(propertyName, value);
assertTrue(jsonObj.has(propertyName));
JsonElement jsonElement = jsonObj.get(propertyName);
assertNotNull(jsonElement);
assertEquals(String.valueOf(value), jsonElement.getAsString());
assertEquals(value, jsonElement.getAsCharacter());
}
}