FREEMARKER-54: Support Version convertion editor between string and Version 
object for spring community


Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/cb43725a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/cb43725a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/cb43725a

Branch: refs/heads/3
Commit: cb43725a79e851c11d4552d3a5ab0b6d70d43e9d
Parents: 9482852
Author: Woonsan Ko <woon...@apache.org>
Authored: Sun Jun 18 23:13:56 2017 -0400
Committer: Woonsan Ko <woon...@apache.org>
Committed: Sun Jun 18 23:13:56 2017 -0400

----------------------------------------------------------------------
 .../freemarker/core/VersionEditorTest.java      | 119 +++++++++++++++++++
 .../apache/freemarker/core/VersionEditor.java   |  53 +++++++++
 .../spring/ConfigurationFactoryBeanTest.java    |   2 +-
 .../freemarker/spring/VersionPropertyTest.java  |  76 ++++++++++++
 4 files changed, 249 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/cb43725a/freemarker-core-test/src/test/java/org/apache/freemarker/core/VersionEditorTest.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core-test/src/test/java/org/apache/freemarker/core/VersionEditorTest.java
 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/VersionEditorTest.java
new file mode 100644
index 0000000..60c4746
--- /dev/null
+++ 
b/freemarker-core-test/src/test/java/org/apache/freemarker/core/VersionEditorTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.freemarker.core;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+
+public class VersionEditorTest {
+
+    @Test
+    public void testFromString() {
+        VersionEditor editor = new VersionEditor();
+        editor.setAsText("1.2.3-beta2");
+        Version v = (Version) editor.getValue();
+        assertEquals("1.2.3-beta2", v.toString());
+        assertEquals("1.2.3-beta2", editor.getAsText());
+    }
+
+    @Test
+    public void testFromString2() {
+        VersionEditor editor = new VersionEditor();
+        editor.setAsText("01.002.0003-20130524");
+        Version v = (Version) editor.getValue();
+        assertEquals("01.002.0003-20130524", v.toString());
+        assertEquals("01.002.0003-20130524", editor.getAsText());
+
+        editor.setAsText("01.002.0003.4");
+        v = (Version) editor.getValue();
+        assertEquals("01.002.0003.4", v.toString());
+        assertEquals("01.002.0003.4", editor.getAsText());
+
+        editor.setAsText("1.2.3.FC");
+        v = (Version) editor.getValue();
+        assertEquals("1.2.3.FC", v.toString());
+        assertEquals("1.2.3.FC", editor.getAsText());
+
+        editor.setAsText("1.2.3mod");
+        v = (Version) editor.getValue();
+        assertEquals("1.2.3mod", v.toString());
+        assertEquals("1.2.3mod", editor.getAsText());
+
+    }
+
+    @Test
+    public void testFromStringIncubating() {
+        VersionEditor editor = new VersionEditor();
+        editor.setAsText("2.3.24-rc01-incubating");
+        Version v = (Version) editor.getValue();
+        assertEquals("2.3.24-rc01-incubating", v.toString());
+        assertEquals("2.3.24-rc01-incubating", editor.getAsText());
+    }
+
+    @Test
+    public void testMalformed() {
+        VersionEditor editor = new VersionEditor();
+
+        try {
+            editor.setAsText("1.2.");
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        try {
+            editor.setAsText("1.2.3.");
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        try {
+            editor.setAsText("1..3");
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        try {
+            editor.setAsText(".2");
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        try {
+            editor.setAsText("a");
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+
+        try {
+            editor.setAsText("-a");
+            fail();
+        } catch (IllegalArgumentException e) {
+            // expected
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/cb43725a/freemarker-core/src/main/java/org/apache/freemarker/core/VersionEditor.java
----------------------------------------------------------------------
diff --git 
a/freemarker-core/src/main/java/org/apache/freemarker/core/VersionEditor.java 
b/freemarker-core/src/main/java/org/apache/freemarker/core/VersionEditor.java
new file mode 100644
index 0000000..7b17ae9
--- /dev/null
+++ 
b/freemarker-core/src/main/java/org/apache/freemarker/core/VersionEditor.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.freemarker.core;
+
+import java.beans.PropertyEditor;
+import java.beans.PropertyEditorSupport;
+
+/**
+ * {@link PropertyEditor} for {@link Version}, to directly populate a 
<code>Version</code> property
+ * instead of using a String property as bridge.
+ */
+public class VersionEditor extends PropertyEditorSupport {
+
+    /**
+     * Create a new VersionEditor.
+     */
+    public VersionEditor() {
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setAsText(String versionString) throws 
IllegalArgumentException {
+        setValue(new Version(versionString));
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getAsText() {
+        Version version = (Version) getValue();
+        return (version != null ? version.toString() : null);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/cb43725a/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java
 
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java
index ddfd320..a77e944 100644
--- 
a/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java
+++ 
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/ConfigurationFactoryBeanTest.java
@@ -106,7 +106,7 @@ public class ConfigurationFactoryBeanTest {
         // </bean>
         BeanDefinition beanDef =
                 
BeanDefinitionBuilder.genericBeanDefinition(ConfigurationFactoryBean.class.getName())
-                .addPropertyValue("incompatibleImprovements", new Version(3, 
0, 0))
+                .addPropertyValue("incompatibleImprovements", "3.0.0")
                 .addPropertyValue("settings", settings)
                 .addPropertyValue("sharedVariables", sharedVars)
                 .addPropertyValue("templateUpdateDelayMilliseconds", 60000)

http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/cb43725a/freemarker-spring/src/test/java/org/apache/freemarker/spring/VersionPropertyTest.java
----------------------------------------------------------------------
diff --git 
a/freemarker-spring/src/test/java/org/apache/freemarker/spring/VersionPropertyTest.java
 
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/VersionPropertyTest.java
new file mode 100644
index 0000000..ff69a75
--- /dev/null
+++ 
b/freemarker-spring/src/test/java/org/apache/freemarker/spring/VersionPropertyTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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.freemarker.spring;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.freemarker.core.Version;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.support.BeanDefinitionBuilder;
+import org.springframework.context.support.GenericApplicationContext;
+
+public class VersionPropertyTest {
+
+    private GenericApplicationContext appContext;
+
+    @Before
+    public void setUp() throws Exception {
+        appContext = new GenericApplicationContext();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (appContext.isActive()) {
+            appContext.stop();
+            appContext.destroy();
+            appContext.close();
+        }
+    }
+
+    @Test
+    public void testVersionPropertySettingByString() throws Exception {
+        BeanDefinition beanDef =
+                
BeanDefinitionBuilder.genericBeanDefinition(VersionHolder.class.getName())
+                .addPropertyValue("version", "3.1.4-beta1592")
+                .getBeanDefinition();
+
+        appContext.registerBeanDefinition("versionHolder", beanDef);
+        appContext.refresh();
+        appContext.start();
+
+        VersionHolder versionHolder = appContext.getBean("versionHolder", 
VersionHolder.class);
+        assertEquals(new Version("3.1.4-beta1592"), 
versionHolder.getVersion());
+    }
+
+    public static class VersionHolder {
+
+        private Version version;
+
+        public Version getVersion() {
+            return version;
+        }
+
+        public void setVersion(Version version) {
+            this.version = version;
+        }
+    }
+}

Reply via email to