Skip to content

Commit

Permalink
fixed #109: ObjectToMap conversion ignores transient properties
Browse files Browse the repository at this point in the history
  • Loading branch information
S1artie committed Jun 1, 2016
1 parent 61315a2 commit 6d025ae
Show file tree
Hide file tree
Showing 7 changed files with 332 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public Map convert(Object aSource, Class<? extends Map> aTargetType, ConversionC
try {
for (PropertyDescriptor tempDescriptor : Introspector.getBeanInfo(aSource.getClass(), Object.class)
.getPropertyDescriptors()) {

if (Boolean.TRUE.equals(tempDescriptor.getValue("transient"))) {
// Skip transient properties, as those are often used as "fake" properties in order to implement a
// getter for them which does some sort of magic. Calling unknown magical stuff in a generic fashion
// when running over Java Beans is not such a good idea and quickly leads to problems, which is the
// reason why transient properties are skipped here. See also issue #109.
continue;
}

Method tempReadMethod = tempDescriptor.getReadMethod();
if (tempReadMethod != null) {
Object tempValue = tempReadMethod.invoke(aSource);
Expand Down Expand Up @@ -83,4 +92,19 @@ public Map convert(Object aSource, Class<? extends Map> aTargetType, ConversionC

return tempKeyValueMap;
}

// protected boolean isTransient(String aFieldName, Class<?> aClass) {
// Class<?> tempClassInFocus = aClass;
// while(tempClassInFocus != null) {
// try {
// Field tempField = tempClassInFocus.getDeclaredField(aFieldName);
//
// if(tempField != null) {
// return tempField.getModifiers() & Modifier
// }
// } catch (NoSuchFieldException | SecurityException exc) {
// // ignore
// }
// }
// }
}
63 changes: 33 additions & 30 deletions de.gebit.integrity.tests/integrity/fixtures/BeanFixture.integrity
Original file line number Diff line number Diff line change
@@ -1,31 +1,34 @@
packagedef integrity.fixtures.basic.beans with

testdef checkSimpleBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#checkSimpleBean

testdef checkCollectionBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#checkCollectionBean

testdef alterPrimitiveBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#alterPrimitiveBean

testdef checkSimpleBeanUntyped uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#checkSimpleBeanUntyped

testdef createSimpleBeanUntyped uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntyped
calldef createSimpleBeanUntypedCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntyped

testdef createMapForSimpleBeanUntyped uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createMapForSimpleBeanUntyped
calldef createMapForSimpleBeanUntypedCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createMapForSimpleBeanUntyped

testdef echoEnumTestBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoEnumTestBean
calldef echoEnumTestBeanCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoEnumTestBean

testdef echoEnumValueFromTestBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoEnumValueFromTestBean
calldef echoEnumValueFromTestBeanCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoEnumValueFromTestBean

testdef echoMap uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoMap
calldef echoMapCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoMap

calldef createPrimitiveTypeArrayTestBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createPrimitiveTypeArrayTestBean

testdef createSimpleBeanUntypedEmpty uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntypedEmpty
calldef createSimpleBeanUntypedEmptyCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntypedEmpty

packagedef integrity.fixtures.basic.beans with

testdef checkSimpleBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#checkSimpleBean

testdef checkCollectionBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#checkCollectionBean

testdef alterPrimitiveBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#alterPrimitiveBean

testdef checkSimpleBeanUntyped uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#checkSimpleBeanUntyped

testdef createSimpleBeanUntyped uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntyped
calldef createSimpleBeanUntypedCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntyped

testdef createMapForSimpleBeanUntyped uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createMapForSimpleBeanUntyped
calldef createMapForSimpleBeanUntypedCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createMapForSimpleBeanUntyped

testdef echoEnumTestBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoEnumTestBean
calldef echoEnumTestBeanCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoEnumTestBean

testdef echoEnumValueFromTestBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoEnumValueFromTestBean
calldef echoEnumValueFromTestBeanCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoEnumValueFromTestBean

testdef echoMap uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoMap
calldef echoMapCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#echoMap

calldef createPrimitiveTypeArrayTestBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createPrimitiveTypeArrayTestBean

testdef createSimpleBeanUntypedEmpty uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntypedEmpty
calldef createSimpleBeanUntypedEmptyCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createSimpleBeanUntypedEmpty

testdef createTransientTestBean uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createTransientTestBean
calldef createTransientTestBeanCall uses de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createTransientTestBean

packageend
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
packagedef integrity.basic.beans with

suitedef beanWithTransientProperty with

// This should succeed, since we don't check the transient property
test integrity.fixtures.basic.beans.createTransientTestBean = {
normalString: "foo"
}

// This should fail as we cannot see the transient property here since it's not converted to the map internally
test integrity.fixtures.basic.beans.createTransientTestBean = {
normalString: "foo"
transientString: "bar"
}

suiteend

packageend
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*******************************************************************************
* Copyright (c) 2013 Rene Schneider, GEBIT Solutions GmbH and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package de.gebit.integrity.tests.junit.basic.beans;

import java.io.IOException;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.junit.Test;

import de.gebit.integrity.runner.exceptions.ModelLoadException;
import de.gebit.integrity.tests.junit.IntegrityJUnitTest;

/**
* JUnit test which checks bean calls.
*
*
* @author Rene Schneider - initial API and implementation
*
*/
public class BeanWithTransientProperty extends IntegrityJUnitTest {

/**
* Performs a suite which does fixture calls with bean values and checks the resulting XML document.
*
* @throws ModelLoadException
* @throws IOException
* @throws JDOMException
*/
@Test
public void test() throws ModelLoadException, IOException, JDOMException {
Document tempResult = executeIntegritySuite(
new String[] { "integrity/suites/basic/beans/beanWithTransientProperty.integrity" },
"integrity.basic.beans.beanWithTransientProperty", null);
assertDocumentMatchesReference(tempResult);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<integrity name="Integrity JUnit Testing" timestamp="01.06.16 13:21" isotimestamp="2016-06-01T13:21:49" duration="27.521">
<variables />
<suite id="0" name="integrity.basic.beans.beanWithTransientProperty" timestamp="01.06.16 13:21:49.0238">
<setup />
<variables />
<statements>
<test id="1" line="6" name="createTransientTestBean" description="creates a transient test bean and returns it" fixture="de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createTransientTestBean" timestamp="01.06.16 13:21:49.0238">
<results duration="0.830" successCount="1" failureCount="0" exceptionCount="0">
<result duration="0.830" description="creates a transient test bean and returns it" type="success">
<parameters />
<comparisons>
<comparison expectedValue="[FORMATTED]{[NL][T]normalString = foo[NL]}" value="[FORMATTED]{[NL][T]normalString = foo[NL]}" type="success" />
</comparisons>
</result>
</results>
</test>
<test id="2" line="11" name="createTransientTestBean" description="creates a transient test bean and returns it" fixture="de.gebit.integrity.tests.fixtures.basic.beans.BeanFixture#createTransientTestBean" timestamp="01.06.16 13:21:49.0253">
<results duration="0.155" successCount="0" failureCount="1" exceptionCount="0">
<result duration="0.155" description="creates a transient test bean and returns it" type="failure">
<parameters />
<comparisons>
<comparison expectedValue="[FORMATTED]{[NL][T]normalString = foo[NL|, ][T][UL][B]transientString = bar[/B][/UL][NL]}" value="[FORMATTED]{[NL][T]normalString = foo[NL]}" type="failure" />
</comparisons>
</result>
</results>
</test>
</statements>
<returns />
<teardown />
<result duration="23.145" successCount="1" failureCount="1" exceptionCount="0" testExceptionCount="0" callExceptionCount="0" />
</suite>
</integrity>

Loading

0 comments on commit 6d025ae

Please sign in to comment.