You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2014/07/21 16:10:02 UTC
svn commit: r1612274 - in /sling/trunk/tooling/ide:
api-test/src/test/java/org/apache/sling/ide/serialization/
api-test/src/test/java/org/apache/sling/ide/transport/
api/src/org/apache/sling/ide/transport/
Author: rombert
Date: Mon Jul 21 14:10:01 2014
New Revision: 1612274
URL: http://svn.apache.org/r1612274
Log:
SLING-3584 - Offline-mode for NodeTypeRegistry
Migrate the StubNodeTypeRegistry from the api-test project as a
FallbackNodeTypeRegistry in the api bundle. The registry now uses
the java.lang.reflect.Proxy mechanism to implement calls to NodeType
instead of mockito. The reason is that we don't want another runtime
dependency for the API bundle.
However, this is still only called in tests and definitely not
production-ready.
Added:
sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/transport/FallbackNodeTypeRegistryTest.java (with props)
sling/trunk/tooling/ide/api/src/org/apache/sling/ide/transport/FallbackNodeTypeRegistry.java (with props)
Removed:
sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/serialization/StubNodeTypeRegistry.java
Modified:
sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/serialization/StubRepository.java
Modified: sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/serialization/StubRepository.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/serialization/StubRepository.java?rev=1612274&r1=1612273&r2=1612274&view=diff
==============================================================================
--- sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/serialization/StubRepository.java (original)
+++ sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/serialization/StubRepository.java Mon Jul 21 14:10:01 2014
@@ -17,6 +17,7 @@
package org.apache.sling.ide.serialization;
import org.apache.sling.ide.transport.Command;
+import org.apache.sling.ide.transport.FallbackNodeTypeRegistry;
import org.apache.sling.ide.transport.FileInfo;
import org.apache.sling.ide.transport.NodeTypeRegistry;
import org.apache.sling.ide.transport.Repository;
@@ -62,17 +63,7 @@ public class StubRepository implements R
@Override
public NodeTypeRegistry getNodeTypeRegistry() {
- final StubNodeTypeRegistry stubNodeTypeRegistry = new StubNodeTypeRegistry();
- stubNodeTypeRegistry.addNodeType("nt:file", new String[] {"nt:hierarchyNode"});
- stubNodeTypeRegistry.addNodeType("nt:folder", new String[] {"nt:hierarchyNode"});
- stubNodeTypeRegistry.addNodeType("nt:hierarchyNode", new String[] {"mix:created", "nt:base"});
- stubNodeTypeRegistry.addNodeType("nt:unstructured", new String[] {"nt:base"});
- stubNodeTypeRegistry.addNodeType("nt:base", new String[] {});
- stubNodeTypeRegistry.addNodeType("sling:OsgiConfig", new String[] {"nt:hierarchyNode", "nt:unstructured"});
- stubNodeTypeRegistry.addNodeType("sling:Folder", new String[] {"nt:folder"});
- stubNodeTypeRegistry.addNodeType("vlt:FullCoverage", new String[] {});
-
- return stubNodeTypeRegistry;
+ return FallbackNodeTypeRegistry.createRegistryWithDefaultNodeTypes();
}
}
\ No newline at end of file
Added: sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/transport/FallbackNodeTypeRegistryTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/transport/FallbackNodeTypeRegistryTest.java?rev=1612274&view=auto
==============================================================================
--- sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/transport/FallbackNodeTypeRegistryTest.java (added)
+++ sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/transport/FallbackNodeTypeRegistryTest.java Mon Jul 21 14:10:01 2014
@@ -0,0 +1,47 @@
+/*
+ * 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.ide.transport;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+import static org.hamcrest.CoreMatchers.notNullValue;
+import static org.junit.Assert.assertThat;
+
+import javax.jcr.nodetype.NodeType;
+
+import org.junit.Test;
+
+public class FallbackNodeTypeRegistryTest {
+
+ @Test
+ public void nodesuperTypesAreFound() {
+ FallbackNodeTypeRegistry registry = FallbackNodeTypeRegistry.createRegistryWithDefaultNodeTypes();
+
+ NodeType nodeType = registry.getNodeType("sling:Folder");
+
+ assertThat("nodeType", nodeType, notNullValue());
+ assertThat("nodeType.name", nodeType.getName(), equalTo("sling:Folder"));
+
+ assertThat("nodeType.declaredSupertypeNames", nodeType.getDeclaredSupertypeNames(),
+ equalTo(new String[] { "nt:folder" }));
+
+ NodeType[] superTypes = nodeType.getSupertypes();
+
+ assertThat("nodeType.superTypes", superTypes, notNullValue());
+ assertThat("nodeType.superTypes.length", superTypes.length, equalTo(1));
+ assertThat("nodeType.superTypes[0].name", superTypes[0].getName(), equalTo("nt:folder"));
+ }
+}
Propchange: sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/transport/FallbackNodeTypeRegistryTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/tooling/ide/api-test/src/test/java/org/apache/sling/ide/transport/FallbackNodeTypeRegistryTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL
Added: sling/trunk/tooling/ide/api/src/org/apache/sling/ide/transport/FallbackNodeTypeRegistry.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/api/src/org/apache/sling/ide/transport/FallbackNodeTypeRegistry.java?rev=1612274&view=auto
==============================================================================
--- sling/trunk/tooling/ide/api/src/org/apache/sling/ide/transport/FallbackNodeTypeRegistry.java (added)
+++ sling/trunk/tooling/ide/api/src/org/apache/sling/ide/transport/FallbackNodeTypeRegistry.java Mon Jul 21 14:10:01 2014
@@ -0,0 +1,143 @@
+/*
+ * 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.ide.transport;
+
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import javax.jcr.nodetype.NodeType;
+
+/**
+ * The <tt>FallbackNodeTypeRegistry</tt> holds a conservative, static, registry of node types
+ *
+ */
+public class FallbackNodeTypeRegistry implements NodeTypeRegistry {
+
+ public static FallbackNodeTypeRegistry createRegistryWithDefaultNodeTypes() {
+
+ final FallbackNodeTypeRegistry registry = new FallbackNodeTypeRegistry();
+
+ registry.addNodeType("nt:file", new String[] { "nt:hierarchyNode" });
+ registry.addNodeType("nt:folder", new String[] { "nt:hierarchyNode" });
+ registry.addNodeType("nt:hierarchyNode", new String[] { "mix:created", "nt:base" });
+ registry.addNodeType("nt:unstructured", new String[] { "nt:base" });
+ registry.addNodeType("nt:base", new String[] {});
+ registry.addNodeType("sling:OsgiConfig", new String[] { "nt:hierarchyNode", "nt:unstructured" });
+ registry.addNodeType("sling:Folder", new String[] { "nt:folder" });
+ registry.addNodeType("sling:OrderedFolder", new String[] { "sling:Folder" });
+ registry.addNodeType("vlt:FullCoverage", new String[] {});
+ registry.addNodeType("mix:created", new String[] {});
+
+ return registry;
+
+ }
+
+ private final List<NodeType> nodeTypes = new ArrayList<NodeType>();
+
+ @Override
+ public boolean isAllowedPrimaryChildNodeType(String parentNodeType, String childNodeType)
+ throws RepositoryException {
+ throw new UnsupportedOperationException("not yet implemented");
+ }
+
+ @Override
+ public Collection<String> getAllowedPrimaryChildNodeTypes(String parentNodeType) throws RepositoryException {
+ throw new UnsupportedOperationException("not yet implemented");
+ }
+
+ public void addNodeType(String name, String[] superTypeNames) {
+
+ nodeTypes.add(getNodeTypeProxy(name, superTypeNames));
+ }
+
+ private NodeType getNodeTypeProxy(final String name, final String[] superTypeNames) {
+
+ InvocationHandler ih = new InvocationHandler() {
+
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+
+ // NodeType.getName()
+ if (method.getName().equals("getName") && method.getParameterTypes().length == 0) {
+ return name;
+ }
+
+ // NodeType.getSupertypeNames();
+ if (method.getName().equals("getDeclaredSupertypeNames") && method.getParameterTypes().length == 0) {
+ return superTypeNames;
+ }
+
+ // NodeType.getDeclaredSupertypeNames();
+ if (method.getName().equals("getSupertypes") && method.getParameterTypes().length == 0) {
+ NodeType[] superTypes = new NodeType[superTypeNames.length];
+ for (int i = 0; i < superTypeNames.length; i++) {
+ String aSuperTypeName = superTypeNames[i];
+ NodeType aSuperType = getNodeType(aSuperTypeName);
+ superTypes[i] = aSuperType;
+ }
+
+ return superTypes;
+ }
+
+ // Object.toString() , Object.hashCode(), Object.equals
+ if (method.getDeclaringClass() == Object.class) {
+ if (method.getName().equals("toString") && method.getParameterTypes().length == 0) {
+ return proxy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(proxy));
+ }
+
+ if (method.getName().equals("hashCode") && method.getParameterTypes().length == 0) {
+ return System.identityHashCode(proxy);
+ }
+
+ if (method.getName().equals("equals") && method.getParameterTypes().length == 1) {
+ return proxy == args[0];
+ }
+ }
+
+ return null;
+ }
+ };
+
+ NodeType nodeType = (NodeType) Proxy.newProxyInstance(NodeType.class.getClassLoader(),
+ new Class[] { NodeType.class }, ih);
+
+ return nodeType;
+ }
+
+ @Override
+ public List<NodeType> getNodeTypes() {
+ return Collections.unmodifiableList(nodeTypes);
+ }
+
+ @Override
+ public NodeType getNodeType(String name) {
+
+ for (NodeType nt : nodeTypes) {
+ if (nt.getName().equals(name)) {
+ return nt;
+ }
+ }
+
+ return null;
+ }
+}
Propchange: sling/trunk/tooling/ide/api/src/org/apache/sling/ide/transport/FallbackNodeTypeRegistry.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sling/trunk/tooling/ide/api/src/org/apache/sling/ide/transport/FallbackNodeTypeRegistry.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision Rev URL