You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:55:15 UTC

[sling-org-apache-sling-models-impl] 02/16: SLING-3547 - correctly dealing with primitive and wrapper default objects. applying slightly modified version of patch from Konrad to split out primitive from wrapper model classes.

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.models.impl-1.0.6
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git

commit 063b5d2ca8d46702e863530df4eba166f07b3716
Author: Justin Edelson <ju...@apache.org>
AuthorDate: Tue May 13 19:35:35 2014 +0000

    SLING-3547 - correctly dealing with primitive and wrapper default objects. applying slightly modified version of patch from Konrad to split out primitive from wrapper model classes.
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1594343 13f79535-47bb-0310-9956-ffa450edef68
---
 .../sling/models/impl/ModelAdapterFactory.java     | 31 ++++++++---
 .../org/apache/sling/models/impl/DefaultTest.java  | 39 ++++++++++++++
 .../testmodels/classes/DefaultPrimitivesModel.java | 58 +++++++++++++++++++++
 .../testmodels/classes/DefaultWrappersModel.java   | 60 ++++++++++++++++++++++
 4 files changed, 182 insertions(+), 6 deletions(-)

diff --git a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
index 7a686fc..21ddc76 100644
--- a/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
+++ b/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
@@ -47,6 +47,7 @@ import javax.inject.Inject;
 import javax.inject.Named;
 
 import org.apache.commons.beanutils.PropertyUtils;
