Skip to content

Commit

Permalink
Merge branch 'v0.12.x_bugfix' into v0.12.x
Browse files Browse the repository at this point in the history
  • Loading branch information
S1artie committed May 28, 2014
2 parents de311fe + 8dc5bd8 commit 8e77ef5
Show file tree
Hide file tree
Showing 10 changed files with 542 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,20 @@ public void convertParameterValuesToFixtureDefinedTypes(Method aFixtureMethod, M
tempConvertedValue = tempConvertedValueArray;
} else {
// if the expected type is an array, we don't want to convert to that array, but to the
// component type, of course
// component type, of course...
Class<?> tempConversionTargetType = tempExpectedType.isArray() ? tempExpectedType
.getComponentType() : tempExpectedType;

// ...except for byte arrays (issue #66), those must be treated specially!
boolean tempSpecialByteArrayMode = false;
if (tempExpectedType == byte[].class || tempExpectedType == Byte[].class) {
tempConversionTargetType = tempExpectedType;
tempSpecialByteArrayMode = true;
}

tempConvertedValue = valueConverter.convertValue(tempConversionTargetType, tempValue, null);
if (tempExpectedType.isArray()) {

if (!tempSpecialByteArrayMode && tempExpectedType.isArray()) {
// ...and if the expected type is an array, now we create one
Object tempNewArray = Array.newInstance(tempExpectedType.getComponentType(), 1);
Array.set(tempNewArray, 0, tempConvertedValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -30,6 +31,7 @@
import de.gebit.integrity.dsl.ConstantValue;
import de.gebit.integrity.dsl.CustomOperation;
import de.gebit.integrity.dsl.StandardOperation;
import de.gebit.integrity.dsl.StringValue;
import de.gebit.integrity.dsl.ValueOrEnumValueOrOperation;
import de.gebit.integrity.dsl.ValueOrEnumValueOrOperationCollection;
import de.gebit.integrity.dsl.Variable;
Expand Down Expand Up @@ -224,6 +226,27 @@ protected Object convertPlainValueToTargetType(Class<?> aTargetType, Class<?> aP
}

if (aTargetType != null && aTargetType.isArray()) {
if (String.class.isAssignableFrom(aValue.getClass())
|| StringValue.class.isAssignableFrom(aValue.getClass())) {
// Could be special case of issue #66: single string to byte array
// I admit that this stuff is not beautiful, but it works, and byte arrays should be the only special
// case of this kind so a more decoupled solution like with the normal conversions seems like a bit of
// overkill here.
if (aTargetType.getComponentType() == Byte.class) {
if (String.class.isAssignableFrom(aValue.getClass())) {
return handleConversionOfStringToByteWrapperArray((String) aValue);
} else if (StringValue.class.isAssignableFrom(aValue.getClass())) {
return handleConversionOfStringValueToByteWrapperArray((StringValue) aValue);
}
} else if (aTargetType.getComponentType() == byte.class) {
if (String.class.isAssignableFrom(aValue.getClass())) {
return handleConversionOfStringToByteArray((String) aValue);
} else if (StringValue.class.isAssignableFrom(aValue.getClass())) {
return handleConversionOfStringValueToByteArray((StringValue) aValue);
}
}
}

Class<?> tempActualParamType = aTargetType.getComponentType();
Object tempResultArray;
if (aValue.getClass().isArray()) {
Expand Down Expand Up @@ -518,9 +541,17 @@ protected Object convertEncapsulatedValueCollectionToTargetType(Class<?> aTarget

Class<?> tempTargetType = null;
Class<? extends Collection> tempCollectionType = null;
boolean tempWrapResultIntoArray = false;
if (aTargetType != null) {
if (aTargetType.isArray()) {
tempTargetType = aTargetType.getComponentType();
if ((aTargetType == Byte[].class || aTargetType == byte[].class)) {
// This is a special case (issue #66): byte arrays are to be treated as "non-array" targets.
tempTargetType = aTargetType;
tempWrapResultIntoArray = false;
} else {
tempTargetType = aTargetType.getComponentType();
tempWrapResultIntoArray = true;
}
} else if (List.class.isAssignableFrom(aTargetType)) {
tempCollectionType = ArrayList.class;
} else if (Set.class.isAssignableFrom(aTargetType)) {
Expand Down Expand Up @@ -572,7 +603,7 @@ protected Object convertEncapsulatedValueCollectionToTargetType(Class<?> aTarget
// but we might need to return this as an array with one element
if (aTargetType == null) {
return tempResult;
} else if (aTargetType.isArray()) {
} else if (tempWrapResultIntoArray) {
Object tempResultArray = Array.newInstance(tempTargetArrayType, 1);
Array.set(tempResultArray, 0, tempResult);
return tempResultArray;
Expand Down Expand Up @@ -1167,4 +1198,55 @@ public ConversionContext safeguardConversionContext(ConversionContext aContext)
return aContext;
}
}

/**
* Handles the special case of issue #66: single string to byte array.
*
* @param aSource
* the string to convert
* @return the byte array
*/
protected byte[] handleConversionOfStringToByteArray(String aSource) {
return aSource.getBytes(Charset.defaultCharset());
}

/**
* Handles the special case of issue #66: single string to byte array.
*
* @param aSource
* the string to convert
* @return the byte array (using the wrapper type)
*/
protected Byte[] handleConversionOfStringToByteWrapperArray(String aSource) {
byte[] tempArray = handleConversionOfStringToByteArray(aSource);
Byte[] tempWrapperArray = new Byte[tempArray.length];

for (int i = 0; i < tempArray.length; i++) {
tempWrapperArray[i] = tempArray[i];
}

return tempWrapperArray;
}

/**
* Handles the special case of issue #66: single string to byte array.
*
* @param aSource
* the string to convert (wrapper type)
* @return the byte array
*/
protected byte[] handleConversionOfStringValueToByteArray(StringValue aSource) {
return handleConversionOfStringToByteArray(aSource.getStringValue());
}

/**
* Handles the special case of issue #66: single string to byte array.
*
* @param aSource
* the string to convert (wrapper type)
* @return the byte array (using the wrapper type)
*/
protected Byte[] handleConversionOfStringValueToByteWrapperArray(StringValue aSource) {
return handleConversionOfStringToByteWrapperArray(aSource.getStringValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package de.gebit.integrity.runner.comparator;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -126,10 +127,24 @@ public boolean compareResult(Object aFixtureResult, ValueOrEnumValueOrOperationC
}
return true;
} else {
// If we arrive here, the expected result is a simple, single value. But the fixture might still
// return an array
// If we arrive here, the expected result is a simple, single value.
ValueOrEnumValueOrOperation tempSingleExpectedResult = anExpectedResult.getValue();
Object tempSingleFixtureResult = aFixtureResult;
// if the expected type is an array, we don't want to convert to that array, but to the

// First see if we have the special case of byte arrays (issue #66). Those must be handled
// separately.
if (tempSingleFixtureResult instanceof byte[]) {
byte[] tempConvertedExpectedResult = (byte[]) valueConverter.convertValue(byte[].class,
tempSingleExpectedResult, null);
return Arrays.equals((byte[]) tempSingleFixtureResult, tempConvertedExpectedResult);
} else if (tempSingleFixtureResult instanceof Byte[]) {
Byte[] tempConvertedExpectedResult = (Byte[]) valueConverter.convertValue(Byte[].class,
tempSingleExpectedResult, null);
return Arrays.equals((Byte[]) tempSingleFixtureResult, tempConvertedExpectedResult);
}

// The fixture might still have returned an array.
// If the expected type is an array, we don't want to convert to that array, but to the
// component type, of course
Class<?> tempConversionTargetType = tempSingleFixtureResult.getClass();
if (tempSingleFixtureResult.getClass().isArray()) {
Expand Down Expand Up @@ -158,8 +173,6 @@ public boolean compareResult(Object aFixtureResult, ValueOrEnumValueOrOperationC
}
}

ValueOrEnumValueOrOperation tempSingleExpectedResult = anExpectedResult.getValue();

return convertAndPerformEqualityCheck(tempSingleFixtureResult, tempSingleExpectedResult,
tempConversionTargetType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
packagedef integrity.fixtures.basic.bytearray with

calldef createByteArrayFromStringCall uses de.gebit.integrity.tests.fixtures.basic.ByteArrayConversionTestFixture#createByteArrayFromString
testdef createByteArrayFromStringTest uses de.gebit.integrity.tests.fixtures.basic.ByteArrayConversionTestFixture#createByteArrayFromString

calldef createByteWrapperArrayFromStringCall uses de.gebit.integrity.tests.fixtures.basic.ByteArrayConversionTestFixture#createByteWrapperArrayFromString
testdef createByteWrapperArrayFromStringTest uses de.gebit.integrity.tests.fixtures.basic.ByteArrayConversionTestFixture#createByteWrapperArrayFromString

testdef testByteArray uses de.gebit.integrity.tests.fixtures.basic.ByteArrayConversionTestFixture#testByteArray
testdef testByteWrapperArray uses de.gebit.integrity.tests.fixtures.basic.ByteArrayConversionTestFixture#testByteWrapperArray

testdef testContainer uses de.gebit.integrity.tests.fixtures.basic.ByteArrayConversionTestFixture#testContainer
testdef testWrapperContainer uses de.gebit.integrity.tests.fixtures.basic.ByteArrayConversionTestFixture#testWrapperContainer

packageend
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import integrity.fixtures.basic.bytearray.*

packagedef integrity.basic.bytearray with

suitedef simpleConversionTests with

-- This should succeed
test testByteArray array: "Test"
-- This should fail
test testByteArray array: "Fail"

-- This should succeed
test testByteWrapperArray array: "Test"
-- This should fail
test testByteWrapperArray array: "Fail"

suiteend

suitedef simpleComparationTests with

-- This should succeed
test createByteArrayFromStringTest string: "Test" = "Test"
-- This should fail
test createByteArrayFromStringTest string: "Test" = "Fail"

-- This should succeed
test createByteWrapperArrayFromStringTest string: "Test" = "Test"
-- This should fail
test createByteWrapperArrayFromStringTest string: "Fail" = "Test"

suiteend

suitedef containerConversionTests with

-- This should succeed
test testContainer container: {
array: "Test"
}
-- This should fail
test testContainer container: {
array: "Fail"
}

-- This should succeed
test testWrapperContainer container: {
array: "Test"
}
-- This should fail
test testWrapperContainer container: {
array: "Fail"
}

suiteend

packageend
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*******************************************************************************
* 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.bytearray;

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 ByteArrayConversionTest 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 simpleConversionTests() throws ModelLoadException, IOException, JDOMException {
Document tempResult = executeIntegritySuite(
new String[] { "integrity/suites/basic/bytearray/byteArrayConversionTest.integrity" },
"integrity.basic.bytearray.simpleConversionTests", null);
assertDocumentMatchesReference(tempResult);
}

/**
* 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 simpleComparationTests() throws ModelLoadException, IOException, JDOMException {
Document tempResult = executeIntegritySuite(
new String[] { "integrity/suites/basic/bytearray/byteArrayConversionTest.integrity" },
"integrity.basic.bytearray.simpleComparationTests", null);
assertDocumentMatchesReference(tempResult);
}

/**
* 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 containerConversionTests() throws ModelLoadException, IOException, JDOMException {
Document tempResult = executeIntegritySuite(
new String[] { "integrity/suites/basic/bytearray/byteArrayConversionTest.integrity" },
"integrity.basic.bytearray.containerConversionTests", null);
assertDocumentMatchesReference(tempResult);
}

}
Loading

0 comments on commit 8e77ef5

Please sign in to comment.