incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1483777 - in /sling/trunk/launchpad: integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/resourceprovider/ test-services/ test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/
Date Fri, 17 May 2013 12:49:54 GMT
Author: bdelacretaz
Date: Fri May 17 12:49:53 2013
New Revision: 1483777

URL: http://svn.apache.org/r1483777
Log:
test/example PlanetsResourceProvider added

Added:
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/resourceprovider/
    sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/resourceprovider/PlanetsResourceProviderTest.java
    sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/
    sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetResource.java
    sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetsResourceProvider.java
Modified:
    sling/trunk/launchpad/test-services/pom.xml

Added: sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/resourceprovider/PlanetsResourceProviderTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/resourceprovider/PlanetsResourceProviderTest.java?rev=1483777&view=auto
==============================================================================
--- sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/resourceprovider/PlanetsResourceProviderTest.java
(added)
+++ sling/trunk/launchpad/integration-tests/src/main/java/org/apache/sling/launchpad/webapp/integrationtest/resourceprovider/PlanetsResourceProviderTest.java
Fri May 17 12:49:53 2013
@@ -0,0 +1,73 @@
+/*
+ * 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.launchpad.webapp.integrationtest.resourceprovider;
+
+import org.apache.sling.commons.testing.integration.HttpTestBase;
+
+/**
+ * Test the PlanetsResourceProvider that the test-services bundles provides */ 
+public class PlanetsResourceProviderTest extends HttpTestBase {
+
+    private void assertStrings(String url, String [] what) throws Exception {
+        final String content = getContent(url, CONTENT_TYPE_JSON);
+        for(String expected : what) {
+            if(expected.startsWith("!")) {
+                assertFalse("NOT expecting '" + expected + "' in " + url, content.contains(expected.substring(1)));
+            } else {
+                assertTrue("Expecting '" + expected + "' in " + url, content.contains(expected));
+            }
+        }
+    }
+    
+    public void testRootResource() throws Exception {
+        assertStrings(
+            HTTP_BASE_URL + "/planets.tidy.-1.json", 
+            new String [] {
+                    "earth",
+                    "moon",
+                    "Moon",
+                    "Resources can have different sets of properties",
+                    "\"name\": \"Uranus\""
+            });
+    }
+    
+    public void testEarthResource() throws Exception {
+        assertStrings(
+                HTTP_BASE_URL + "/planets.tidy.-1.json", 
+                new String [] {
+                        "earth",
+                        "moon",
+                        "Moon",
+                        "Resources can have different sets of properties",
+                        "\"name\": \"Uranus\"",
+                        "384"
+                });
+    }
+    
+    public void testMoonResource() throws Exception {
+        assertStrings(
+                HTTP_BASE_URL + "/planets/earth/moon.tidy.json", 
+                new String [] {
+                        "!earth",
+                        "!moon",
+                        "Moon",
+                        "!Resources can have different sets of properties",
+                        "!\"name\": \"Uranus\"",
+                        "384"
+                });
+    }
+}

Modified: sling/trunk/launchpad/test-services/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/pom.xml?rev=1483777&r1=1483776&r2=1483777&view=diff
==============================================================================
--- sling/trunk/launchpad/test-services/pom.xml (original)
+++ sling/trunk/launchpad/test-services/pom.xml Fri May 17 12:49:53 2013
@@ -77,6 +77,20 @@
                     </instructions>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>org.apache.sling</groupId>
+                <artifactId>maven-sling-plugin</artifactId>
+                <version>2.1.0</version>
+                <executions>
+                    <execution>
+                        <id>generate-adapter-metadata</id>
+                        <phase>process-classes</phase>
+                        <goals>
+                            <goal>generate-adapter-metadata</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
         </plugins>
     </build>
     <dependencies>
@@ -127,6 +141,11 @@
             <version>2.0.6</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>adapter-annotations</artifactId>