+import org.apache.commons.lang.ArrayUtils;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -446,21 +447,39 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable {
                 if (componentType == Integer.TYPE) {
                     return defaultAnnotation.intValues();
                 }
+                if (componentType == Integer.class) {
+                    return ArrayUtils.toObject(defaultAnnotation.intValues());
+                }
                 if (componentType == Long.TYPE) {
                     return defaultAnnotation.longValues();
                 }
+                if (componentType == Long.class) {
+                    return ArrayUtils.toObject(defaultAnnotation.longValues());
+                }
                 if (componentType == Boolean.TYPE) {
                     return defaultAnnotation.booleanValues();
                 }
+                if (componentType == Boolean.class) {
+                    return ArrayUtils.toObject(defaultAnnotation.booleanValues());
+                }
                 if (componentType == Short.TYPE) {
                     return defaultAnnotation.shortValues();
                 }
+                if (componentType == Short.class) {
+                    return ArrayUtils.toObject(defaultAnnotation.shortValues());
+                }
                 if (componentType == Float.TYPE) {
                     return defaultAnnotation.floatValues();
                 }
+                if (componentType == Float.class) {
+                    return ArrayUtils.toObject(defaultAnnotation.floatValues());
+                }
                 if (componentType == Double.TYPE) {
                     return defaultAnnotation.doubleValues();
                 }
+                if (componentType == Double.class) {
+                    return ArrayUtils.toObject(defaultAnnotation.doubleValues());
+                }
 
                 log.warn("Default values for {} are not supported", componentType);
                 return null;
@@ -468,22 +487,22 @@ public class ModelAdapterFactory implements AdapterFactory, Runnable {
                 if (injectedClass == String.class) {
                     return defaultAnnotation.values()[0];
                 }
-                if (injectedClass == Integer.TYPE) {
+                if (injectedClass == Integer.class) {
                     return defaultAnnotation.intValues()[0];
                 }
-                if (injectedClass == Long.TYPE) {
+                if (injectedClass == Long.class) {
                     return defaultAnnotation.longValues()[0];
                 }
-                if (injectedClass == Boolean.TYPE) {
+                if (injectedClass == Boolean.class) {
                     return defaultAnnotation.booleanValues()[0];
                 }
-                if (injectedClass == Short.TYPE) {
+                if (injectedClass == Short.class) {
                     return defaultAnnotation.shortValues()[0];
                 }
-                if (injectedClass == Float.TYPE) {
+                if (injectedClass == Float.class) {
                     return defaultAnnotation.floatValues()[0];
                 }
-                if (injectedClass == Double.TYPE) {
+                if (injectedClass == Double.class) {
                     return defaultAnnotation.doubleValues()[0];
                 }
 
diff --git a/src/test/java/org/apache/sling/models/impl/DefaultTest.java b/src/test/java/org/apache/sling/models/impl/DefaultTest.java
index a0ca9e7..32787d3 100644
--- a/src/test/java/org/apache/sling/models/impl/DefaultTest.java
+++ b/src/test/java/org/apache/sling/models/impl/DefaultTest.java
@@ -19,13 +19,16 @@ package org.apache.sling.models.impl;
 import static org.junit.Assert.*;
 import static org.mockito.Mockito.*;
 
+import java.util.Arrays;
 import java.util.Collections;
 
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ValueMap;
 import org.apache.sling.api.wrappers.ValueMapDecorator;
 import org.apache.sling.models.impl.injectors.ValueMapInjector;
+import org.apache.sling.models.testmodels.classes.DefaultPrimitivesModel;
 import org.apache.sling.models.testmodels.classes.DefaultStringModel;
+import org.apache.sling.models.testmodels.classes.DefaultWrappersModel;
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -67,4 +70,40 @@ public class DefaultTest {
         assertEquals("firstDefault", model.getFirstProperty());
         assertEquals(2, model.getSecondProperty().length);
     }
+
+    @Test
+    public void testDefaultPrimitives() {
+        ValueMap vm = new ValueMapDecorator(Collections.<String, Object>emptyMap());
+
+        Resource res = mock(Resource.class);
+        when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+        DefaultPrimitivesModel model = factory.getAdapter(res, DefaultPrimitivesModel.class);
+        assertNotNull(model);
+
+        assertEquals(true, model.getBooleanProperty());
+        // we need to wait for JUnit 4.12 for this assertArrayEquals to be working on primitive boolean arrays, https://github.com/junit-team/junit/issues/86!
+        assertTrue(Arrays.equals(new boolean[] { true, true }, model.getBooleanArrayProperty()));
+
+        assertEquals(1L, model.getLongProperty());
+        assertArrayEquals(new long[] { 1L, 1L }, model.getLongArrayProperty());
+    }
+
+    @Test
+    public void testDefaultWrappers() {
+        ValueMap vm = new ValueMapDecorator(Collections.<String, Object>emptyMap());
+
+        Resource res = mock(Resource.class);
+        when(res.adaptTo(ValueMap.class)).thenReturn(vm);
+
+        DefaultWrappersModel model = factory.getAdapter(res, DefaultWrappersModel.class);
+        assertNotNull(model);
+
+        assertEquals(Boolean.valueOf(true), model.getBooleanWrapperProperty());
+        // we need to wait for JUnit 4.12 for this assertArrayEquals to be working on primitive boolean arrays, https://github.com/junit-team/junit/issues/86!
+        assertTrue(Arrays.equals(new Boolean[] { Boolean.TRUE, Boolean.TRUE }, model.getBooleanWrapperArrayProperty()));
+
+        assertEquals(Long.valueOf(1L), model.getLongWrapperProperty());
+        assertArrayEquals(new Long[] { Long.valueOf(1L), Long.valueOf(1L) }, model.getLongWrapperArrayProperty());
+    }
 }
diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/DefaultPrimitivesModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/DefaultPrimitivesModel.java
new file mode 100644
index 0000000..b90d708
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/testmodels/classes/DefaultPrimitivesModel.java
@@ -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.sling.models.testmodels.classes;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Default;
+import org.apache.sling.models.annotations.Model;
+
+@Model(adaptables = Resource.class)
+public class DefaultPrimitivesModel {
+
+    @Inject
+    @Default(booleanValues = true)
+    private boolean booleanProperty;
+    
+    @Inject
+    @Default(booleanValues = { true, true })
+    private boolean[] booleanArrayProperty;
+
+    @Inject
+    @Default(longValues = 1L)
+    private long longProperty;
+    
+    @Inject
+    @Default(longValues = { 1L, 1L })
+    private long[] longArrayProperty;
+    public boolean getBooleanProperty() {
+        return booleanProperty;
+    }
+
+    public boolean[] getBooleanArrayProperty() {
+        return booleanArrayProperty;
+    }
+
+    public long getLongProperty() {
+        return longProperty;
+    }
+
+    public long[] getLongArrayProperty() {
+        return longArrayProperty;
+    }
+}
diff --git a/src/test/java/org/apache/sling/models/testmodels/classes/DefaultWrappersModel.java b/src/test/java/org/apache/sling/models/testmodels/classes/DefaultWrappersModel.java
new file mode 100644
index 0000000..a64c23c
--- /dev/null
+++ b/src/test/java/org/apache/sling/models/testmodels/classes/DefaultWrappersModel.java
@@ -0,0 +1,60 @@
+/*
+ * 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.models.testmodels.classes;
+
+import javax.inject.Inject;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Default;
+import org.apache.sling.models.annotations.Model;
+
+@Model(adaptables = Resource.class)
+public class DefaultWrappersModel {
+
+    @Inject
+    @Default(booleanValues = true)
+    private Boolean booleanWrapperProperty;
+
+    @Inject
+    @Default(booleanValues = { true, true })
+    private Boolean[] booleanWrapperArrayProperty;
+
+    @Inject
+    @Default(longValues = 1L)
+    private Long longWrapperProperty;
+
+    @Inject
+    @Default(longValues = { 1L, 1L })
+    private Long[] longWrapperArrayProperty;
+
+    public Boolean getBooleanWrapperProperty() {
+        return booleanWrapperProperty;
+    }
+
+    public Boolean[] getBooleanWrapperArrayProperty() {
+        return booleanWrapperArrayProperty;
+    }
+
+    public Long getLongWrapperProperty() {
+        return longWrapperProperty;
+    }
+
+    public Long[] getLongWrapperArrayProperty() {
+        return longWrapperArrayProperty;
+    }
+
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.