integration test of Sling Models and ResourceWrapper

git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1798489 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/sling/models/it/DecoratedTest.java b/src/main/java/org/apache/sling/models/it/DecoratedTest.java
new file mode 100644
index 0000000..0f4b91e
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/it/DecoratedTest.java
@@ -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.models.it;
+
+import org.apache.commons.lang.RandomStringUtils;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.resource.ResourceWrapper;
+import org.apache.sling.junit.annotations.SlingAnnotationsTestRunner;
+import org.apache.sling.junit.annotations.TestReference;
+import org.apache.sling.models.factory.ModelFactory;
+import org.apache.sling.models.it.models.SelfModel;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+
+import static org.junit.Assert.*;
+
+@RunWith(SlingAnnotationsTestRunner.class)
+public class DecoratedTest {
+
+    @TestReference
+    private ResourceResolverFactory rrFactory;
+
+    @TestReference
+    private ModelFactory modelFactory;
+    
+    private String value;
+    private ResourceResolver resolver;
+    private Resource resource;
+    private Node createdNode;
+    
+    @Before
+    public void setUp() throws Exception {
+        value = RandomStringUtils.randomAlphanumeric(10);
+
+        resolver = rrFactory.getAdministrativeResourceResolver(null);
+        Session session = resolver.adaptTo(Session.class);
+        Node rootNode = session.getRootNode();
+        createdNode = rootNode.addNode("test_" + RandomStringUtils.randomAlphanumeric(10));
+        createdNode.setProperty("testProperty", value);
+        createdNode.setProperty("decorate", true);
+        session.save();
+
+        resource = resolver.getResource(createdNode.getPath());
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (createdNode != null) {
+            createdNode.remove();
+        }
+        if (resolver != null) {
+            resolver.close();
+        }
+    }
+
+    @Test
+    public void testInjectDecoratedResource() {
+        assertTrue("Resource is wrapped", resource instanceof ResourceWrapper);
+        SelfModel model = modelFactory.createModel(resource, SelfModel.class);
+    
+        assertNotNull("Model is null", model);
+        assertTrue("Model is not wrapped", model.getResource() instanceof ResourceWrapper);
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/models/it/models/SelfModel.java b/src/main/java/org/apache/sling/models/it/models/SelfModel.java
new file mode 100644
index 0000000..2902acc
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/it/models/SelfModel.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.it.models;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.injectorspecific.Self;
+
+@Model(adaptables = Resource.class)
+public class SelfModel {
+
+    @Self
+    private Resource resource;
+
+    public Resource getResource() {
+        return resource;
+    }
+}
diff --git a/src/main/java/org/apache/sling/models/it/services/TestResourceDecorator.java b/src/main/java/org/apache/sling/models/it/services/TestResourceDecorator.java
new file mode 100644
index 0000000..2650af7
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/it/services/TestResourceDecorator.java
@@ -0,0 +1,44 @@
+/*
+ * 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.it.services;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceDecorator;
+import org.apache.sling.api.resource.ResourceWrapper;
+import org.apache.sling.api.resource.ValueMap;
+
+import javax.servlet.http.HttpServletRequest;
+
+@Component
+@Service
+public class TestResourceDecorator implements ResourceDecorator {
+    @Override
+    public Resource decorate(Resource resource) {
+        ValueMap map = resource.adaptTo(ValueMap.class);
+        if (map != null && map.containsKey("decorate")) {
+            return new ResourceWrapper(resource);
+        }
+        return null;
+    }
+
+    @Override
+    public Resource decorate(Resource resource, HttpServletRequest request) {
+        return decorate(resource);
+    }
+}