From 4c8b3f56461aaf8e76f736fe44d0a07598ee5b2a Mon Sep 17 00:00:00 2001 From: YOUNG CHA Date: Mon, 25 Mar 2019 11:37:09 +0900 Subject: [PATCH] Add testcase for obfuscated enum class --- gson/pom.xml | 97 +++++++++++++++++++ .../functional/EnumWithObfuscatedTest.java | 62 ++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java diff --git a/gson/pom.xml b/gson/pom.xml index 8f233fe715..ee2b5e19eb 100644 --- a/gson/pom.xml +++ b/gson/pom.xml @@ -16,6 +16,12 @@ junit test + + com.github.wvengen + proguard-maven-plugin + 2.0.14 + test + @@ -73,6 +79,97 @@ + + com.coderplus.maven.plugins + copy-rename-maven-plugin + 1.0 + + + pre-obfuscate-class + process-test-classes + + rename + + + + + ${project.build.directory}/test-classes/com/google/gson/functional/EnumWithObfuscatedTest.class + ${project.build.directory}/test-classes-proguard-injar/com/google/gson/functional/EnumWithObfuscatedTest.class + + + ${project.build.directory}/test-classes/com/google/gson/functional/EnumWithObfuscatedTest$Gender.class + ${project.build.directory}/test-classes-proguard-injar/com/google/gson/functional/EnumWithObfuscatedTest$Gender.class + + + + + + + + com.github.wvengen + proguard-maven-plugin + + + process-test-classes + proguard + + + + 6.0.2 + test-classes-proguard-injar + test-classes-proguard-outjar + **/*.class + + + + + + + + + + + + + + ${project.build.directory}/classes + ${java.home}/jmods/java.base.jmod + + + + + net.sf.proguard + proguard-base + 6.0.2 + runtime + + + + + maven-resources-plugin + 2.7 + + + post-obfuscate-class + process-test-classes + + copy-resources + + + ${project.build.directory}/test-classes/com/google/gson/functional + + + ${project.build.directory}/test-classes-proguard-outjar/classes/classes/com/google/gson/functional + + EnumWithObfuscatedTest.class + EnumWithObfuscatedTest$Gender.class + + + + + + + diff --git a/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java b/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java new file mode 100644 index 0000000000..1d1d4fbfe6 --- /dev/null +++ b/gson/src/test/java/com/google/gson/functional/EnumWithObfuscatedTest.java @@ -0,0 +1,62 @@ +/* + * 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 java.lang.reflect.Field; + +import com.google.gson.Gson; +import com.google.gson.annotations.SerializedName; + +import junit.framework.TestCase; + +/** + * Functional tests for enums with Proguard. + * + * @author Young Cha + */ +public class EnumWithObfuscatedTest extends TestCase { + private Gson gson; + + @Override + protected void setUp() throws Exception { + super.setUp(); + gson = new Gson(); + } + + public enum Gender { + @SerializedName("MAIL") + MALE, + + @SerializedName("FEMAIL") + FEMALE + } + + public void testEnumClassWithObfuscated() { + boolean foundNonObfuscatedEnumConstant = false; + for (Gender enumConstant: Gender.class.getEnumConstants()) { + try { + Gender.class.getField(enumConstant.name()); + foundNonObfuscatedEnumConstant = true; + } catch (NoSuchFieldException ignore) { + } + } + assertFalse("Enum is not obfuscated", foundNonObfuscatedEnumConstant); + + assertEquals(Gender.MALE, gson.fromJson("\"MAIL\"", Gender.class)); + assertEquals("\"MAIL\"", gson.toJson(Gender.MALE, Gender.class)); + } +}