|
Java example source code file (VersioningTest.java)
The VersioningTest.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.annotations.Since; import com.google.gson.annotations.Until; import com.google.gson.common.TestTypes.BagOfPrimitives; import junit.framework.TestCase; /** * Functional tests for versioning support in Gson. * * @author Inderjeet Singh * @author Joel Leitch */ public class VersioningTest extends TestCase { private static final int A = 0; private static final int B = 1; private static final int C = 2; private static final int D = 3; private GsonBuilder builder; @Override protected void setUp() throws Exception { super.setUp(); builder = new GsonBuilder(); } public void testVersionedUntilSerialization() { Version1 target = new Version1(); Gson gson = builder.setVersion(1.29).create(); String json = gson.toJson(target); assertTrue(json.contains("\"a\":" + A)); gson = builder.setVersion(1.3).create(); json = gson.toJson(target); assertFalse(json.contains("\"a\":" + A)); } public void testVersionedUntilDeserialization() { Gson gson = builder.setVersion(1.3).create(); String json = "{\"a\":3,\"b\":4,\"c\":5}"; Version1 version1 = gson.fromJson(json, Version1.class); assertEquals(A, version1.a); } public void testVersionedClassesSerialization() { Gson gson = builder.setVersion(1.0).create(); String json1 = gson.toJson(new Version1()); String json2 = gson.toJson(new Version1_1()); assertEquals(json1, json2); } public void testVersionedClassesDeserialization() { Gson gson = builder.setVersion(1.0).create(); String json = "{\"a\":3,\"b\":4,\"c\":5}"; Version1 version1 = gson.fromJson(json, Version1.class); assertEquals(3, version1.a); assertEquals(4, version1.b); Version1_1 version1_1 = gson.fromJson(json, Version1_1.class); assertEquals(3, version1_1.a); assertEquals(4, version1_1.b); assertEquals(C, version1_1.c); } public void testIgnoreLaterVersionClassSerialization() { Gson gson = builder.setVersion(1.0).create(); assertEquals("null", gson.toJson(new Version1_2())); } public void testIgnoreLaterVersionClassDeserialization() { Gson gson = builder.setVersion(1.0).create(); String json = "{\"a\":3,\"b\":4,\"c\":5,\"d\":6}"; Version1_2 version1_2 = gson.fromJson(json, Version1_2.class); // Since the class is versioned to be after 1.0, we expect null // This is the new behavior in Gson 2.0 assertNull(version1_2); } public void testVersionedGsonWithUnversionedClassesSerialization() { Gson gson = builder.setVersion(1.0).create(); BagOfPrimitives target = new BagOfPrimitives(10, 20, false, "stringValue"); assertEquals(target.getExpectedJson(), gson.toJson(target)); } public void testVersionedGsonWithUnversionedClassesDeserialization() { Gson gson = builder.setVersion(1.0).create(); String json = "{\"longValue\":10,\"intValue\":20,\"booleanValue\":false}"; BagOfPrimitives expected = new BagOfPrimitives(); expected.longValue = 10; expected.intValue = 20; expected.booleanValue = false; BagOfPrimitives actual = gson.fromJson(json, BagOfPrimitives.class); assertEquals(expected, actual); } public void testVersionedGsonMixingSinceAndUntilSerialization() { Gson gson = builder.setVersion(1.0).create(); SinceUntilMixing target = new SinceUntilMixing(); String json = gson.toJson(target); assertFalse(json.contains("\"b\":" + B)); gson = builder.setVersion(1.2).create(); json = gson.toJson(target); assertTrue(json.contains("\"b\":" + B)); gson = builder.setVersion(1.3).create(); json = gson.toJson(target); assertFalse(json.contains("\"b\":" + B)); } public void testVersionedGsonMixingSinceAndUntilDeserialization() { String json = "{\"a\":5,\"b\":6}"; Gson gson = builder.setVersion(1.0).create(); SinceUntilMixing result = gson.fromJson(json, SinceUntilMixing.class); assertEquals(5, result.a); assertEquals(B, result.b); gson = builder.setVersion(1.2).create(); result = gson.fromJson(json, SinceUntilMixing.class); assertEquals(5, result.a); assertEquals(6, result.b); gson = builder.setVersion(1.3).create(); result = gson.fromJson(json, SinceUntilMixing.class); assertEquals(5, result.a); assertEquals(B, result.b); } private static class Version1 { @Until(1.3) int a = A; @Since(1.0) int b = B; } private static class Version1_1 extends Version1 { @Since(1.1) int c = C; } @Since(1.2) private static class Version1_2 extends Version1_1 { @SuppressWarnings("unused") int d = D; } private static class SinceUntilMixing { int a = A; @Since(1.1) @Until(1.3) int b = B; } } Other Java examples (source code examples)Here is a short list of links related to this Java VersioningTest.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.