Author: radu Date: Fri Sep 1 11:10:34 2017 New Revision: 1806931 URL: http://svn.apache.org/viewvc?rev=1806931&view=rev Log: SLING-7097 - The SlingRuntimeObjectModel should adapt a target object to ValueMap as a last resort
* delegate first to the super class in SlingRuntimeObjectModel#getProperty * added tests for all possible Adaptable interactions Added: sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/runtime/ sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModelTest.java Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModel.java Modified: sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModel.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModel.java?rev=1806931&r1=1806930&r2=1806931&view=diff ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModel.java (original) +++ sling/trunk/bundles/scripting/sightly/engine/src/main/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModel.java Fri Sep 1 11:10:34 2017 @@ -26,17 +26,14 @@ public class SlingRuntimeObjectModel ext @Override protected Object getProperty(Object target, Object propertyObj) { - Object result = null; - if (target instanceof Adaptable) { + Object result = super.getProperty(target, propertyObj); + if (result == null && target instanceof Adaptable) { ValueMap valueMap = ((Adaptable) target).adaptTo(ValueMap.class); if (valueMap != null) { String property = toString(propertyObj); result = valueMap.get(property); } } - if (result == null) { - result = super.getProperty(target, propertyObj); - } return result; } Added: sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModelTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModelTest.java?rev=1806931&view=auto ============================================================================== --- sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModelTest.java (added) +++ sling/trunk/bundles/scripting/sightly/engine/src/test/java/org/apache/sling/scripting/sightly/impl/engine/runtime/SlingRuntimeObjectModelTest.java Fri Sep 1 11:10:34 2017 @@ -0,0 +1,86 @@ +/******************************************************************************* + * 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.sling.scripting.sightly.impl.engine.runtime; + +import java.util.HashMap; +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +import org.apache.sling.api.adapter.Adaptable; +import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.api.wrappers.ValueMapDecorator; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SlingRuntimeObjectModelTest { + + private static final String VALUE_MAP_VALUE = "ValueMap value"; + private static final String METHOD_VALUE = "Method value"; + private SlingRuntimeObjectModel slingRuntimeObjectModel = new SlingRuntimeObjectModel(); + + @Test + public void getPropertyFromAdaptableWithField() throws Exception { + assertEquals("Expected public fields to have priority over ValueMap adaptable's properties.", FieldTestMockAdaptable.test, + slingRuntimeObjectModel.getProperty(new FieldTestMockAdaptable(), "test")); + } + + @Test + public void getPropertyFromAdaptableWithMethod() throws Exception { + assertEquals("Expected public methods to have priority over ValueMap adaptable's properties.", METHOD_VALUE, + slingRuntimeObjectModel.getProperty(new MethodTestMockAdaptable(), "test")); + }@Test + public void getPropertyFromAdaptable() throws Exception { + assertEquals("Expected to solve property from ValueMap returned by an adaptable.", VALUE_MAP_VALUE, + slingRuntimeObjectModel.getProperty(new AdaptableTestMock(), "test")); + } + + private abstract class MockAdaptable implements Adaptable { + + ValueMap getValueMap() { + return new ValueMapDecorator(new HashMap<String, Object>() {{ + put("test", VALUE_MAP_VALUE); + }}); + } + + @CheckForNull + @Override + public <AdapterType> AdapterType adaptTo(@Nonnull Class<AdapterType> aClass) { + if (aClass == ValueMap.class) { + return (AdapterType) getValueMap(); + } + return null; + } + } + + public class FieldTestMockAdaptable extends MockAdaptable { + + public static final String test = "Field value"; + + } + + public class MethodTestMockAdaptable extends MockAdaptable { + public String getTest() { + return METHOD_VALUE; + } + } + + public class AdaptableTestMock extends MockAdaptable {} + +}