https://issues.apache.org/jira/browse/AMQ-4145 IntrospectionSupport does not convert from primitive to Wrapper classes appropriately

Added two tests to show the conversion was not happening properly, and added an identity converter to the TypeConversionSupport to do a pass-through conversion on types that are the same after a primitive to wrapper class conversion

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1404165 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Christian Posta 2012-10-31 14:23:45 +00:00
parent 9a6a83fe0d
commit f7c799309d
3 changed files with 144 additions and 0 deletions

View File

@ -29,6 +29,13 @@ import org.apache.activemq.command.ActiveMQDestination;
*/
public final class TypeConversionSupport {
private static final Converter IDENTITY_CONVERTER = new Converter() {
@Override
public Object convert(Object value) {
return value;
}
};
private static class ConversionKey {
final Class from;
final Class to;
@ -191,6 +198,10 @@ public final class TypeConversionSupport {
to = convertPrimitiveTypeToWrapperType(to);
}
if (from.equals(to)) {
return IDENTITY_CONVERTER;
}
return CONVERSION_MAP.get(new ConversionKey(from, to));
}

View File

@ -0,0 +1,80 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.activemq.transport.tcp;
import junit.framework.TestCase;
import org.apache.activemq.transport.*;
import javax.net.ServerSocketFactory;
import java.net.Socket;
import java.net.URI;
import java.util.HashMap;
/**
* @author <a href="http://www.christianposta.com/blog">Christian Posta</a>
*/
public class TcpTransportServerTest extends TestCase{
public void testDefaultPropertiesSetOnTransport() throws Exception {
TcpTransportServer server = (TcpTransportServer) TransportFactory.bind(new URI("tcp://localhost:61616?trace=true"));
server.setTransportOption(new HashMap<String, Object>());
server.setAcceptListener(new TransportAcceptListener() {
@Override
public void onAccept(Transport transport) {
assertTrue("This transport does not have a TransportLogger!!", hasTransportLogger(transport));
}
@Override
public void onAcceptError(Exception error) {
fail("Should not have received an error!");
}
});
server.start();
Socket socket = new Socket("localhost", 61616);
server.handleSocket(socket);
server.stop();
}
private boolean hasTransportLogger(Transport transport) {
boolean end = false;
Transport current = transport;
while(!end) {
if (current instanceof TransportFilter) {
TransportFilter filter = (TransportFilter) current;
if(filter instanceof TransportLogger){
return true;
}
current = filter.getNext();
}
else {
end = true;
}
}
return false;
}
}

View File

@ -0,0 +1,53 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.activemq.util;
import junit.framework.TestCase;
/**
* @author <a href="http://www.christianposta.com/blog">Christian Posta</a>
*/
public class IntrospectionSupportTest extends TestCase {
class DummyClass {
private boolean trace;
DummyClass(boolean trace) {
this.trace = trace;
}
public boolean isTrace() {
return trace;
}
public void setTrace(boolean trace) {
this.trace = trace;
}
}
public void testSetPropertyPrimitiveWithWrapperValue() {
// Wrapper value
Boolean value = Boolean.valueOf(true);
DummyClass dummyClass = new DummyClass(false);
// dummy field expects a primitive
IntrospectionSupport.setProperty(dummyClass, "trace", value);
assertTrue(dummyClass.isTrace());
}
}