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 50eb3e232b86ff37a8bee26f07ed86ce40e1bba9 Author: Justin Edelson <[email protected]> AuthorDate: Mon Jun 2 17:43:18 2014 +0000 unit test improvements git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/models/impl@1599279 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/sling/models/impl/ConstructorTest.java | 3 +- .../org/apache/sling/models/impl/DefaultTest.java | 19 ++++++-- .../{ViaTest.java => InvalidAdaptationsTest.java} | 56 +++++++++++++--------- .../sling/models/impl/MultipleInjectorTest.java | 11 +++-- .../sling/models/impl/OSGiInjectionTest.java | 5 +- .../sling/models/impl/RequestInjectionTest.java | 6 +-- .../models/impl/ResourceModelClassesTest.java | 7 +-- .../models/impl/ResourceModelInterfacesTest.java | 7 +-- .../sling/models/impl/ServicePropertiesMap.java | 32 +++++++++++++ .../java/org/apache/sling/models/impl/ViaTest.java | 6 +-- .../interfaces/PropertyModelWithDefaults.java | 35 ++++++++++++++ 11 files changed, 132 insertions(+), 55 deletions(-) diff --git a/src/test/java/org/apache/sling/models/impl/ConstructorTest.java b/src/test/java/org/apache/sling/models/impl/ConstructorTest.java index 130818d..3f89b8f 100644 --- a/src/test/java/org/apache/sling/models/impl/ConstructorTest.java +++ b/src/test/java/org/apache/sling/models/impl/ConstructorTest.java @@ -59,8 +59,7 @@ public class ConstructorTest { factory = new ModelAdapterFactory(); factory.activate(componentCtx); - factory.bindInjector(new RequestAttributeInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L)); + factory.bindInjector(new RequestAttributeInjector(), new ServicePropertiesMap(1, 1)); } @Test 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 32787d3..aa6de10 100644 --- a/src/test/java/org/apache/sling/models/impl/DefaultTest.java +++ b/src/test/java/org/apache/sling/models/impl/DefaultTest.java @@ -29,13 +29,13 @@ 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.apache.sling.models.testmodels.interfaces.PropertyModelWithDefaults; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; import org.osgi.service.component.ComponentContext; @RunWith(MockitoJUnitRunner.class) @@ -54,8 +54,7 @@ public class DefaultTest { when(componentCtx.getBundleContext()).thenReturn(bundleContext); factory = new ModelAdapterFactory(); factory.activate(componentCtx); - factory.bindInjector(new ValueMapInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L)); + factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(0, 0)); } @Test @@ -72,6 +71,20 @@ public class DefaultTest { } @Test + public void testDefaultStringValueOnInterface() { + ValueMap vm = new ValueMapDecorator(Collections.<String, Object>singletonMap("first", "first value")); + + Resource res = mock(Resource.class); + when(res.adaptTo(ValueMap.class)).thenReturn(vm); + + PropertyModelWithDefaults model = factory.getAdapter(res, PropertyModelWithDefaults.class); + assertNotNull(model); + assertEquals("first value", model.getFirst()); + assertEquals("second default", model.getSecond()); + } + + + @Test public void testDefaultPrimitives() { ValueMap vm = new ValueMapDecorator(Collections.<String, Object>emptyMap()); diff --git a/src/test/java/org/apache/sling/models/impl/ViaTest.java b/src/test/java/org/apache/sling/models/impl/InvalidAdaptationsTest.java similarity index 62% copy from src/test/java/org/apache/sling/models/impl/ViaTest.java copy to src/test/java/org/apache/sling/models/impl/InvalidAdaptationsTest.java index 0485030..4c545af 100644 --- a/src/test/java/org/apache/sling/models/impl/ViaTest.java +++ b/src/test/java/org/apache/sling/models/impl/InvalidAdaptationsTest.java @@ -16,63 +16,73 @@ */ package org.apache.sling.models.impl; -import static org.mockito.Mockito.*; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; import java.util.Collections; +import java.util.Map; -import org.apache.commons.lang.RandomStringUtils; import org.apache.sling.api.SlingHttpServletRequest; 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.annotations.Model; +import org.apache.sling.models.impl.injectors.ChildResourceInjector; import org.apache.sling.models.impl.injectors.ValueMapInjector; -import org.apache.sling.models.testmodels.classes.ViaModel; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; import org.osgi.service.component.ComponentContext; @RunWith(MockitoJUnitRunner.class) -public class ViaTest { - - @Mock - private Resource resource; - - @Mock - private SlingHttpServletRequest request; +public class InvalidAdaptationsTest { @Mock private ComponentContext componentCtx; @Mock private BundleContext bundleContext; - + private ModelAdapterFactory factory; @Before public void setup() { when(componentCtx.getBundleContext()).thenReturn(bundleContext); - when(request.getResource()).thenReturn(resource); + factory = new ModelAdapterFactory(); factory.activate(componentCtx); - factory.bindInjector(new ValueMapInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L)); + factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(1, 1)); + factory.bindInjector(new ChildResourceInjector(), new ServicePropertiesMap(2, 0)); } @Test - public void testProjectionToResource() { - String value = RandomStringUtils.randomAlphanumeric(10); - ValueMap map = new ValueMapDecorator(Collections.<String, Object> singletonMap("firstProperty", value)); - when(resource.adaptTo(ValueMap.class)).thenReturn(map); - - ViaModel model = factory.getAdapter(request, ViaModel.class); - assertNotNull(model); - assertEquals(value, model.getFirstProperty()); + public void testNonModelClass() { + Map<String, Object> emptyMap = Collections.<String, Object> emptyMap(); + + Resource res = mock(Resource.class); + when(res.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(emptyMap)); + + assertNull(factory.getAdapter(res, NonModel.class)); + } + + @Test + public void testWrongAdaptableClass() { + Map<String, Object> emptyMap = Collections.<String, Object> emptyMap(); + + Resource res = mock(Resource.class); + when(res.adaptTo(ValueMap.class)).thenReturn(new ValueMapDecorator(emptyMap)); + + assertNull(factory.getAdapter(res, RequestModel.class)); + } + + private class NonModel { + } + + @Model(adaptables = SlingHttpServletRequest.class) + private class RequestModel { } } diff --git a/src/test/java/org/apache/sling/models/impl/MultipleInjectorTest.java b/src/test/java/org/apache/sling/models/impl/MultipleInjectorTest.java index 087c075..b973952 100644 --- a/src/test/java/org/apache/sling/models/impl/MultipleInjectorTest.java +++ b/src/test/java/org/apache/sling/models/impl/MultipleInjectorTest.java @@ -19,7 +19,7 @@ package org.apache.sling.models.impl; import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import java.util.Collections; +import java.lang.reflect.AnnotatedElement; import javax.inject.Inject; @@ -29,6 +29,7 @@ import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.Source; import org.apache.sling.models.impl.injectors.BindingsInjector; import org.apache.sling.models.impl.injectors.RequestAttributeInjector; +import org.apache.sling.models.spi.DisposalCallbackRegistry; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,7 +37,6 @@ import org.mockito.Mock; import org.mockito.Spy; import org.mockito.runners.MockitoJUnitRunner; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; import org.osgi.service.component.ComponentContext; @RunWith(MockitoJUnitRunner.class) @@ -68,9 +68,8 @@ public class MultipleInjectorTest { factory = new ModelAdapterFactory(); factory.activate(componentCtx); - factory.bindInjector(bindingsInjector, Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 2L)); - factory.bindInjector(attributesInjector, - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 1L)); + factory.bindInjector(bindingsInjector, new ServicePropertiesMap(2, 2)); + factory.bindInjector(attributesInjector, new ServicePropertiesMap(1, 1)); when(request.getAttribute(SlingBindings.class.getName())).thenReturn(bindings); } @@ -89,6 +88,8 @@ public class MultipleInjectorTest { assertEquals(obj.firstAttribute, bindingsValue); verifyNoMoreInteractions(attributesInjector); + verify(bindingsInjector).getValue(eq(request), eq("firstAttribute"), eq(String.class), any(AnnotatedElement.class), any(DisposalCallbackRegistry.class)); + verifyNoMoreInteractions(bindingsInjector); } @Test diff --git a/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java b/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java index 9812003..1058323 100644 --- a/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java +++ b/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java @@ -17,9 +17,9 @@ package org.apache.sling.models.impl; import static org.junit.Assert.*; +import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; -import java.util.Collections; import java.util.Dictionary; import org.apache.sling.api.SlingHttpServletRequest; @@ -43,7 +43,6 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.osgi.framework.BundleContext; import org.osgi.framework.BundleListener; -import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; import org.osgi.service.component.ComponentContext; @@ -70,7 +69,7 @@ public class OSGiInjectionTest { OSGiServiceInjector injectorFactory = new OSGiServiceInjector(); injectorFactory.activate(componentCtx); - factory.bindInjector(injectorFactory, Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L)); + factory.bindInjector(injectorFactory, new ServicePropertiesMap(1, 1)); bindings.setSling(helper); } diff --git a/src/test/java/org/apache/sling/models/impl/RequestInjectionTest.java b/src/test/java/org/apache/sling/models/impl/RequestInjectionTest.java index 8a48ef0..9f495e3 100644 --- a/src/test/java/org/apache/sling/models/impl/RequestInjectionTest.java +++ b/src/test/java/org/apache/sling/models/impl/RequestInjectionTest.java @@ -19,8 +19,6 @@ package org.apache.sling.models.impl; import static org.junit.Assert.*; import static org.mockito.Mockito.*; -import java.util.Collections; - import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.scripting.SlingBindings; import org.apache.sling.api.scripting.SlingScriptHelper; @@ -32,7 +30,6 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; import org.osgi.service.component.ComponentContext; @RunWith(MockitoJUnitRunner.class) @@ -62,8 +59,7 @@ public class RequestInjectionTest { factory = new ModelAdapterFactory(); factory.activate(componentCtx); - factory.bindInjector(new BindingsInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L)); + factory.bindInjector(new BindingsInjector(), new ServicePropertiesMap(1, 1)); } @Test diff --git a/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java b/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java index 8f6aaf7..a4de53f 100644 --- a/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java +++ b/src/test/java/org/apache/sling/models/impl/ResourceModelClassesTest.java @@ -43,7 +43,6 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; import org.osgi.service.component.ComponentContext; @RunWith(MockitoJUnitRunner.class) @@ -63,10 +62,8 @@ public class ResourceModelClassesTest { factory = new ModelAdapterFactory(); factory.activate(componentCtx); - factory.bindInjector(new ValueMapInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 1L)); - factory.bindInjector(new ChildResourceInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L)); + factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(2, 2)); + factory.bindInjector(new ChildResourceInjector(), new ServicePropertiesMap(1, 1)); } @Test diff --git a/src/test/java/org/apache/sling/models/impl/ResourceModelInterfacesTest.java b/src/test/java/org/apache/sling/models/impl/ResourceModelInterfacesTest.java index 663d653..6999525 100644 --- a/src/test/java/org/apache/sling/models/impl/ResourceModelInterfacesTest.java +++ b/src/test/java/org/apache/sling/models/impl/ResourceModelInterfacesTest.java @@ -43,7 +43,6 @@ import org.mockito.invocation.InvocationOnMock; import org.mockito.runners.MockitoJUnitRunner; import org.mockito.stubbing.Answer; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; import org.osgi.service.component.ComponentContext; @RunWith(MockitoJUnitRunner.class) @@ -62,10 +61,8 @@ public class ResourceModelInterfacesTest { when(componentCtx.getBundleContext()).thenReturn(bundleContext); factory = new ModelAdapterFactory(); factory.activate(componentCtx); - factory.bindInjector(new ValueMapInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 1L)); - factory.bindInjector(new ChildResourceInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L)); + factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(2, 2)); + factory.bindInjector(new ChildResourceInjector(), new ServicePropertiesMap(1, 1)); } @Test diff --git a/src/test/java/org/apache/sling/models/impl/ServicePropertiesMap.java b/src/test/java/org/apache/sling/models/impl/ServicePropertiesMap.java new file mode 100644 index 0000000..884f61e --- /dev/null +++ b/src/test/java/org/apache/sling/models/impl/ServicePropertiesMap.java @@ -0,0 +1,32 @@ +/* + * 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.impl; + +import java.util.HashMap; + +import org.osgi.framework.Constants; + +@SuppressWarnings("serial") +public class ServicePropertiesMap extends HashMap<String, Object> { + + public ServicePropertiesMap(long serviceId, long serviceRanking) { + super(); + put(Constants.SERVICE_ID, serviceId); + put(Constants.SERVICE_RANKING, serviceRanking); + } + +} diff --git a/src/test/java/org/apache/sling/models/impl/ViaTest.java b/src/test/java/org/apache/sling/models/impl/ViaTest.java index 0485030..822e07c 100644 --- a/src/test/java/org/apache/sling/models/impl/ViaTest.java +++ b/src/test/java/org/apache/sling/models/impl/ViaTest.java @@ -16,8 +16,8 @@ */ package org.apache.sling.models.impl; -import static org.mockito.Mockito.*; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; import java.util.Collections; @@ -34,7 +34,6 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; import org.osgi.service.component.ComponentContext; @RunWith(MockitoJUnitRunner.class) @@ -60,8 +59,7 @@ public class ViaTest { when(request.getResource()).thenReturn(resource); factory = new ModelAdapterFactory(); factory.activate(componentCtx); - factory.bindInjector(new ValueMapInjector(), - Collections.<String, Object> singletonMap(Constants.SERVICE_ID, 0L)); + factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(1, 1)); } @Test diff --git a/src/test/java/org/apache/sling/models/testmodels/interfaces/PropertyModelWithDefaults.java b/src/test/java/org/apache/sling/models/testmodels/interfaces/PropertyModelWithDefaults.java new file mode 100644 index 0000000..ec34e62 --- /dev/null +++ b/src/test/java/org/apache/sling/models/testmodels/interfaces/PropertyModelWithDefaults.java @@ -0,0 +1,35 @@ +/* + * 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.interfaces; + +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; +import org.apache.sling.models.annotations.Optional; + +@Model(adaptables = Resource.class) +public interface PropertyModelWithDefaults { + + @Inject @Optional @Default(values = "first default") + public String getFirst(); + + @Inject @Optional @Default(values = "second default") + public String getSecond(); + +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
