Author: jverhaeg(a)redhat.com
Date: 2008-09-15 10:02:57 -0400 (Mon, 15 Sep 2008)
New Revision: 516
Added:
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java
Modified:
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNamespaceRegistry.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrWorkspace.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNamespaceRegistryTest.java
trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
Log:
DNA-90: Implemented namespace-related functionality in JCR implementation.
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNamespaceRegistry.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNamespaceRegistry.java 2008-09-11
23:33:03 UTC (rev 515)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrNamespaceRegistry.java 2008-09-15
14:02:57 UTC (rev 516)
@@ -21,73 +21,74 @@
*/
package org.jboss.dna.jcr;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
+import java.util.Iterator;
+import java.util.Set;
import javax.jcr.NamespaceException;
import javax.jcr.NamespaceRegistry;
+import javax.jcr.RepositoryException;
import javax.jcr.UnsupportedRepositoryOperationException;
-import net.jcip.annotations.Immutable;
-import org.jboss.dna.common.util.ArgCheck;
/**
* @author John Verhaeg
* @author Randall Hauch
*/
-@Immutable
-class JcrNamespaceRegistry implements NamespaceRegistry {
+final class JcrNamespaceRegistry implements NamespaceRegistry {
- private Map<String, String> prefix2UriMap = new HashMap<String,
String>();
+ private org.jboss.dna.spi.graph.NamespaceRegistry dnaNamespaceRegistry;
- JcrNamespaceRegistry() {
- prefix2UriMap.put("", "");
- prefix2UriMap.put("dna", "http://www.jboss.org/dna/1.0");
- prefix2UriMap.put("jcr", "http://www.jcp.org/jcr/1.0");
- prefix2UriMap.put("mix", "http://www.jcp.org/jcr/mix/1.0");
- prefix2UriMap.put("nt", "http://www.jcp.org/jcr/nt/1.0");
- prefix2UriMap.put("xml",
"http://www.w3.org/XML/1998/namespace");
+ JcrNamespaceRegistry( org.jboss.dna.spi.graph.NamespaceRegistry dnaNamespaceRegistry
) {
+ this.dnaNamespaceRegistry = dnaNamespaceRegistry;
}
/**
* {@inheritDoc}
+ *
+ * @see javax.jcr.NamespaceRegistry#getPrefix(java.lang.String)
*/
- public String getPrefix( String uri ) throws NamespaceException {
- ArgCheck.isNotNull(uri, "uri");
- for (Entry<String, String> entry : prefix2UriMap.entrySet()) {
- if (uri.equals(entry.getValue())) {
- return entry.getKey();
- }
+ public String getPrefix( String uri ) throws NamespaceException, RepositoryException
{
+ String prefix = dnaNamespaceRegistry.getPrefixForNamespaceUri(uri, false);
+ if (prefix == null) {
+ throw new NamespaceException();
}
- throw new NamespaceException();
+ return prefix;
}
/**
* {@inheritDoc}
+ *
+ * @see javax.jcr.NamespaceRegistry#getPrefixes()
*/
public String[] getPrefixes() {
- String[] prefixes = prefix2UriMap.keySet().toArray(new
String[prefix2UriMap.size()]);
- Arrays.sort(prefixes);
+ Set<String> uris = dnaNamespaceRegistry.getRegisteredNamespaceUris();
+ String[] prefixes = new String[uris.size()];
+ Iterator<String> iter = uris.iterator();
+ for (int ndx = 0; iter.hasNext(); ndx++) {
+ prefixes[ndx] = dnaNamespaceRegistry.getPrefixForNamespaceUri(iter.next(),
false);
+ }
return prefixes;
}
/**
* {@inheritDoc}
+ *
+ * @see javax.jcr.NamespaceRegistry#getURI(java.lang.String)
*/
public String getURI( String prefix ) throws NamespaceException {
- ArgCheck.isNotNull(prefix, "prefix");
- String uri = prefix2UriMap.get(prefix);
- if (uri == null) throw new NamespaceException();
+ String uri = dnaNamespaceRegistry.getNamespaceForPrefix(prefix);
+ if (uri == null) {
+ throw new NamespaceException();
+ }
return uri;
}
/**
* {@inheritDoc}
+ *
+ * @see javax.jcr.NamespaceRegistry#getURIs()
*/
public String[] getURIs() {
- String[] uris = prefix2UriMap.values().toArray(new
String[prefix2UriMap.size()]);
- Arrays.sort(uris);
- return uris;
+ Set<String> uris = dnaNamespaceRegistry.getRegisteredNamespaceUris();
+ return uris.toArray(new String[uris.size()]);
}
/**
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java 2008-09-11 23:33:03 UTC
(rev 515)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java 2008-09-15 14:02:57 UTC
(rev 516)
@@ -204,6 +204,10 @@
return StringUtil.EMPTY_STRING_ARRAY;
}
+ ExecutionContext getExecutionContext() {
+ return executionContext;
+ }
+
/**
* {@inheritDoc}
*
@@ -262,31 +266,28 @@
/**
* {@inheritDoc}
*
- * @throws UnsupportedOperationException always
* @see javax.jcr.Session#getNamespacePrefix(java.lang.String)
*/
- public String getNamespacePrefix( String uri ) {
- throw new UnsupportedOperationException();
+ public String getNamespacePrefix( String uri ) throws RepositoryException {
+ return workspace.getNamespaceRegistry().getPrefix(uri);
}
/**
* {@inheritDoc}
*
- * @throws UnsupportedOperationException always
* @see javax.jcr.Session#getNamespacePrefixes()
*/
- public String[] getNamespacePrefixes() {
- throw new UnsupportedOperationException();
+ public String[] getNamespacePrefixes() throws RepositoryException {
+ return workspace.getNamespaceRegistry().getPrefixes();
}
/**
* {@inheritDoc}
*
- * @throws UnsupportedOperationException always
* @see javax.jcr.Session#getNamespaceURI(java.lang.String)
*/
- public String getNamespaceURI( String prefix ) {
- throw new UnsupportedOperationException();
+ public String getNamespaceURI( String prefix ) throws RepositoryException {
+ return workspace.getNamespaceRegistry().getURI(prefix);
}
private Node getNode( Path path ) throws RepositoryException {
Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrWorkspace.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrWorkspace.java 2008-09-11 23:33:03
UTC (rev 515)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrWorkspace.java 2008-09-15 14:02:57
UTC (rev 516)
@@ -40,6 +40,7 @@
private final String name;
private final JcrSession session;
+ private final NamespaceRegistry namespaceRegistry;
/**
* @param session the session that owns this workspace; may not be null
@@ -52,6 +53,7 @@
assert name != null;
this.session = session;
this.name = name;
+ this.namespaceRegistry = new
JcrNamespaceRegistry(session.getExecutionContext().getNamespaceRegistry());
// Ensure workspace with supplied name is accessible
// if (name == null) name =
session.getDnaRepository().getSource(session.getSubject()).getName();
// String matchedName = null;
@@ -68,6 +70,8 @@
/**
* {@inheritDoc}
+ *
+ * @see javax.jcr.Workspace#clone(java.lang.String, java.lang.String,
java.lang.String, boolean)
*/
public void clone( String srcWorkspace,
String srcAbsPath,
@@ -137,9 +141,11 @@
/**
* {@inheritDoc}
+ *
+ * @see javax.jcr.Workspace#getNamespaceRegistry()
*/
public NamespaceRegistry getNamespaceRegistry() {
- throw new UnsupportedOperationException();
+ return namespaceRegistry;
}
/**
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNamespaceRegistryTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNamespaceRegistryTest.java 2008-09-11
23:33:03 UTC (rev 515)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrNamespaceRegistryTest.java 2008-09-15
14:02:57 UTC (rev 516)
@@ -27,7 +27,10 @@
import static org.junit.Assert.assertThat;
import javax.jcr.NamespaceException;
import javax.jcr.UnsupportedRepositoryOperationException;
+import org.jboss.dna.spi.ExecutionContext;
+import org.jboss.dna.spi.ExecutionContextFactory;
import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
/**
@@ -35,37 +38,42 @@
*/
public class JcrNamespaceRegistryTest {
+ static ExecutionContext executionContext;
private JcrNamespaceRegistry registry;
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ ExecutionContextFactory factory = TestUtil.getExecutionContextFactory();
+ executionContext = factory.create();
+ }
+
@Before
public void before() {
- registry = new JcrNamespaceRegistry();
+ registry = new JcrNamespaceRegistry(executionContext.getNamespaceRegistry());
}
@Test
public void shouldProvidePrefixes() {
String[] prefixes = registry.getPrefixes();
assertThat(prefixes, notNullValue());
- assertThat(prefixes.length, is(6));
assertThat(prefixes, hasItemInArray(""));
assertThat(prefixes, hasItemInArray("dna"));
assertThat(prefixes, hasItemInArray("jcr"));
assertThat(prefixes, hasItemInArray("mix"));
assertThat(prefixes, hasItemInArray("nt"));
- assertThat(prefixes, hasItemInArray("xml"));
+ // assertThat(prefixes, hasItemInArray("xml"));
}
@Test
public void shouldProvideUris() {
String[] uris = registry.getURIs();
assertThat(uris, notNullValue());
- assertThat(uris.length, is(6));
assertThat(uris, hasItemInArray(""));
assertThat(uris,
hasItemInArray("http://www.jboss.org/dna/1.0"));
assertThat(uris,
hasItemInArray("http://www.jcp.org/jcr/1.0"));
assertThat(uris,
hasItemInArray("http://www.jcp.org/jcr/mix/1.0"));
assertThat(uris,
hasItemInArray("http://www.jcp.org/jcr/nt/1.0"));
- assertThat(uris,
hasItemInArray("http://www.w3.org/XML/1998/namespace"));
+ // assertThat(uris,
hasItemInArray("http://www.w3.org/XML/1998/namespace"));
}
@Test( expected = UnsupportedRepositoryOperationException.class )
@@ -95,7 +103,7 @@
assertThat(registry.getPrefix("http://www.jcp.org/jcr/1.0"),
is("jcr"));
assertThat(registry.getPrefix("http://www.jcp.org/jcr/mix/1.0"),
is("mix"));
assertThat(registry.getPrefix("http://www.jcp.org/jcr/nt/1.0"),
is("nt"));
-
assertThat(registry.getPrefix("http://www.w3.org/XML/1998/namespace&...,
is("xml"));
+//
assertThat(registry.getPrefix("http://www.w3.org/XML/1998/namespace&...,
is("xml"));
}
@Test
@@ -105,7 +113,7 @@
assertThat(registry.getURI("jcr"),
is("http://www.jcp.org/jcr/1.0"));
assertThat(registry.getURI("mix"),
is("http://www.jcp.org/jcr/mix/1.0"));
assertThat(registry.getURI("nt"),
is("http://www.jcp.org/jcr/nt/1.0"));
- assertThat(registry.getURI("xml"),
is("http://www.w3.org/XML/1998/namespace"));
+// assertThat(registry.getURI("xml"),
is("http://www.w3.org/XML/1998/namespace"));
}
@Test( expected = NamespaceException.class )
Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java 2008-09-11 23:33:03
UTC (rev 515)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrSessionTest.java 2008-09-15 14:02:57
UTC (rev 516)
@@ -23,6 +23,7 @@
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
+import static org.hamcrest.core.IsNot.not;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.assertThat;
@@ -37,6 +38,7 @@
import java.util.Map;
import java.util.UUID;
import javax.jcr.Item;
+import javax.jcr.NamespaceException;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.PropertyType;
@@ -64,13 +66,13 @@
*/
public class JcrSessionTest {
- private static final String WORKSPACE_NAME = JcrI18n.defaultWorkspaceName.text();
+ static final String WORKSPACE_NAME = JcrI18n.defaultWorkspaceName.text();
- private static ExecutionContext executionContext;
- private static SimpleRepository simpleRepository;
- private static RepositoryConnectionFactory connectionFactory;
- private static RepositoryConnection connection;
- private static Repository repository;
+ static ExecutionContext executionContext;
+ static SimpleRepository simpleRepository;
+ static RepositoryConnectionFactory connectionFactory;
+ static RepositoryConnection connection;
+ static Repository repository;
@BeforeClass
public static void beforeClass() throws Exception {
@@ -287,4 +289,51 @@
public void shouldNotAllowItemExistsWithEmptyPath() throws Exception {
session.itemExists("");
}
+
+ @Test( expected = IllegalArgumentException.class )
+ public void shouldNotAllowNoNamespaceUri() throws Exception {
+ session.getNamespacePrefix(null);
+ }
+
+ @Test( expected = NamespaceException.class )
+ public void shouldNotProvidePrefixForUnknownUri() throws Exception {
+ session.getNamespacePrefix("bogus");
+ }
+
+ @Test
+ public void shouldProvideNamespacePrefix() throws Exception {
+
assertThat(session.getNamespacePrefix("http://www.jboss.org/dna/1.0&...,
is("dna"));
+
assertThat(session.getNamespacePrefix("http://www.jcp.org/jcr/1.0&qu...,
is("jcr"));
+
assertThat(session.getNamespacePrefix("http://www.jcp.org/jcr/mix/1....,
is("mix"));
+
assertThat(session.getNamespacePrefix("http://www.jcp.org/jcr/nt/1.0...,
is("nt"));
+
assertThat(session.getNamespacePrefix("http://www.jcp.org/jcr/sv/1.0...,
is("sv"));
+ //
assertThat(session.getNamespacePrefix("http://www.w3.org/XML/1998/na...,
is("xml"));
+ }
+
+ @Test
+ public void shouldProvideNamespacePrefixes() throws Exception {
+ String[] prefixes = session.getNamespacePrefixes();
+ assertThat(prefixes, notNullValue());
+ assertThat(prefixes.length, is(not(0)));
+ }
+
+ @Test( expected = IllegalArgumentException.class )
+ public void shouldNotAllowNoNamespacePrefix() throws Exception {
+ session.getNamespaceURI(null);
+ }
+
+ @Test( expected = NamespaceException.class )
+ public void shouldNotProvideUriForUnknownPrefix() throws Exception {
+ session.getNamespaceURI("bogus");
+ }
+
+ @Test
+ public void shouldProvideNamespaceUri() throws Exception {
+ assertThat(session.getNamespaceURI("dna"),
is("http://www.jboss.org/dna/1.0"));
+ assertThat(session.getNamespaceURI("jcr"),
is("http://www.jcp.org/jcr/1.0"));
+ assertThat(session.getNamespaceURI("mix"),
is("http://www.jcp.org/jcr/mix/1.0"));
+ assertThat(session.getNamespaceURI("nt"),
is("http://www.jcp.org/jcr/nt/1.0"));
+ assertThat(session.getNamespaceURI("sv"),
is("http://www.jcp.org/jcr/sv/1.0"));
+ // assertThat(session.getNamespaceURI("xml"),
is("http://www.w3.org/XML/1998/namespace"));
+ }
}
Added: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java
(rev 0)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java 2008-09-15
14:02:57 UTC (rev 516)
@@ -0,0 +1,154 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.dna.jcr;
+
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.notNullValue;
+import static org.junit.Assert.assertThat;
+import javax.jcr.Session;
+import javax.jcr.Workspace;
+import org.jboss.dna.spi.ExecutionContextFactory;
+import org.jboss.dna.spi.connector.SimpleRepository;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mockito.MockitoAnnotations;
+
+/**
+ * @author jverhaeg
+ */
+public class JcrWorkspaceTest {
+
+ @BeforeClass
+ public static void beforeClass() throws Exception {
+ ExecutionContextFactory factory = TestUtil.getExecutionContextFactory();
+ JcrSessionTest.executionContext = factory.create();
+ JcrSessionTest.simpleRepository =
SimpleRepository.get(JcrSessionTest.WORKSPACE_NAME);
+ JcrSessionTest.connectionFactory =
TestUtil.createJackRabbitConnectionFactory(JcrSessionTest.simpleRepository,
+
JcrSessionTest.executionContext);
+ JcrSessionTest.repository = new JcrRepository(factory,
JcrSessionTest.connectionFactory);
+ }
+
+ @AfterClass
+ public static void afterClass() {
+ SimpleRepository.shutdownAll();
+ }
+
+ private Session session;
+ private Workspace workspace;
+
+ @Before
+ public void before() throws Exception {
+ MockitoAnnotations.initMocks(this);
+ session = JcrSessionTest.repository.login();
+ workspace = session.getWorkspace();
+ }
+
+ @After
+ public void after() throws Exception {
+ if (session.isLive()) {
+ session.logout();
+ }
+ }
+
+ @Test( expected = AssertionError.class )
+ public void shouldNotAllowNoSession() throws Exception {
+ new JcrWorkspace(null, JcrSessionTest.WORKSPACE_NAME);
+ }
+
+ @Test( expected = AssertionError.class )
+ public void shouldNotAllowNoWorkspaceName() throws Exception {
+ new JcrWorkspace((JcrSession)session, null);
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowClone() throws Exception {
+ workspace.clone(null, null, null, false);
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowCopy() throws Exception {
+ workspace.copy(null, null);
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowCopyFromOtherWorkspace() throws Exception {
+ workspace.copy(null, null, null);
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowGetAccessibleWorkspaceNames() throws Exception {
+ workspace.getAccessibleWorkspaceNames();
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowImportContentHandler() throws Exception {
+ workspace.getImportContentHandler(null, 0);
+ }
+
+ @Test
+ public void shouldProvideName() throws Exception {
+ assertThat(workspace.getName(), is(JcrSessionTest.WORKSPACE_NAME));
+ }
+
+ @Test
+ public void shouldProvideNamespaceRegistry() throws Exception {
+ assertThat(workspace.getNamespaceRegistry(), notNullValue());
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowGetNodeTypeManager() throws Exception {
+ workspace.getNodeTypeManager();
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowGetObservationManager() throws Exception {
+ workspace.getObservationManager();
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowGetQueryManager() throws Exception {
+ workspace.getQueryManager();
+ }
+
+ @Test
+ public void shouldProvideSession() throws Exception {
+ assertThat(workspace.getSession(), is(session));
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowImportXml() throws Exception {
+ workspace.importXML(null, null, 0);
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowMove() throws Exception {
+ workspace.move(null, null);
+ }
+
+ @Test( expected = UnsupportedOperationException.class )
+ public void shouldNotAllowRestore() throws Exception {
+ workspace.restore(null, false);
+ }
+}
Property changes on: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain