### Question:
Mapping { String getResourceType(String nodePath, String nodeType) { String result = null; if(path!=null && nodePath.startsWith(path) && nodeTypeMatches(nodeType)) { final String [] paths = nodePath.split("/"); if(paths.length >= resourceTypeIndex+1) { result = paths[resourceTypeIndex]; } } return result; } Mapping(String definition); @Override String toString(); static final String DEFAULT_NODE_TYPE; }### Answer:
@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"); assertNotNull("nt:file must match", m.getResourceType("/content/foo1", "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))"); assertNotNull("nt:file must match", m.getResourceType("/content/foo1", "nt:file")); assertEquals("foo1", m.getResourceType("/content/foo1", "nt:file")); assertNotNull("nt:test must match", m.getResourceType("/content/foo1", "nt:test")); assertEquals("foo2", m.getResourceType("/content/foo2", "nt:test")); assertNull(m.getResourceType("/content/foo1", "nt:unstructured")); assertNull(m.getResourceType("/content/foo1", "some:type")); }
@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])); } } }