Skip to content

Commit

Permalink
Fixed #284 Failed to convert value of type java.nio.HeapByteBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Nov 16, 2017
1 parent 0368841 commit 13fa1e2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import java.io.*;
import java.nio.ByteBuffer;
import java.util.*;

/**
Expand Down Expand Up @@ -82,7 +83,7 @@ public static <T> T convertIfNecessary(Object target, Class<T> type) {

Properties props = new Properties();
try {
props.load(new StringReader(mapString.substring(1, mapString.length() - 1).replace(", ", "\n")));
props.load(new StringReader(mapString.substring(1, mapString.length() - 1).replaceAll(",\\s*", "\n")));
} catch (IOException e) {
throw new CitrusRuntimeException("Failed to reconstruct object of type map", e);
}
Expand Down Expand Up @@ -130,6 +131,14 @@ public static <T> T convertIfNecessary(Object target, Class<T> type) {
}
}

if (ByteBuffer.class.isAssignableFrom(target.getClass()) && type.equals(String.class)) {
return (T) new String(((ByteBuffer)target).array());
}

if (byte[].class.isAssignableFrom(target.getClass()) && type.equals(String.class)) {
return (T) new String((byte[]) target);
}

try {
return new SimpleTypeConverter().convertIfNecessary(target, type);
} catch (ConversionNotSupportedException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2006-2017 the original author or authors.
*
* 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.consol.citrus.util;

import org.springframework.xml.transform.StringSource;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.xml.transform.Source;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;

/**
* @author Christoph Deppisch
* @since 2.7
*/
public class TypeConversionUtilsTest {

@Test
public void testConvertIfNecessary() throws Exception {
String payload = "Hello Citrus!";

Assert.assertEquals(TypeConversionUtils.convertIfNecessary("[a,b,c]", String[].class).length, 3);
Assert.assertEquals(TypeConversionUtils.convertIfNecessary("[a, b, c]", String[].class).length, 3);
Assert.assertEquals(TypeConversionUtils.convertIfNecessary("[a,b,c]", List.class).size(), 3);
Assert.assertEquals(TypeConversionUtils.convertIfNecessary("[a, b, c]", List.class).size(), 3);
Assert.assertEquals(TypeConversionUtils.convertIfNecessary("{key=value}", Map.class).get("key"), "value");
Assert.assertEquals(TypeConversionUtils.convertIfNecessary("{key1=value1, key2=value2}", Map.class).get("key2"), "value2");
Assert.assertEquals(TypeConversionUtils.convertIfNecessary("{key1=value1,key2=value2}", Map.class).get("key2"), "value2");
Assert.assertEquals(TypeConversionUtils.convertIfNecessary(payload, InputStream.class).getClass(), ByteArrayInputStream.class);
Assert.assertEquals(TypeConversionUtils.convertIfNecessary(payload, Source.class).getClass(), StringSource.class);
Assert.assertEquals(TypeConversionUtils.convertIfNecessary(payload, byte[].class), payload.getBytes());
Assert.assertEquals(TypeConversionUtils.convertIfNecessary(payload.getBytes(), String.class), payload);
Assert.assertEquals(TypeConversionUtils.convertIfNecessary(ByteBuffer.wrap(payload.getBytes()), String.class), payload);
}

}

0 comments on commit 13fa1e2

Please sign in to comment.