You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2014/05/13 21:35:35 UTC
svn commit: r1594343 - in /sling/trunk/bundles/extensions/models/impl/src:
main/java/org/apache/sling/models/impl/
test/java/org/apache/sling/models/impl/
test/java/org/apache/sling/models/testmodels/classes/
Author: justin
Date: Tue May 13 19:35:35 2014
New Revision: 1594343
URL: http://svn.apache.org/r1594343
Log:
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.
Added:
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultPrimitivesModel.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultWrappersModel.java
Modified:
sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/DefaultTest.java
Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java?rev=1594343&r1=1594342&r2=1594343&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java Tue May 13 19:35:35 2014
@@ -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 impleme
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 impleme
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];
}
Modified: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/DefaultTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/DefaultTest.java?rev=1594343&r1=1594342&r2=1594343&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/DefaultTest.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/impl/DefaultTest.java Tue May 13 19:35:35 2014
@@ -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());
+ }
}
Added: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultPrimitivesModel.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultPrimitivesModel.java?rev=1594343&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultPrimitivesModel.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultPrimitivesModel.java Tue May 13 19:35:35 2014
@@ -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;
+ }
+}
Added: sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultWrappersModel.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultWrappersModel.java?rev=1594343&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultWrappersModel.java (added)
+++ sling/trunk/bundles/extensions/models/impl/src/test/java/org/apache/sling/models/testmodels/classes/DefaultWrappersModel.java Tue May 13 19:35:35 2014
@@ -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;
+ }
+
+}