+            <version>1.0.0</version>
+        </dependency>
+        <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
         </dependency>

Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetResource.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetResource.java?rev=1483777&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetResource.java
(added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetResource.java
Fri May 17 12:49:53 2013
@@ -0,0 +1,94 @@
+/*
+ * 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.launchpad.testservices.resourceprovider;
+
+import java.util.HashMap;
+
+import org.apache.sling.api.resource.AbstractResource;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.wrappers.ValueMapDecorator;
+import org.apache.sling.adapter.annotations.Adaptable;
+import org.apache.sling.adapter.annotations.Adapter;
+
+/** A Sling Resource that represents a planet */
+@Adaptable(adaptableClass=Resource.class, adapters={
+    @Adapter({ValueMap.class})
+})
+public class PlanetResource extends AbstractResource implements Resource {
+
+    private final String path;
+    private final ResourceMetadata metadata;
+    private final ValueMap valueMap;
+    private final ResourceResolver resolver;
+    
+    public static final String RESOURCE_TYPE = "sling/test-services/planet";
+    
+    static class PlanetValueMap extends ValueMapDecorator {
+        PlanetValueMap(String name, int distance) {
+            super(new HashMap<String, Object>());
+            put("name", name);
+            put("distance", distance);
+        }
+    }
+    
+    PlanetResource(ResourceResolver resolver, String path, ValueMap valueMap) {
+        this.path = path;
+                
+        this.valueMap = valueMap;
+        this.resolver = resolver;
+        
+        metadata = new ResourceMetadata();
+        metadata.setResolutionPath(path);
+    }
+    
+    @Override
+    public String toString() {
+        return getClass().getSimpleName() + " " + path;
+    }
+    
+    public String getPath() {
+        return path;
+    }
+
+    public ResourceMetadata getResourceMetadata() {
+        return metadata;
+    }
+
+    public ResourceResolver getResourceResolver() {
+        return resolver;
+    }
+
+    public String getResourceSuperType() {
+        return null;
+    }
+
+    public String getResourceType() {
+        return RESOURCE_TYPE;
+    }
+    
+    @Override
+    @SuppressWarnings("unchecked")
+    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+        if(type == ValueMap.class) {
+            return (AdapterType)valueMap;
+        }
+        return super.adaptTo(type);
+    }
+}
\ No newline at end of file

Added: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetsResourceProvider.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetsResourceProvider.java?rev=1483777&view=auto
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetsResourceProvider.java
(added)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/resourceprovider/PlanetsResourceProvider.java
Fri May 17 12:49:53 2013
@@ -0,0 +1,114 @@
+/*
+ * 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.launchpad.testservices.resourceprovider;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceProvider;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.SyntheticResource;
+import org.apache.sling.api.resource.ValueMap;
+
+/** Test/example ResourceProvider that provides info about
+ *  the Solar System's planets at /planets */
+@Component
+@Service
+@Properties({
+    @Property(name=ResourceProvider.ROOTS, value=PlanetsResourceProvider.ROOT)
+})
+public class PlanetsResourceProvider implements ResourceProvider {
+
+    /** Our planet data. PlanetResource is created when resolving, as
+     *  it points to a specific ResourceResolver. */
+    private static final Map<String, ValueMap> PLANETS = new HashMap<String, ValueMap>();
+    
+    /** This can be configurable of course */ 
+    public static final String ROOT = "planets";
+    public static final String ABS_ROOT = "/" + ROOT;
+    
+    /* Planet data from http://nineplanets.org/data.html
+     * (not that we care much about accuracy anyway ;-) 
+     */
+    static {
+        definePlanet("Mercury", 57910);
+        definePlanet("Venus", 108200);
+        definePlanet("Earth", 149600).put("comment", "Resources can have different sets of
properties");
+        definePlanet("Mars", 227940);
+        definePlanet("Jupiter", 4332);
+        definePlanet("Saturn", 10759);
+        definePlanet("Uranus", 30685);
+        definePlanet("Neptune", 60190);
+
+        // Add the moon to test a two-level hierarchy
+        final String moonPath = ABS_ROOT + "/earth/moon";
+        PLANETS.put(moonPath, new PlanetResource.PlanetValueMap("Moon", 384));
+    }
+    
+    /** ResourceProvider interface */
+    public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String
path) {
+        // Synthetic resource for our root, so that /planets works
+        if((ABS_ROOT).equals(path)) {
+            return new SyntheticResource(resolver, path, PlanetResource.RESOURCE_TYPE);
+        }
+        
+        // Not root, return a Planet if we have one
+        final ValueMap data = PLANETS.get(path);
+        return data == null ? null : new PlanetResource(resolver, path, data);
+    }
+
+    /** ResourceProvider interface */
+    public Resource getResource(ResourceResolver resolver, String path) {
+        return getResource(resolver, null, path);
+    }
+
+    /** ResourceProvider interface */
+    public Iterator<Resource> listChildren(Resource parent) {
+        if(parent.getPath().startsWith(ABS_ROOT)) {
+            final List<Resource> kids = new ArrayList<Resource>();
+            for(Map.Entry<String, ValueMap> e : PLANETS.entrySet()) {
+                if(parent.getPath().equals(parentPath(e.getKey()))) {
+                    kids.add(new PlanetResource(parent.getResourceResolver(), e.getKey(),
e.getValue()));
+                }
+            }
+            return kids.iterator();
+        } else {
+            return null;
+        }
+    }
+    
+    private static String parentPath(String path) {
+        final int lastSlash = path.lastIndexOf("/");
+        return lastSlash > 0 ? path.substring(0, lastSlash) : "";
+    }
+    
+    private static ValueMap definePlanet(String name, int distance) {
+        final ValueMap valueMap = new PlanetResource.PlanetValueMap(name, distance);
+        PLANETS.put(ABS_ROOT + "/" + name.toLowerCase(), valueMap);
+        return valueMap;
+    }
+}
\ No newline at end of file



Mime
View raw message