+++ /dev/null
-<?xml version="1.0"?>
-<!--
- ============LICENSE_START=======================================================
- Copyright (C) 2019 AT&T Technologies. All rights reserved.
- ================================================================================
- 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.
-
- SPDX-License-Identifier: Apache-2.0
- ============LICENSE_END=========================================================
--->
-
-<!--
- NOTE: The sole purpose of this supression file is to allow "$" in field and method
- names so that the gson class tests can verify that those fields are ignored
- when doing serialization and de-serialization.
- -->
-
-<!DOCTYPE suppressions PUBLIC
- "-//Puppy Crawl//DTD Suppressions 1.0//EN"
- "http://www.puppycrawl.com/dtds/suppressions_1_0.dtd">
-
-<suppressions>
- <suppress checks="MemberName"
- files="AdapterTest.java|ClassWalkerTest.java"
- lines="1-9999"/>
- <suppress checks="MethodName"
- files="AdapterTest.java"
- lines="1-9999"/>
-</suppressions>
*/
private static final Pattern VALID_NAME_PAT = Pattern.compile("[a-zA-Z_]\\w*");
+ /**
+ * Factory to access objects. Overridden by junit tests.
+ */
+ private static Factory factory = new Factory();
+
/**
* Name of the property within the json structure containing the item.
*/
* @return {@code true} if the field is managed by the walker, {@code false} otherwise
*/
public static boolean isManaged(Field field) {
- return VALID_NAME_PAT.matcher(field.getName()).matches();
+ return VALID_NAME_PAT.matcher(factory.getName(field)).matches();
}
/**
* otherwise
*/
public static boolean isManaged(Method method) {
- return VALID_NAME_PAT.matcher(method.getName()).matches();
+ return VALID_NAME_PAT.matcher(factory.getName(method)).matches();
}
/**
}
// no name provided - use it as is
- return (isManaged(field) ? field.getName() : null);
+ return (isManaged(field) ? factory.getName(field) : null);
}
/**
return null;
}
- String name = method.getName();
+ String name = factory.getName(method);
if (name.startsWith("get")) {
return name.substring(3);
return null;
}
- String name = method.getName();
+ String name = factory.getName(method);
if (name.startsWith("set")) {
return name.substring(3);
* @return the field fully qualified name
*/
public static String getQualifiedName(Field field) {
- return (field.getDeclaringClass().getName() + "." + field.getName());
+ return (field.getDeclaringClass().getName() + "." + factory.getName(field));
}
/**
* @return the method's fully qualified name
*/
public static String getQualifiedName(Method method) {
- return (method.getDeclaringClass().getName() + "." + method.getName());
+ return (method.getDeclaringClass().getName() + "." + factory.getName(method));
}
/**
return conv;
}
}
+
+ /**
+ * Factory used to access various objects.
+ */
+ public static class Factory {
+
+ public String getName(Field field) {
+ return field.getName();
+ }
+
+ public String getName(Method method) {
+ return method.getName();
+ }
+ }
}
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
+import org.junit.After;
+import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.common.gson.JacksonExclusionStrategy;
import org.onap.policy.common.gson.annotation.GsonJsonProperty;
+import org.onap.policy.common.gson.internal.Adapter.Factory;
import org.onap.policy.common.gson.internal.DataAdapterFactory.Data;
import org.onap.policy.common.gson.internal.DataAdapterFactory.DerivedData;
+import org.powermock.reflect.Whitebox;
public class AdapterTest {
+ private static final String GET_INVALID_NAME = "get$InvalidName";
+ private static final String SET_INVALID_NAME = "set$InvalidName";
private static final String EMPTY_ALIAS = "emptyAlias";
private static final String GET_VALUE = ".getValue";
private static final String GET_VALUE_NAME = "getValue";
+ private static final String SET_VALUE_NAME = "setValue";
private static final String VALUE_NAME = "value";
private static final String MY_NAME = AdapterTest.class.getName();
+ private static final String FACTORY_FIELD = "factory";
private static DataAdapterFactory dataAdapter = new DataAdapterFactory();
private static Gson gson = new GsonBuilder().registerTypeAdapterFactory(dataAdapter)
.setExclusionStrategies(new JacksonExclusionStrategy()).create();
+ private static Factory saveFactory;
+
/*
* The remaining fields are just used within the tests.
*/
protected String unaliased;
- protected String $invalidFieldName;
-
private List<Data> listField;
private Data dataField;
+ @BeforeClass
+ public static void setUpBeforeClass() {
+ saveFactory = Whitebox.getInternalState(Adapter.class, FACTORY_FIELD);
+ }
+
+ @After
+ public void tearDown() {
+ Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, saveFactory);
+ }
@Test
public void testIsManagedField() {
assertTrue(Adapter.isManaged(field(VALUE_NAME)));
- assertFalse(Adapter.isManaged(field("$invalidFieldName")));
+ // return an invalid field name
+ Factory factory = mock(Factory.class);
+ when(factory.getName(any(Field.class))).thenReturn("$invalidFieldName");
+ Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);
+ assertFalse(Adapter.isManaged(field(VALUE_NAME)));
}
@Test
public void testIsManagedMethod() {
assertTrue(Adapter.isManaged(mget(GET_VALUE_NAME)));
- assertFalse(Adapter.isManaged(mget("get$InvalidName")));
- assertFalse(Adapter.isManaged(mset("set$InvalidName")));
+ // return an invalid method name
+ Factory factory = mock(Factory.class);
+ Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);
+
+ when(factory.getName(any(Method.class))).thenReturn(GET_INVALID_NAME);
+ assertFalse(Adapter.isManaged(mget(GET_VALUE_NAME)));
+
+ when(factory.getName(any(Method.class))).thenReturn(SET_INVALID_NAME);
+ assertFalse(Adapter.isManaged(mset(SET_VALUE_NAME)));
}
@Test
// test setter
- adapter = new Adapter(gson, mset("setValue"), String.class);
+ adapter = new Adapter(gson, mset(SET_VALUE_NAME), String.class);
assertEquals(VALUE_NAME, adapter.getPropName());
assertEquals(MY_NAME + ".setValue", adapter.getFullName());
assertEquals(EMPTY_ALIAS, Adapter.detmPropName(field(EMPTY_ALIAS)));
assertEquals("name-with-alias", Adapter.detmPropName(field("nameWithAlias")));
assertEquals("unaliased", Adapter.detmPropName(field("unaliased")));
- assertEquals(null, Adapter.detmPropName(field("$invalidFieldName")));
+
+ // return an invalid field name
+ Factory factory = mock(Factory.class);
+ when(factory.getName(any(Field.class))).thenReturn("$invalidFieldName");
+ Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);
+ assertEquals(null, Adapter.detmPropName(field(VALUE_NAME)));
}
@Test
assertEquals(null, Adapter.detmGetterPropName(mget("isString")));
assertEquals(null, Adapter.detmGetterPropName(mget("noGet")));
assertEquals(null, Adapter.detmGetterPropName(mget("get")));
- assertEquals(null, Adapter.detmGetterPropName(mget("get$InvalidName")));
+
+ // return an invalid method name
+ Factory factory = mock(Factory.class);
+ Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);
+
+ when(factory.getName(any(Method.class))).thenReturn(GET_INVALID_NAME);
+ assertEquals(null, Adapter.detmGetterPropName(mget(GET_VALUE_NAME)));
}
@Test
assertEquals("plain", Adapter.detmSetterPropName(mset("setPlain")));
assertEquals(null, Adapter.detmSetterPropName(mset("noSet")));
assertEquals(null, Adapter.detmSetterPropName(mset("set")));
- assertEquals(null, Adapter.detmSetterPropName(mset("set$InvalidName")));
+
+ // return an invalid method name
+ Factory factory = mock(Factory.class);
+ Whitebox.setInternalState(Adapter.class, FACTORY_FIELD, factory);
+
+ when(factory.getName(any(Method.class))).thenReturn(SET_INVALID_NAME);
+ assertEquals(null, Adapter.detmSetterPropName(mset(SET_VALUE_NAME)));
}
@Test
return "";
}
- // name has a bogus character
- protected String get$InvalidName() {
- return "";
- }
-
protected void setValue(String text) {
// do nothing
// do nothing
}
- // name has a bogus character
- protected void set$InvalidName(String text) {
- // do nothing
- }
-
// returns a list
protected List<Data> getMyList() {
return listField;