Author: davsclaus Date: Wed Sep 30 14:15:09 2009 New Revision: 820276 URL: http://svn.apache.org/viewvc?rev=820276&view=rev Log: MR-187: Added more unit tests.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceDummyFallbackConverter.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticDummyFallbackConverter.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticFallbackConverterTest.java (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?rev=820276&r1=820275&r2=820276&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java Wed Sep 30 14:15:09 2009 @@ -164,6 +164,12 @@ // fallback converters for (TypeConverter fallback : fallbackConverters) { Object rc = fallback.convertTo(type, exchange, value); + + if (Void.TYPE.equals(rc)) { + // it cannot be converted so give up + return Void.TYPE; + } + if (rc != null) { // add it as a known type converter since we found a fallback that could do it if (LOG.isDebugEnabled()) { Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java?rev=820276&r1=820275&r2=820276&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/FutureTypeConverter.java Wed Sep 30 14:15:09 2009 @@ -45,6 +45,7 @@ this.converter = converter; } + @SuppressWarnings("unchecked") private <T> T doConvertTo(Class<T> type, Exchange exchange, Object value) throws Exception { // do not convert to stream cache if (StreamCache.class.isAssignableFrom(value.getClass())) { @@ -56,7 +57,8 @@ Future future = (Future) value; if (future.isCancelled()) { - return null; + // return void to indicate its not possible to convert at this time + return (T) Void.TYPE; } // do some trace logging as the get is blocking until the response is ready @@ -79,7 +81,8 @@ } if (body == null) { - return null; + // return void to indicate its not possible to convert at this time + return (T) Void.TYPE; } // maybe from is already the type we want Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java?rev=820276&r1=820275&r2=820276&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/InstanceMethodFallbackTypeConverter.java Wed Sep 30 14:15:09 2009 @@ -59,7 +59,7 @@ throw new RuntimeCamelException("Could not instantiate an instance of: " + type.getCanonicalName()); } return useExchange - ? (T)ObjectHelper.invokeMethod(method, instance, type, value, exchange, registry) : (T)ObjectHelper + ? (T)ObjectHelper.invokeMethod(method, instance, type, exchange, value, registry) : (T)ObjectHelper .invokeMethod(method, instance, type, value, registry); } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java?rev=820276&r1=820275&r2=820276&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/ToStringTypeConverter.java Wed Sep 30 14:15:09 2009 @@ -16,8 +16,11 @@ */ package org.apache.camel.impl.converter; +import java.util.concurrent.Future; + import org.apache.camel.Exchange; import org.apache.camel.TypeConverter; +import org.apache.camel.component.file.GenericFile; /** * A simple converter that can convert any object to a String type by using the @@ -30,6 +33,17 @@ @SuppressWarnings("unchecked") public <T> T convertTo(Class<T> toType, Object value) { if (value != null) { + + // should not try to convert future + if (Future.class.isAssignableFrom(value.getClass())) { + return (T) Void.TYPE; + } + + // should not try to convert files + if (GenericFile.class.isAssignableFrom(value.getClass())) { + return (T) Void.TYPE; + } + if (toType.equals(String.class)) { return (T)value.toString(); } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java?rev=820276&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java Wed Sep 30 14:15:09 2009 @@ -0,0 +1,50 @@ +/** + * 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.camel.converter; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.LoggingLevel; +import org.apache.camel.NoTypeConversionAvailableException; +import org.apache.camel.impl.DefaultExchange; + +/** + * @version $Revision$ + */ +public class EnumConverterTest extends ContextTestSupport { + + public void testMandatoryConvertEnum() throws Exception { + LoggingLevel level = context.getTypeConverter().mandatoryConvertTo(LoggingLevel.class, "DEBUG"); + assertEquals(LoggingLevel.DEBUG, level); + } + + public void testMandatoryConvertWithExchangeEnum() throws Exception { + Exchange exchange = new DefaultExchange(context); + LoggingLevel level = context.getTypeConverter().mandatoryConvertTo(LoggingLevel.class, exchange, "WARN"); + assertEquals(LoggingLevel.WARN, level); + } + + public void testMandatoryConvertFailed() throws Exception { + try { + context.getTypeConverter().mandatoryConvertTo(LoggingLevel.class, "XXX"); + fail("Should have thrown an exception"); + } catch (NoTypeConversionAvailableException e) { + // expected + } + } + +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/EnumConverterTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java?rev=820276&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java Wed Sep 30 14:15:09 2009 @@ -0,0 +1,108 @@ +/** + * 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.camel.converter; + +import java.sql.Timestamp; +import java.util.concurrent.Future; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.NoTypeConversionAvailableException; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultExchange; + +/** + * @version $Revision$ + */ +public class FutureConverterTest extends ContextTestSupport { + + public void testConvertFuture() { + Future future = template.asyncRequestBody("direct:foo", "Hello World"); + + String out = context.getTypeConverter().convertTo(String.class, future); + assertEquals("Bye World", out); + } + + public void testConvertMandatoryFuture() throws Exception { + Future future = template.asyncRequestBody("direct:foo", "Hello World"); + + String out = context.getTypeConverter().mandatoryConvertTo(String.class, future); + assertEquals("Bye World", out); + } + + public void testConvertMandatoryFutureWithExchange() throws Exception { + Exchange exchange = new DefaultExchange(context); + Future future = template.asyncRequestBody("direct:foo", "Hello World"); + + String out = context.getTypeConverter().mandatoryConvertTo(String.class, exchange, future); + assertEquals("Bye World", out); + } + + public void testConvertMandatoryFutureWithExchangeFailed() throws Exception { + Exchange exchange = new DefaultExchange(context); + Future future = template.asyncRequestBody("direct:foo", "Hello World"); + + try { + context.getTypeConverter().mandatoryConvertTo(Timestamp.class, exchange, future); + fail("Should have thrown an exception"); + } catch (NoTypeConversionAvailableException e) { + // expected + } + } + + public void testConvertFutureWithExchangeFailed() throws Exception { + Exchange exchange = new DefaultExchange(context); + Future future = template.asyncRequestBody("direct:foo", "Hello World"); + + Timestamp out = context.getTypeConverter().convertTo(Timestamp.class, exchange, future); + assertNull(out); + } + + public void testConvertFutureCancelled() { + Future future = template.asyncRequestBody("direct:foo", "Hello World"); + future.cancel(true); + + Object out = context.getTypeConverter().convertTo(String.class, future); + // should be null since its cancelled + assertNull(out); + } + + public void testConvertFutureCancelledThenOkay() { + Future future = template.asyncRequestBody("direct:foo", "Hello World"); + future.cancel(true); + + Object out = context.getTypeConverter().convertTo(String.class, future); + // should be null since its cancelled + assertNull(out); + + future = template.asyncRequestBody("direct:foo", "Hello World"); + + out = context.getTypeConverter().convertTo(String.class, future); + // not cancelled so we get the result this time + assertEquals("Bye World", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:foo").delay(500).transform(constant("Bye World")); + } + }; + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/FutureConverterTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceDummyFallbackConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceDummyFallbackConverter.java?rev=820276&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceDummyFallbackConverter.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceDummyFallbackConverter.java Wed Sep 30 14:15:09 2009 @@ -0,0 +1,40 @@ +/** + * 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.camel.converter; + +import java.util.Currency; + +import org.apache.camel.Converter; +import org.apache.camel.Exchange; +import org.apache.camel.FallbackConverter; +import org.apache.camel.spi.TypeConverterRegistry; + +/** + * @version $Revision$ + */ +...@converter +public class InstanceDummyFallbackConverter { + + @FallbackConverter + public Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { + if (Currency.class.isAssignableFrom(value.getClass())) { + return "Money talks"; + } + return null; + } + +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceDummyFallbackConverter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceDummyFallbackConverter.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java?rev=820276&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java Wed Sep 30 14:15:09 2009 @@ -0,0 +1,68 @@ +/** + * 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.camel.converter; + +import java.sql.Date; +import java.sql.Timestamp; +import java.util.Currency; +import java.util.Locale; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.NoTypeConversionAvailableException; +import org.apache.camel.impl.DefaultExchange; + +/** + * @version $Revision$ + */ +public class InstanceFallbackConverterTest extends ContextTestSupport { + + public void testIntanceFallbackConverter() throws Exception { + Exchange exchange = new DefaultExchange(context); + Currency cur = Currency.getInstance(Locale.US); + + String money = context.getTypeConverter().convertTo(String.class, exchange, cur); + assertEquals("Money talks", money); + } + + public void testIntanceFallbackMandatoryConverter() throws Exception { + Exchange exchange = new DefaultExchange(context); + Currency cur = Currency.getInstance(Locale.US); + + String money = context.getTypeConverter().mandatoryConvertTo(String.class, exchange, cur); + assertEquals("Money talks", money); + } + + public void testIntanceFallbackMandatoryFailed() throws Exception { + Exchange exchange = new DefaultExchange(context); + + try { + context.getTypeConverter().mandatoryConvertTo(Date.class, exchange, new Timestamp(0)); + fail("Should have thrown an exception"); + } catch (NoTypeConversionAvailableException e) { + // expected + } + } + + public void testIntanceFallbackFailed() throws Exception { + Exchange exchange = new DefaultExchange(context); + + Date out = context.getTypeConverter().convertTo(Date.class, exchange, new Timestamp(0)); + assertNull(out); + } + +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/InstanceFallbackConverterTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticDummyFallbackConverter.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticDummyFallbackConverter.java?rev=820276&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticDummyFallbackConverter.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticDummyFallbackConverter.java Wed Sep 30 14:15:09 2009 @@ -0,0 +1,40 @@ +/** + * 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.camel.converter; + +import java.util.TimeZone; + +import org.apache.camel.Converter; +import org.apache.camel.Exchange; +import org.apache.camel.FallbackConverter; +import org.apache.camel.spi.TypeConverterRegistry; + +/** + * @version $Revision$ + */ +...@converter +public class StaticDummyFallbackConverter { + + @FallbackConverter + public static Object convertTo(Class<?> type, Exchange exchange, Object value, TypeConverterRegistry registry) { + if (TimeZone.class.isAssignableFrom(value.getClass())) { + return "Time talks"; + } + return null; + } + +} \ No newline at end of file Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticDummyFallbackConverter.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticDummyFallbackConverter.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticFallbackConverterTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticFallbackConverterTest.java?rev=820276&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticFallbackConverterTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticFallbackConverterTest.java Wed Sep 30 14:15:09 2009 @@ -0,0 +1,67 @@ +/** + * 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.camel.converter; + +import java.sql.Date; +import java.sql.Timestamp; +import java.util.TimeZone; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.NoTypeConversionAvailableException; +import org.apache.camel.impl.DefaultExchange; + +/** + * @version $Revision$ + */ +public class StaticFallbackConverterTest extends ContextTestSupport { + + public void testStaticFallbackConverter() throws Exception { + Exchange exchange = new DefaultExchange(context); + TimeZone tz = TimeZone.getDefault(); + + String money = context.getTypeConverter().convertTo(String.class, exchange, tz); + assertEquals("Time talks", money); + } + + public void testStaticFallbackMandatoryConverter() throws Exception { + Exchange exchange = new DefaultExchange(context); + TimeZone tz = TimeZone.getDefault(); + + String money = context.getTypeConverter().mandatoryConvertTo(String.class, exchange, tz); + assertEquals("Time talks", money); + } + + public void testStaticFallbackMandatoryFailed() throws Exception { + Exchange exchange = new DefaultExchange(context); + + try { + context.getTypeConverter().mandatoryConvertTo(Date.class, exchange, new Timestamp(0)); + fail("Should have thrown an exception"); + } catch (NoTypeConversionAvailableException e) { + // expected + } + } + + public void testStaticFallbackFailed() throws Exception { + Exchange exchange = new DefaultExchange(context); + + Date out = context.getTypeConverter().convertTo(Date.class, exchange, new Timestamp(0)); + assertNull(out); + } + +} \ No newline at end of file Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticFallbackConverterTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/converter/StaticFallbackConverterTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date