2011-01-22 23:15:06 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 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 com.google.gson.annotations.Expose;
|
|
|
|
|
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
2011-02-04 04:09:41 +01:00
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
2011-01-22 23:15:06 +01:00
|
|
|
/**
|
2011-11-21 07:14:23 +01:00
|
|
|
* Unit tests for GsonBuilder.REQUIRE_EXPOSE_DESERIALIZE.
|
2011-01-22 23:15:06 +01:00
|
|
|
*
|
|
|
|
* @author Joel Leitch
|
|
|
|
*/
|
|
|
|
public class ExposeAnnotationExclusionStrategyTest extends TestCase {
|
2011-11-22 07:07:18 +01:00
|
|
|
private ExclusionStrategy serializationStrategy = new GsonExclusionStrategy(
|
|
|
|
GsonExclusionStrategy.IGNORE_VERSIONS, 0, true, true, true, true, false);
|
|
|
|
private ExclusionStrategy deserializationStrategy = new GsonExclusionStrategy(
|
|
|
|
GsonExclusionStrategy.IGNORE_VERSIONS, 0, true, true, true, false, true);
|
2011-01-22 23:15:06 +01:00
|
|
|
|
|
|
|
public void testNeverSkipClasses() throws Exception {
|
2011-02-04 04:09:41 +01:00
|
|
|
assertFalse(deserializationStrategy.shouldSkipClass(MockObject.class));
|
|
|
|
assertFalse(serializationStrategy.shouldSkipClass(MockObject.class));
|
2011-01-22 23:15:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testSkipNonAnnotatedFields() throws Exception {
|
|
|
|
FieldAttributes f = createFieldAttributes("hiddenField");
|
2011-02-04 04:09:41 +01:00
|
|
|
assertTrue(deserializationStrategy.shouldSkipField(f));
|
|
|
|
assertTrue(serializationStrategy.shouldSkipField(f));
|
2011-01-22 23:15:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testSkipExplicitlySkippedFields() throws Exception {
|
|
|
|
FieldAttributes f = createFieldAttributes("explicitlyHiddenField");
|
2011-02-04 04:09:41 +01:00
|
|
|
assertTrue(deserializationStrategy.shouldSkipField(f));
|
|
|
|
assertTrue(serializationStrategy.shouldSkipField(f));
|
2011-01-22 23:15:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testNeverSkipExposedAnnotatedFields() throws Exception {
|
|
|
|
FieldAttributes f = createFieldAttributes("exposedField");
|
2011-02-04 04:09:41 +01:00
|
|
|
assertFalse(deserializationStrategy.shouldSkipField(f));
|
|
|
|
assertFalse(serializationStrategy.shouldSkipField(f));
|
2011-01-22 23:15:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testNeverSkipExplicitlyExposedAnnotatedFields() throws Exception {
|
|
|
|
FieldAttributes f = createFieldAttributes("explicitlyExposedField");
|
2011-02-04 04:09:41 +01:00
|
|
|
assertFalse(deserializationStrategy.shouldSkipField(f));
|
|
|
|
assertFalse(serializationStrategy.shouldSkipField(f));
|
2011-01-22 23:15:06 +01:00
|
|
|
}
|
2011-11-21 06:42:30 +01:00
|
|
|
|
2011-01-22 23:15:06 +01:00
|
|
|
public void testDifferentSerializeAndDeserializeField() throws Exception {
|
|
|
|
FieldAttributes f = createFieldAttributes("explicitlyDifferentModeField");
|
2011-02-04 04:09:41 +01:00
|
|
|
assertTrue(deserializationStrategy.shouldSkipField(f));
|
|
|
|
assertFalse(serializationStrategy.shouldSkipField(f));
|
2011-01-22 23:15:06 +01:00
|
|
|
}
|
2011-11-21 06:42:30 +01:00
|
|
|
|
2011-01-22 23:15:06 +01:00
|
|
|
private static FieldAttributes createFieldAttributes(String fieldName) throws Exception {
|
|
|
|
Field f = MockObject.class.getField(fieldName);
|
2011-11-21 06:42:30 +01:00
|
|
|
return new FieldAttributes(f);
|
2011-01-22 23:15:06 +01:00
|
|
|
}
|
2011-11-21 06:42:30 +01:00
|
|
|
|
2011-01-22 23:15:06 +01:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private static class MockObject {
|
|
|
|
@Expose
|
|
|
|
public final int exposedField = 0;
|
|
|
|
|
|
|
|
@Expose(serialize=true, deserialize=true)
|
|
|
|
public final int explicitlyExposedField = 0;
|
|
|
|
|
|
|
|
@Expose(serialize=false, deserialize=false)
|
|
|
|
public final int explicitlyHiddenField = 0;
|
2011-11-21 06:42:30 +01:00
|
|
|
|
2011-01-22 23:15:06 +01:00
|
|
|
@Expose(serialize=true, deserialize=false)
|
|
|
|
public final int explicitlyDifferentModeField = 0;
|
|
|
|
|
|
|
|
public final int hiddenField = 0;
|
|
|
|
}
|
|
|
|
}
|