diff --git a/pom.xml b/pom.xml index 8f2d7bfa8..4bf991324 100644 --- a/pom.xml +++ b/pom.xml @@ -36,7 +36,6 @@ spring-cloud-stream spring-cloud-stream-binders spring-cloud-stream-codec - spring-cloud-stream-tuple spring-cloud-stream-starters spring-cloud-stream-rxjava spring-cloud-stream-test-support diff --git a/spring-cloud-stream-binders/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/KafkaTestBinder.java b/spring-cloud-stream-binders/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/KafkaTestBinder.java index 544bedd0c..e4209c5b9 100644 --- a/spring-cloud-stream-binders/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/KafkaTestBinder.java +++ b/spring-cloud-stream-binders/spring-cloud-stream-binder-kafka/src/test/java/org/springframework/cloud/stream/binder/kafka/KafkaTestBinder.java @@ -21,7 +21,6 @@ import java.util.List; import org.springframework.cloud.stream.binder.AbstractTestBinder; import org.springframework.cloud.stream.test.junit.kafka.KafkaTestSupport; import org.springframework.cloud.stream.test.junit.kafka.TestKafkaCluster; -import org.springframework.cloud.stream.tuple.integration.TupleKryoRegistrar; import org.springframework.context.support.GenericApplicationContext; import org.springframework.integration.codec.Codec; import org.springframework.integration.codec.kryo.KryoRegistrar; @@ -29,6 +28,7 @@ import org.springframework.integration.codec.kryo.PojoCodec; import org.springframework.integration.kafka.support.LoggingProducerListener; import org.springframework.integration.kafka.support.ProducerListener; import org.springframework.integration.kafka.support.ZookeeperConnect; +import org.springframework.integration.tuple.TupleKryoRegistrar; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.Registration; diff --git a/spring-cloud-stream-binders/spring-cloud-stream-binder-redis/src/test/java/org/springframework/cloud/stream/binder/redis/AbstractRedisSerializerTests.java b/spring-cloud-stream-binders/spring-cloud-stream-binder-redis/src/test/java/org/springframework/cloud/stream/binder/redis/AbstractRedisSerializerTests.java index 79ff879a5..deb396a43 100644 --- a/spring-cloud-stream-binders/spring-cloud-stream-binder-redis/src/test/java/org/springframework/cloud/stream/binder/redis/AbstractRedisSerializerTests.java +++ b/spring-cloud-stream-binders/spring-cloud-stream-binder-redis/src/test/java/org/springframework/cloud/stream/binder/redis/AbstractRedisSerializerTests.java @@ -31,9 +31,9 @@ import org.joda.time.DateTime; import org.junit.Before; import org.junit.Test; -import org.springframework.cloud.stream.tuple.Tuple; -import org.springframework.cloud.stream.tuple.TupleBuilder; import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.tuple.Tuple; +import org.springframework.tuple.TupleBuilder; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; diff --git a/spring-cloud-stream-binders/spring-cloud-stream-binder-test/src/test/java/org/springframework/cloud/stream/binder/MessageChannelBinderSupportTests.java b/spring-cloud-stream-binders/spring-cloud-stream-binder-test/src/test/java/org/springframework/cloud/stream/binder/MessageChannelBinderSupportTests.java index 6967f7543..4cc2287a8 100644 --- a/spring-cloud-stream-binders/spring-cloud-stream-binder-test/src/test/java/org/springframework/cloud/stream/binder/MessageChannelBinderSupportTests.java +++ b/spring-cloud-stream-binders/spring-cloud-stream-binder-test/src/test/java/org/springframework/cloud/stream/binder/MessageChannelBinderSupportTests.java @@ -29,18 +29,18 @@ import org.junit.Before; import org.junit.Test; import org.springframework.cloud.stream.binder.AbstractBinder.JavaClassMimeTypeConversion; -import org.springframework.cloud.stream.tuple.DefaultTuple; -import org.springframework.cloud.stream.tuple.Tuple; -import org.springframework.cloud.stream.tuple.TupleBuilder; -import org.springframework.cloud.stream.tuple.integration.TupleKryoRegistrar; import org.springframework.integration.codec.kryo.KryoRegistrar; import org.springframework.integration.codec.kryo.PojoCodec; import org.springframework.integration.support.MessageBuilder; +import org.springframework.integration.tuple.TupleKryoRegistrar; import org.springframework.messaging.Message; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.converter.ContentTypeResolver; import org.springframework.messaging.support.GenericMessage; +import org.springframework.tuple.DefaultTuple; +import org.springframework.tuple.Tuple; +import org.springframework.tuple.TupleBuilder; import org.springframework.util.MimeType; import org.springframework.util.MimeTypeUtils; diff --git a/spring-cloud-stream-dependencies/pom.xml b/spring-cloud-stream-dependencies/pom.xml index 48c46f9a2..8d62adf3a 100644 --- a/spring-cloud-stream-dependencies/pom.xml +++ b/spring-cloud-stream-dependencies/pom.xml @@ -15,6 +15,8 @@ 1.0.14 4.2.4.RELEASE + 1.0.0.BUILD-SNAPSHOT + 1.0.0.BUILD-SNAPSHOT @@ -74,9 +76,14 @@ ${project.version} - org.springframework.cloud - spring-cloud-stream-tuple - ${project.version} + org.springframework + spring-tuple + ${spring.tuple.version} + + + org.springframework.integration + spring-integration-tuple + ${spring.integration.tuple.version} org.springframework.cloud diff --git a/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/config/MessageChannelConfigurerTests.java b/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/config/MessageChannelConfigurerTests.java index a618405a9..31182146f 100644 --- a/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/config/MessageChannelConfigurerTests.java +++ b/spring-cloud-stream-integration-tests/src/test/java/org/springframework/cloud/stream/config/MessageChannelConfigurerTests.java @@ -37,7 +37,6 @@ import org.springframework.cloud.stream.binder.redis.config.RedisMessageChannelB import org.springframework.cloud.stream.messaging.Sink; import org.springframework.cloud.stream.messaging.Source; import org.springframework.cloud.stream.test.junit.redis.RedisTestSupport; -import org.springframework.cloud.stream.tuple.Tuple; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.integration.support.MessageBuilder; @@ -45,6 +44,7 @@ import org.springframework.messaging.Message; import org.springframework.messaging.MessageHandler; import org.springframework.messaging.MessagingException; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.tuple.Tuple; /** * @author Ilayaperumal Gopinathan diff --git a/spring-cloud-stream-tuple/pom.xml b/spring-cloud-stream-tuple/pom.xml deleted file mode 100644 index 420279ffd..000000000 --- a/spring-cloud-stream-tuple/pom.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - 4.0.0 - - spring-cloud-stream-tuple - jar - spring-cloud-stream-tuple - Spring Cloud Stream Tuple - - - org.springframework.cloud - spring-cloud-stream-parent - 1.0.0.BUILD-SNAPSHOT - - - - - com.esotericsoftware - kryo-shaded - - - org.springframework - spring-core - - - com.fasterxml.jackson.core - jackson-databind - - - org.springframework - spring-context - - - org.springframework.integration - spring-integration-core - - - org.springframework.boot - spring-boot-starter-logging - - - org.springframework.boot - spring-boot-starter-test - test - - - diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultMutableTuple.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultMutableTuple.java deleted file mode 100644 index 69389a703..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultMutableTuple.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright 2016 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 org.springframework.cloud.stream.tuple; - -import java.util.List; - -import org.springframework.core.convert.support.ConfigurableConversionService; - -/** - * Extension of {@link DefaultTuple} that also allows tuple {@link MutableTuple mutation}. - * - *

Note that this implementation is not threadsafe.

- * - * @author Eric Bottard - */ -public class DefaultMutableTuple extends DefaultTuple implements MutableTuple { - - /** - * Construct a new DefaultMutableTuple, given a list of names and object values along - * with a conversion service. - * @param names The list of String names to associate with the list of Object values - * @param values The list of Object values to associate with the list of String names - * @param configurableConversionService A conversion service instance that can Converting strings to Java types. - */ - public DefaultMutableTuple(List names, List values, ConfigurableConversionService configurableConversionService) { - super(names, values, configurableConversionService); - } - - @Override - public void setValue(int index, Object value) { - if (index < 0 || index >= size()) { - throw new IndexOutOfBoundsException(); - } - values.set(index, value); - } - - @Override - public void setValue(String name, Object value) { - int index = indexOf(name); - if (index != -1) { - setValue(index, value); - } else { - names.add(name); - values.add(value); - } - } - - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultTuple.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultTuple.java deleted file mode 100644 index 8b479a76b..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultTuple.java +++ /dev/null @@ -1,628 +0,0 @@ -/* - * Copyright 2013-2016 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 org.springframework.cloud.stream.tuple; - -import java.math.BigDecimal; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import org.springframework.core.convert.TypeDescriptor; -import org.springframework.core.convert.converter.Converter; -import org.springframework.core.convert.support.ConfigurableConversionService; -import org.springframework.expression.EvaluationContext; -import org.springframework.expression.Expression; -import org.springframework.expression.ExpressionParser; -import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.expression.spel.support.StandardEvaluationContext; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; - -/** - * Default implementation of Tuple interface. - * - * @author Mark Pollack - * @author David Turanski - * @author Michael Minella - */ -public class DefaultTuple implements Tuple { - - protected List names; - - protected List values; - - protected transient ConfigurableConversionService configurableConversionService; - - protected transient Converter tupleToStringConverter = new TupleToJsonStringConverter(); - - /** - * Construct a new DefaultMutableTuple, given a list of names and object values along - * with a conversion service. - * - * @param names The list of names to associate with the list of values - * @param values The list of values to associate with the list of names - * @param configurableConversionService A conversion service instance that can Converting strings to Java types. - */ - public DefaultTuple(List names, List values, ConfigurableConversionService - configurableConversionService) { - Assert.notNull(names); - Assert.notNull(values); - Assert.notNull(configurableConversionService); - if (values.size() != names.size()) { - throw new IllegalArgumentException("Field names must be same length as values: names=" + names - + ", values=" + values); - } - this.names = new ArrayList<>(names); - this.values = new ArrayList<>(values); // shallow copy - this.configurableConversionService = configurableConversionService; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.cloud.stream.tuple.Tuple#size() - */ - @Override - public int size() { - return values.size(); - } - - /** - * Return the values for all the fields in this tuple - * @return an unmodifiable List of names. - */ - @Override - public List getFieldNames() { - return Collections.unmodifiableList(names); - } - - /** - * Return the values for all the fields in this tuple - * @return an unmodifiable List list of values. - */ - @Override - public List getValues() { - return Collections.unmodifiableList(values); - } - - @Override - public int getFieldCount() { - return this.names.size(); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.cloud.stream.tuple.Tuple#hasName(java.lang.String) - */ - @Override - public boolean hasFieldName(String name) { - return names.contains(name); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.cloud.stream.tuple.Tuple#getValue(java.lang.String) - */ - @Override - public Object getValue(String name) { - int index = indexOf(name); - if (index == -1) { - throw new IllegalArgumentException("Field name [" + name + "] does not exist"); - } - return getValue(index); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.cloud.stream.tuple.Tuple#getValue(int) - */ - @Override - public Object getValue(int index) { - return values.get(index); - } - - @SuppressWarnings("rawtypes") - @Override - public List getFieldTypes() { - ArrayList types = new ArrayList<>(values.size()); - for (Object val : values) { - types.add(val.getClass()); - } - return Collections.unmodifiableList(types); - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((names == null) ? 0 : names.hashCode()); - result = prime * result + ((values == null) ? 0 : values.hashCode()); - return result; - } - - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (!(obj instanceof DefaultTuple)) { - return false; - } - DefaultTuple other = (DefaultTuple) obj; - if (names == null) { - if (other.names != null) { - return false; - } - } - else if (!names.equals(other.names)) { - return false; - } - if (values == null) { - if (other.values != null) { - return false; - } - } - else if (!values.equals(other.values)) { - return false; - } - return true; - } - - @Override - public String getString(String name) { - int index = indexOf(name); - return (index == -1) ? null : getString(index); - } - - @Override - public String getString(int index) { - return readAndTrim(index); - } - - @Override - public Tuple getTuple(int index) { - return convert(values.get(index), Tuple.class); - } - - @Override - public Tuple getTuple(String name) { - return getTuple(indexOf(name)); - } - - /** - * @param index the index of the value - * @return the converted raw value, trimmed - */ - private String readAndTrim(int index) { - Object rawValue = values.get(index); - if (rawValue != null) { - String value = convert(rawValue, String.class); - if (value != null) { - return value.trim(); - } - else { - return null; - } - } - else { - return null; - } - } - - @Override - public String getRawString(String name) { - int index = indexOf(name); - return (index == -1) ? null : getRawString(index); - } - - @Override - public String getRawString(int index) { - Object rawValue = values.get(index); - if (rawValue != null) { - String value = convert(rawValue, String.class); - if (value != null) { - return value; - } - else { - return null; - } - } - else { - return null; - } - } - - @Override - public char getChar(int index) { - String value = readAndTrim(index); - if (value != null) { - Assert.isTrue(value.length() == 1, "Cannot convert field value '" + value + "' to char."); - return value.charAt(0); - } - return '\u0000'; - - } - - @Override - public char getChar(String name) { - return getChar(indexOf(name)); - } - - @Override - public boolean getBoolean(int index) { - return getBoolean(index, "true"); - } - - @Override - public boolean getBoolean(String name) { - return getBoolean(indexOf(name)); - } - - @Override - public boolean getBoolean(int index, String trueValue) { - Assert.notNull(trueValue, "'trueValue' cannot be null."); - String value = readAndTrim(index); - return trueValue.equals(value); - - } - - @Override - public boolean getBoolean(String name, String trueValue) { - return getBoolean(indexOf(name), trueValue); - } - - @Override - public byte getByte(String name) { - int index = indexOf(name); - return (index == -1) ? 0 : getByte(index); - } - - @Override - public byte getByte(int index) { - Byte b = convert(values.get(index), Byte.class); - return (b != null) ? b : 0; - } - - @Override - public byte getByte(String name, byte defaultValue) { - int index = indexOf(name); - return (index == -1) ? defaultValue : getByte(index, defaultValue); - } - - @Override - public byte getByte(int index, byte defaultValue) { - Byte b = convert(values.get(index), Byte.class); - return (b != null) ? b : defaultValue; - } - - @Override - public short getShort(String name) { - int index = indexOf(name); - return (index == -1) ? 0 : getShort(index); - } - - @Override - public short getShort(int index) { - Short s = convert(values.get(index), Short.class); - return (s != null) ? s : 0; - } - - @Override - public short getShort(String name, short defaultValue) { - int index = indexOf(name); - return (index == -1) ? defaultValue : getShort(index, defaultValue); - } - - @Override - public short getShort(int index, short defaultValue) { - Short s = convert(values.get(index), Short.class); - return (s != null) ? s : defaultValue; - } - - @Override - public int getInt(String name) { - int index = indexOf(name); - return (index == -1) ? 0 : getInt(index); - } - - @Override - public int getInt(int index) { - Integer i = convert(values.get(index), Integer.class); - return (i != null) ? i : 0; - } - - @Override - public int getInt(String name, int defaultValue) { - int index = indexOf(name); - return (index == -1) ? defaultValue : getInt(index, defaultValue); - } - - @Override - public int getInt(int index, int defaultValue) { - Integer i = convert(values.get(index), Integer.class); - return (i != null) ? i : defaultValue; - } - - @Override - public long getLong(String name) { - int index = indexOf(name); - return (index == -1) ? 0 : getLong(index); - } - - @Override - public long getLong(int index) { - Long l = convert(values.get(index), Long.class); - return (l != null) ? l : 0; - } - - @Override - public long getLong(String name, long defaultValue) { - int index = indexOf(name); - return (index == -1) ? defaultValue : getLong(index, defaultValue); - } - - @Override - public long getLong(int index, long defaultValue) { - Long l = convert(values.get(index), Long.class); - return (l != null) ? l : defaultValue; - } - - @Override - public float getFloat(String name) { - int index = indexOf(name); - return (index == -1) ? 0 : getFloat(index); - } - - @Override - public float getFloat(int index) { - Float f = convert(values.get(index), Float.class); - return (f != null) ? f : 0; - } - - @Override - public float getFloat(String name, float defaultValue) { - int index = indexOf(name); - return (index == -1) ? defaultValue : getFloat(index, defaultValue); - } - - @Override - public float getFloat(int index, float defaultValue) { - Float f = convert(values.get(index), Float.class); - return (f != null) ? f : defaultValue; - } - - @Override - public double getDouble(String name) { - int index = indexOf(name); - return (index == -1) ? 0 : getDouble(index); - } - - @Override - public double getDouble(int index) { - Double d = convert(values.get(index), Double.class); - return (d != null) ? d : 0; - } - - @Override - public double getDouble(String name, double defaultValue) { - int index = indexOf(name); - return (index == -1) ? defaultValue : getDouble(index, defaultValue); - } - - @Override - public double getDouble(int index, double defaultValue) { - Double d = convert(values.get(index), Double.class); - return (d != null) ? d : defaultValue; - } - - @Override - public BigDecimal getBigDecimal(String name) { - return getBigDecimal(indexOf(name)); - } - - @Override - public BigDecimal getBigDecimal(int index) { - return convert(values.get(index), BigDecimal.class); - } - - @Override - public BigDecimal getBigDecimal(String name, BigDecimal defaultValue) { - int index = indexOf(name); - return (index == -1) ? defaultValue : getBigDecimal(index, defaultValue); - } - - @Override - public BigDecimal getBigDecimal(int index, BigDecimal defaultValue) { - BigDecimal bd = convert(values.get(index), BigDecimal.class); - return (bd != null) ? bd : defaultValue; - } - - @Override - public Date getDate(int index) { - return convert(values.get(index), Date.class); - } - - @Override - public Date getDate(String name) { - return getDate(indexOf(name)); - } - - @Override - public Date getDate(String name, Date defaultValue) { - int index = indexOf(name); - return (index == -1) ? defaultValue : getDate(index, defaultValue); - } - - @Override - public Date getDate(int index, Date defaultValue) { - Date d = getDate(index); - return (d != null) ? d : defaultValue; - } - - @Override - public Date getDateWithPattern(int index, String pattern) { - StringToDateConverter converter = new StringToDateConverter(pattern); - return converter.convert(this.readAndTrim(index)); - } - - @Override - public Date getDateWithPattern(String name, String pattern) { - try { - return getDateWithPattern(indexOf(name), pattern); - } - catch (IllegalArgumentException e) { - throw new IllegalArgumentException(e.getMessage() + ", name: [" + name + "]"); - } - } - - @Override - public Date getDateWithPattern(int index, String pattern, Date defaultValue) { - try { - Date d = getDateWithPattern(index, pattern); - return (d != null) ? d : defaultValue; - } - catch (IllegalArgumentException e) { - return defaultValue; - } - - } - - @Override - public Date getDateWithPattern(String name, String pattern, Date defaultValue) { - try { - return getDateWithPattern(indexOf(name), pattern, defaultValue); - } - catch (IllegalArgumentException e) { - throw new IllegalArgumentException(e.getMessage() + ", name: [" + name + "]"); - } - } - - @Override - public T getValue(String name, Class valueClass) { - Object value = values.get(indexOf(name)); - return convert(value, valueClass); - } - - @Override - public T getValue(int index, Class valueClass) { - return convert(values.get(index), valueClass); - } - - @SuppressWarnings("unchecked") - @Override - public Tuple select(String expression) { - EvaluationContext context = new StandardEvaluationContext(toMap()); - ExpressionParser parser = new SpelExpressionParser(); - Expression exp = parser.parseExpression(expression); - - Object result = exp.getValue(context); - Map resultMap = null; - if (ClassUtils.isAssignableValue(Map.class, result)) { - resultMap = (Map) result; - } - if (resultMap != null) { - return toTuple(resultMap); - } - else { - return new DefaultTuple(new ArrayList(0), new ArrayList<>(0), - this.configurableConversionService); - } - } - - /** - * @return names and values as a {@code Map} - */ - Map toMap() { - Map map = new LinkedHashMap<>(values.size()); - for (int i = 0; i < values.size(); i++) { - map.put(names.get(i), values.get(i)); - } - return map; - } - - Tuple toTuple(Map resultMap) { - - List newNames = new ArrayList<>(); - List newValues = new ArrayList<>(); - for (String name : resultMap.keySet()) { - newNames.add(name); - } - for (Object value : resultMap.values()) { - newValues.add(value); - } - return new DefaultTuple(newNames, newValues, this.configurableConversionService); - - } - - @SuppressWarnings("unchecked") - T convert(Object value, Class targetType) { - return (T) configurableConversionService.convert(value, TypeDescriptor.forObject(value), - TypeDescriptor.valueOf(targetType)); - } - - /** - * Find the index in the names collection for the given name. - * Returns -1 if not found. - */ - protected int indexOf(String name) { - return names.indexOf(name); - } - - /** - * @param tupleToStringConverter used to convert a {@link Tuple} to a String - */ - protected void setTupleToStringConverter(Converter tupleToStringConverter) { - Assert.notNull(tupleToStringConverter, "tupleToStringConverter cannot be null"); - this.tupleToStringConverter = tupleToStringConverter; - } - - /** - * Returns a JSON representation of this Tuple. - */ - @Override - public String toString() { - if (tupleToStringConverter == null) { - return new TupleToJsonStringConverter().convert(this); - } - return tupleToStringConverter.convert(this); - } - - - public ConfigurableConversionService getConversionService() { - return this.configurableConversionService; - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultTupleConversionService.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultTupleConversionService.java deleted file mode 100644 index aa8363dff..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/DefaultTupleConversionService.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2002-2013 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 org.springframework.cloud.stream.tuple; - -import java.util.Collection; - -import org.springframework.core.convert.ConversionService; -import org.springframework.format.support.DefaultFormattingConversionService; - -/** - * Base {@link ConversionService} implementation suitable for use with {@link Tuple} - * - * @author David Turanski - * - */ -public class DefaultTupleConversionService extends DefaultFormattingConversionService { - - public DefaultTupleConversionService() { - /* - * DefaultFormattingConversionService provides Collection -> Object conversion which will produce the first item - * if the target type matches. Here, this results in an unfortunate side effect, getTuple(List list) - * would return a Tuple. In this case it is preferable to treat it as an error if the argument is not a Tuple. - */ - removeConvertible(Collection.class, Object.class); - } -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonBytesToTupleConverter.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonBytesToTupleConverter.java deleted file mode 100644 index acb603d28..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonBytesToTupleConverter.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 2013 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 org.springframework.cloud.stream.tuple; - -import org.springframework.core.convert.converter.Converter; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * @author David Turanski - * - */ -public class JsonBytesToTupleConverter implements Converter { - - private final ObjectMapper mapper = new ObjectMapper(); - - private final JsonNodeToTupleConverter jsonNodeToTupleConverter = new JsonNodeToTupleConverter(); - - public JsonBytesToTupleConverter() { - mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); - } - - @Override - public Tuple convert(byte[] source) { - if (source == null) { - return null; - } - try { - return jsonNodeToTupleConverter.convert(mapper.readTree(source)); - } - catch (Exception e) { - throw new IllegalArgumentException(e.getMessage(), e); - } - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonNodeToTupleConverter.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonNodeToTupleConverter.java deleted file mode 100644 index a083e0fb0..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonNodeToTupleConverter.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2013-2015 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 org.springframework.cloud.stream.tuple; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map.Entry; - -import org.springframework.core.convert.converter.Converter; - -import com.fasterxml.jackson.databind.JsonNode; - -/** - * @author David Turanski - */ -public class JsonNodeToTupleConverter implements Converter { - - @Override - public Tuple convert(JsonNode root) { - TupleBuilder builder = TupleBuilder.tuple(); - if (root.isValueNode()) { - return builder.of("value", root.asText()); - } - try { - for (Iterator> it = root.fields(); it.hasNext();) { - Entry entry = it.next(); - String name = entry.getKey(); - JsonNode node = entry.getValue(); - if (node.isObject()) { - // tuple - builder.addEntry(name, convert(node)); - } - else if (node.isArray()) { - builder.addEntry(name, nodeToList(node)); - } - else if (node.isNull()) { - builder.addEntry(name, null); - } - else if (node.isBoolean()) { - builder.addEntry(name, node.booleanValue()); - } - else if (node.isNumber()) { - builder.addEntry(name, node.numberValue()); - } - else { - builder.addEntry(name, node.asText()); - } - } - } - catch (Exception e) { - throw new RuntimeException(e); - } - return builder.build(); - } - - private List nodeToList(JsonNode node) { - List list = new ArrayList(node.size()); - for (int i = 0; i < node.size(); i++) { - JsonNode item = node.get(i); - if (item.isObject()) { - list.add(convert(item)); - } - else if (item.isArray()) { - list.add(nodeToList(item)); - } - else if (item.isNull()) { - list.add(null); - } - else if (item.isBoolean()) { - list.add(item.booleanValue()); - } - else if (item.isNumber()) { - list.add(item.numberValue()); - } - else { - list.add(item.asText()); - } - } - return list; - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonStringToTupleConverter.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonStringToTupleConverter.java deleted file mode 100644 index 0e7af4fc3..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/JsonStringToTupleConverter.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2002-2015 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 org.springframework.cloud.stream.tuple; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Map.Entry; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -import org.springframework.core.convert.converter.Converter; - -/** - * @author David Turanski - */ -public class JsonStringToTupleConverter implements Converter { - - private final ObjectMapper mapper = new ObjectMapper(); - - public JsonStringToTupleConverter() { - mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); - } - - @Override - public Tuple convert(String source) { - TupleBuilder builder = TupleBuilder.tuple(); - try { - - JsonNode root = mapper.readTree(source); - for (Iterator> it = root.fields(); it.hasNext();) { - Entry entry = it.next(); - String name = entry.getKey(); - JsonNode node = entry.getValue(); - if (node.isObject()) { - // tuple - builder.addEntry(name, convert(node.toString())); - } - else if (node.isArray()) { - builder.addEntry(name, nodeToList(node)); - } - else if (node.isNull()) { - builder.addEntry(name, null); - } - else if (node.isBoolean()) { - builder.addEntry(name, node.booleanValue()); - } - else if (node.isNumber()) { - builder.addEntry(name, node.numberValue()); - } - else { - builder.addEntry(name, mapper.treeToValue(node, Object.class)); - } - } - } - catch (Exception e) { - throw new RuntimeException(e); - } - return builder.build(); - } - - private List nodeToList(JsonNode node) throws JsonProcessingException { - List list = new ArrayList(node.size()); - for (int i = 0; i < node.size(); i++) { - JsonNode item = node.get(i); - if (item.isObject()) { - list.add(convert(item.toString())); - } - else if (item.isArray()) { - list.add(nodeToList(item)); - } - else if (item.isNull()) { - list.add(null); - } - else if (item.isBoolean()) { - list.add(item.booleanValue()); - } - else if (item.isNumber()) { - list.add(item.numberValue()); - } - else { - list.add(mapper.treeToValue(item, Object.class)); - } - } - return list; - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/LocaleAwareStringToNumberConverterFactory.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/LocaleAwareStringToNumberConverterFactory.java deleted file mode 100644 index e5fa8fdd8..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/LocaleAwareStringToNumberConverterFactory.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2013 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 org.springframework.cloud.stream.tuple; - -import java.text.NumberFormat; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.core.convert.converter.ConverterFactory; -import org.springframework.util.NumberUtils; - -/** - * Converts from a String any JDK-standard Number implementation. - * - *

- * Support Number classes including Byte, Short, Integer, Float, Double, Long, BigInteger, BigDecimal. This class - * delegates to {@link NumberUtils#parseNumber(String, Class, NumberFormat)} to perform the conversion. - * - * @author Mark Pollack - * @author Keith Donald - * - * @see Byte - * @see Short - * @see Integer - * @see Long - * @see java.math.BigInteger - * @see Float - * @see Double - * @see java.math.BigDecimal - * @see NumberUtils - */ -public class LocaleAwareStringToNumberConverterFactory implements ConverterFactory { - - private NumberFormat numberFormat; - - public LocaleAwareStringToNumberConverterFactory(NumberFormat numberFormat) { - this.numberFormat = numberFormat; - } - - @Override - public Converter getConverter(Class targetType) { - return new StringToNumber(targetType, numberFormat); - } - - private static final class StringToNumber implements Converter { - - private final Class targetType; - - private NumberFormat numberFormat; - - public StringToNumber(Class targetType, NumberFormat numberFormat) { - this.targetType = targetType; - this.numberFormat = numberFormat; - } - - @Override - public T convert(String source) { - return NumberUtils.parseNumber(source, this.targetType, numberFormat); - } - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/MutableTuple.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/MutableTuple.java deleted file mode 100644 index eacfcfff2..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/MutableTuple.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2016 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 org.springframework.cloud.stream.tuple; - -/** - * Extension of {@link Tuple} that allows mutating and addition of fields. - * - * @author Eric Bottard - */ -public interface MutableTuple extends Tuple { - - /** - * Sets the value of an already existing property, given its index. - */ - void setValue(int index, Object value); - - /** - * Sets the value of a property, by name. If this Tuple does not currently hold - * a property under that name, a new mapping is added at the end and the size of this - * Tuple is extended by 1. - */ - void setValue(String name, Object value); - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/StringToDateConverter.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/StringToDateConverter.java deleted file mode 100644 index 76550a6dc..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/StringToDateConverter.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2013 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 org.springframework.cloud.stream.tuple; - -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - -import org.springframework.core.convert.converter.Converter; - -/** - * Converter for Strings to Date that can take into account date patterns - * - * @author Mark Pollack - * - */ -public class StringToDateConverter implements Converter { - - private final static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd"; - - private DateFormat dateFormat; - - public StringToDateConverter() { - this.dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN); - this.dateFormat.setLenient(false); - - } - - public StringToDateConverter(String pattern) { - this.dateFormat = new SimpleDateFormat(pattern); - this.dateFormat.setLenient(false); - } - - public StringToDateConverter(DateFormat dateFormat) { - this.dateFormat = dateFormat; - } - - @Override - public Date convert(String source) { - try { - return dateFormat.parse(source); - } - catch (ParseException e) { - String pattern; - if (dateFormat instanceof SimpleDateFormat) { - pattern = ((SimpleDateFormat) dateFormat).toPattern(); - } - else { - pattern = dateFormat.toString(); - } - throw new IllegalArgumentException(e.getMessage() + ", format: [" + pattern + "]"); - } - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/Tuple.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/Tuple.java deleted file mode 100644 index cd019b550..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/Tuple.java +++ /dev/null @@ -1,559 +0,0 @@ -/* - * Copyright 2013-2016 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 org.springframework.cloud.stream.tuple; - -import java.math.BigDecimal; -import java.util.Date; -import java.util.List; - -import org.springframework.core.convert.ConversionFailedException; - -/** - * Data structure that stores a fixed number of ordered key-value pairs and adds convenience methods to access values in - * a type-safe manner. - * - * Values do not need to be of the same type. When accessing values, - * Spring's type conversion system is used to convert the value type to the requested type. The type conversion system - * is extensible. - * - * This interface only allows querying properties. Concrete Tuples can also implement {@link MutableTuple} to allow - * mutation. Tuples are created using the TupleBuilder class. - * - * @author Mark Pollack - * - */ -public interface Tuple { - - /** - * Return the number of elements in this tuple. - * - * @return number of elements - */ - int size(); - - /** - * Return the fields names that can reference elements by name in this tuple - * - * @return list of field names - */ - List getFieldNames(); - - /** - * Return the values for all the fields in this tuple - * - * @return list of values. - */ - List getValues(); - - /** - * Return true if the tuple contains a field of the given name - * - * @param name the name of the field - * @return true if present, otherwise false - */ - boolean hasFieldName(String name); - - /** - * Return the Java types of the fields in this tuple. - * - * @return the Java types of the fields in this tuple. - */ - @SuppressWarnings("rawtypes") - List getFieldTypes(); - - /** - * Return the number of fields in this tuple. - * - * @return the number of fields in this tuple. - */ - int getFieldCount(); - - /** - * Return the value of the field given the name - * - * @param name the name of the field - * @return value of the field - * @throws IllegalArgumentException if the name is not present - */ - Object getValue(String name); - - /** - * Return the value of the field given the index position - * - * @param index position in the tuple - * @return value of the field - * @throws IndexOutOfBoundsException if the index position is out of bounds. - */ - Object getValue(int index); - - /** - * Return the value of the field given the name - * - * @param name the field name - * @param valueClass Class to coerce the value into. - * @return value of the field - */ - T getValue(String name, Class valueClass); - - /** - * Return the value of the field given the index position - * - * @param index position in the tuple - * @param valueClass Class to coerce the value into - * @return value of the field - */ - T getValue(int index, Class valueClass); - - /** - * Read the {@link String} value given the field 'name'. - * - * @param name the field name. - * @return value of the field - */ - String getString(String name); - - /** - * Read the String value given the index position - * - * @param index position in the tuple - * @return value of the field - */ - String getString(int index); - - /** - * Read the {@link Tuple} value given the field 'name'. - * - * @param name the field name. - * @return value of the field - */ - Tuple getTuple(String name); - - /** - * Read the Tuple value given the index position - * - * @param index position in the tuple - * @return value of the field - */ - Tuple getTuple(int index); - - /** - * Read the {@link String} value at index 'index' including trailing whitespace (don't trim). - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - String getRawString(int index); - - /** - * Read the {@link String} value from column with given 'name' including trailing whitespace (don't - * trim). - * - * @param name the field name. - */ - String getRawString(String name); - - /** - * Read the 'char' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - char getChar(int index); - - - /** - * Read the 'char' value from field with given 'name'. - * - * @param name the field name. - * @throws IllegalArgumentException if a field with given name is not defined. - */ - char getChar(String name); - - /** - * Read the 'boolean' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - boolean getBoolean(int index); - - /** - * Read the 'boolean' value from field with given 'name'. - * - * @param name the field name. - * @throws IllegalArgumentException if a field with given name is not defined. - */ - boolean getBoolean(String name); - - /** - * Read the 'boolean' value at index 'index'. - * - * @param index the field index. - * @param trueValue the value that signifies {@link Boolean#TRUE true}; case-sensitive. - * @throws IndexOutOfBoundsException if the index is out of bounds, or if the supplied trueValue is - * null. - */ - boolean getBoolean(int index, String trueValue); - - /** - * Read the 'boolean' value from column with given 'name'. - * - * @param name the field name. - * @param trueValue the value that signifies {@link Boolean#TRUE true}; case-sensitive. - * @throws IllegalArgumentException if a column with given name is not defined, or if the supplied - * trueValue is null. - */ - boolean getBoolean(String name, String trueValue); - - /** - * Read the 'byte' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - byte getByte(int index); - - /** - * Read the 'byte' value from column with given 'name'. - * - * @param name the field name. - */ - byte getByte(String name); - - - /** - * Read the 'byte' value at index 'index'. using the supplied defaultValue if - * the field value is a zero length string or null. - * - * @param index the field index. - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - byte getByte(int index, byte defaultValue); - - /** - * Read the 'byte' value from column with given 'name'. using the supplied - * defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param defaultValue the default value to return if field value is not found. - */ - byte getByte(String name, byte defaultValue); - - /** - * Read the 'short' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - short getShort(int index); - - /** - * Read the 'short' value from column with given 'name'. - * - * @param name the field name. - */ - short getShort(String name); - - /** - * Read the 'short' value at index 'index'. using the supplied defaultValue - * if the field value is a zero length string or null. - * - * @param index the field index. - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - short getShort(int index, short defaultValue); - - /** - * Read the 'short' value from column with given 'name'. using the supplied - * defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param defaultValue the default value to return if field value is not found. - */ - short getShort(String name, short defaultValue); - - /** - * Read the 'int' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - int getInt(int index); - - /** - * Read the 'int' value from column with given 'name'. - * - * @param name the field name. - */ - int getInt(String name); - - /** - * Read the 'int' value at index 'index'. using the supplied defaultValue if - * the field value is a zero length string or null. - * - * @param index the field index. - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - int getInt(int index, int defaultValue); - - /** - * Read the 'int' value from column with given 'name'. using the supplied - * defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param defaultValue the default value to return if field value is not found. - */ - int getInt(String name, int defaultValue); - - /** - * Read the 'long' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - long getLong(int index); - - /** - * Read the 'int' value from column with given 'name'. - * - * @param name the field name. - */ - long getLong(String name); - - /** - * Read the 'long' value at index 'index'. using the supplied defaultValue if - * the field value is a zero length string or null. - * - * @param index the field index. - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - long getLong(int index, long defaultValue); - - /** - * Read the 'long' value from column with given 'name'. using the supplied - * defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param defaultValue the default value to return if field value is not found. - */ - long getLong(String name, long defaultValue); - - /** - * Read the 'float' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - float getFloat(int index); - - /** - * Read the 'float' value from column with given 'name'. - * - * @param name the field name. - */ - float getFloat(String name); - - /** - * Read the 'float' value at index 'index'. using the supplied defaultValue - * if the field value is a zero length string or null. - * - * @param index the field index. - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - float getFloat(int index, float defaultValue); - - /** - * Read the 'float' value from column with given 'name'. using the supplied - * defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param defaultValue the default value to return if field value is not found. - */ - float getFloat(String name, float defaultValue); - - - /** - * Read the 'double' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - double getDouble(int index); - - /** - * Read the 'double' value from column with given 'name'. - * - * @param name the field name. - */ - double getDouble(String name); - - - /** - * Read the 'double' value at index 'index'. using the supplied defaultValue - * if the field value is a zero length string or null. - * - * @param index the field index. - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - double getDouble(int index, double defaultValue); - - /** - * Read the 'double' value from column with given 'name'. using the supplied - * defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param defaultValue the default value to return if field value is not found. - */ - double getDouble(String name, double defaultValue); - - /** - * Read the 'BigDecimal' value at index 'index'. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - BigDecimal getBigDecimal(int index); - - /** - * Read the 'BigDecimal' value from column with given 'name'. - * - * @param name the field name. - */ - BigDecimal getBigDecimal(String name); - - /** - * Read the 'BigDecimal' value at index 'index'. using the supplied - * defaultValue if the field value is a zero length string or null. - * - * @param index the field index. - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - */ - BigDecimal getBigDecimal(int index, BigDecimal defaultValue); - - /** - * Read the 'BigDecimal' value from column with given 'name'. using the supplied - * defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param defaultValue the default value to return if field value is not found. - */ - BigDecimal getBigDecimal(String name, BigDecimal defaultValue); - - /** - * Read the java.util.Date value in default format at designated column index. - * - * @param index the field index. - * @throws IndexOutOfBoundsException if the index is out of bounds. - * @throws ConversionFailedException if the value is not parseable - */ - Date getDate(int index); - - /** - * Read the java.util.Date value in default format at designated column with given name. - * - * @param name the field name. - * @throws IllegalArgumentException if a column with given name is not defined - * @throws ConversionFailedException if the value is not parseable - */ - Date getDate(String name); - - /** - * Read the java.util.Date value in default format at designated column index using the - * supplied defaultValue if the field value is a zero length string or null. - * - * @param index the field index. - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - * @throws ConversionFailedException if the value is not parseable - */ - Date getDate(int index, Date defaultValue); - - /** - * Read the java.util.Date value in default format at designated column with given name. - * using the supplied defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param defaultValue the default value to return if field value is not found. - * @throws IllegalArgumentException if a column with given name is not defined - * @throws ConversionFailedException if the value is not parseable - */ - Date getDate(String name, Date defaultValue); - - /** - * Read the java.util.Date value in default format at designated column index. - * - * @param index the field index. - * @param pattern the pattern describing the date and time format - * @throws IndexOutOfBoundsException if the index is out of bounds. - * @throws IllegalArgumentException if the date cannot be parsed. - * - */ - Date getDateWithPattern(int index, String pattern); - - /** - * Read the java.util.Date value in given format from column with given name. - * - * @param name the field name. - * @param pattern the pattern describing the date and time format - * @throws IllegalArgumentException if a column with given name is not defined or if the specified field cannot be - * parsed - * - */ - Date getDateWithPattern(String name, String pattern); - - /** - * Read the java.util.Date value in default format at designated column index. using the - * supplied defaultValue if the field value is a zero length string or null. - * - * @param index the field index. - * @param pattern the pattern describing the date and time format - * @param defaultValue the default value to return if field value is not found. - * @throws IndexOutOfBoundsException if the index is out of bounds. - * @throws IllegalArgumentException if the date cannot be parsed. - * - */ - Date getDateWithPattern(int index, String pattern, Date defaultValue); - - /** - * Read the java.util.Date value in given format from column with given name. using the - * supplied defaultValue if the field value is a zero length string or null. - * - * @param name the field name. - * @param pattern the pattern describing the date and time format - * @param defaultValue the default value to return if field value is not found. - * @throws IllegalArgumentException if a column with given name is not defined or if the specified field cannot be - * parsed - * - */ - Date getDateWithPattern(String name, String pattern, Date defaultValue); - - /** - * Use SpEL expression to return a subset of the tuple that matches the expression - * - * @param expression SpEL expression to select from a Map, e.g. ?[key.startsWith('b')] - * @return a new Tuple with data selected from the current instance. - */ - Tuple select(String expression); - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleBuilder.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleBuilder.java deleted file mode 100644 index 4cc3be629..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleBuilder.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * Copyright 2013-2016 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 org.springframework.cloud.stream.tuple; - -import java.text.DateFormat; -import java.text.NumberFormat; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Locale; - -import org.springframework.core.convert.converter.Converter; -import org.springframework.core.convert.support.ConfigurableConversionService; -import org.springframework.util.Assert; - -/** - * Builder class to create Tuple instances. - * - * Default Locale is US for NumberFormat and default DatePattern is "yyyy-MM-dd" - * - * Note: Using a custom conversion service that is instance based (not configured - * as a singleton) will have significant performance impacts. - * - * @author Mark Pollack - * @author David Turanski - * @author Michael Minella - * @author Gunnar Hillert - */ -public class TupleBuilder { - - private List names = new ArrayList<>(); - - private List values = new ArrayList<>(); - - private final boolean mutable; - - private ConfigurableConversionService customConversionService = null; - - private final static String DEFAULT_DATE_PATTERN = "yyyy-MM-dd"; - - private final static Locale DEFAULT_LOCALE = Locale.US; - - private static final ConfigurableConversionService defaultConversionService; - - private static Converter tupleToStringConverter = new TupleToJsonStringConverter(); - - private static Converter stringToTupleConverter = new JsonStringToTupleConverter(); - - static { - defaultConversionService = new DefaultTupleConversionService(); - defaultConversionService.addConverterFactory(new LocaleAwareStringToNumberConverterFactory(NumberFormat - .getInstance(DEFAULT_LOCALE))); - DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN); - dateFormat.setLenient(false); - defaultConversionService.addConverter(new StringToDateConverter(dateFormat)); - } - - /** - * Return a new builder that will create immutable Tuples. - */ - public static TupleBuilder mutableTuple() { - return new TupleBuilder(true); - } - - /** - * Return a new builder that will create {@link MutableTuple}. - */ - public static TupleBuilder tuple() { - return new TupleBuilder(false); - } - - /** - * Creates a builder that will create immutable Tuples. - */ - public TupleBuilder() { - this(false); - } - - private TupleBuilder(boolean mutable) { - this.mutable = mutable; - } - - public Tuple of(String k1, Object v1) { - return newTuple(namesOf(k1), valuesOf(v1)); - } - - public Tuple of(String k1, Object v1, String k2, Object v2) { - addEntry(k1, v1); - addEntry(k2, v2); - return build(); - } - - public Tuple of(String k1, Object v1, String k2, Object v2, String k3, Object v3) { - addEntry(k1, v1); - addEntry(k2, v2); - addEntry(k3, v3); - return build(); - } - - public Tuple of(String k1, Object v1, String k2, Object v2, String k3, Object v3, String k4, Object v4) { - addEntry(k1, v1); - addEntry(k2, v2); - addEntry(k3, v3); - addEntry(k4, v4); - return build(); - } - - public Tuple ofNamesAndValues(List names, List values) { - this.names = names; - this.values = values; - return build(); - } - - public TupleBuilder put(String k1, Object v1) { - addEntry(k1, v1); - return this; - } - - /** - * Add all names and values of the tuple to the built tuple. - * @param tuple names and value to add to the tuple being built - * @return builder to continue creating a new tuple instance - */ - public TupleBuilder putAll(Tuple tuple) { - for (int i = 0; i < tuple.size(); i++) { - Object value = tuple.getValues().get(i); - String name = tuple.getFieldNames().get(i); - addEntry(name, value); - } - return this; - } - - public Tuple build() { - return newTuple(names, values); - } - - public static Tuple fromString(String source) { - return stringToTupleConverter.convert(source); - } - - public TupleBuilder setConfigurableConversionService(ConfigurableConversionService formattingConversionService) { - Assert.notNull(formattingConversionService); - this.customConversionService = formattingConversionService; - return this; - } - - public ConversionServiceBuilder setFormats(Locale locale, DateFormat dateFormat) { - return new ConversionServiceBuilder(this, locale, dateFormat); - } - - void addEntry(String k1, Object v1) { - names.add(k1); - values.add(v1); - } - - static List valuesOf(Object v1) { - ArrayList values = new ArrayList<>(); - values.add(v1); - return Collections.unmodifiableList(values); - } - - static List namesOf(String k1) { - List fields = new ArrayList<>(1); - fields.add(k1); - return Collections.unmodifiableList(fields); - - } - - protected Tuple newTuple(List names, List values) { - DefaultTuple tuple; - - ConfigurableConversionService conversionService = customConversionService != null ? customConversionService : defaultConversionService; - tuple = mutable - ? new DefaultMutableTuple(names, values, conversionService) - : new DefaultTuple(names, values, conversionService); - tuple.setTupleToStringConverter(tupleToStringConverter); - return tuple; - } - - /** - * Provides the ability to inject a {@link ConfigurableConversionService} as a way to - * customize conversion behavior in the built {@link Tuple}. - * - * @author Michael Minella - */ - public static class ConversionServiceBuilder { - - private TupleBuilder builder; - - private Locale locale; - - private DateFormat dateFormat; - - ConversionServiceBuilder(TupleBuilder builder, Locale locale, DateFormat dateFormat) { - this.builder = builder; - this.locale = locale; - this.dateFormat = dateFormat; - } - - public TupleBuilder setConfigurableConversionService(ConfigurableConversionService formattingConversionService) { - Assert.notNull(formattingConversionService); - - if (locale != null) { - formattingConversionService.addConverterFactory(new LocaleAwareStringToNumberConverterFactory(NumberFormat - .getInstance(locale))); - } - - if (dateFormat != null) { - formattingConversionService.addConverter(new StringToDateConverter(dateFormat)); - } - - builder.setConfigurableConversionService(formattingConversionService); - - return builder; - } - } -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleJsonMarshaller.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleJsonMarshaller.java deleted file mode 100644 index 094270f86..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleJsonMarshaller.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2015 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 org.springframework.cloud.stream.tuple; - - -/** - * @author David Turanski - * - */ -public class TupleJsonMarshaller extends TupleStringMarshaller { - - public TupleJsonMarshaller() { - super(new TupleToJsonStringConverter(), new JsonStringToTupleConverter()); - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleStringMarshaller.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleStringMarshaller.java deleted file mode 100644 index d48e53460..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleStringMarshaller.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2015 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 org.springframework.cloud.stream.tuple; - -import org.springframework.core.convert.converter.Converter; - -/** - * @author David Turanski - * - */ -public class TupleStringMarshaller { - - private final Converter tupleToStringConverter; - - private final Converter stringToTupleConverter; - - public TupleStringMarshaller(Converter tupleToStringConverter, - Converter stringToTupleConverter) { - this.tupleToStringConverter = tupleToStringConverter; - this.stringToTupleConverter = stringToTupleConverter; - } - - public Tuple toTuple(String source) { - return stringToTupleConverter.convert(source); - } - - public String fromTuple(Tuple source) { - return tupleToStringConverter.convert(source); - } -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleToJsonStringConverter.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleToJsonStringConverter.java deleted file mode 100644 index 641bb7621..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/TupleToJsonStringConverter.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright 2002-2015 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 org.springframework.cloud.stream.tuple; - -import com.fasterxml.jackson.databind.node.ArrayNode; -import com.fasterxml.jackson.databind.node.BaseJsonNode; -import org.springframework.core.convert.converter.Converter; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.node.ObjectNode; - -import java.util.List; - -/** - * Converts a Tuple to JSON representation - * - * @author David Turanski - * @author Gunnar Hillert - */ -public class TupleToJsonStringConverter implements Converter { - - private final ObjectMapper mapper = new ObjectMapper(); - - @Override - public String convert(Tuple source) { - ObjectNode root = toObjectNode(source); - String json = null; - try { - json = mapper.writeValueAsString(root); - } - catch (Exception e) { - throw new IllegalArgumentException("Tuple to string conversion failed", e); - } - return json; - } - - private ObjectNode toObjectNode(Tuple source) { - ObjectNode root = mapper.createObjectNode(); - for (int i = 0; i < source.size(); i++) { - Object value = source.getValues().get(i); - String name = source.getFieldNames().get(i); - if (value == null) { - root.putNull(name); - } else { - root.putPOJO(name, toNode(value)); - } - } - return root; - } - - private ArrayNode toArrayNode(List source) { - ArrayNode array = mapper.createArrayNode(); - for (Object value : source) { - if (value != null) { - array.add(toNode(value)); - } - } - return array; - } - - private BaseJsonNode toNode(Object value) { - if (value != null) { - if (value instanceof Tuple) { - return toObjectNode((Tuple) value); - } - else if (value instanceof List) { - return toArrayNode((List) value); - } - else if (!value.getClass().isPrimitive()) { - return mapper.getNodeFactory().pojoNode(value); - } - else { - return mapper.valueToTree(value); - } - } - return null; - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/JsonToTupleTransformer.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/JsonToTupleTransformer.java deleted file mode 100644 index 3e8da7ef5..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/JsonToTupleTransformer.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2013 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 org.springframework.cloud.stream.tuple.integration; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import org.springframework.integration.transformer.AbstractPayloadTransformer; -import org.springframework.cloud.stream.tuple.Tuple; -import org.springframework.cloud.stream.tuple.TupleBuilder; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * Converts from a json string into a tuple data structure. - * - * @author Mark Fisher - */ -public class JsonToTupleTransformer extends AbstractPayloadTransformer { - - private final ObjectMapper mapper = new ObjectMapper(); - - public JsonToTupleTransformer() { - mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); - } - - @Override - public Tuple transformPayload(String json) throws Exception { - List names = new ArrayList(); - List values = new ArrayList(); - JsonNode node = this.mapper.readTree(json); - Iterator fieldNames = node.fieldNames(); - while (fieldNames.hasNext()) { - String name = fieldNames.next(); - JsonNode valueNode = node.get(name); - Object value = mapper.treeToValue(valueNode, Object.class); - names.add(name); - values.add(value); - } - return TupleBuilder.tuple().ofNamesAndValues(names, values); - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/MapToTupleTransformer.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/MapToTupleTransformer.java deleted file mode 100644 index ba3d014b8..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/MapToTupleTransformer.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 2013-2015 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 org.springframework.cloud.stream.tuple.integration; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.springframework.cloud.stream.tuple.Tuple; -import org.springframework.cloud.stream.tuple.TupleBuilder; -import org.springframework.integration.transformer.AbstractPayloadTransformer; - -/** - * Converts from a Map to the Tuple data structure. - * - * @author Mark Pollack - * @author Ilayaperumal Gopinathan - */ -public class MapToTupleTransformer extends AbstractPayloadTransformer, Tuple> { - - @Override - public Tuple transformPayload(Map map) { - - List newNames = new ArrayList(); - List newValues = new ArrayList(); - for (Map.Entry entry: map.entrySet()) { - newNames.add(entry.getKey().toString()); - newValues.add(entry.getValue()); - } - return TupleBuilder.tuple().ofNamesAndValues(newNames, newValues); - - } -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/TupleKryoRegistrar.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/TupleKryoRegistrar.java deleted file mode 100644 index f9268c598..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/TupleKryoRegistrar.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2015 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 org.springframework.cloud.stream.tuple.integration; - -import java.util.ArrayList; -import java.util.List; - -import org.springframework.cloud.stream.tuple.kryo.DefaultTupleSerializer; -import org.springframework.integration.codec.kryo.AbstractKryoRegistrar; -import org.springframework.integration.codec.kryo.KryoRegistrar; -import org.springframework.cloud.stream.tuple.DefaultTuple; - -import com.esotericsoftware.kryo.Registration; -import com.esotericsoftware.kryo.serializers.CollectionSerializer; - -/** - * A {@link KryoRegistrar} used to register a Tuple serializer. - * - * @author David Turanski - */ -public class TupleKryoRegistrar extends AbstractKryoRegistrar { - - private final static int TUPLE_REGISTRATION_ID = 43; - - private final static int ARRAY_LIST_REGISTRATION_ID = 44; - - private final DefaultTupleSerializer defaultTupleSerializer = new DefaultTupleSerializer(); - - private final CollectionSerializer collectionSerializer = new CollectionSerializer(); - - - @Override - public List getRegistrations() { - List registrations = new ArrayList<>(2); - registrations.add(new Registration(DefaultTuple.class, defaultTupleSerializer, TUPLE_REGISTRATION_ID)); - registrations.add(new Registration(ArrayList.class, collectionSerializer, ARRAY_LIST_REGISTRATION_ID)); - return registrations; - } -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/package-info.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/package-info.java deleted file mode 100644 index f2c407d50..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/integration/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * Contains classes that support tuple integration such as tuple transformers. - */ - -package org.springframework.cloud.stream.tuple.integration; diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/kryo/DefaultTupleSerializer.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/kryo/DefaultTupleSerializer.java deleted file mode 100644 index 65dca4bed..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/kryo/DefaultTupleSerializer.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2015 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 org.springframework.cloud.stream.tuple.kryo; - -import java.util.ArrayList; -import java.util.List; - -import org.springframework.cloud.stream.tuple.Tuple; -import org.springframework.cloud.stream.tuple.TupleBuilder; - -import com.esotericsoftware.kryo.Kryo; -import com.esotericsoftware.kryo.Serializer; -import com.esotericsoftware.kryo.io.Input; -import com.esotericsoftware.kryo.io.Output; - -/** - * Serializes Tuples by writing the field names and then the values as class/object pairs. - * - * @author David Turanski - */ -public class DefaultTupleSerializer extends Serializer { - @Override - public void write(Kryo kryo, Output output, Tuple tuple) { - kryo.writeObject(output, tuple.getFieldNames()); - for (Object val: tuple.getValues()) { - kryo.writeClassAndObject(output, val); - } - } - - @Override - @SuppressWarnings("unchecked") - public Tuple read(Kryo kryo, Input input, Class type) { - List names = kryo.readObject(input, ArrayList.class); - List values = new ArrayList<>(names.size()); - for (int i = 0; i < names.size(); i++) { - Object val = kryo.readClassAndObject(input); - values.add(i, val); - } - return TupleBuilder.tuple().ofNamesAndValues(names, values); - } -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/kryo/package-info.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/kryo/package-info.java deleted file mode 100644 index c8e4b4bc4..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/kryo/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2014 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. - */ - -/** - * Contains tuple processor classes. - */ - -package org.springframework.cloud.stream.tuple.kryo; diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/package-info.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/package-info.java deleted file mode 100644 index cb67ae4c3..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * Base package for tuple classes. - */ - -package org.springframework.cloud.stream.tuple; diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/processor/TupleProcessor.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/processor/TupleProcessor.java deleted file mode 100644 index 3d3a14a77..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/processor/TupleProcessor.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2013 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 org.springframework.cloud.stream.tuple.processor; - -import org.springframework.cloud.stream.tuple.Tuple; - -/** - * Simple type-safe process callback method that returns another Tuple - * - * @author Mark Pollack - * - */ -public interface TupleProcessor { - - Tuple process(Tuple tuple); -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/processor/package-info.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/processor/package-info.java deleted file mode 100644 index d0f935d3b..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/processor/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * Contains tuple processor classes. - */ - -package org.springframework.cloud.stream.tuple.processor; diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/spel/TuplePropertyAccessor.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/spel/TuplePropertyAccessor.java deleted file mode 100644 index 43e6c9650..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/spel/TuplePropertyAccessor.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2013-2016 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 org.springframework.cloud.stream.tuple.spel; - -import org.springframework.cloud.stream.tuple.MutableTuple; -import org.springframework.expression.AccessException; -import org.springframework.expression.EvaluationContext; -import org.springframework.expression.PropertyAccessor; -import org.springframework.expression.TypedValue; -import org.springframework.cloud.stream.tuple.Tuple; - -/** - * A {@link PropertyAccessor} implementation that enables reading of {@link Tuple} values using dot notation within SpEL - * expressions. - * - * @author Mark Fisher - * @author Eric Bottard - */ -public class TuplePropertyAccessor implements PropertyAccessor { - - @Override - public Class[] getSpecificTargetClasses() { - return new Class[] { Tuple.class, MutableTuple.class}; - } - - @Override - public boolean canRead(EvaluationContext context, Object target, String name) throws AccessException { - Tuple tuple = (Tuple) target; - if (tuple.hasFieldName(name)) { - return true; - } - return maybeIndex(name, tuple) != null; - } - - /** - * Return an integer if the String property name can be parsed as an int, or null otherwise. - */ - private Integer maybeIndex(String name, Tuple tuple) { - Integer index = null; - try { - int i = Integer.parseInt(name); - if (i > -1 && tuple.size() > i) { - index = i; - } - } - catch (NumberFormatException e) { - // not an integer - } - return index; - } - - @Override - public TypedValue read(EvaluationContext context, Object target, String name) throws AccessException { - Tuple tuple = (Tuple) target; - boolean hasKey = false; - Object value = null; - if (tuple.hasFieldName(name)) { - hasKey = true; - value = tuple.getValue(name); - } - else { - Integer index = maybeIndex(name, tuple); - if (index != null) { - hasKey = true; - value = tuple.getValue(index); - } - } - if (value == null && !hasKey) { - throw new TupleAccessException(name); - } - return new TypedValue(value); - } - - @Override - public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException { - return target instanceof MutableTuple; - } - - @Override - public void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException { - MutableTuple tuple = (MutableTuple) target; - Integer index = maybeIndex(name, tuple); - if (index != null) { - tuple.setValue(index, newValue); - } else { - tuple.setValue(name, newValue); - } - } - - /** - * Exception thrown from {@code read} in order to reset a cached PropertyAccessor, allowing other accessors to have - * a try. - */ - @SuppressWarnings("serial") - private static class TupleAccessException extends AccessException { - - private final String name; - - public TupleAccessException(String name) { - super(null); - this.name = name; - } - - @Override - public String getMessage() { - return "Tuple does not contain a value for field name '" + this.name + "'"; - } - } - -} diff --git a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/spel/package-info.java b/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/spel/package-info.java deleted file mode 100644 index 830847f6e..000000000 --- a/spring-cloud-stream-tuple/src/main/java/org/springframework/cloud/stream/tuple/spel/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * Contains tuple SpEL accessor classes. - */ - -package org.springframework.cloud.stream.tuple.spel; diff --git a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/AbstractTupleMarshallerTests.java b/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/AbstractTupleMarshallerTests.java deleted file mode 100644 index 79fc85ec6..000000000 --- a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/AbstractTupleMarshallerTests.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2002-2013 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 org.springframework.cloud.stream.tuple; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Before; -import org.junit.Test; -import org.springframework.core.io.Resource; - -import java.io.IOException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.springframework.cloud.stream.tuple.TupleBuilder.tuple; - -/** - * @author David Turanski - * - */ -public abstract class AbstractTupleMarshallerTests { - - private TupleStringMarshaller marshaller; - - private ObjectMapper mapper = new ObjectMapper(); - - @Before - public void setUp() { - marshaller = getMarshaller(); - mapper.enable(SerializationFeature.INDENT_OUTPUT); - } - - @Test - public void testMarshallSimpleTuple() { - - DefaultTuple tuple = (DefaultTuple) tuple().of("foo", "bar", "int", 1); - String result = marshaller.fromTuple(tuple); - Tuple newTuple = marshaller.toTuple(result); - assertEquals(2, newTuple.getFieldCount()); - assertEquals("bar", newTuple.getString(0)); - assertEquals(1, newTuple.getInt(1)); - } - - @Test - public void testMarshallNestedTuple() { - Tuple t1 = tuple().of("foo", "bar", "int", 1); - Tuple tuple = tuple().of("t1", t1, "hello", "world"); - - String result = marshaller.fromTuple(tuple); - Tuple newTuple = marshaller.toTuple(result); - - assertEquals(2, newTuple.getFieldCount()); - assertEquals("world", newTuple.getString(1)); - - Tuple nested = newTuple.getTuple(0); - assertEquals(2, nested.getFieldCount()); - assertEquals("bar", nested.getString(0)); - assertEquals(1, nested.getInt(1)); - } - - @Test - public void testMarshallTupleWithCollection() { - List values = Arrays.asList("a", "b", "c"); - Tuple tuple = tuple().of("list", values); - - String result = marshaller.fromTuple(tuple); - Tuple newTuple = marshaller.toTuple(result); - assertTrue(newTuple.getValue(0) instanceof List); - List list = (List) newTuple.getValue(0); - assertEquals(values.size(), list.size()); - } - - @Test - public void testMarshallTupleWithMap() { - Map map = new HashMap(); - map.put("k1", "v1"); - map.put("k2", "v2"); - Tuple tuple = tuple().of("map", map); - - String result = marshaller.fromTuple(tuple); - - Tuple newTuple = marshaller.toTuple(result); - assertNotNull(newTuple.getValue("map")); - - // Map is converted to a tuple - assertEquals("v1", newTuple.getTuple("map").getString("k1")); - assertEquals("v2", newTuple.getTuple("map").getString("k2")); - } - - public String prettyPrintJson(String json) throws IOException { - Object jsonObject = mapper.readValue(json, Object.class); - return mapper.writeValueAsString(jsonObject); - } - - public String readJson(Resource resource) throws IOException { - Object jsonObject = mapper.readValue(resource.getInputStream(), Object.class); - return mapper.writeValueAsString(jsonObject); - } - - - - protected abstract TupleStringMarshaller getMarshaller(); -} diff --git a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/DefaultTupleTestForBatch.java b/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/DefaultTupleTestForBatch.java deleted file mode 100644 index a28344fef..000000000 --- a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/DefaultTupleTestForBatch.java +++ /dev/null @@ -1,466 +0,0 @@ -/* - * Copyright 2013 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 org.springframework.cloud.stream.tuple; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.springframework.cloud.stream.tuple.TupleBuilder.tuple; - -import java.math.BigDecimal; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Locale; - -import org.junit.Before; -import org.junit.Test; - -import org.springframework.core.convert.ConversionFailedException; - -/** - * This is a port of the FieldSet tests from Spring Batch - * - */ -public class DefaultTupleTestForBatch { - - Tuple tuple; - - List values; - - List names; - - - @Before - public void setUp() throws Exception { - - Object[] tokens = new String[] { "TestString", "true", "C", "10", "-472", "354224", "543", "124.3", "424.3", - "1,3245", - null, "2007-10-12", "12-10-2007", "" }; - String[] nameArray = new String[] { "String", "Boolean", "Char", "Byte", "Short", "Integer", "Long", "Float", - "Double", - "BigDecimal", "Null", "Date", "DatePattern", "BlankInput" }; - - names = Arrays.asList(nameArray); - values = Arrays.asList(tokens); - /* - * values = new ArrayList(); for (String token : tokens) { values.add(token); } - */ - tuple = tuple().ofNamesAndValues(names, values); - assertEquals(14, tuple.size()); - - } - - @Test - public void testNames() throws Exception { - // MLP - tuples always have names, FieldSet in Spring Batch doesn't require a name. - assertThat(tuple.getFieldCount(), is(tuple.getFieldNames().size())); - } - - @Test - public void testReadString() { - assertThat(tuple.getString(0), is("TestString")); - assertThat(tuple.getString("String"), is("TestString")); - } - - @Test - public void testReadChar() { - assertThat(tuple.getChar(2), is('C')); - assertThat(tuple.getChar("Char"), is('C')); - } - - @Test - public void testReadBooleanTrue() { - assertThat(tuple.getBoolean(1), is(true)); - assertThat(tuple.getBoolean("Boolean"), is(true)); - } - - @Test - public void testReadByte() { - assertTrue(tuple.getByte(3) == 10); - assertTrue(tuple.getByte("Byte") == 10); - } - - @Test - public void testReadShort() { - assertTrue(tuple.getShort(4) == -472); - assertTrue(tuple.getShort("Short") == -472); - } - - @Test - public void testReadIntegerAsFloat() { - assertEquals(354224, tuple.getFloat(5), .001); - assertEquals(354224, tuple.getFloat("Integer"), .001); - } - - @Test - public void testReadFloat() throws Exception { - assertTrue(tuple.getFloat(7) == 124.3F); - assertTrue(tuple.getFloat("Float") == 124.3F); - } - - @Test - public void testReadIntegerAsDouble() throws Exception { - assertEquals(354224, tuple.getDouble(5), .001); - assertEquals(354224, tuple.getDouble("Integer"), .001); - } - - @Test - public void testReadDouble() throws Exception { - assertTrue(tuple.getDouble(8) == 424.3); - assertTrue(tuple.getDouble("Double") == 424.3); - } - - @Test - public void testReadBigDecimal() throws Exception { - BigDecimal bd = new BigDecimal("424.3"); - assertEquals(bd, tuple.getBigDecimal(8)); - assertEquals(bd, tuple.getBigDecimal("Double")); - } - - @Test - public void testReadBigBigDecimal() throws Exception { - BigDecimal bd = new BigDecimal("12345678901234567890"); - Tuple tuple = TupleBuilder.tuple().of("bigd", "12345678901234567890"); - assertEquals(bd, tuple.getBigDecimal(0)); - } - - @Test - public void testReadBigDecimalWithFormat() throws Exception { - Tuple numberFormatTuple = TupleBuilder.tuple() - .setFormats(Locale.US, null) - .setConfigurableConversionService(new DefaultTupleConversionService()) - .ofNamesAndValues(tuple.getFieldNames(), tuple.getValues()); - BigDecimal bd = new BigDecimal("424.3"); - assertEquals(bd, numberFormatTuple.getBigDecimal(8)); - } - - @Test - public void testReadBigDecimalWithEuroFormat() throws Exception { - Tuple numberFormatTuple = TupleBuilder.tuple() - .setFormats(Locale.GERMANY, null) - .setConfigurableConversionService(new DefaultTupleConversionService()) - .ofNamesAndValues(tuple.getFieldNames(), tuple.getValues()); - BigDecimal bd = new BigDecimal("1.3245"); - assertEquals(bd, numberFormatTuple.getBigDecimal(9)); - } - - @Test - public void testReadNonExistentField() { - String s = tuple.getString("something"); - assertThat(s, nullValue()); - } - - @Test - public void testReadIndexOutOfRange() { - try { - tuple.getShort(-1); - fail("field set returns value even index is out of range!"); - } - catch (IndexOutOfBoundsException e) { - assertTrue(true); - } - - try { - tuple.getShort(99); - fail("field set returns value even index is out of range!"); - } - catch (Exception e) { - assertTrue(true); - } - } - - @Test - public void testReadBooleanWithTrueValue() { - assertTrue(tuple.getBoolean(1, "true")); - assertFalse(tuple.getBoolean(1, "incorrect trueValue")); - - assertTrue(tuple.getBoolean("Boolean", "true")); - assertFalse(tuple.getBoolean("Boolean", "incorrect trueValue")); - } - - @Test - public void testReadBooleanFalse() { - Tuple t = TupleBuilder.tuple().of("foo", false); - assertFalse(t.getBoolean(0)); - } - - @Test - public void testReadCharException() { - try { - tuple.getChar(1); - fail("the value read was not a character, exception expected"); - } - catch (IllegalArgumentException expected) { - assertTrue(true); - } - - try { - tuple.getChar("Boolean"); - fail("the value read was not a character, exception expected"); - } - catch (IllegalArgumentException expected) { - assertTrue(true); - } - } - - - @Test - public void testReadInt() throws Exception { - assertThat(354224, equalTo(tuple.getInt(5))); - assertThat(354224, equalTo(tuple.getInt("Integer"))); - } - - @Test - public void testReadIntWithSeparator() { - Tuple t = TupleBuilder.tuple().of("foo", "354,224"); - assertThat(354224, equalTo(t.getInt(0))); - } - - @Test - public void testReadIntWithSeparatorAndFormat() throws Exception { - Tuple t = TupleBuilder.tuple() - .setFormats(Locale.GERMAN, null) - .setConfigurableConversionService(new DefaultTupleConversionService()) - .of("foo", "354.224"); - assertThat(354224, equalTo(t.getInt(0))); - } - - @Test - public void testReadBlankInt() { - - // Trying to parse a blank field as an integer, but without a default - // value should throw a NumberFormatException - try { - tuple.getInt(13); - fail(); - } - catch (ConversionFailedException ex) { - // expected - } - - try { - tuple.getInt("BlankInput"); - fail(); - } - catch (ConversionFailedException ex) { - // expected - } - - } - - @Test - public void testReadLong() throws Exception { - assertThat(543L, equalTo(tuple.getLong(6))); - assertThat(543L, equalTo(tuple.getLong("Long"))); - } - - @Test - public void testReadLongWithPadding() throws Exception { - Tuple t = TupleBuilder.tuple().of("foo", "000009"); - assertThat(9L, equalTo(t.getLong(0))); - } - - @Test - public void testReadIntWithNullValue() { - assertThat(5, equalTo(tuple.getInt(10, 5))); - assertThat(5, equalTo(tuple.getInt("Null", 5))); - } - - @Test - public void testReadIntWithDefaultAndNotNull() { - assertThat(354224, equalTo(tuple.getInt(5, 5))); - assertThat(354224, equalTo(tuple.getInt("Integer", 5))); - } - - @Test - public void testReadLongWithNullValue() { - long defaultValue = 5; - int indexOfNull = 10; - int indexNotNull = 6; - String nameNull = "Null"; - String nameNotNull = "Long"; - long longValueAtIndex = 543; - - assertThat(defaultValue, equalTo(tuple.getLong(indexOfNull, defaultValue))); - assertThat(longValueAtIndex, equalTo(tuple.getLong(indexNotNull, defaultValue))); - - assertThat(defaultValue, equalTo(tuple.getLong(nameNull, defaultValue))); - assertThat(longValueAtIndex, equalTo(tuple.getLong(nameNotNull, defaultValue))); - } - - @Test - public void testReadBigDecimalInvalid() { - int index = 0; - - try { - tuple.getBigDecimal(index); - fail("field value is not a number, exception expected"); - } - catch (ConversionFailedException e) { - assertTrue(e.getMessage().indexOf("TestString") > 0); - } - - } - - @Test - public void testReadBigDecimalByNameInvalid() throws Exception { - try { - tuple.getBigDecimal("String"); - fail("field value is not a number, exception expected"); - } - catch (ConversionFailedException e) { - assertTrue(e.getMessage().indexOf("TestString") > 0); - } - } - - - @Test - public void testReadDate() throws Exception { - assertNotNull(tuple.getDate(11)); - assertNotNull(tuple.getDate("Date")); - } - - @Test - public void testReadDateWithDefault() { - Date date = null; - assertEquals(date, tuple.getDateWithPattern(13, "dd-MM-yyyy", date)); - assertEquals(date, tuple.getDateWithPattern("BlankInput", "dd-MM-yyyy", date)); - } - - @Test - public void testReadDateWithFormat() throws Exception { - SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy"); - Tuple t = TupleBuilder.tuple() - .setFormats(null, dateFormat) - .setConfigurableConversionService(new DefaultTupleConversionService()) - .of("foo", "13/01/1999"); - assertEquals(dateFormat.parse("13/01/1999"), t.getDate(0)); - } - - @Test - public void testReadDateInvalid() throws Exception { - try { - tuple.getDate(0); - fail("field value is not a date, exception expected"); - } - catch (ConversionFailedException e) { - assertTrue(e.getMessage().indexOf("TestString") > 0); - } - - } - - @Test - public void testReadDateInvalidByName() throws Exception { - - try { - tuple.getDate("String"); - fail("field value is not a date, exception expected"); - } - catch (ConversionFailedException e) { - assertTrue(e.getMessage().indexOf("TestString") > 0); - // assertTrue(e.getMessage().indexOf("name: [String]") > 0); - } - - } - - @Test - public void testReadDateInvalidWithPattern() throws Exception { - - try { - tuple.getDateWithPattern(0, "dd-MM-yyyy"); - fail("field value is not a date, exception expected"); - } - catch (IllegalArgumentException e) { - assertTrue(e.getMessage().indexOf("dd-MM-yyyy") > 0); - } - } - - @Test - public void testReadDateWithPatternAndDefault() { - Date date = null; - assertEquals(date, tuple.getDateWithPattern(13, "dd-MM-yyyy", date)); - assertEquals(date, tuple.getDateWithPattern("BlankInput", "dd-MM-yyyy", date)); - } - - @Test - public void testStrictReadDateWithPattern() throws Exception { - - Tuple t = tuple().of("foo", "50-2-13"); - try { - t.getDateWithPattern(0, "dd-MM-yyyy"); - fail("field value is not a valid date for strict parser, exception expected"); - } - catch (IllegalArgumentException e) { - String message = e.getMessage(); - assertTrue("Message did not contain: " + message, message.indexOf("dd-MM-yyyy") > 0); - } - } - - @Test - public void testStrictReadDateWithPatternAndStrangeDate() throws Exception { - - Tuple t = tuple().of("foo", "5550212"); - try { - System.err.println(t.getDateWithPattern(0, "yyyyMMdd")); - fail("field value is not a valid date for strict parser, exception expected"); - } - catch (IllegalArgumentException e) { - String message = e.getMessage(); - assertTrue("Message did not contain: " + message, message.indexOf("yyyyMMdd") > 0); - } - } - - @Test - public void testReadDateByNameInvalidWithPattern() throws Exception { - - try { - tuple.getDateWithPattern("String", "dd-MM-yyyy"); - fail("field value is not a date, exception expected"); - } - catch (IllegalArgumentException e) { - assertTrue(e.getMessage().indexOf("dd-MM-yyyy") > 0); - assertTrue(e.getMessage().indexOf("String") > 0); - } - } - - @Test - public void testPaddedLong() { - Tuple t = tuple().of("foo", "00000009"); - // FieldSet fs = new DefaultFieldSet(new String[] { "00000009" }); - - long value = t.getLong(0); - assertEquals(value, 9); - } - - @Test - public void testReadRawString() { - String name = "fieldName"; - String value = " string with trailing whitespace "; - Tuple t = tuple().of(name, value); - - assertEquals(value, t.getRawString(0)); - assertEquals(value, t.getRawString(name)); - } -} diff --git a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/DefaultTupleTests.java b/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/DefaultTupleTests.java deleted file mode 100644 index c940f1894..000000000 --- a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/DefaultTupleTests.java +++ /dev/null @@ -1,497 +0,0 @@ -/* - * Copyright 2013 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 org.springframework.cloud.stream.tuple; - -import static org.hamcrest.Matchers.allOf; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.Matchers.sameInstance; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.springframework.cloud.stream.tuple.TupleBuilder.tuple; - -import java.awt.Color; -import java.io.IOException; -import java.math.BigDecimal; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.List; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -import org.springframework.core.convert.ConverterNotFoundException; - -public class DefaultTupleTests { - - @Test(expected = IllegalArgumentException.class) - public void nullForNameArray() { - List values = new ArrayList(); - values.add("bar"); - tuple().ofNamesAndValues(null, values); - } - - @Test(expected = IllegalArgumentException.class) - public void nullForValueArray() { - List names = new ArrayList(); - names.add("foo"); - tuple().ofNamesAndValues(names, null); - } - - @Rule - public ExpectedException thrown = ExpectedException.none(); - - @Test - public void notEqualNumberOfNamesAndValues() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Field names must be same length as values: names=[foo, oof], values=[bar]"); - List names = new ArrayList(); - names.add("foo"); - names.add("oof"); - List values = new ArrayList(); - values.add("bar"); - tuple().ofNamesAndValues(names, values); - } - - @Test - public void accessNonExistentEntry() { - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("Field name [does-not-exist] does not exist"); - Tuple tuple = TupleBuilder.tuple().of("foo", "bar"); - Object v = tuple.getValue("does-not-exist"); - assertThat(v, nullValue()); - } - - @Test - public void singleEntry() { - Tuple tuple = TupleBuilder.tuple().of("foo", "bar"); - assertThat(tuple.size(), equalTo(1)); - List names = tuple.getFieldNames(); - assertThat(names.get(0), equalTo("foo")); - assertThat((String) tuple.getValue("foo"), equalTo("bar")); - assertThat(tuple.hasFieldName("foo"), equalTo(true)); - assertThat(tuple.getValue("foo").toString(), equalTo("bar")); - } - - @Test - public void twoEntries() { - Tuple tuple = TupleBuilder.tuple().of("up", "down", "charm", "strange"); - assertTwoEntries(tuple); - } - - @Test - public void testTupleAsValue() { - Tuple t1 = tuple().of("up", 1, "down", 2); - Tuple t2 = tuple().of("1stgen", t1, "charm", 3, "strange", 4); - assertThat(t2.size(), equalTo(3)); - Tuple t3 = t2.getTuple(0); - assertThat(t3.size(), equalTo(2)); - assertThat(t3.getFieldNames().get(0), equalTo("up")); - assertThat(t3.getFieldNames().get(1), equalTo("down")); - assertThat(t3.getInt("up"), equalTo(1)); - assertThat(t3.getInt("down"), equalTo(2)); - Tuple t4 = t2.getTuple("1stgen"); - assertThat(t3, equalTo(t4)); - } - - /** - * @param tuple - */ - private void assertTwoEntries(Tuple tuple) { - assertThat(tuple.size(), equalTo(2)); - assertThat(tuple.getFieldNames().get(0), equalTo("up")); - assertThat(tuple.getFieldNames().get(1), equalTo("charm")); - assertThat((String) tuple.getValue("up"), equalTo("down")); - assertThat((String) tuple.getValue("charm"), equalTo("strange")); - } - - @Test - public void threeEntries() { - Tuple tuple = TupleBuilder.tuple().of("up", 1, "charm", 2, "top", 3); - assertThat(tuple.size(), equalTo(3)); - assertThat(tuple.getFieldNames().get(0), equalTo("up")); - assertThat(tuple.getFieldNames().get(1), equalTo("charm")); - assertThat(tuple.getFieldNames().get(2), equalTo("top")); - // access by name - assertThat((Integer) tuple.getValue("up"), equalTo(1)); - assertThat((Integer) tuple.getValue("charm"), equalTo(2)); - assertThat((Integer) tuple.getValue("top"), equalTo(3)); - // access by position - assertThat((Integer) tuple.getValue(0), equalTo(1)); - assertThat((Integer) tuple.getValue(1), equalTo(2)); - assertThat((Integer) tuple.getValue(2), equalTo(3)); - // access from separate collection - List values = tuple.getValues(); - assertThat((Integer) values.get(0), equalTo(1)); - assertThat((Integer) values.get(1), equalTo(2)); - assertThat((Integer) values.get(2), equalTo(3)); - } - - @Test - public void fourEntries() { - Tuple tuple = TupleBuilder.tuple().of("up", 1, "charm", 2, "top", 3, "e", 4); - assertThat(tuple.size(), equalTo(4)); - assertThat(tuple.getFieldNames().get(0), equalTo("up")); - assertThat(tuple.getFieldNames().get(1), equalTo("charm")); - assertThat(tuple.getFieldNames().get(2), equalTo("top")); - assertThat(tuple.getFieldNames().get(3), equalTo("e")); - // access by name - assertThat((Integer) tuple.getValue("up"), equalTo(1)); - assertThat((Integer) tuple.getValue("charm"), equalTo(2)); - assertThat((Integer) tuple.getValue("top"), equalTo(3)); - assertThat((Integer) tuple.getValue("e"), equalTo(4)); - // access by position - assertThat((Integer) tuple.getValue(0), equalTo(1)); - assertThat((Integer) tuple.getValue(1), equalTo(2)); - assertThat((Integer) tuple.getValue(2), equalTo(3)); - assertThat((Integer) tuple.getValue(3), equalTo(4)); - } - - @Test - public void getValue() { - Tuple tuple = TupleBuilder.tuple().of("up", 1, "charm", 2.0, "top", true); - assertThat(tuple.getValue(0, Integer.class), equalTo(1)); - assertThat(tuple.getValue(0, String.class), equalTo("1")); - assertThat(tuple.getValue(1, Double.class), equalTo(2.0D)); - assertThat(tuple.getValue(2, Boolean.class), equalTo(true)); - assertThat(tuple.getValue(2, String.class), equalTo("true")); - - assertThat(tuple.getValue("up", Integer.class), equalTo(1)); - assertThat(tuple.getValue("up", String.class), equalTo("1")); - assertThat(tuple.getValue("charm", Double.class), equalTo(2.0D)); - assertThat(tuple.getValue("top", Boolean.class), equalTo(true)); - assertThat(tuple.getValue("top", String.class), equalTo("true")); - } - - @Test - public void testPrimitiveGetters() { - Tuple tuple = TupleBuilder.tuple().of("up", "down", "charm", 2.0, "top", true); - assertThat(tuple.getBoolean("top"), equalTo(true)); - assertThat(tuple.getBoolean(2), equalTo(true)); - assertThat(tuple.getBoolean("up", "down"), equalTo(true)); - } - - @Test - public void testToString() throws JsonProcessingException, IOException { - Tuple tuple = TupleBuilder.tuple().put("up", "down").put("charm", "strange").build(); - - String tupleString = tuple.toString(); - - // valid JSON - new ObjectMapper().readTree(tupleString); - - Tuple tupleFromString = TupleBuilder.fromString(tupleString); - assertEquals(2, tupleFromString.getFieldCount()); - assertEquals("down", tupleFromString.getString("up")); - assertEquals("strange", tupleFromString.getString("charm")); - - } - - @Test - public void testPutApi() { - TupleBuilder builder = TupleBuilder.tuple(); - Tuple tuple = builder.put("up", "down").put("charm", "strange").build(); - assertTwoEntries(tuple); - } - - @Test - public void testPutAllApi() { - Tuple tuple = TupleBuilder.tuple().put("red", "rot").put("brown", "braun").put("blue", "blau").put("yellow", "gelb") - .put("beige", "beige").build(); - assertThat(tuple.size(), equalTo(5)); - Tuple tuplePlusOne = TupleBuilder.tuple().putAll(tuple).put("up", 1).build(); - assertThat(tuplePlusOne.size(), equalTo(6)); - assertThat(tuplePlusOne.getFieldNames().get(0), equalTo("red")); - assertThat(tuplePlusOne.getFieldNames().get(5), equalTo("up")); - } - - @Test - public void testEqualsAndHashCodeSunnyDay() { - Tuple tuple1 = TupleBuilder.tuple().of("up", 1, "charm", 2, "top", 3); - Tuple tuple2 = TupleBuilder.tuple().of("up", 1, "charm", 2, "top", 3); - assertThat(tuple1, equalTo(tuple2)); - assertThat(tuple1.hashCode(), equalTo(tuple2.hashCode())); - assertThat(tuple1, not(sameInstance(tuple2))); - } - - @Test - public void testEqualsAndHashFailureCases() { - Tuple tuple1 = TupleBuilder.tuple().of("up", 1, "charm", 2, "top", 3); - Tuple tuple2 = TupleBuilder.tuple().of("up", 2, "charm", 3, "top", 4); - assertThat(tuple1, not(equalTo((tuple2)))); - assertThat(tuple1.hashCode(), not(equalTo(tuple2.hashCode()))); - - tuple1 = TupleBuilder.tuple().of("up", 1, "charm", 2, "top", 3); - tuple2 = TupleBuilder.tuple().of("top", 1, "charm", 2, "up", 3); - assertThat(tuple1, not(equalTo((tuple2)))); - assertThat(tuple1.hashCode(), not(equalTo(tuple2.hashCode()))); - } - - @SuppressWarnings("rawtypes") - @Test - public void testGetFieldTypes() { - Tuple tuple = TupleBuilder.tuple().of("up", 1, "charm", 2, "top", 3); - Class[] expectedTypes = new Class[] { Integer.class, Integer.class, Integer.class }; - assertThat(tuple.getFieldTypes(), equalTo(Arrays.asList(expectedTypes))); - - tuple = TupleBuilder.tuple().of("up", 1, "charm", 2.0f, "top", "bottom"); - expectedTypes = new Class[] { Integer.class, Float.class, String.class }; - assertThat(tuple.getFieldTypes(), equalTo(Arrays.asList(expectedTypes))); - - tuple = TupleBuilder.tuple().of("up", 1, "charm", 2.0, "top", true); - expectedTypes = new Class[] { Integer.class, Double.class, Boolean.class }; - assertThat(tuple.getFieldTypes(), equalTo(Arrays.asList(expectedTypes))); - - } - - @Test - public void testGetString() { - // test conversions of string, int, and float. - Tuple tuple = TupleBuilder.tuple().of("up", "down", "charm", 2, "top", 2.0f); - assertThat(tuple.getString("up"), equalTo("down")); - assertThat(tuple.getString("charm"), equalTo("2")); - assertThat(tuple.getString("top"), equalTo("2.0")); - } - - @Test - public void testGetNullValue() { - Tuple tuple = tuple().of("foo", null); - // non primitive types will return null - assertThat(tuple.getString("foo"), nullValue()); - assertThat(tuple.getBigDecimal("foo"), nullValue()); - assertThat(tuple.getDate("foo"), nullValue()); - - // primitive types will return default values - assertThat(tuple.getChar("foo"), equalTo('\u0000')); - assertThat(tuple.getBoolean("foo"), equalTo(false)); - byte b = 0; - assertThat(tuple.getByte("foo"), equalTo(b)); - short s = 0; - assertThat(tuple.getShort("foo"), equalTo(s)); - assertThat(tuple.getInt("foo"), equalTo(0)); - assertThat(tuple.getLong("foo"), equalTo(0L)); - assertThat(tuple.getFloat("foo"), equalTo(0f)); - assertThat(tuple.getDouble("foo"), equalTo(0d)); - - } - - @Test - public void testGetStringThatFails() { - Tuple tuple = TupleBuilder.tuple().of("up", "down", "charm", 2, "top", 2.0f, "black", Color.black); - thrown.expect(ConverterNotFoundException.class); - thrown.expectMessage(allOf(containsString("No converter found capable of converting from type"), - containsString("java.awt.Color"), - containsString("java.lang.String"))); - assertThat(tuple.getString("black"), equalTo("omg")); - } - - @Test - public void testSelection() { - Tuple tuple = tuple().put("red", "rot").put("brown", "braun").put("blue", "blau").put("yellow", "gelb") - .put("beige", "beige").build(); - Tuple selectedTuple = tuple.select("?[key.startsWith('b')]"); - assertThat(selectedTuple.size(), equalTo(3)); - - selectedTuple = tuple.select("^[key.startsWith('b')]"); - assertThat(selectedTuple.size(), equalTo(1)); - assertThat(selectedTuple.getFieldNames().get(0), equalTo("brown")); - assertThat(selectedTuple.getString(0), equalTo("braun")); - - selectedTuple = tuple.select("?[value.length() < 4]"); - assertThat(selectedTuple.size(), equalTo(1)); - assertThat(selectedTuple.getFieldNames().get(0), equalTo("red")); - assertThat(selectedTuple.getString(0), equalTo("rot")); - } - - @Test - public void testReadByteWithDefault() { - // with a value - byte b = 1; - Tuple t = tuple().of("foo", b); - byte defaultByte = 2; - assertTrue(t.getByte(0, defaultByte) == b); - assertTrue(t.getByte("foo", defaultByte) == b); - assertTrue(t.getByte("bar", defaultByte) == defaultByte); - - // with a null value - t = tuple().of("foo", null); - assertTrue(t.getByte(0, defaultByte) == defaultByte); - assertTrue(t.getByte("foo", defaultByte) == defaultByte); - assertTrue(t.getByte("bar", defaultByte) == defaultByte); - - } - - @Test - public void testReadShortWithDefault() { - // with a value - short s = 1; - Tuple t = tuple().of("foo", s); - short defaultShort = 2; - assertTrue(t.getShort(0, defaultShort) == s); - assertTrue(t.getShort("foo", defaultShort) == s); - assertTrue(t.getShort("bar", defaultShort) == defaultShort); - - // with a null value - t = tuple().of("foo", null); - assertTrue(t.getShort(0, defaultShort) == defaultShort); - assertTrue(t.getShort("foo", defaultShort) == defaultShort); - assertTrue(t.getShort("bar", defaultShort) == defaultShort); - - } - - @Test - public void testReadIntWithDefault() { - // with a value - int i = 1; - Tuple t = tuple().of("foo", i); - int defaultInt = 2; - assertTrue(t.getInt(0, defaultInt) == i); - assertTrue(t.getInt("foo", defaultInt) == i); - assertTrue(t.getInt("bar", defaultInt) == defaultInt); - - // with a null value - t = tuple().of("foo", null); - assertTrue(t.getInt(0, defaultInt) == defaultInt); - assertTrue(t.getInt("foo", defaultInt) == defaultInt); - assertTrue(t.getInt("bar", defaultInt) == defaultInt); - - } - - @Test - public void testReadLongWithDefault() { - // with a value - long l = 1; - Tuple t = tuple().of("foo", l); - int defaultLong = 2; - assertTrue(t.getLong(0, defaultLong) == l); - assertTrue(t.getLong("foo", defaultLong) == l); - assertTrue(t.getLong("bar", defaultLong) == defaultLong); - - // with a null value - t = tuple().of("foo", null); - assertTrue(t.getLong(0, defaultLong) == defaultLong); - assertTrue(t.getLong("foo", defaultLong) == defaultLong); - assertTrue(t.getLong("bar", defaultLong) == defaultLong); - - } - - @Test - public void testReadFloatWithDefault() { - // with a value - float f = 1; - Tuple t = tuple().of("foo", f); - float defaultFloat = 2.0f; - assertTrue(t.getFloat(0, defaultFloat) == f); - assertTrue(t.getFloat("foo", defaultFloat) == f); - assertTrue(t.getFloat("bar", defaultFloat) == defaultFloat); - - // with a null value - t = tuple().of("foo", null); - assertTrue(t.getFloat(0, defaultFloat) == defaultFloat); - assertTrue(t.getFloat("foo", defaultFloat) == defaultFloat); - assertTrue(t.getFloat("bar", defaultFloat) == defaultFloat); - - } - - @Test - public void testReadDoubleWithDefault() { - // with a value - double d = 1; - Tuple t = tuple().of("foo", d); - double defaultDouble = 2.0d; - assertTrue(t.getDouble(0, defaultDouble) == d); - assertTrue(t.getDouble("foo", defaultDouble) == d); - assertTrue(t.getDouble("bar", defaultDouble) == defaultDouble); - - // with a null value - t = tuple().of("foo", null); - assertTrue(t.getDouble(0, defaultDouble) == defaultDouble); - assertTrue(t.getDouble("foo", defaultDouble) == defaultDouble); - assertTrue(t.getDouble("bar", defaultDouble) == defaultDouble); - - } - - @Test - public void testReadBigDecimalWithDefault() { - // with a value - BigDecimal bd = new BigDecimal(1); - Tuple t = tuple().of("foo", bd); - BigDecimal defaultBigDecimal = new BigDecimal(2); - assertTrue(t.getBigDecimal(0, defaultBigDecimal) == bd); - assertTrue(t.getBigDecimal("foo", defaultBigDecimal) == bd); - assertTrue(t.getBigDecimal("bar", defaultBigDecimal) == defaultBigDecimal); - - // with a null value - t = tuple().of("foo", null); - assertTrue(t.getBigDecimal(0, defaultBigDecimal) == defaultBigDecimal); - assertTrue(t.getBigDecimal("foo", defaultBigDecimal) == defaultBigDecimal); - assertTrue(t.getBigDecimal("bar", defaultBigDecimal) == defaultBigDecimal); - - } - - @Test - public void testReadDateWithDefault() throws InterruptedException { - // with a value - Date date = new Date(); - Tuple t = tuple().of("foo", date); - Thread.sleep(1000); - Date defaultDate = new Date(); - assertTrue(t.getDate(0, defaultDate) == date); - assertTrue(t.getDate("foo", defaultDate) == date); - assertTrue(t.getDate("bar", defaultDate) == defaultDate); - - // with a null value - t = tuple().of("foo", null); - assertTrue(t.getDate(0, defaultDate) == defaultDate); - assertTrue(t.getDate("foo", defaultDate) == defaultDate); - assertTrue(t.getDate("bar", defaultDate) == defaultDate); - - } - - @Test - public void testReadDateWithPattern() throws ParseException, InterruptedException { - Tuple t = tuple().of("foo", "24-12-2013"); - Date d = t.getDateWithPattern(0, "dd-MM-yyyy"); - SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); - assertEquals(dateFormat.parse("24-12-2013"), d); - Thread.sleep(1000); - Date defaultDate = new Date(); - assertTrue(t.getDateWithPattern("foo", "xyz-abc", defaultDate) == defaultDate); - assertTrue(t.getDateWithPattern(0, "xyz-abc", defaultDate) == defaultDate); - - } - - @Test - public void testCollectionToTupleConversionFails() { - thrown.expect(ConverterNotFoundException.class); - Tuple t1 = tuple().of("hello", "world"); - Tuple t2 = tuple().of("foo", "bar"); - List list = Arrays.asList(t1, t2); - Tuple t = tuple().of("list", list); - t.getTuple("list"); - } - -} diff --git a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/JsonBytesToTupleConverterTests.java b/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/JsonBytesToTupleConverterTests.java deleted file mode 100644 index 09cb024da..000000000 --- a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/JsonBytesToTupleConverterTests.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2002-2013 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 org.springframework.cloud.stream.tuple; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -/** - * @author David Turanski - * - */ -public class JsonBytesToTupleConverterTests { - - private JsonBytesToTupleConverter converter = new JsonBytesToTupleConverter(); - - @Test - public void testSimple() { - String json = "{\"symbol\":\"VMW\",\"price\":73}"; - Tuple t = converter.convert(json.getBytes()); - assertEquals("VMW", t.getValue("symbol")); - } -} diff --git a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/SpringToDateConverterTests.java b/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/SpringToDateConverterTests.java deleted file mode 100644 index 21064cf94..000000000 --- a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/SpringToDateConverterTests.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2014 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 org.springframework.cloud.stream.tuple; - -import org.junit.Test; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; - -import static org.junit.Assert.assertEquals; - -public class SpringToDateConverterTests { - - @Test - public void testCtor() throws ParseException { - StringToDateConverter converter = new StringToDateConverter(); - Date d = converter.convert("2013-05-02"); - - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - assertEquals(dateFormat.parse("2013-05-02"), d); - } - -} diff --git a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/TupleJsonMarshallerTests.java b/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/TupleJsonMarshallerTests.java deleted file mode 100644 index a74db773c..000000000 --- a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/TupleJsonMarshallerTests.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2002-2013 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 org.springframework.cloud.stream.tuple; - -import org.junit.Test; -import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.Resource; - -import java.io.IOException; -import java.util.List; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -/** - * @author David Turanski - * - */ -public class TupleJsonMarshallerTests extends AbstractTupleMarshallerTests { - - /* - * (non-Javadoc) - * - * @see org.springframework.xd.tuple.AbstractTupleMarshallerTests#getMarshaller() - */ - @Override - protected TupleJsonMarshaller getMarshaller() { - return new TupleJsonMarshaller(); - } - - @Test - public void testComplexJson() throws IOException { - Resource jsonFile = new ClassPathResource("/tweet.json"); - assertTrue(jsonFile.exists()); - - String source = readJson(jsonFile); - TupleJsonMarshaller marshaller = getMarshaller(); - Tuple tuple = marshaller.toTuple(source); - assertEquals("Gabriel", tuple.getTuple("user").getString("name")); - List mentions = (List) tuple.getTuple("entities").getValue("user_mentions"); - assertEquals(2, mentions.size()); - assertTrue(mentions.get(0) instanceof Tuple); - Tuple t = (Tuple) mentions.get(0); - assertEquals("someoneFollowed", t.getString("screen_name")); - } - - @Test - public void testJsonWithArrays() throws IOException { - Resource jsonFile = new ClassPathResource("/jsonWithArrays.json"); - assertTrue(jsonFile.exists()); - String json = readJson(jsonFile); - - //Convert to tuple - TupleJsonMarshaller marshaller = getMarshaller(); - Tuple tuple = marshaller.toTuple(json); - - //Validate contents - List body = (List) tuple.getValue("body"); - Tuple t2 = body.get(0).getTuple("har"); - Tuple t3 = t2.getTuple("log"); - List pages = (List)t3.getValue("pages"); - assertEquals(2, pages.size()); - - //Convert back to json. - String convertedJson = prettyPrintJson(marshaller.fromTuple(tuple)); - assertEquals(json, convertedJson); - } - -} diff --git a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/spel/TuplePropertyAccessorTests.java b/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/spel/TuplePropertyAccessorTests.java deleted file mode 100644 index 2c0570b42..000000000 --- a/spring-cloud-stream-tuple/src/test/java/org/springframework/cloud/stream/tuple/spel/TuplePropertyAccessorTests.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright 2016 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 org.springframework.cloud.stream.tuple.spel; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -import org.hamcrest.CoreMatchers; -import org.junit.Before; -import org.junit.Test; - -import org.springframework.cloud.stream.tuple.Tuple; -import org.springframework.cloud.stream.tuple.TupleBuilder; -import org.springframework.expression.spel.SpelEvaluationException; -import org.springframework.expression.spel.standard.SpelExpressionParser; -import org.springframework.expression.spel.support.StandardEvaluationContext; - -/** - * Unit tests for {@link TuplePropertyAccessor}. - * - * @author Eric Bottard - * @author Mark Fisher - */ -public class TuplePropertyAccessorTests { - - private final SpelExpressionParser parser = new SpelExpressionParser(); - - private final StandardEvaluationContext context = new StandardEvaluationContext(); - - @Before - public void setup() { - this.context.addPropertyAccessor(new TuplePropertyAccessor()); - } - - @Test - public void testSimplePropertyByFieldName() { - Tuple tuple = TupleBuilder.tuple().of("foo", "bar"); - String result = evaluate("foo", tuple, String.class); - assertThat(result, is("bar")); - } - - @Test - public void testSimplePropertyByIndex() { - Tuple tuple = TupleBuilder.tuple().of("foo", "bar"); - String result = evaluate("['0']", tuple, String.class); - assertThat(result, is("bar")); - } - - @Test(expected = SpelEvaluationException.class) - public void failOnNegativeIndex() { - Tuple tuple = TupleBuilder.tuple().of("foo", "bar"); - evaluate("['-3']", tuple, String.class); - } - - @Test - public void testNestedPropertyByFieldName() { - Tuple child = TupleBuilder.tuple().of("b", 123); - Tuple tuple = TupleBuilder.tuple().of("a", child); - int result = evaluate("a.b", tuple, Integer.class); - assertThat(result, is(123)); - } - - @Test - public void testNestedPropertyByIndex() { - Tuple child = TupleBuilder.tuple().of("b", 123); - Tuple tuple = TupleBuilder.tuple().of("a", child); - int result = evaluate("a['0']", tuple, Integer.class); - assertThat(result, is(123)); - } - - @Test - public void testNestedPropertyByIndexOnly() { - Tuple child = TupleBuilder.tuple().of("b", 123); - Tuple tuple = TupleBuilder.tuple().of("a", child); - int result = evaluate("['0']['0']", tuple, Integer.class); - assertThat(result, is(123)); - } - - @Test - public void testArrayPropertyByFieldName() { - Tuple tuple = TupleBuilder.tuple().of("numbers", new Integer[] { 1, 2, 3 }); - int result = evaluate("numbers[1]", tuple, Integer.class); - assertThat(result, is(2)); - } - - @Test - public void testArrayPropertyByIndex() { - Tuple tuple = TupleBuilder.tuple().of("numbers", new Integer[] { 1, 2, 3 }); - int result = evaluate("['0'][0]", tuple, Integer.class); - assertThat(result, is(1)); - } - - @Test - public void testNestedArrayPropertyByFieldName() { - Tuple child = TupleBuilder.tuple().of("numbers", new Integer[] { 7, 8, 9 }); - Tuple tuple = TupleBuilder.tuple().of("child", child); - int result = evaluate("child.numbers[1]", tuple, Integer.class); - assertThat(result, is(8)); - } - - @Test - public void testNestedArrayPropertyByIndex() { - Tuple child = TupleBuilder.tuple().of("numbers", new Integer[] { 7, 8, 9 }); - Tuple tuple = TupleBuilder.tuple().of("child", child); - int result = evaluate("child['0'][2]", tuple, Integer.class); - assertThat(result, is(9)); - } - - @Test - public void testNestedArrayPropertyByIndexOnly() { - Tuple child = TupleBuilder.tuple().of("numbers", new Integer[] { 7, 8, 9 }); - Tuple tuple = TupleBuilder.tuple().of("child", child); - int result = evaluate("['0']['0'][1]", tuple, Integer.class); - assertThat(result, is(8)); - } - - // Write - - @Test - public void testSimpleWriteOnExistingProperty() { - Tuple tuple = TupleBuilder.mutableTuple().of("foo", "bar"); - write("foo", tuple, 123); - assertThat(tuple.getInt("foo"), is(123)); - } - - @Test - public void testSimpleWriteOnNewProperty() { - Tuple tuple = TupleBuilder.mutableTuple().of("foo", "bar"); - write("other", tuple, 123); - assertThat(tuple.getInt("other"), is(123)); - assertThat(tuple.getValue("foo"), CoreMatchers.is("bar")); - } - - @Test - public void testSimpleWriteOnExistingPropertyByIndex() { - Tuple tuple = TupleBuilder.mutableTuple().of("foo", "bar"); - write("['0']", tuple, 123); - assertThat(tuple.getInt("foo"), is(123)); - } - - @Test - public void testSimpleWriteOnNewPropertyByIndex() { - Tuple tuple = TupleBuilder.mutableTuple().of("foo", "bar"); - write("['12']", tuple, 123); - assertThat(tuple.getInt("12"), is(123)); - assertThat(tuple.getValue("foo"), CoreMatchers.is("bar")); - } - - private T evaluate(String expression, Tuple tuple, Class expectedType) { - return parser.parseExpression(expression).getValue(this.context, tuple, expectedType); - } - - private void write(String expression, Tuple tuple, Object value) { - parser.parseExpression(expression).setValue(this.context, tuple, value); - } - -} diff --git a/spring-cloud-stream-tuple/src/test/resources/jsonWithArrays.json b/spring-cloud-stream-tuple/src/test/resources/jsonWithArrays.json deleted file mode 100644 index ea868a54d..000000000 --- a/spring-cloud-stream-tuple/src/test/resources/jsonWithArrays.json +++ /dev/null @@ -1,53 +0,0 @@ -{ - "body" : [ { - "dataType" : "har", - "har" : { - "log" : { - "browser" : { - "name" : "Google Chrome", - "version" : "44.0.2403.155" - }, - "creator" : { - "name" : "My extension", - "version" : "0.23.6" - }, - "pages" : [ { - "_requestTimings" : { - "blocked" : -1, - "connect" : -1, - "dns" : -1, - "receive" : 11, - "send" : -1, - "ssl" : -1, - "wait" : null - }, - "_requestUrl" : "https://google.com" - }, { - "_requestTimings" : { - "blocked" : -1, - "connect" : -1, - "dns" : -1, - "receive" : 11, - "send" : -1, - "ssl" : -1, - "wait" : 244 - }, - "_requestUrl" : "https://google.com" - } ], - "version" : "1.2" - } - }, - "testId" : 1 - } ], - "bodyType" : "models.MultiMessage", - "headers" : { - "appInstance" : "localhost/127.0.0.1:8080", - "clientIp" : "0:0:0:0:0:0:0:1", - "host" : "localhost:8080", - "requestId" : "27acf948-33ff-491c-8be7-1beb4b8c95d9", - "requestMethod" : "POST", - "requestUrl" : "http://localhost:8080/har", - "timestamp" : 1445914510549, - "userPrincipal" : "235" - } -} \ No newline at end of file diff --git a/spring-cloud-stream-tuple/src/test/resources/tweet.json b/spring-cloud-stream-tuple/src/test/resources/tweet.json deleted file mode 100644 index d936f0157..000000000 --- a/spring-cloud-stream-tuple/src/test/resources/tweet.json +++ /dev/null @@ -1 +0,0 @@ -{"created_at":"Mon Jul 15 14:00:51 +0000 2013","id":1234567890000000,"id_str":"1234567890000000","text":"@someoneFollowed @ScreenName Hi","source":"web","truncated":false,"in_reply_to_status_id":1234567890,"in_reply_to_status_id_str":"1234567890","in_reply_to_user_id":12345678,"in_reply_to_user_id_str":"12345678","in_reply_to_screen_name":"someoneFollowed","user":{"id":1447674373,"id_str":"1447674373","name":"Gabriel","screen_name":"Django","location":"\u2661 Mile \u2661 Alice \u2661 Lia \u2661 ","url":null,"description":"\u25bd F\u00e3 this is a description \u25b3","protected":false,"followers_count":1569,"friends_count":1525,"listed_count":0,"created_at":"Wed May 22 01:13:27 +0000 2013","favourites_count":45,"utc_offset":-10800,"time_zone":"Brasilia","geo_enabled":false,"verified":false,"statuses_count":7930,"lang":"pt","contributors_enabled":false,"is_translator":false,"profile_background_color":"FFFFFF","profile_background_image_url":"http:\/\/a0.twimg.com\/profile_background_images\/8888000000\/abc.png","profile_background_image_url_https":"https:\/\/si0.twimg.com\/profile_background_images\/888800000013245\/abc.png","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/88000000\/449e706e706ba4574eab44904c97ed0d_normal.png","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/8888000000888888\/0_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/12345678\/12345678","profile_link_color":"5374BA","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"E6E6E6","profile_text_color":"2EC7D7","profile_use_background_image":true,"default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"someoneFollowed","name":"Someone Followed","id":12345678,"id_str":"12345678","indices":[0,13]},{"screen_name":"ScreenName","name":"My Name","id":86754321,"id_str":"86754321","indices":[14,27]}]},"favorited":false,"retweeted":false,"filter_level":"medium","lang":"und"} \ No newline at end of file diff --git a/spring-cloud-stream/pom.xml b/spring-cloud-stream/pom.xml index bcab3a05c..71c8a7a2b 100644 --- a/spring-cloud-stream/pom.xml +++ b/spring-cloud-stream/pom.xml @@ -31,8 +31,12 @@ spring-integration-core - org.springframework.cloud - spring-cloud-stream-tuple + org.springframework + spring-tuple + + + org.springframework.integration + spring-integration-tuple org.springframework.retry diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/ChannelBindingServiceConfiguration.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/ChannelBindingServiceConfiguration.java index 0e6c66a83..8817bf6ba 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/ChannelBindingServiceConfiguration.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/config/ChannelBindingServiceConfiguration.java @@ -42,7 +42,6 @@ import org.springframework.cloud.stream.binding.MessageChannelConfigurer; import org.springframework.cloud.stream.binding.MessageConverterConfigurer; import org.springframework.cloud.stream.binding.MessageHistoryTrackerConfigurer; import org.springframework.cloud.stream.binding.OutputBindingLifecycle; -import org.springframework.cloud.stream.tuple.spel.TuplePropertyAccessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.DependsOn; @@ -55,6 +54,7 @@ import org.springframework.integration.support.MessageBuilderFactory; import org.springframework.messaging.MessageChannel; import org.springframework.messaging.core.DestinationResolutionException; import org.springframework.messaging.core.DestinationResolver; +import org.springframework.tuple.spel.TuplePropertyAccessor; /** * Configuration class that provides necessary beans for {@link MessageChannel} binding. diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/JsonToTupleMessageConverter.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/JsonToTupleMessageConverter.java index c6650cba3..2bf07610f 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/JsonToTupleMessageConverter.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/JsonToTupleMessageConverter.java @@ -20,10 +20,10 @@ import java.util.ArrayList; import java.util.List; import org.springframework.messaging.Message; +import org.springframework.tuple.Tuple; +import org.springframework.tuple.TupleBuilder; import org.springframework.util.MimeType; import org.springframework.util.MimeTypeUtils; -import org.springframework.cloud.stream.tuple.Tuple; -import org.springframework.cloud.stream.tuple.TupleBuilder; /** diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/MessageConverterUtils.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/MessageConverterUtils.java index cfb854811..692ca40c3 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/MessageConverterUtils.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/MessageConverterUtils.java @@ -20,10 +20,10 @@ import static org.springframework.util.MimeType.valueOf; import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON; import static org.springframework.util.MimeTypeUtils.APPLICATION_OCTET_STREAM; +import org.springframework.tuple.DefaultTuple; +import org.springframework.tuple.Tuple; import org.springframework.util.ClassUtils; import org.springframework.util.MimeType; -import org.springframework.cloud.stream.tuple.DefaultTuple; -import org.springframework.cloud.stream.tuple.Tuple; import org.springframework.util.StringUtils; diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/PojoToStringMessageConverter.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/PojoToStringMessageConverter.java index 6b420e773..c2661c262 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/PojoToStringMessageConverter.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/PojoToStringMessageConverter.java @@ -15,9 +15,9 @@ */ package org.springframework.cloud.stream.converter; -import org.springframework.cloud.stream.tuple.Tuple; -import org.springframework.cloud.stream.tuple.TupleBuilder; import org.springframework.messaging.Message; +import org.springframework.tuple.Tuple; +import org.springframework.tuple.TupleBuilder; import org.springframework.util.MimeTypeUtils; diff --git a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/TupleToJsonMessageConverter.java b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/TupleToJsonMessageConverter.java index a6b9c1835..aea185817 100644 --- a/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/TupleToJsonMessageConverter.java +++ b/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/converter/TupleToJsonMessageConverter.java @@ -20,8 +20,8 @@ import java.io.IOException; import org.springframework.beans.factory.annotation.Value; import org.springframework.messaging.Message; +import org.springframework.tuple.Tuple; import org.springframework.util.MimeTypeUtils; -import org.springframework.cloud.stream.tuple.Tuple; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature;