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 @@
<groupId>org.osgi</groupId>
<artifactId>org.osgi.compendium</artifactId>
</dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-simple</artifactId>
+ <scope>test</scope>
+ </dependency>
+
</dependencies>
</project>
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