|
Java example source code file (UncategorizedTest.java)
The UncategorizedTest.java Java example source code/* * 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.functional; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import com.google.gson.common.TestTypes.BagOfPrimitives; import com.google.gson.common.TestTypes.ClassOverridingEquals; import com.google.gson.reflect.TypeToken; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; import java.lang.reflect.Type; /** * Functional tests that do not fall neatly into any of the existing classification. * * @author Inderjeet Singh * @author Joel Leitch */ public class UncategorizedTest extends TestCase { private Gson gson = null; @Override protected void setUp() throws Exception { super.setUp(); gson = new Gson(); } public void testInvalidJsonDeserializationFails() throws Exception { try { gson.fromJson("adfasdf1112,,,\":", BagOfPrimitives.class); fail("Bad JSON should throw a ParseException"); } catch (JsonParseException expected) { } try { gson.fromJson("{adfasdf1112,,,\":}", BagOfPrimitives.class); fail("Bad JSON should throw a ParseException"); } catch (JsonParseException expected) { } } public void testObjectEqualButNotSameSerialization() throws Exception { ClassOverridingEquals objA = new ClassOverridingEquals(); ClassOverridingEquals objB = new ClassOverridingEquals(); objB.ref = objA; String json = gson.toJson(objB); assertEquals(objB.getExpectedJson(), json); } public void testStaticFieldsAreNotSerialized() { BagOfPrimitives target = new BagOfPrimitives(); assertFalse(gson.toJson(target).contains("DEFAULT_VALUE")); } public void testGsonInstanceReusableForSerializationAndDeserialization() { BagOfPrimitives bag = new BagOfPrimitives(); String json = gson.toJson(bag); BagOfPrimitives deserialized = gson.fromJson(json, BagOfPrimitives.class); assertEquals(bag, deserialized); } /** * This test ensures that a custom deserializer is able to return a derived class instance for a * base class object. For a motivation for this test, see Issue 37 and * http://groups.google.com/group/google-gson/browse_thread/thread/677d56e9976d7761 */ public void testReturningDerivedClassesDuringDeserialization() { Gson gson = new GsonBuilder().registerTypeAdapter(Base.class, new BaseTypeAdapter()).create(); String json = "{\"opType\":\"OP1\"}"; Base base = gson.fromJson(json, Base.class); assertTrue(base instanceof Derived1); assertEquals(OperationType.OP1, base.opType); json = "{\"opType\":\"OP2\"}"; base = gson.fromJson(json, Base.class); assertTrue(base instanceof Derived2); assertEquals(OperationType.OP2, base.opType); } /** * Test that trailing whitespace is ignored. * http://code.google.com/p/google-gson/issues/detail?id=302 */ public void testTrailingWhitespace() throws Exception { List<Integer> integers = gson.fromJson("[1,2,3] \n\n ", new TypeToken<List Other Java examples (source code examples)Here is a short list of links related to this Java UncategorizedTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.