Skip to content
Snippets Groups Projects
Commit e1053dc9 authored by anttil's avatar anttil
Browse files

Whitelist for classes for which duplicate @JsonTypeName value is allowed.

SVN: 28442
parent e68c54dd
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2012 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.openbis.common.api.server.json;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* If a class is annotated with JsonUniqueCheckIgnore, the JsonAnnotationTest will not complain
* about it having a duplicate JsonTypeName.
*
* @author anttil
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface JsonUniqueCheckIgnore
{
}
...@@ -4,7 +4,6 @@ import org.apache.commons.lang.builder.EqualsBuilder; ...@@ -4,7 +4,6 @@ import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import ch.systemsx.cisd.base.annotation.JsonObject; import ch.systemsx.cisd.base.annotation.JsonObject;
import ch.systemsx.cisd.openbis.common.api.server.json.JsonUniqueCheckIgnore;
/* /*
* Copyright 2012 ETH Zuerich, CISD * Copyright 2012 ETH Zuerich, CISD
...@@ -25,7 +24,6 @@ import ch.systemsx.cisd.openbis.common.api.server.json.JsonUniqueCheckIgnore; ...@@ -25,7 +24,6 @@ import ch.systemsx.cisd.openbis.common.api.server.json.JsonUniqueCheckIgnore;
/** /**
* @author pkupczyk * @author pkupczyk
*/ */
@JsonUniqueCheckIgnore
@JsonObject(ObjectWithTypeAFactory.TYPE) @JsonObject(ObjectWithTypeAFactory.TYPE)
public class ObjectWithTypeALegalDuplicate public class ObjectWithTypeALegalDuplicate
{ {
......
...@@ -4,7 +4,6 @@ import org.apache.commons.lang.builder.EqualsBuilder; ...@@ -4,7 +4,6 @@ import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import ch.systemsx.cisd.base.annotation.JsonObject; import ch.systemsx.cisd.base.annotation.JsonObject;
import ch.systemsx.cisd.openbis.common.api.server.json.JsonUniqueCheckIgnore;
/* /*
* Copyright 2012 ETH Zuerich, CISD * Copyright 2012 ETH Zuerich, CISD
...@@ -26,7 +25,6 @@ import ch.systemsx.cisd.openbis.common.api.server.json.JsonUniqueCheckIgnore; ...@@ -26,7 +25,6 @@ import ch.systemsx.cisd.openbis.common.api.server.json.JsonUniqueCheckIgnore;
* @author pkupczyk * @author pkupczyk
*/ */
@JsonObject(ObjectWithTypeBFactory.TYPE) @JsonObject(ObjectWithTypeBFactory.TYPE)
@JsonUniqueCheckIgnore
public class ObjectWithTypeBIllegalDuplicate extends ObjectWithType public class ObjectWithTypeBIllegalDuplicate extends ObjectWithType
{ {
......
...@@ -31,7 +31,6 @@ import org.testng.annotations.Test; ...@@ -31,7 +31,6 @@ import org.testng.annotations.Test;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import ch.systemsx.cisd.base.annotation.JsonObject; import ch.systemsx.cisd.base.annotation.JsonObject;
import ch.systemsx.cisd.openbis.common.api.server.json.JsonUniqueCheckIgnore;
import ch.systemsx.cisd.openbis.common.api.server.json.util.ClassReferences; import ch.systemsx.cisd.openbis.common.api.server.json.util.ClassReferences;
import ch.systemsx.cisd.openbis.dss.generic.shared.api.v1.IDssServiceRpcGeneric; import ch.systemsx.cisd.openbis.dss.generic.shared.api.v1.IDssServiceRpcGeneric;
import ch.systemsx.cisd.openbis.dss.screening.shared.api.v1.IDssServiceRpcScreening; import ch.systemsx.cisd.openbis.dss.screening.shared.api.v1.IDssServiceRpcScreening;
...@@ -91,16 +90,22 @@ public class JsonAnnotationTest ...@@ -91,16 +90,22 @@ public class JsonAnnotationTest
@Test @Test
public void jsonTypeNamesAreUnique() public void jsonTypeNamesAreUnique()
{ {
// Classes that are allowed to have already existing @JsonTypeName
Collection<String> whiteList = new HashSet<String>();
whiteList
.add("ch.systemsx.cisd.openbis.common.api.server.json.object.ObjectWithTypeALegalDuplicate");
whiteList
.add("ch.systemsx.cisd.openbis.common.api.server.json.object.ObjectWithTypeBIllegalDuplicate");
whiteList.add("ch.systemsx.cisd.openbis.generic.shared.api.v1.dto.ExperimentIdentifier");
Map<String, Collection<Class<?>>> names = new HashMap<String, Collection<Class<?>>>(); Map<String, Collection<Class<?>>> names = new HashMap<String, Collection<Class<?>>>();
for (Class<?> clazz : ClassReferences.ref.getTypesAnnotatedWith(JsonObject.class)) for (Class<?> clazz : ClassReferences.ref.getTypesAnnotatedWith(JsonObject.class))
{ {
if (clazz.getAnnotation(JsonUniqueCheckIgnore.class) != null) if (whiteList.contains(clazz.getCanonicalName()) == false)
{ {
continue; String name = clazz.getAnnotation(JsonObject.class).value();
addValueToCollectionMap(names, name, clazz);
} }
String name = clazz.getAnnotation(JsonObject.class).value();
addValueToCollectionMap(names, name, clazz);
} }
assertThat(duplicatedValuesIn(names), is(emptyMap)); assertThat(duplicatedValuesIn(names), is(emptyMap));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment