Repository: incubator-freemarker Updated Branches: refs/heads/2.3-gae edc0e411f -> 25d4695e8
In Eclipse excluded classes that require Java 8 language level, because setting that language level on the project will generate diamonds, which then won't compile with Java 5. Project: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/commit/25d4695e Tree: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/tree/25d4695e Diff: http://git-wip-us.apache.org/repos/asf/incubator-freemarker/diff/25d4695e Branch: refs/heads/2.3-gae Commit: 25d4695e842f765fc45dac4cc2c1f756b0242d70 Parents: edc0e41 Author: ddekany <[email protected]> Authored: Tue Mar 14 14:22:14 2017 +0100 Committer: ddekany <[email protected]> Committed: Tue Mar 14 14:22:14 2017 +0100 ---------------------------------------------------------------------- README | 3 + .../template/DefaultEnumerationAdapter.java | 5 - .../beans/BeansWrapperBridgeMethodsTest.java | 65 ----- .../ext/beans/BeansWrapperJava8Test.java | 239 ------------------- .../BridgeMethodsWithDefaultMethodBean.java | 29 --- .../BridgeMethodsWithDefaultMethodBean2.java | 23 -- .../BridgeMethodsWithDefaultMethodBeanBase.java | 31 --- ...BridgeMethodsWithDefaultMethodBeanBase2.java | 28 --- .../Java8BeansWrapperBridgeMethodsTest.java | 65 +++++ .../ext/beans/Java8BeansWrapperTest.java | 239 +++++++++++++++++++ ...Java8BridgeMethodsWithDefaultMethodBean.java | 29 +++ ...ava8BridgeMethodsWithDefaultMethodBean2.java | 23 ++ ...8BridgeMethodsWithDefaultMethodBeanBase.java | 31 +++ ...BridgeMethodsWithDefaultMethodBeanBase2.java | 28 +++ 14 files changed, 418 insertions(+), 420 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/README ---------------------------------------------------------------------- diff --git a/README b/README index bcd8f20..f26c29e 100644 --- a/README +++ b/README @@ -200,6 +200,8 @@ apply it to your development environment: - Press "Finish" - Eclipse will indicate many errors at this point; it's expected, read on. - Project -> Properties -> Java Compiler + - Set "Compiler Compliance Level" to "1.5" (you will have to uncheck + "Use compliance from execution environment" for that) - In Errors/Warnings, check in "Enable project specific settings", then set "Forbidden reference (access rules)" from "Error" to "Warning". - You will still have errors on these java files (because different java @@ -210,6 +212,7 @@ apply it to your development environment: _Jython22*.java _FreeMarkerPageContext2.java FreeMarkerJspFactory2.java + Java8*.java Also, close these files if they are open. Now you shouldn't have any errors. - At Project -> Properties -> Java Code Style -> Formatter, check in "Enable project specific settings", and then select "FreeMarker" as active profile. http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/main/java/freemarker/template/DefaultEnumerationAdapter.java ---------------------------------------------------------------------- diff --git a/src/main/java/freemarker/template/DefaultEnumerationAdapter.java b/src/main/java/freemarker/template/DefaultEnumerationAdapter.java index 59549b4..6a26fc1 100644 --- a/src/main/java/freemarker/template/DefaultEnumerationAdapter.java +++ b/src/main/java/freemarker/template/DefaultEnumerationAdapter.java @@ -56,17 +56,14 @@ public class DefaultEnumerationAdapter extends WrappingTemplateModel implements this.enumeration = enumeration; } - @Override public Object getWrappedObject() { return enumeration; } - @Override public Object getAdaptedObject(Class<?> hint) { return getWrappedObject(); } - @Override public TemplateModelIterator iterator() throws TemplateModelException { return new SimpleTemplateModelIterator(); } @@ -82,7 +79,6 @@ public class DefaultEnumerationAdapter extends WrappingTemplateModel implements private boolean enumerationOwnedByMe; - @Override public TemplateModel next() throws TemplateModelException { if (!enumerationOwnedByMe) { checkNotOwner(); @@ -98,7 +94,6 @@ public class DefaultEnumerationAdapter extends WrappingTemplateModel implements return value instanceof TemplateModel ? (TemplateModel) value : wrap(value); } - @Override public boolean hasNext() throws TemplateModelException { // Calling hasNext may looks safe, but I have met sync. problems. if (!enumerationOwnedByMe) { http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/BeansWrapperBridgeMethodsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/BeansWrapperBridgeMethodsTest.java b/src/test/java/freemarker/ext/beans/BeansWrapperBridgeMethodsTest.java deleted file mode 100644 index 634fae4..0000000 --- a/src/test/java/freemarker/ext/beans/BeansWrapperBridgeMethodsTest.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * 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 freemarker.ext.beans; - -import static org.junit.Assert.*; - -import java.util.Collections; - -import org.junit.Test; - -import freemarker.template.Configuration; -import freemarker.template.TemplateHashModel; -import freemarker.template.TemplateMethodModelEx; -import freemarker.template.TemplateModelException; - -public class BeansWrapperBridgeMethodsTest { - - @Test - public void testWithoutDefaultMethod() throws TemplateModelException { - test(BridgeMethodsBean.class); - } - - @Test - public void testWithDefaultMethod() throws TemplateModelException { - test(BridgeMethodsWithDefaultMethodBean.class); - } - - @Test - public void testWithDefaultMethod2() throws TemplateModelException { - test(BridgeMethodsWithDefaultMethodBean2.class); - } - - private void test(Class<?> pClass) throws TemplateModelException { - BeansWrapper ow = new BeansWrapperBuilder(Configuration.VERSION_2_3_26).build(); - TemplateHashModel wrapped; - try { - wrapped = (TemplateHashModel) ow.wrap(pClass.newInstance()); - } catch (Exception e) { - throw new IllegalStateException(e); - } - - TemplateMethodModelEx m1 = (TemplateMethodModelEx) wrapped.get("m1"); - assertEquals(BridgeMethodsBean.M1_RETURN_VALUE, "" + m1.exec(Collections.emptyList())); - - TemplateMethodModelEx m2 = (TemplateMethodModelEx) wrapped.get("m2"); - assertNull(m2.exec(Collections.emptyList())); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/BeansWrapperJava8Test.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/BeansWrapperJava8Test.java b/src/test/java/freemarker/ext/beans/BeansWrapperJava8Test.java deleted file mode 100644 index abdb0f5..0000000 --- a/src/test/java/freemarker/ext/beans/BeansWrapperJava8Test.java +++ /dev/null @@ -1,239 +0,0 @@ -/* - * 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 freemarker.ext.beans; - -import static org.junit.Assert.*; - -import java.util.Collections; - -import org.junit.Test; - -import freemarker.template.Configuration; -import freemarker.template.TemplateHashModel; -import freemarker.template.TemplateMethodModelEx; -import freemarker.template.TemplateModelException; -import freemarker.template.TemplateNumberModel; -import freemarker.template.TemplateScalarModel; -import freemarker.template.TemplateSequenceModel; - -public class BeansWrapperJava8Test { - - @Test - public void testDefaultMethodNotRecognized() throws TemplateModelException { - BeansWrapperBuilder owb = new BeansWrapperBuilder(Configuration.VERSION_2_3_0); - // owb.setTreatDefaultMethodsAsBeanMembers(false); - BeansWrapper ow = owb.build(); - TemplateHashModel wrappedBean = (TemplateHashModel) ow.wrap(new Java8DefaultMethodsBean()); - - { - TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_PROP); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.NORMAL_PROP_VALUE, prop.getAsString()); - } - { - // This is overridden in the subclass, so it's visible even without default method support: - TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_PROP_2); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.PROP_2_OVERRIDE_VALUE, prop.getAsString()); - } - assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP)); - assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP)); - { - // We don't see the default method indexed reader, but see the plain reader method in the subclass. - TemplateNumberModel prop = (TemplateNumberModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_VALUE, prop.getAsNumber()); - } - { - // We don't see the default method non-indexed reader that would spoil the indexed reader in the subclass. - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_2_VALUE, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - { - // We don't see the default method non-indexed reader that would spoil the indexed reader in the subclass. - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_3_VALUE, - ((TemplateNumberModel) prop.get(0)).getAsNumber()); - } - { - // We don't see the default method indexed reader, but see the plain array reader in the subclass. - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.ARRAY_PROP_2_VALUE_0, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - { - // Only present in the subclass. - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.INDEXED_PROP_4); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_4_VALUE, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - { - // We don't see the default method non-indexed reader - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_3); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_3_VALUE, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - - { - TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( - Java8DefaultMethodsBean.NORMAL_ACTION); - assertNotNull(action); - assertEquals( - Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, - ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); - } - assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_ACTION)); - { - TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( - Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION); - assertNotNull(action); - assertEquals( - Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION_RETURN_VALUE, - ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); - } - } - - @Test - public void testDefaultMethodRecognized() throws TemplateModelException { - BeansWrapperBuilder owb = new BeansWrapperBuilder(Configuration.VERSION_2_3_0); - owb.setTreatDefaultMethodsAsBeanMembers(true); - BeansWrapper ow = owb.build(); - TemplateHashModel wrappedBean = (TemplateHashModel) ow.wrap(new Java8DefaultMethodsBean()); - - { - TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_PROP); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.NORMAL_PROP_VALUE, prop.getAsString()); - } - { - // This is overridden in the subclass, so it's visible even without default method support: - TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_PROP_2); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.PROP_2_OVERRIDE_VALUE, prop.getAsString()); - } - { - TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get( - Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP_VALUE, prop.getAsString()); - } - { - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP_VALUE, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - { - // We see default method indexed read method, but it's invalidated by normal getter in the subclass - TemplateNumberModel prop = (TemplateNumberModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_VALUE, prop.getAsNumber()); - } - { - // The default method read method invalidates the indexed read method in the subclass - TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2_VALUE, prop.getAsString()); - } - { - // The default method read method invalidates the indexed read method in the subclass - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3_VALUE_0, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - { - // We see the default method indexed reader, which overrides the plain array reader in the subclass. - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2_VALUE_0, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - { - // We do see the default method non-indexed reader, but the subclass has a matching indexed reader, so that - // takes over. - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_3); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_3_VALUE, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - { - // Only present in the subclass. - TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( - Java8DefaultMethodsBean.INDEXED_PROP_4); - assertNotNull(prop); - assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_4_VALUE, - ((TemplateScalarModel) prop.get(0)).getAsString()); - } - { - TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( - Java8DefaultMethodsBean.NORMAL_ACTION); - assertNotNull(action); - assertEquals( - Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, - ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); - } - - { - TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( - Java8DefaultMethodsBean.NORMAL_ACTION); - assertNotNull(action); - assertEquals( - Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, - ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); - } - { - TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( - Java8DefaultMethodsBean.DEFAULT_METHOD_ACTION); - assertNotNull(action); - assertEquals( - Java8DefaultMethodsBean.DEFAULT_METHOD_ACTION_RETURN_VALUE, - ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); - } - { - TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( - Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION); - assertNotNull(action); - assertEquals( - Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION_RETURN_VALUE, - ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); - } - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBean.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBean.java b/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBean.java deleted file mode 100644 index 25b67fe..0000000 --- a/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBean.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 freemarker.ext.beans; - -public class BridgeMethodsWithDefaultMethodBean implements BridgeMethodsWithDefaultMethodBeanBase<String> { - - static final String M1_RETURN_VALUE = "m1ReturnValue"; - - public String m1() { - return M1_RETURN_VALUE; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBean2.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBean2.java b/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBean2.java deleted file mode 100644 index a2261c9..0000000 --- a/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBean2.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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 freemarker.ext.beans; - -public class BridgeMethodsWithDefaultMethodBean2 implements BridgeMethodsWithDefaultMethodBeanBase2 { - // All inherited -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBeanBase.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBeanBase.java b/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBeanBase.java deleted file mode 100644 index 2dbd459..0000000 --- a/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBeanBase.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 freemarker.ext.beans; - -public interface BridgeMethodsWithDefaultMethodBeanBase<T> { - - default T m1() { - return null; - } - - default T m2() { - return null; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBeanBase2.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBeanBase2.java b/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBeanBase2.java deleted file mode 100644 index 2886d8f..0000000 --- a/src/test/java/freemarker/ext/beans/BridgeMethodsWithDefaultMethodBeanBase2.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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 freemarker.ext.beans; - -public interface BridgeMethodsWithDefaultMethodBeanBase2 extends BridgeMethodsWithDefaultMethodBeanBase<String> { - - @Override - default String m1() { - return BridgeMethodsWithDefaultMethodBean.M1_RETURN_VALUE; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/Java8BeansWrapperBridgeMethodsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/Java8BeansWrapperBridgeMethodsTest.java b/src/test/java/freemarker/ext/beans/Java8BeansWrapperBridgeMethodsTest.java new file mode 100644 index 0000000..11d1d9e --- /dev/null +++ b/src/test/java/freemarker/ext/beans/Java8BeansWrapperBridgeMethodsTest.java @@ -0,0 +1,65 @@ +/* + * 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 freemarker.ext.beans; + +import static org.junit.Assert.*; + +import java.util.Collections; + +import org.junit.Test; + +import freemarker.template.Configuration; +import freemarker.template.TemplateHashModel; +import freemarker.template.TemplateMethodModelEx; +import freemarker.template.TemplateModelException; + +public class Java8BeansWrapperBridgeMethodsTest { + + @Test + public void testWithoutDefaultMethod() throws TemplateModelException { + test(BridgeMethodsBean.class); + } + + @Test + public void testWithDefaultMethod() throws TemplateModelException { + test(Java8BridgeMethodsWithDefaultMethodBean.class); + } + + @Test + public void testWithDefaultMethod2() throws TemplateModelException { + test(Java8BridgeMethodsWithDefaultMethodBean2.class); + } + + private void test(Class<?> pClass) throws TemplateModelException { + BeansWrapper ow = new BeansWrapperBuilder(Configuration.VERSION_2_3_26).build(); + TemplateHashModel wrapped; + try { + wrapped = (TemplateHashModel) ow.wrap(pClass.newInstance()); + } catch (Exception e) { + throw new IllegalStateException(e); + } + + TemplateMethodModelEx m1 = (TemplateMethodModelEx) wrapped.get("m1"); + assertEquals(BridgeMethodsBean.M1_RETURN_VALUE, "" + m1.exec(Collections.emptyList())); + + TemplateMethodModelEx m2 = (TemplateMethodModelEx) wrapped.get("m2"); + assertNull(m2.exec(Collections.emptyList())); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/Java8BeansWrapperTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/Java8BeansWrapperTest.java b/src/test/java/freemarker/ext/beans/Java8BeansWrapperTest.java new file mode 100644 index 0000000..e385dab --- /dev/null +++ b/src/test/java/freemarker/ext/beans/Java8BeansWrapperTest.java @@ -0,0 +1,239 @@ +/* + * 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 freemarker.ext.beans; + +import static org.junit.Assert.*; + +import java.util.Collections; + +import org.junit.Test; + +import freemarker.template.Configuration; +import freemarker.template.TemplateHashModel; +import freemarker.template.TemplateMethodModelEx; +import freemarker.template.TemplateModelException; +import freemarker.template.TemplateNumberModel; +import freemarker.template.TemplateScalarModel; +import freemarker.template.TemplateSequenceModel; + +public class Java8BeansWrapperTest { + + @Test + public void testDefaultMethodNotRecognized() throws TemplateModelException { + BeansWrapperBuilder owb = new BeansWrapperBuilder(Configuration.VERSION_2_3_0); + // owb.setTreatDefaultMethodsAsBeanMembers(false); + BeansWrapper ow = owb.build(); + TemplateHashModel wrappedBean = (TemplateHashModel) ow.wrap(new Java8DefaultMethodsBean()); + + { + TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_PROP); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.NORMAL_PROP_VALUE, prop.getAsString()); + } + { + // This is overridden in the subclass, so it's visible even without default method support: + TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_PROP_2); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.PROP_2_OVERRIDE_VALUE, prop.getAsString()); + } + assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP)); + assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP)); + { + // We don't see the default method indexed reader, but see the plain reader method in the subclass. + TemplateNumberModel prop = (TemplateNumberModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_VALUE, prop.getAsNumber()); + } + { + // We don't see the default method non-indexed reader that would spoil the indexed reader in the subclass. + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_2_VALUE, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + { + // We don't see the default method non-indexed reader that would spoil the indexed reader in the subclass. + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_3_VALUE, + ((TemplateNumberModel) prop.get(0)).getAsNumber()); + } + { + // We don't see the default method indexed reader, but see the plain array reader in the subclass. + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.ARRAY_PROP_2_VALUE_0, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + { + // Only present in the subclass. + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.INDEXED_PROP_4); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_4_VALUE, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + { + // We don't see the default method non-indexed reader + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_3); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_3_VALUE, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + + { + TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( + Java8DefaultMethodsBean.NORMAL_ACTION); + assertNotNull(action); + assertEquals( + Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, + ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); + } + assertNull(wrappedBean.get(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_ACTION)); + { + TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( + Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION); + assertNotNull(action); + assertEquals( + Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION_RETURN_VALUE, + ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); + } + } + + @Test + public void testDefaultMethodRecognized() throws TemplateModelException { + BeansWrapperBuilder owb = new BeansWrapperBuilder(Configuration.VERSION_2_3_0); + owb.setTreatDefaultMethodsAsBeanMembers(true); + BeansWrapper ow = owb.build(); + TemplateHashModel wrappedBean = (TemplateHashModel) ow.wrap(new Java8DefaultMethodsBean()); + + { + TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get(Java8DefaultMethodsBean.NORMAL_PROP); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.NORMAL_PROP_VALUE, prop.getAsString()); + } + { + // This is overridden in the subclass, so it's visible even without default method support: + TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_PROP_2); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.PROP_2_OVERRIDE_VALUE, prop.getAsString()); + } + { + TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get( + Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_PROP_VALUE, prop.getAsString()); + } + { + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBeanBase.DEFAULT_METHOD_INDEXED_PROP_VALUE, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + { + // We see default method indexed read method, but it's invalidated by normal getter in the subclass + TemplateNumberModel prop = (TemplateNumberModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.NOT_AN_INDEXED_PROP_VALUE, prop.getAsNumber()); + } + { + // The default method read method invalidates the indexed read method in the subclass + TemplateScalarModel prop = (TemplateScalarModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_2_VALUE, prop.getAsString()); + } + { + // The default method read method invalidates the indexed read method in the subclass + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_NOT_AN_INDEXED_PROP_3_VALUE_0, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + { + // We see the default method indexed reader, which overrides the plain array reader in the subclass. + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_2_VALUE_0, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + { + // We do see the default method non-indexed reader, but the subclass has a matching indexed reader, so that + // takes over. + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_INDEXED_PROP_3); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_3_VALUE, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + { + // Only present in the subclass. + TemplateSequenceModel prop = (TemplateSequenceModel) wrappedBean.get( + Java8DefaultMethodsBean.INDEXED_PROP_4); + assertNotNull(prop); + assertEquals(Java8DefaultMethodsBean.INDEXED_PROP_4_VALUE, + ((TemplateScalarModel) prop.get(0)).getAsString()); + } + { + TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( + Java8DefaultMethodsBean.NORMAL_ACTION); + assertNotNull(action); + assertEquals( + Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, + ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); + } + + { + TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( + Java8DefaultMethodsBean.NORMAL_ACTION); + assertNotNull(action); + assertEquals( + Java8DefaultMethodsBean.NORMAL_ACTION_RETURN_VALUE, + ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); + } + { + TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( + Java8DefaultMethodsBean.DEFAULT_METHOD_ACTION); + assertNotNull(action); + assertEquals( + Java8DefaultMethodsBean.DEFAULT_METHOD_ACTION_RETURN_VALUE, + ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); + } + { + TemplateMethodModelEx action = (TemplateMethodModelEx) wrappedBean.get( + Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION); + assertNotNull(action); + assertEquals( + Java8DefaultMethodsBean.OVERRIDDEN_DEFAULT_METHOD_ACTION_RETURN_VALUE, + ((TemplateScalarModel) action.exec(Collections.emptyList())).getAsString()); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBean.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBean.java b/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBean.java new file mode 100644 index 0000000..d6df29f --- /dev/null +++ b/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBean.java @@ -0,0 +1,29 @@ +/* + * 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 freemarker.ext.beans; + +public class Java8BridgeMethodsWithDefaultMethodBean implements Java8BridgeMethodsWithDefaultMethodBeanBase<String> { + + static final String M1_RETURN_VALUE = "m1ReturnValue"; + + public String m1() { + return M1_RETURN_VALUE; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBean2.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBean2.java b/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBean2.java new file mode 100644 index 0000000..b61c2af --- /dev/null +++ b/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBean2.java @@ -0,0 +1,23 @@ +/* + * 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 freemarker.ext.beans; + +public class Java8BridgeMethodsWithDefaultMethodBean2 implements Java8BridgeMethodsWithDefaultMethodBeanBase2 { + // All inherited +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBeanBase.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBeanBase.java b/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBeanBase.java new file mode 100644 index 0000000..7ef0b1c --- /dev/null +++ b/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBeanBase.java @@ -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 freemarker.ext.beans; + +public interface Java8BridgeMethodsWithDefaultMethodBeanBase<T> { + + default T m1() { + return null; + } + + default T m2() { + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-freemarker/blob/25d4695e/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBeanBase2.java ---------------------------------------------------------------------- diff --git a/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBeanBase2.java b/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBeanBase2.java new file mode 100644 index 0000000..aa218f5 --- /dev/null +++ b/src/test/java/freemarker/ext/beans/Java8BridgeMethodsWithDefaultMethodBeanBase2.java @@ -0,0 +1,28 @@ +/* + * 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 freemarker.ext.beans; + +public interface Java8BridgeMethodsWithDefaultMethodBeanBase2 extends Java8BridgeMethodsWithDefaultMethodBeanBase<String> { + + @Override + default String m1() { + return Java8BridgeMethodsWithDefaultMethodBean.M1_RETURN_VALUE; + } + +}
