diff --git a/core/src/main/java/com/exadel/aem/toolkit/core/CoreConstants.java b/core/src/main/java/com/exadel/aem/toolkit/core/CoreConstants.java index 38bb08e47..b531ce239 100644 --- a/core/src/main/java/com/exadel/aem/toolkit/core/CoreConstants.java +++ b/core/src/main/java/com/exadel/aem/toolkit/core/CoreConstants.java @@ -29,6 +29,7 @@ public class CoreConstants { public static final String PN_APPEND = "append"; public static final String PN_CHECKED = "checked"; + public static final String PN_DEFAULT_VALUE = "defaultValue"; public static final String PN_ITEM_RESOURCE_TYPE = "itemResourceType"; public static final String PN_LIMIT = "limit"; public static final String PN_LIST_ITEM = "listItem"; diff --git a/core/src/main/java/com/exadel/aem/toolkit/core/injectors/BaseInjector.java b/core/src/main/java/com/exadel/aem/toolkit/core/injectors/BaseInjector.java index b91805586..57c0751cf 100644 --- a/core/src/main/java/com/exadel/aem/toolkit/core/injectors/BaseInjector.java +++ b/core/src/main/java/com/exadel/aem/toolkit/core/injectors/BaseInjector.java @@ -21,13 +21,18 @@ import javax.annotation.CheckForNull; import javax.annotation.Nonnull; +import org.apache.commons.lang3.ArrayUtils; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.spi.DisposalCallbackRegistry; import org.apache.sling.models.spi.Injector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.exadel.aem.toolkit.core.injectors.utils.CastResult; +import com.exadel.aem.toolkit.core.injectors.utils.CastUtil; + /** * Represents a base for a Sling injector. A descendant of this class must extract an annotation from a Java class * member and provide a value that matches the annotation. This value is subsequently assigned to the Java class member @@ -72,8 +77,17 @@ public final Object getValue( if (Objects.isNull(value)) { logNullValue(element, annotation); } + return populateDefaultValue(value, type, element); + } - return value; + protected Object populateDefaultValue(Object value, Type type, AnnotatedElement element) { + Object effectiveValue = value instanceof CastResult ? ((CastResult) value).getValue() : value; + boolean isFallback = value instanceof CastResult && ((CastResult) value).isFallback(); + if ((effectiveValue == null || isFallback) && element.isAnnotationPresent(Default.class)) { + Object defaultValue = getDefaultValue(element.getDeclaredAnnotation(Default.class)); + effectiveValue = CastUtil.toType(defaultValue, type).getValue(); + } + return effectiveValue; } /** @@ -112,4 +126,24 @@ private void logNullValue(AnnotatedElement annotatedElement, T annotation) { LOG.debug(BRIEF_INJECTION_ERROR_MESSAGE, annotation); } } + + private Object getDefaultValue(Default defaultAnnotation) { + if (ArrayUtils.isNotEmpty(defaultAnnotation.values())) { + return defaultAnnotation.values(); + } else if (ArrayUtils.isNotEmpty(defaultAnnotation.booleanValues())) { + return defaultAnnotation.booleanValues(); + } else if (ArrayUtils.isNotEmpty(defaultAnnotation.doubleValues())) { + return defaultAnnotation.doubleValues(); + } else if (ArrayUtils.isNotEmpty(defaultAnnotation.floatValues())) { + return defaultAnnotation.floatValues(); + } else if (ArrayUtils.isNotEmpty(defaultAnnotation.intValues())) { + return defaultAnnotation.intValues(); + } else if (ArrayUtils.isNotEmpty(defaultAnnotation.longValues())) { + return defaultAnnotation.longValues(); + } else if (ArrayUtils.isNotEmpty(defaultAnnotation.shortValues())) { + return defaultAnnotation.shortValues(); + } else { + return new String[0]; + } + } } diff --git a/core/src/main/java/com/exadel/aem/toolkit/core/injectors/RequestParamInjector.java b/core/src/main/java/com/exadel/aem/toolkit/core/injectors/RequestParamInjector.java index a39420a6f..b7ef80a28 100644 --- a/core/src/main/java/com/exadel/aem/toolkit/core/injectors/RequestParamInjector.java +++ b/core/src/main/java/com/exadel/aem/toolkit/core/injectors/RequestParamInjector.java @@ -121,5 +121,4 @@ private String[] getRequestParameterValues(SlingHttpServletRequest request, Stri .filter(StringUtils::isNotEmpty) .toArray(String[]::new); } - } diff --git a/core/src/main/java/com/exadel/aem/toolkit/core/injectors/utils/CastResult.java b/core/src/main/java/com/exadel/aem/toolkit/core/injectors/utils/CastResult.java new file mode 100644 index 000000000..1e55efb36 --- /dev/null +++ b/core/src/main/java/com/exadel/aem/toolkit/core/injectors/utils/CastResult.java @@ -0,0 +1,20 @@ +package com.exadel.aem.toolkit.core.injectors.utils; + +public class CastResult { + + private final Object value; + private final boolean isFallback; + + CastResult(Object value, boolean isFallback) { + this.value = value; + this.isFallback = isFallback; + } + + public Object getValue() { + return value; + } + + public boolean isFallback() { + return isFallback; + } +} diff --git a/core/src/main/java/com/exadel/aem/toolkit/core/injectors/utils/CastUtil.java b/core/src/main/java/com/exadel/aem/toolkit/core/injectors/utils/CastUtil.java index 36bdaf6f4..473fd636a 100644 --- a/core/src/main/java/com/exadel/aem/toolkit/core/injectors/utils/CastUtil.java +++ b/core/src/main/java/com/exadel/aem/toolkit/core/injectors/utils/CastUtil.java @@ -53,7 +53,7 @@ private CastUtil() { * @param type A {@link Type} reference; usually reflects the type of Java class member * @return Transformed value; returns null in case type casting is not possible */ - public static Object toType(Object value, Type type) { + public static CastResult toType(Object value, Type type) { return toType(value, type, CastUtil::toInstanceOfType); } @@ -67,27 +67,28 @@ public static Object toType(Object value, Type type) { * object or primitive, the {@code converter} processes it as is * @return Transformed value; returns null in case type casting is not possible */ - public static Object toType(Object value, Type type, BiFunction converter) { + public static CastResult toType(Object value, Type type, BiFunction converter) { if (value == null) { return null; } + boolean fallback = value.getClass().isArray() && Array.getLength(value) == 0; Class elementType = TypeUtil.getElementType(type); if (TypeUtil.isArray(type)) { - return toArray(value, elementType, converter); + return new CastResult(toArray(value, elementType, converter), fallback); } if (TypeUtil.isSupportedCollection(type, true)) { return Set.class.equals(TypeUtil.getRawType(type)) - ? toCollection(value, elementType, converter, LinkedHashSet::new) - : toCollection(value, elementType, converter, ArrayList::new); + ? new CastResult(toCollection(value, elementType, converter, LinkedHashSet::new), fallback) + : new CastResult(toCollection(value, elementType, converter, ArrayList::new), fallback); } if (Object.class.equals(type)) { - return converter.apply(value, type); + return new CastResult(converter.apply(value, type), fallback); } - return converter.apply(extractFirstElement(value), type); + return new CastResult(converter.apply(extractFirstElement(value), type), fallback); } /** @@ -108,7 +109,7 @@ private static Object toArray(Object value, Class type, BiFunction assertTrue(model.getObjectValue() instanceof RequestParameter)); } + @Test + public void shouldInjectDefaultString() { + super.shouldInjectDefaultString(); + } + @Test public void shouldInjectStringArray() { super.shouldInjectStringArray((model, payload) -> { @@ -78,6 +83,11 @@ public void shouldInjectStringArray() { }); } + @Test + public void shouldInjectDefaultStringArray() { + super.shouldInjectDefaultStringArray(); + } + @Test public void shouldInjectStringCollection() { super.shouldInjectStringCollection((model, payload) -> { @@ -91,11 +101,21 @@ public void shouldInjectInteger() { super.shouldInjectInteger(RequestParamInjectorTest::assertObjectValueEquals); } + @Test + public void shouldInjectDefaultInteger() { + super.shouldInjectDefaultInteger(); + } + @Test public void shouldInjectIntegerArray() { super.shouldInjectIntegerCollection(RequestParamInjectorTest::assertArrayOrCollectionValueEquals); } + @Test + public void shouldInjectDefaultIntegerArray() { + super.shouldInjectDefaultIntegerArray(); + } + @Test public void shouldInjectIntegerCollection() { super.shouldInjectIntegerCollection(RequestParamInjectorTest::assertArrayOrCollectionValueEquals); @@ -111,11 +131,21 @@ public void shouldInjectLong() { super.shouldInjectLong(RequestParamInjectorTest::assertObjectValueEquals); } + @Test + public void shouldInjectDefaultLong() { + super.shouldInjectDefaultLong(); + } + @Test public void shouldInjectLongArray() { super.shouldInjectLongArray(RequestParamInjectorTest::assertArrayOrCollectionValueEquals); } + @Test + public void shouldInjectDefaultLongArray() { + super.shouldInjectDefaultLongArray(); + } + @Test public void shouldInjectLongCollection() { super.shouldInjectLongCollection(RequestParamInjectorTest::assertArrayOrCollectionValueEquals); @@ -131,11 +161,21 @@ public void shouldInjectDouble() { super.shouldInjectDouble(RequestParamInjectorTest::assertObjectValueEquals); } + @Test + public void shouldInjectDefaultDouble() { + super.shouldInjectDefaultDouble(); + } + @Test public void shouldInjectDoubleArray() { super.shouldInjectDoubleArray(RequestParamInjectorTest::assertArrayOrCollectionValueEquals); } + @Test + public void shouldInjectDefaultDoubleArray() { + super.shouldInjectDefaultDoubleArray(); + } + @Test public void shouldInjectDoubleCollection() { super.shouldInjectDoubleCollection(RequestParamInjectorTest::assertArrayOrCollectionValueEquals); @@ -151,11 +191,21 @@ public void shouldInjectBoolean() { super.shouldInjectBoolean(RequestParamInjectorTest::assertObjectValueEquals); } + @Test + public void shouldInjectDefaultBoolean() { + super.shouldInjectDefaultBoolean(); + } + @Test public void shouldInjectBooleanArray() { super.shouldInjectBooleanArray(RequestParamInjectorTest::assertArrayOrCollectionValueEquals); } + @Test + public void shouldInjectDefaultBooleanArray() { + super.shouldInjectDefaultBooleanArray(); + } + @Test public void shouldInjectBooleanCollection() { super.shouldInjectBooleanCollection(RequestParamInjectorTest::assertArrayOrCollectionValueEquals); diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestPropertyInjectorTestBase.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestPropertyInjectorTestBase.java index 8a6f32f56..3197a38cf 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestPropertyInjectorTestBase.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestPropertyInjectorTestBase.java @@ -63,7 +63,9 @@ abstract class RequestPropertyInjectorTestBase { private static final String MODELS_PACKAGE_NAME = CoreConstants.ROOT_PACKAGE + ".core.injectors.models.requestproperty"; private static final String EXPECTED_STRING = "Hello World"; + private static final String EXPECTED_DEFAULT_STRING = "default"; static final String[] EXPECTED_STRING_ARRAY = {"Hello", "World"}; + static final String[] EXPECTED_DEFAULT_STRING_ARRAY = {"Default", "values"}; private static final List EXPECTED_STRING_LIST = Arrays.asList(EXPECTED_STRING_ARRAY); private static final byte EXPECTED_BYTE = 42; @@ -72,12 +74,16 @@ abstract class RequestPropertyInjectorTestBase { private static final List HALF_PARSEABLE_INTEGERS = Arrays.asList("foo", 42); private static final Integer EXPECTED_INTEGER = 42; + private static final Integer EXPECTED_DEFAULT_INTEGER = 10; private static final int[] EXPECTED_INTEGER_ARRAY = {42, 43, 44}; + private static final int[] EXPECTED_DEFAULT_INTEGER_ARRAY = {10, 11, 12}; private static final List EXPECTED_INTEGER_LIST = Arrays.asList(42, 43, 44); private static final List EXPECTED_HALF_PARSED_INTEGER_LIST = Arrays.asList(0, 42); private static final long EXPECTED_LONG = 42L; + private static final long EXPECTED_DEFAULT_LONG = 10L; private static final long[] EXPECTED_LONG_ARRAY = {42L, 43L, 44}; + private static final long[] EXPECTED_DEFAULT_LONG_ARRAY = {10L, 11L, 12L}; private static final List EXPECTED_LONG_LIST = Arrays.asList(42L, 43L, 44L); private static final List EXPECTED_HALF_PARSED_LONG_LIST = Arrays.asList(0L, 42L); @@ -87,11 +93,14 @@ abstract class RequestPropertyInjectorTestBase { private static final List HALF_PARSEABLE_FLOATING_POINT_VALUES = Arrays.asList("foo", 42.1f, 43.1d, "44.1d"); private static final double EXPECTED_DOUBLE = 42.1d; + private static final double EXPECTED_DEFAULT_DOUBLE = 1.1d; private static final double[] EXPECTED_DOUBLE_ARRAY = {42.1d, 43.1d, 44.1d}; + private static final double[] EXPECTED_DEFAULT_DOUBLE_ARRAY = {1.1d, 1.2d, 1.3d}; static final List EXPECTED_DOUBLE_LIST = Arrays.asList(42.1d, 43.1d, 44.1d); static final List EXPECTED_HALF_PARSED_DOUBLE_LIST = Arrays.asList(0d, 42.1d, 43.1d, 44.1d); private static final boolean[] EXPECTED_BOOLEAN_ARRAY = {true, true, false}; + private static final boolean[] EXPECTED_DEFAULT_BOOLEAN_ARRAY = {true, false, true}; private static final List EXPECTED_BOOLEAN_LIST = Arrays.asList(true, true, false); @Rule @@ -129,6 +138,12 @@ void shouldInjectString(Consumer> objectValueChecker) { assertEquals(EXPECTED_STRING, model.getValueSupplier().getValue()); } + void shouldInjectDefaultString() { + Strings model = context.request().adaptTo(Strings.class); + assertNotNull(model); + assertEquals(EXPECTED_DEFAULT_STRING, model.getDefaultValue()); + } + void shouldInjectStringArray() { shouldInjectStringArray(RequestPropertyInjectorTestBase::assertObjectValueEquals); } @@ -157,6 +172,12 @@ void shouldInjectStringArray(BiConsumer, Object> objectVal assertEquals(EXPECTED_STRING, model.getConstructorValue()[0]); } + void shouldInjectDefaultStringArray() { + StringArrays model = context.request().adaptTo(StringArrays.class); + assertNotNull(model); + assertArrayEquals(EXPECTED_DEFAULT_STRING_ARRAY, model.getDefaultValue()); + } + void shouldInjectStringCollection() { shouldInjectStringArray((model, payload) -> { if (EXPECTED_STRING_LIST.equals(payload)) { @@ -220,6 +241,12 @@ void shouldInjectInteger(BiConsumer, Object> objectValueCh assertEquals(0, model.getValueSupplier().getValue().intValue()); } + void shouldInjectDefaultInteger() { + Integers model = context.request().adaptTo(Integers.class); + assertNotNull(model); + assertEquals(EXPECTED_DEFAULT_INTEGER, model.getDefaultValue()); + } + void shouldInjectIntegerArray() { shouldInjectIntegerArray(RequestPropertyInjectorTestBase::assertObjectValueEquals); } @@ -290,6 +317,12 @@ void shouldInjectIntegerCollection(BiConsumer, Object> obj assertEquals(EXPECTED_INTEGER, model.getConstructorValue().toArray()[0]); } + void shouldInjectDefaultIntegerArray() { + IntegerCollections model = context.request().adaptTo(IntegerCollections.class); + assertNotNull(model); + assertArrayEquals(EXPECTED_DEFAULT_INTEGER_ARRAY, model.getDefaultValue()); + } + void shouldInjectUnparseableIntegerCollection() { shouldInjectUnparseableIntegerCollection(RequestPropertyInjectorTestBase::assertObjectValueEquals); } @@ -335,6 +368,13 @@ void shouldInjectLong(BiConsumer, Object> objectValueCheck assertEquals(0L, model.getValueSupplier().getValue().longValue()); } + void shouldInjectDefaultLong() { + Longs model = context.request().adaptTo(Longs.class); + assertNotNull(model); + assertNotNull(model.getDefaultValue()); + assertEquals(EXPECTED_DEFAULT_LONG, model.getDefaultValue().longValue()); + } + void shouldInjectLongArray() { shouldInjectLongArray(RequestPropertyInjectorTestBase::assertObjectValueEquals); } @@ -369,6 +409,12 @@ void shouldInjectLongArray(BiConsumer, Object> objectValue assertEquals(EXPECTED_LONG, (long) model.getConstructorValue()[0]); } + void shouldInjectDefaultLongArray() { + LongArrays model = context.request().adaptTo(LongArrays.class); + assertNotNull(model); + assertArrayEquals(EXPECTED_DEFAULT_LONG_ARRAY, model.getDefaultValue()); + } + void shouldInjectLongCollection() { shouldInjectLongCollection(RequestPropertyInjectorTestBase::assertObjectValueEquals); } @@ -451,6 +497,13 @@ void shouldInjectDouble(BiConsumer, Object> objectValueChe assertEquals(0, model.getValueSupplier().getValue().intValue()); } + void shouldInjectDefaultDouble() { + Doubles model = context.request().adaptTo(Doubles.class); + assertNotNull(model); + assertNotNull(model.getDefaultValue()); + assertEquals(EXPECTED_DEFAULT_DOUBLE, model.getDefaultValue(), 0.0001); + } + void shouldInjectDoubleArray() { shouldInjectDoubleArray(RequestPropertyInjectorTestBase::assertObjectValueEquals); } @@ -484,6 +537,12 @@ void shouldInjectDoubleArray(BiConsumer, Object> objectVal assertEquals(EXPECTED_DOUBLE, model.getConstructorValue()[0], 0.0d); } + void shouldInjectDefaultDoubleArray() { + DoubleArrays model = context.request().adaptTo(DoubleArrays.class); + assertNotNull(model); + assertArrayEquals(EXPECTED_DEFAULT_DOUBLE_ARRAY, model.getDefaultValue(), 0.0001); + } + void shouldInjectDoubleCollection() { shouldInjectDoubleCollection(RequestPropertyInjectorTestBase::assertObjectValueEquals); } @@ -549,6 +608,12 @@ void shouldInjectBoolean(BiConsumer, Object> objectValueCh assertTrue(model.getValueSupplier().getValue()); } + void shouldInjectDefaultBoolean() { + Booleans model = context.request().adaptTo(Booleans.class); + assertNotNull(model); + assertTrue(model.getDefaultValue()); + } + void shouldInjectBooleanArray() { shouldInjectBooleanArray(RequestPropertyInjectorTestBase::assertObjectValueEquals); } @@ -576,6 +641,12 @@ void shouldInjectBooleanArray(BiConsumer, Object> objectVa assertTrue(model.getConstructorValue()[0]); } + void shouldInjectDefaultBooleanArray() { + BooleanArrays model = context.request().adaptTo(BooleanArrays.class); + assertNotNull(model); + assertArrayEquals(EXPECTED_DEFAULT_BOOLEAN_ARRAY, model.getDefaultValue()); + } + void shouldInjectBooleanCollection() { shouldInjectBooleanCollection(RequestPropertyInjectorTestBase::assertObjectValueEquals); } diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestSelectorsInjectorTest.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestSelectorsInjectorTest.java index 73926f415..cf82bdd0c 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestSelectorsInjectorTest.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestSelectorsInjectorTest.java @@ -56,11 +56,21 @@ public void shouldInjectString() { super.shouldInjectString(); } + @Test + public void shouldInjectDefaultString() { + super.shouldInjectDefaultString(); + } + @Test public void shouldInjectStringArray() { super.shouldInjectStringArray(RequestSelectorsInjectorTest::assertStringifiedCollectionsEqual); } + @Test + public void shouldInjectDefaultStringArray() { + super.shouldInjectDefaultStringArray(); + } + @Test public void shouldInjectStringCollection() { super.shouldInjectStringCollection(RequestSelectorsInjectorTest::assertStringifiedCollectionsEqual); @@ -71,11 +81,21 @@ public void shouldInjectInteger() { super.shouldInjectInteger(RequestSelectorsInjectorTest::assertStringifiedValuesEqual); } + @Test + public void shouldInjectDefaultInteger() { + super.shouldInjectDefaultInteger(); + } + @Test public void shouldInjectIntegerArray() { super.shouldInjectIntegerArray(RequestSelectorsInjectorTest::assertStringifiedCollectionsEqual); } + @Test + public void shouldInjectDefaultIntegerArray() { + super.shouldInjectDefaultIntegerArray(); + } + @Test public void shouldInjectIntegerCollection() { super.shouldInjectIntegerCollection(RequestSelectorsInjectorTest::assertStringifiedCollectionsEqual); @@ -91,11 +111,21 @@ public void shouldInjectLong() { super.shouldInjectLong(RequestSelectorsInjectorTest::assertStringifiedValuesEqual); } + @Test + public void shouldInjectDefaultLong() { + super.shouldInjectDefaultLong(); + } + @Test public void shouldInjectLongArray() { super.shouldInjectLongArray(RequestSelectorsInjectorTest::assertStringifiedCollectionsEqual); } + @Test + public void shouldInjectDefaultLongArray() { + super.shouldInjectDefaultLongArray(); + } + @Test public void shouldInjectLongCollection() { super.shouldInjectLongCollection(RequestSelectorsInjectorTest::assertStringifiedCollectionsEqual); @@ -111,11 +141,21 @@ public void shouldInjectBoolean() { super.shouldInjectBoolean(RequestSelectorsInjectorTest::assertStringifiedValuesEqual); } + @Test + public void shouldInjectDefaultBoolean() { + super.shouldInjectDefaultBoolean(); + } + @Test public void shouldInjectBooleanArray() { super.shouldInjectBooleanArray(RequestSelectorsInjectorTest::assertStringifiedCollectionsEqual); } + @Test + public void shouldInjectDefaultBooleanArray() { + super.shouldInjectDefaultBooleanArray(); + } + @Test public void shouldInjectBooleanCollection() { super.shouldInjectBooleanCollection(RequestSelectorsInjectorTest::assertStringifiedCollectionsEqual); diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestSuffixInjectorTest.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestSuffixInjectorTest.java index caffb756a..96ba23726 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestSuffixInjectorTest.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/RequestSuffixInjectorTest.java @@ -62,6 +62,11 @@ public void shouldInjectString() { model.getObjectValue())); } + @Test + public void shouldInjectDefaultString() { + super.shouldInjectDefaultString(); + } + @Test public void shouldInjectResource() { ((MockRequestPathInfo) context.request().getRequestPathInfo()).setSuffix(DEFAULT_SUFFIX); diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/BooleanArrays.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/BooleanArrays.java index 0a1020975..9d7a6699d 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/BooleanArrays.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/BooleanArrays.java @@ -18,6 +18,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -33,6 +34,10 @@ public class BooleanArrays extends RequestAdapterBase { @RequestProperty private boolean[] value; + @Default(booleanValues = {true, false, true}) + @RequestProperty + private boolean[] defaultValue; + @Self private Supplier supplier; @@ -45,6 +50,10 @@ public Boolean[] getValue() { return ArrayUtils.toObject(value); } + public boolean[] getDefaultValue() { + return defaultValue; + } + @Override public ValueSupplier getValueSupplier() { return supplier; diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Booleans.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Booleans.java index 2bc52d75d..8ccf4e2ea 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Booleans.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Booleans.java @@ -17,6 +17,7 @@ import javax.inject.Named; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -32,6 +33,10 @@ public class Booleans extends RequestAdapterBase { @RequestProperty private boolean value; + @Default(booleanValues = true) + @RequestProperty + private boolean defaultValue; + @Self private Supplier supplier; @@ -44,6 +49,10 @@ public Boolean getValue() { return value; } + public boolean getDefaultValue() { + return defaultValue; + } + @Override public ValueSupplier getValueSupplier() { return supplier; diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/DoubleArrays.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/DoubleArrays.java index ad7eea455..deabde19a 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/DoubleArrays.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/DoubleArrays.java @@ -18,6 +18,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -33,6 +34,10 @@ public class DoubleArrays extends RequestAdapterBase { @RequestProperty private double[] value; + @Default(doubleValues = {1.1d, 1.2d, 1.3d}) + @RequestProperty + private double[] defaultValue; + @Self private Supplier supplier; @@ -45,6 +50,10 @@ public Double[] getValue() { return ArrayUtils.toObject(value); } + public double[] getDefaultValue() { + return defaultValue; + } + @Override public ValueSupplier getValueSupplier() { return supplier; diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Doubles.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Doubles.java index 67f93a391..3351c03be 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Doubles.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Doubles.java @@ -18,6 +18,7 @@ import javax.inject.Named; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -33,6 +34,10 @@ public class Doubles extends RequestAdapterBase { @RequestProperty private Double value; + @Default(doubleValues = 1.1d) + @RequestProperty + private Double defaultValue; + @Self private Supplier supplier; @@ -46,6 +51,10 @@ public Double getValue() { return value; } + public Double getDefaultValue() { + return defaultValue; + } + @Override public ValueSupplier getValueSupplier() { return supplier; diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/IntegerCollections.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/IntegerCollections.java index f69532bda..dc653cfd1 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/IntegerCollections.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/IntegerCollections.java @@ -20,6 +20,7 @@ import javax.inject.Named; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -35,6 +36,10 @@ public class IntegerCollections extends RequestAdapterBase> @RequestProperty private Set value; + @Default(intValues = {10, 11, 12}) + @RequestProperty + private int[] defaultValue; + @Self private Supplier supplier; @@ -47,6 +52,10 @@ public Collection getValue() { return value; } + public int[] getDefaultValue() { + return defaultValue; + } + @Override public ValueSupplier> getValueSupplier() { return supplier; diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Integers.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Integers.java index 94103ce18..1a872cad1 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Integers.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Integers.java @@ -17,6 +17,7 @@ import javax.inject.Named; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -32,6 +33,10 @@ public class Integers extends RequestAdapterBase { @RequestProperty private int value; + @Default(intValues = 10) + @RequestProperty + private Integer defaultValue; + @Self private Supplier supplier; @@ -44,6 +49,10 @@ public Integer getValue() { return value; } + public Integer getDefaultValue() { + return defaultValue; + } + @Override public ValueSupplier getValueSupplier() { return supplier; diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/LongArrays.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/LongArrays.java index 848d03577..9ee94c9d8 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/LongArrays.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/LongArrays.java @@ -18,6 +18,7 @@ import org.apache.commons.lang3.ArrayUtils; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -33,6 +34,10 @@ public class LongArrays extends RequestAdapterBase { @RequestProperty private long[] value; + @Default(longValues = {10L, 11L, 12L}) + @RequestProperty + private long[] defaultValue; + @Self private Supplier supplier; @@ -45,6 +50,10 @@ public Long[] getValue() { return ArrayUtils.toObject(value); } + public long[] getDefaultValue() { + return defaultValue; + } + @Override public ValueSupplier getValueSupplier() { return supplier; diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Longs.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Longs.java index be039097d..32cedda73 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Longs.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Longs.java @@ -18,6 +18,7 @@ import javax.inject.Named; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -33,6 +34,10 @@ public class Longs extends RequestAdapterBase { @RequestProperty private Long value; + @Default(longValues = 10L) + @RequestProperty + private Long defaultValue; + @Self private Supplier supplier; @@ -46,6 +51,10 @@ public Long getValue() { return value; } + public Long getDefaultValue() { + return defaultValue; + } + @Override public ValueSupplier getValueSupplier() { return supplier; diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/StringArrays.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/StringArrays.java index 01bb14166..5862dd204 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/StringArrays.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/StringArrays.java @@ -17,6 +17,7 @@ import javax.inject.Named; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -32,6 +33,10 @@ public class StringArrays extends RequestAdapterBase { @RequestProperty private String[] value; + @Default(values = {"Default", "values"}) + @RequestProperty + private String[] defaultValue; + @Self private Supplier valueSupplier; @@ -50,6 +55,10 @@ public String[] getValue() { return value; } + public String[] getDefaultValue() { + return defaultValue; + } + @Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public interface Supplier extends ValueSupplier { @RequestProperty(name = CoreConstants.PN_VALUE) diff --git a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Strings.java b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Strings.java index 05c86e5b6..1587a5aab 100644 --- a/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Strings.java +++ b/core/src/test/java/com/exadel/aem/toolkit/core/injectors/models/requestproperty/Strings.java @@ -18,6 +18,7 @@ import javax.inject.Named; import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.models.annotations.Default; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.Self; @@ -33,12 +34,17 @@ public class Strings extends RequestAdapterBase { @RequestProperty private String value; + @Default(values = "default") + @RequestProperty + private String defaultValue; + @Self private Supplier valueSupplier; @Inject public Strings(@RequestProperty @Named(CoreConstants.PN_VALUE) String value) { super(value); + } @Override @@ -52,6 +58,10 @@ public String getValue() { return value; } + public String getDefaultValue() { + return defaultValue; + } + @Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public interface Supplier extends ValueSupplier { @RequestProperty(name = CoreConstants.PN_VALUE)