You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@labs.apache.org by si...@apache.org on 2008/09/16 16:25:39 UTC
svn commit: r695900 - in /labs/magma/trunk/foundation-conversion: ./ src/
src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/magma/ src/main/java/org/apache/magma/conversion/
src/main/java/org/apache/magma/co...
Author: simoneg
Date: Tue Sep 16 07:25:38 2008
New Revision: 695900
URL: http://svn.apache.org/viewvc?rev=695900&view=rev
Log:
Initial import.
Added:
labs/magma/trunk/foundation-conversion/pom.xml
labs/magma/trunk/foundation-conversion/src/
labs/magma/trunk/foundation-conversion/src/main/
labs/magma/trunk/foundation-conversion/src/main/java/
labs/magma/trunk/foundation-conversion/src/main/java/org/
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConversionInHandler.aj
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConverterInPropertyInfo.aj
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BaseConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BooleanConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ClassConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ConversionException.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converters.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DateConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DoubleConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/EnumConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/HookConversionToHandler.aj
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/IntegerConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/LongConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/PrimitiveTypeConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/StringConverter.java
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/utils/
labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/utils/UseConvertersForLocalizedMessages.aj
labs/magma/trunk/foundation-conversion/src/main/resources/
labs/magma/trunk/foundation-conversion/src/main/resources/META-INF/
labs/magma/trunk/foundation-conversion/src/main/resources/META-INF/services/
labs/magma/trunk/foundation-conversion/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter
labs/magma/trunk/foundation-conversion/src/test/
labs/magma/trunk/foundation-conversion/src/test/java/
labs/magma/trunk/foundation-conversion/src/test/java/org/
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConversionTestBean.java
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConvertersOnBeanTest.java
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestDateConverterFromStringCorrect.java
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterFromStringCorrect.java
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterToStringCorrect.java
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringCorrect.java
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringInCorrect.java
labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterToStringCorrect.java
Added: labs/magma/trunk/foundation-conversion/pom.xml
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/pom.xml?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/pom.xml (added)
+++ labs/magma/trunk/foundation-conversion/pom.xml Tue Sep 16 07:25:38 2008
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one or more
+contributor license agreements. See the NOTICE file distributed with
+this work for additional information regarding copyright ownership.
+The ASF licenses this file to You under the Apache License, Version 2.0
+(the "License"); you may not use this file except in compliance with
+the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+-->
+<project
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.apache.magma</groupId>
+ <artifactId>magma-parent</artifactId>
+ <version>1</version>
+ </parent>
+ <groupId>org.apache.magma</groupId>
+ <artifactId>foundation-conversion</artifactId>
+ <name>Foundation Conversion</name>
+ <version>0.0.1-SNAPSHOT</version>
+ <description>
+ Provides functionality to convert to and from string for bean
+ properties
+ </description>
+ <packaging>magma</packaging>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.magma.tools</groupId>
+ <artifactId>maven-magma-plugin</artifactId>
+ <extensions>true</extensions>
+ </plugin>
+ </plugins>
+ </build>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.magma</groupId>
+ <artifactId>foundation-beans</artifactId>
+ <version>0.0.1-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.4</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+</project>
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConversionInHandler.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConversionInHandler.aj?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConversionInHandler.aj (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConversionInHandler.aj Tue Sep 16 07:25:38 2008
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.beans.BeanHandler;
+import org.apache.magma.beans.PropertyInfo;
+
+public privileged aspect AddConversionInHandler {
+
+ private Map<String,String> BeanHandler.stringValues = new HashMap<String,String>();
+ private Set<String> BeanHandler.explicitlySet = new HashSet<String>();
+
+ public void BeanHandler.setStringValue(String field, String value) {
+ PropertyInfo property = data.getProperty(field);
+ if (property == null) throw new MagmaException("Cannot find a property named {0} in class {1}", field, bean.getClass().getName());
+ if (!property.isWriteable()) throw new MagmaException("Property {0} is read only in class {1}", field, bean.getClass().getName());
+ explicitlySet.add(field);
+ stringValues.put(field, value);
+ }
+
+ public void BeanHandler.setStringValues(Map<String, String> values) {
+ for (Map.Entry<String, String> entry : values.entrySet()) {
+ setStringValue(entry.getKey(), entry.getValue());
+ }
+ }
+
+ public String BeanHandler.getStringValue(String field) {
+ PropertyInfo property = data.getProperty(field);
+ if (property == null) throw new MagmaException("Cannot find a proprty named {0} in class {1}", field, bean.getClass().getName());
+ if (!property.isReadable()) throw new MagmaException("Property {0} is not readable in class {1}", field, bean.getClass().getName());
+ return stringValues.get(field);
+ }
+
+ public void BeanHandler.rollbackStringValues() {
+ this.stringValues.clear();
+ Set<String> names = this.data.getPropertyNames();
+ for (String name : names) {
+ PropertyInfo property = this.data.getProperty(name);
+ Object val = this.values.get(name);
+ if (val != null) {
+ Converter conv = property.getConverter();
+ if (conv != null) {
+ this.stringValues.put(name, conv.toString(val));
+ } else {
+ if (val instanceof String) {
+ this.stringValues.put(name, (String)val);
+ } else {
+ this.stringValues.put(name, "N/A");
+ //throw new MagmaException("Property {0}.{1} is not a string but does not have a convertor", this.data.getBeanClass().getName(), name);
+ }
+ }
+ } else {
+ this.stringValues.put(name, null);
+ }
+ }
+ }
+
+ public void BeanHandler.commitStringValues() {
+ ConversionException exc = null;
+ for (String name : explicitlySet) {
+ PropertyInfo property = data.getProperty(name);
+ String val = stringValues.get(name);
+ if (!property.isWriteable()) continue;
+ Converter conv = property.getConverter();
+ if (conv != null) {
+ try {
+ setValue(name, conv.fromString(val));
+ } catch (ConversionException e) {
+ e.setSubject(property);
+ if (exc == null) {
+ exc = e;
+ } else {
+ exc.join(e);
+ }
+ }
+ } else {
+ if (property.getType().equals(String.class)) {
+ setValue(name, (String)val);
+ } else {
+ throw new MagmaException("Property {0}.{1} is not a string but does not have a convertor", data.getBeanClass().getName(), name);
+ }
+ }
+ }
+ if (exc != null) throw exc;
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConverterInPropertyInfo.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConverterInPropertyInfo.aj?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConverterInPropertyInfo.aj (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/AddConverterInPropertyInfo.aj Tue Sep 16 07:25:38 2008
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.beans.PropertyInfo;
+
+import java.beans.PropertyDescriptor;
+import java.util.ServiceLoader;
+
+import org.apache.commons.beanutils.MethodUtils;
+
+public aspect AddConverterInPropertyInfo {
+
+ private Converter PropertyInfo.converter = null;
+
+ public Converter PropertyInfo.getConverter() {
+ return this.converter;
+ }
+
+ after(PropertyInfo info, PropertyDescriptor desc) :
+ execution(* PropertyInfo.init(PropertyDescriptor, Class)) && target(info) && args(desc,..) {
+ if (info.getType() == null) return;
+ info.converter = Converters.getConverterFor(info.getType());
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BaseConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BaseConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BaseConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BaseConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+public abstract class BaseConverter<T> implements Converter<T> {
+
+ private Class<T> myclass = null;
+
+ protected BaseConverter(Class<T> myclass) {
+ this.myclass = myclass;
+ }
+
+ public boolean converts(Class<?> clazz) {
+ return myclass.isAssignableFrom(clazz);
+ }
+
+ public Converter<T> build(Class<? extends T> clazz) {
+ return this;
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BooleanConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BooleanConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BooleanConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/BooleanConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+public class BooleanConverter extends PrimitiveTypeConverter<Boolean> {
+
+ public BooleanConverter() {
+ super(Boolean.class);
+ }
+
+ public Boolean fromString(String value) {
+ if (value == null) return onNative ? Boolean.FALSE : null;
+ value = value.toLowerCase();
+ return value.equals("t") || value.equals("true") || value.equals("yes") || value.equals("y") || value.equals("1");
+ }
+
+ public String toString(Boolean value) {
+ return value == null || !value ? "No" : "Yes";
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ClassConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ClassConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ClassConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ClassConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.basics.LocalizableString;
+
+public class ClassConverter extends BaseConverter<Class> {
+
+ public ClassConverter() {
+ super(Class.class);
+ }
+
+ public Class fromString(String value) {
+ try {
+ return Class.forName(value);
+ } catch (ClassNotFoundException e) {
+ throw new ConversionException(new LocalizableString("InvalidClass", value), e);
+ }
+ }
+
+ public String toString(Class value) {
+ return value.getName();
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ConversionException.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ConversionException.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ConversionException.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/ConversionException.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.basics.MagmaException;
+import org.apache.magma.beans.PropertyInfo;
+
+public class ConversionException extends MagmaException {
+
+
+
+ public ConversionException(String message, Object... args) {
+ super(message, args);
+ }
+
+ public ConversionException(LocalizableString error) {
+ super(error);
+ }
+
+ public ConversionException(LocalizableString error, Throwable t) {
+ super(error, t);
+ }
+
+ public ConversionException(Throwable t, String message, Object... args) {
+ super(t, message, args);
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+
+public interface Converter<T> {
+
+ public T fromString(String value);
+
+ public String toString(T value);
+
+ public boolean converts(Class<?> clazz);
+
+ public Converter<T> build(Class<? extends T> clazz);
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converters.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converters.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converters.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/Converters.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import java.util.ServiceLoader;
+
+import org.apache.commons.beanutils.MethodUtils;
+
+public class Converters {
+
+ public static <T> Converter<T> getConverterFor(Class<T> type) {
+ Class simpletype = MethodUtils.toNonPrimitiveClass(type);
+ ServiceLoader<Converter> loader = ServiceLoader.load(Converter.class);
+ for (Converter converter : loader) {
+ if (converter.converts(simpletype)) return converter.build(type);
+ }
+ return null;
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DateConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DateConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DateConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DateConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.basics.LocalizableString;
+
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+public class DateConverter extends BaseConverter<Date> {
+
+ private static List<String> patterns = new ArrayList<String>();
+
+ static {
+ patterns.add("yy");
+ patterns.add("yyyy");
+ patterns.add("yyyy-MM");
+ patterns.add("yyyy-MM-dd");
+ patterns.add("yyyy-MM-dd HH:mm");
+ patterns.add("yyyy-MM-dd HH:mm:ss");
+ patterns.add("yyyy-MM-dd HH:mm:ss.SSS");
+ }
+
+ private LocalizableString parsingError = new LocalizableString("DateConverter.notAValidDateString");
+
+ public DateConverter() {
+ super(Date.class);
+ }
+
+ public Date fromString(String value) {
+ if (value == null || value.length() == 0) return null;
+ SimpleDateFormat sdf = null;
+ int len = value.length();
+ for (String patt : patterns) {
+ if (patt.length() == len) {
+ sdf = new SimpleDateFormat(patt);
+ }
+ }
+ if (sdf == null) throw new ConversionException(parsingError);
+ Date ret = null;
+ try {
+ ret = sdf.parse(value);
+ } catch (ParseException e) {
+ throw new ConversionException(parsingError, e);
+ }
+ return ret;
+ }
+
+ public String toString(Date value) {
+ if (value == null) return "";
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+ String ret = sdf.format(value);
+ // TODO cut all 00 fields at the end of the string
+ return ret;
+ }
+
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DoubleConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DoubleConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DoubleConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/DoubleConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.basics.LocalizableString;
+
+public class DoubleConverter extends PrimitiveTypeConverter<Double> {
+
+ private LocalizableString parsingError = new LocalizableString("DoubleConverter.notANumber");
+
+ public DoubleConverter() {
+ super(Double.class);
+ }
+
+ public Double fromString(String value) {
+ if (value == null || value.length() == 0) return onNative ? 0d : null;
+ double val = 0;
+ try {
+ val = Long.parseLong(value);
+ } catch (NumberFormatException e) {
+ throw new ConversionException(parsingError);
+ }
+ return val;
+ }
+
+ public String toString(Double value) {
+ if (value == null) return "";
+ return value.toString();
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/EnumConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/EnumConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/EnumConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/EnumConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+public class EnumConverter implements Converter<Enum> {
+
+ private Class<? extends Enum> myclazz = null;
+
+ public EnumConverter() {}
+
+ public EnumConverter(Class<? extends Enum> clazz) {
+ this.myclazz = clazz;
+ }
+
+ public Enum fromString(String value) {
+ if (value == null || value.length() == 0) return null;
+ return Enum.valueOf(this.myclazz, value);
+ }
+
+ public String toString(Enum value) {
+ return value.name();
+ }
+
+ public Converter<Enum> build(Class<? extends Enum> clazz) {
+ return new EnumConverter(clazz);
+ }
+
+ public boolean converts(Class<?> clazz) {
+ return clazz.isEnum();
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/HookConversionToHandler.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/HookConversionToHandler.aj?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/HookConversionToHandler.aj (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/HookConversionToHandler.aj Tue Sep 16 07:25:38 2008
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.beans.BeanHandler;
+
+public aspect HookConversionToHandler {
+
+ after(BeanHandler handler) : execution(public void BeanHandler.rollback()) && this(handler) {
+ handler.rollbackStringValues();
+ }
+
+ before(BeanHandler handler) : execution(public void BeanHandler.commit()) && this(handler) {
+ handler.commitStringValues();
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/IntegerConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/IntegerConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/IntegerConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/IntegerConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.basics.LocalizableString;
+
+public class IntegerConverter extends PrimitiveTypeConverter<Integer> {
+
+ private LocalizableString parsingError = new LocalizableString("IntegerConverter.notANumber");
+
+ public IntegerConverter() {
+ super(Integer.class);
+ }
+
+ public Integer fromString(String value) {
+ if (value == null || value.length() == 0) return onNative ? 0 : null;
+ int val = 0;
+ try {
+ val = Integer.parseInt(value);
+ } catch (NumberFormatException e) {
+ throw new ConversionException(parsingError);
+ }
+ return val;
+ }
+
+ public String toString(Integer value) {
+ if (value == null) return "";
+ return value.toString();
+ }
+
+ @Override
+ public Converter<Integer> build(Class<? extends Integer> clazz) {
+ // TODO Auto-generated method stub
+ return super.build(clazz);
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/LongConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/LongConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/LongConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/LongConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,45 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.basics.LocalizableString;
+
+public class LongConverter extends PrimitiveTypeConverter<Long> {
+
+ private LocalizableString parsingError = new LocalizableString("LongConverter.notANumber");
+
+ public LongConverter() {
+ super(Long.class);
+ }
+
+ public Long fromString(String value) {
+ if (value == null || value.length() == 0) return onNative ? 0l : null;
+ long val = 0;
+ try {
+ val = Long.parseLong(value);
+ } catch (NumberFormatException e) {
+ throw new ConversionException(parsingError);
+ }
+ return val;
+ }
+
+ public String toString(Long value) {
+ if (value == null) return "";
+ return value.toString();
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/PrimitiveTypeConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/PrimitiveTypeConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/PrimitiveTypeConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/PrimitiveTypeConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.apache.magma.basics.MagmaException;
+
+public abstract class PrimitiveTypeConverter<T> extends BaseConverter<T> {
+
+ private PrimitiveTypeConverter<T> nativeConverter = null;
+
+ protected boolean onNative = false;
+
+ protected PrimitiveTypeConverter(Class<T> myclass) {
+ super(myclass);
+ }
+
+ @Override
+ public Converter<T> build(Class<? extends T> clazz) {
+ if (clazz.isPrimitive()) {
+ if (nativeConverter == null) {
+ try {
+ nativeConverter = getClass().newInstance();
+ } catch (Exception e) {
+ throw new MagmaException(e, "Error creating native counterpart for {0}", this.getClass());
+ }
+ nativeConverter.onNative = true;
+ }
+ return nativeConverter;
+ }
+ return super.build(clazz);
+ }
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/StringConverter.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/StringConverter.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/StringConverter.java (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/StringConverter.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+public class StringConverter extends BaseConverter<String> {
+
+ public StringConverter() {
+ super(String.class);
+ }
+
+ public String fromString(String value) {
+ return value;
+ }
+
+ public String toString(String value) {
+ return value;
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/utils/UseConvertersForLocalizedMessages.aj
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/utils/UseConvertersForLocalizedMessages.aj?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/utils/UseConvertersForLocalizedMessages.aj (added)
+++ labs/magma/trunk/foundation-conversion/src/main/java/org/apache/magma/conversion/utils/UseConvertersForLocalizedMessages.aj Tue Sep 16 07:25:38 2008
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion.utils;
+
+import java.util.Iterator;
+
+import org.apache.commons.beanutils.MethodUtils;
+import java.util.Date;
+import org.apache.magma.basics.LocalizableString;
+import org.apache.magma.conversion.Converter;
+import org.apache.magma.conversion.Converters;
+
+public privileged aspect UseConvertersForLocalizedMessages {
+
+ private boolean LocalizableString.converted = false;
+
+ before(LocalizableString ls) : execution(String LocalizableString.toString()) && this(ls) {
+ if (ls.converted) return;
+ for (int i = 0; i < ls.args.length; i++) {
+ Object arg = ls.args[i];
+ if (arg == null) continue;
+ Class<?> argclass = arg.getClass();
+ if (argclass.isPrimitive()) continue;
+ if (argclass.equals(String.class)) continue;
+ if (argclass.equals(Date.class)) continue;
+ if (MethodUtils.getPrimitiveType(argclass) != null) continue;
+ Converter converter = Converters.getConverterFor(argclass);
+ if (converter == null) {
+ ls.args[i] = arg.toString();
+ } else {
+ ls.args[i] = converter.toString(arg);
+ }
+ }
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter (added)
+++ labs/magma/trunk/foundation-conversion/src/main/resources/META-INF/services/org.apache.magma.conversion.Converter Tue Sep 16 07:25:38 2008
@@ -0,0 +1,22 @@
+#Licensed to the Apache Software Foundation (ASF) under one or more
+#contributor license agreements. See the NOTICE file distributed with
+#this work for additional information regarding copyright ownership.
+#The ASF licenses this file to You under the Apache License, Version 2.0
+#(the "License"); you may not use this file except in compliance with
+#the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+#Unless required by applicable law or agreed to in writing, software
+#distributed under the License is distributed on an "AS IS" BASIS,
+#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#See the License for the specific language governing permissions and
+#limitations under the License.
+org.apache.magma.conversion.StringConverter
+org.apache.magma.conversion.IntegerConverter
+org.apache.magma.conversion.DoubleConverter
+org.apache.magma.conversion.LongConverter
+org.apache.magma.conversion.DateConverter
+org.apache.magma.conversion.ClassConverter
+org.apache.magma.conversion.EnumConverter
+org.apache.magma.conversion.BooleanConverter
Added: labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConversionTestBean.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConversionTestBean.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConversionTestBean.java (added)
+++ labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConversionTestBean.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import org.junit.Ignore;
+
+import org.apache.magma.beans.MagmaBean;
+
+@Ignore("This is not a test class")
+@MagmaBean
+public class ConversionTestBean {
+
+ private int intField = 0;
+ private Integer integerField = Integer.valueOf(5);
+ private String stringField = "test";
+ private String nullField = null;
+
+ public enum TestEnum {
+ VALA,
+ VALB,
+ VALC
+ }
+
+ private TestEnum enumField = TestEnum.VALA;
+
+ public int getIntField() {
+ return intField;
+ }
+ public void setIntField(int intField) {
+ this.intField = intField;
+ }
+ public Integer getIntegerField() {
+ return integerField;
+ }
+ public void setIntegerField(Integer integerField) {
+ this.integerField = integerField;
+ }
+ public String getStringField() {
+ return stringField;
+ }
+ public void setStringField(String stringField) {
+ this.stringField = stringField;
+ }
+ public String getNullField() {
+ return nullField;
+ }
+ public void setNullField(String nullField) {
+ this.nullField = nullField;
+ }
+ public TestEnum getEnumField() {
+ return enumField;
+ }
+ public void setEnumField(TestEnum enumField) {
+ this.enumField = enumField;
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConvertersOnBeanTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConvertersOnBeanTest.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConvertersOnBeanTest.java (added)
+++ labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/ConvertersOnBeanTest.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.magma.conversion;
+
+import static org.junit.Assert.assertEquals;
+import org.apache.magma.beans.BeanData;
+import org.apache.magma.beans.BeanHandler;
+import org.apache.magma.beans.PropertyInfo;
+import org.apache.magma.conversion.ConversionTestBean.TestEnum;
+
+import org.junit.Before;
+import org.junit.Test;
+
+
+public class ConvertersOnBeanTest {
+
+ private ConversionTestBean bean = null;
+ private BeanData beanData = null;
+
+ @Before
+ public void initBean() {
+ bean = new ConversionTestBean();
+ beanData = bean.beanData();
+ }
+
+ @Test
+ public void integerBeanProps() throws Exception {
+ PropertyInfo property = beanData.getProperty("intField");
+ //assertEquals(PrimitiveUnwrappingConverter.class.getName(), property.getConverter().getClass().getName());
+ assertEquals(IntegerConverter.class.getName(), property.getConverter().getClass().getName());
+ property = beanData.getProperty("integerField");
+ assertEquals(IntegerConverter.class.getName(), property.getConverter().getClass().getName());
+ }
+
+ @Test
+ public void handlingConversion() throws Exception {
+ BeanHandler handler = bean.handler();
+ assertEquals("0", handler.getStringValue("intField"));
+
+ handler.setStringValue("intField", "1");
+ handler.commit();
+
+ assertEquals(1, bean.getIntField());
+
+ handler.setStringValue("stringField", "test");
+ handler.commit();
+ assertEquals("test", bean.getStringField());
+
+ handler.setStringValue("enumField", TestEnum.VALB.name());
+ handler.commit();
+ assertEquals(TestEnum.VALB, bean.getEnumField());
+ }
+
+ @Test
+ public void handlingPrimitivesDifferently() throws Exception {
+ bean.setIntField(1);
+
+ BeanHandler handler = bean.handler();
+ assertEquals("1", handler.getStringValue("intField"));
+
+ handler.setStringValue("intField", "");
+ handler.setStringValue("integerField", "");
+ handler.commit();
+
+ assertEquals(0, bean.getIntField());
+ assertEquals(null, bean.getIntegerField());
+ }
+
+ @Test
+ public void mixedSet() throws Exception {
+ BeanHandler handler = bean.handler();
+ handler.setStringValue("intField", "1");
+ handler.setValue("stringField", "test");
+ handler.commit();
+
+ assertEquals(1, bean.getIntField());
+ assertEquals("test", bean.getStringField());
+ }
+
+
+}
Added: labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestDateConverterFromStringCorrect.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestDateConverterFromStringCorrect.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestDateConverterFromStringCorrect.java (added)
+++ labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestDateConverterFromStringCorrect.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ *
+ */
+package org.apache.magma.conversion;
+
+import static org.junit.Assert.assertEquals;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+import org.junit.Before;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+@RunWith(Theories.class)
+public class TestDateConverterFromStringCorrect {
+ public DateConverter converter;
+ public SimpleDateFormat sdf;
+
+ @Before
+ public void initConverter() {
+ converter = new DateConverter();
+ sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
+ }
+
+ @DataPoint public static String[]
+ set1 = {"2008", "2008-01-01 00:00:00.000"},
+ set2 = {"2008-02", "2008-02-01 00:00:00.000"},
+ set3 = {"2008-02-02", "2008-02-02 00:00:00.000"},
+ set4 = {"2008-02-02 15:40", "2008-02-02 15:40:00.000"},
+ set5 = {"2008-02-02 15:40:10", "2008-02-02 15:40:10.000"},
+ set6 = {"2008-02-02 15:40:10.423", "2008-02-02 15:40:10.423"};
+
+ @Theory public void convert(String[] vals) {
+ Date d = converter.fromString(vals[0]);
+ String ctrl = sdf.format(d);
+ assertEquals(ctrl, vals[1]);
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterFromStringCorrect.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterFromStringCorrect.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterFromStringCorrect.java (added)
+++ labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterFromStringCorrect.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ *
+ */
+package org.apache.magma.conversion;
+
+import static org.junit.Assert.assertEquals;
+import org.apache.magma.conversion.ConversionTestBean.TestEnum;
+
+import org.junit.Before;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+@RunWith(Theories.class)
+public class TestEnumConverterFromStringCorrect {
+ public EnumConverter converter;
+
+ @Before
+ public void initConverter() {
+ converter = new EnumConverter(ConversionTestBean.TestEnum.class);
+ }
+
+ @DataPoint public static Object[]
+ set1 = {TestEnum.VALA.name(), TestEnum.VALA},
+ set2 = {TestEnum.VALB.name(), TestEnum.VALB},
+ set3 = {TestEnum.VALB.name(), TestEnum.VALB};
+
+ @Theory public void convert(Object[] vals) {
+ assertEquals(converter.fromString((String)vals[0]), vals[1]);
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterToStringCorrect.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterToStringCorrect.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterToStringCorrect.java (added)
+++ labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestEnumConverterToStringCorrect.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ *
+ */
+package org.apache.magma.conversion;
+
+import static org.junit.Assert.assertEquals;
+import org.apache.magma.conversion.ConversionTestBean.TestEnum;
+
+import org.junit.Before;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+@RunWith(Theories.class)
+public class TestEnumConverterToStringCorrect {
+ public EnumConverter converter;
+
+ @Before
+ public void initConverter() {
+ converter = new EnumConverter(TestEnum.class);
+ }
+
+ @DataPoint public static Object[]
+ set1 = {TestEnum.VALA, TestEnum.VALA.name()},
+ set2 = {TestEnum.VALB, TestEnum.VALB.name()},
+ set3 = {TestEnum.VALB, TestEnum.VALB.name()};
+
+ @Theory public void convert(Object[] vals) {
+ assertEquals(converter.toString((TestEnum)vals[0]), vals[1]);
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringCorrect.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringCorrect.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringCorrect.java (added)
+++ labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringCorrect.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ *
+ */
+package org.apache.magma.conversion;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+@RunWith(Theories.class)
+public class TestIntegerConverterFromStringCorrect {
+ public IntegerConverter converter;
+
+ @Before
+ public void initConverter() {
+ converter = new IntegerConverter();
+ }
+
+ @DataPoint public static Object[]
+ set1 = {"1", 1},
+ set2 = {"0", 0},
+ set3 = {"-1", -1},
+ set4 = {Integer.toString(Integer.MAX_VALUE), Integer.MAX_VALUE};
+
+ @Theory public void convert(Object[] vals) {
+ assertEquals(converter.fromString((String)vals[0]), vals[1]);
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringInCorrect.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringInCorrect.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringInCorrect.java (added)
+++ labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterFromStringInCorrect.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ *
+ */
+package org.apache.magma.conversion;
+
+import static org.junit.Assert.fail;
+
+import org.junit.Before;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+@RunWith(Theories.class)
+public class TestIntegerConverterFromStringInCorrect {
+ public IntegerConverter converter;
+
+ @Before
+ public void initConverter() {
+ converter = new IntegerConverter();
+ }
+
+ @DataPoint public static String
+ set1 = "a",
+ set2 = "1.2",
+ set3 = "1a";
+
+ @Theory
+ public void convert(String val) {
+ try {
+ converter.fromString(val);
+ fail("Should throw exception for value " + val);
+ } catch (ConversionException e) {
+ return;
+ }
+ }
+
+}
Added: labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterToStringCorrect.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterToStringCorrect.java?rev=695900&view=auto
==============================================================================
--- labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterToStringCorrect.java (added)
+++ labs/magma/trunk/foundation-conversion/src/test/java/org/apache/magma/conversion/TestIntegerConverterToStringCorrect.java Tue Sep 16 07:25:38 2008
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/**
+ *
+ */
+package org.apache.magma.conversion;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.experimental.theories.DataPoint;
+import org.junit.experimental.theories.Theories;
+import org.junit.experimental.theories.Theory;
+import org.junit.runner.RunWith;
+
+@RunWith(Theories.class)
+public class TestIntegerConverterToStringCorrect {
+ public IntegerConverter converter;
+
+ @Before
+ public void initConverter() {
+ converter = new IntegerConverter();
+ }
+
+ @DataPoint public static Object[]
+ set1 = {"1", 1},
+ set2 = {"0", 0},
+ set3 = {"-1", -1},
+ set4 = {Integer.toString(Integer.MAX_VALUE), Integer.MAX_VALUE},
+ set5 = {"", null};
+
+ @Theory public void convert(Object[] vals) {
+ assertEquals(converter.toString((Integer)vals[1]), vals[0]);
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org