Return-Path: Delivered-To: apmail-sling-commits-archive@www.apache.org Received: (qmail 45686 invoked from network); 3 Feb 2010 18:10:08 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 3 Feb 2010 18:10:08 -0000 Received: (qmail 26128 invoked by uid 500); 3 Feb 2010 18:10:08 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 26073 invoked by uid 500); 3 Feb 2010 18:10:08 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 26064 invoked by uid 99); 3 Feb 2010 18:10:08 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Feb 2010 18:10:08 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 03 Feb 2010 18:10:07 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id D73E923889F1; Wed, 3 Feb 2010 18:09:46 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r906160 - in /sling/trunk/samples/path-based-rtp: ./ src/main/java/org/apache/sling/samples/pathbasedrtp/ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/sling/ src/test/java/org/apache/sling/s... Date: Wed, 03 Feb 2010 18:09:46 -0000 To: commits@sling.apache.org From: bdelacretaz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100203180946.D73E923889F1@eris.apache.org> Author: bdelacretaz Date: Wed Feb 3 18:09:46 2010 New Revision: 906160 URL: http://svn.apache.org/viewvc?rev=906160&view=rev Log: SLING-1347 - factor out Mapping class and add tests Added: sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java (with props) sling/trunk/samples/path-based-rtp/src/test/ sling/trunk/samples/path-based-rtp/src/test/java/ sling/trunk/samples/path-based-rtp/src/test/java/org/ sling/trunk/samples/path-based-rtp/src/test/java/org/apache/ sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/ sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/ sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/ sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java (with props) Modified: sling/trunk/samples/path-based-rtp/pom.xml sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/DefaultResourceTypeProvider.java Modified: sling/trunk/samples/path-based-rtp/pom.xml URL: http://svn.apache.org/viewvc/sling/trunk/samples/path-based-rtp/pom.xml?rev=906160&r1=906159&r2=906160&view=diff ============================================================================== --- sling/trunk/samples/path-based-rtp/pom.xml (original) +++ sling/trunk/samples/path-based-rtp/pom.xml Wed Feb 3 18:09:46 2010 @@ -103,5 +103,17 @@ org.osgi org.osgi.compendium + + + junit + junit + test + + + org.slf4j + slf4j-simple + test + + Modified: sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/DefaultResourceTypeProvider.java URL: http://svn.apache.org/viewvc/sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/DefaultResourceTypeProvider.java?rev=906160&r1=906159&r2=906160&view=diff ============================================================================== --- sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/DefaultResourceTypeProvider.java (original) +++ sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/DefaultResourceTypeProvider.java Wed Feb 3 18:09:46 2010 @@ -57,32 +57,7 @@ */ private static final String PROP_PATH_MAPPING = "path.mapping"; - private static final Logger log = LoggerFactory.getLogger(DefaultResourceTypeProvider.class); - - /** Map a path prefix to a (1-based) index in the path components */ - static class Mapping { - String path; - int resourceTypeIndex; - - Mapping(String definition) { - final String [] parts = definition.split(":"); - if(parts.length != 2) { - log.debug("Invalid Mapping definition ignored: {}", definition); - } else { - path = parts[0]; - try { - resourceTypeIndex = Integer.parseInt(parts[1]); - } catch(Exception e) { - log.warn("Invalid path index in Mapping {}", definition); - } - } - } - - @Override - public String toString() { - return "Mapping: path=" + path + ", resource type index=" + resourceTypeIndex; - } - } + private final Logger log = LoggerFactory.getLogger(getClass()); private Mapping [] mappings; @@ -94,14 +69,10 @@ final String nt = node.getPrimaryNodeType().getName(); final String path = node.getPath(); for(Mapping m : mappings) { - if(path.startsWith(m.path) && "nt:unstructured".equals(nt)) { - final String [] paths = node.getPath().split("/"); - if(paths.length >= m.resourceTypeIndex+1) { - result = paths[m.resourceTypeIndex]; - log.debug("Default resource type {} used for Node {}", - result, path); - break; - } + result = m.getResourceType(path, nt); + if(result != null) { + log.debug("Default resource type {} used for Node {}", result, path); + break; } } } @@ -128,5 +99,4 @@ } } } - -} +} \ No newline at end of file Added: sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java URL: http://svn.apache.org/viewvc/sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java?rev=906160&view=auto ============================================================================== --- sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java (added) +++ sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java Wed Feb 3 18:09:46 2010 @@ -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.samples.pathbasedrtp; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Map a path prefix to a (1-based) index in the path components */ +class Mapping { + private final Logger log = LoggerFactory.getLogger(getClass()); + + String path; + int resourceTypeIndex; + + Mapping(String definition) { + final String [] parts = definition.split(":"); + if(parts.length != 2) { + log.debug("Invalid Mapping definition ignored: {}", definition); + } else { + path = parts[0]; + try { + resourceTypeIndex = Integer.parseInt(parts[1]); + } catch(Exception e) { + log.warn("Invalid path index in Mapping {}", definition); + } + } + } + + @Override + public String toString() { + return "Mapping: path=" + path + ", resource type index=" + resourceTypeIndex; + } + + String getResourceType(String nodePath, String nodeType) { + String result = null; + if(path!=null && nodePath.startsWith(path) && "nt:unstructured".equals(nodeType)) { + final String [] paths = nodePath.split("/"); + if(paths.length >= resourceTypeIndex+1) { + result = paths[resourceTypeIndex]; + } + } + return result; + } +} \ No newline at end of file Propchange: sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/samples/path-based-rtp/src/main/java/org/apache/sling/samples/pathbasedrtp/Mapping.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Rev URL Added: sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java URL: http://svn.apache.org/viewvc/sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java?rev=906160&view=auto ============================================================================== --- sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java (added) +++ sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java Wed Feb 3 18:09:46 2010 @@ -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.samples.pathbasedrtp; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import org.junit.Test; + +public class MappingTest { + + @Test + public void testSimpleMapping() { + final Mapping m = new Mapping("/content:2"); + + final String[][] testCases = { + { "/content/foo", "nt:unstructured", "foo" }, + { "/content/foo/bar", "nt:unstructured", "foo" }, + { "/not/content/foo/bar", "nt:unstructured", null }, + { "/content/foo/bar", "nt:file", null } + }; + + for(int i=0; i < testCases.length; i++) { + final String [] tc = testCases[i]; + if(tc[2] == null) { + assertNull("At index " + i, m.getResourceType(tc[0], tc[1])); + } else { + assertEquals("At index " + i, tc[2], m.getResourceType(tc[0], tc[1])); + } + } + } + + @Test + public void testLevel2() { + final Mapping m = new Mapping("/content:3"); + assertEquals("bar1", m.getResourceType("/content/foo/bar1", "nt:unstructured")); + assertEquals("bar2", m.getResourceType("/content/foo/bar2/wii", "nt:unstructured")); + assertNull(m.getResourceType("/", "nt:unstructured")); + assertNull(m.getResourceType("/content", "nt:unstructured")); + } + + @Test + public void testLevel3() { + final Mapping m = new Mapping("/content:3"); + assertEquals("bar1", m.getResourceType("/content/foo/bar1", "nt:unstructured")); + assertEquals("bar2", m.getResourceType("/content/foo/bar2/wii", "nt:unstructured")); + assertEquals("bar3", m.getResourceType("/content/foo/bar3/wii2/xx", "nt:unstructured")); + assertNull(m.getResourceType("/", "nt:unstructured")); + assertNull(m.getResourceType("/content", "nt:unstructured")); + assertNull(m.getResourceType("/content/foo", "nt:unstructured")); + } + + /* + @Test + public void testNodetypeString() { + final Mapping m = new Mapping("/content:2:nt:file"); + assertEquals("foo1", m.getResourceType("/content/foo1", "nt:file")); + assertNull(m.getResourceType("/content/foo1", "nt:unstructured")); + assertNull(m.getResourceType("/content/foo1", "some:type")); + } + + @Test + public void testNodetypeRegexp() { + final Mapping m = new Mapping("/content:2:(nt:(file|test))"); + assertEquals("foo1", m.getResourceType("/content/foo1", "nt:file")); + assertEquals("foo2", m.getResourceType("/content/foo2", "nt:test")); + assertNull(m.getResourceType("/content/foo1", "nt:unstructured")); + assertNull(m.getResourceType("/content/foo1", "some:type")); + } + */ +} \ No newline at end of file Propchange: sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/samples/path-based-rtp/src/test/java/org/apache/sling/samples/pathbasedrtp/MappingTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Rev URL