geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ga...@apache.org
Subject svn commit: r765248 - in /geronimo/sandbox/blueprint/org.apache.felix.blueprint/src: main/java/org/apache/felix/blueprint/reflect/ test/java/org/apache/felix/blueprint/ test/resources/
Date Wed, 15 Apr 2009 15:53:45 GMT
Author: gawor
Date: Wed Apr 15 15:53:44 2009
New Revision: 765248

URL: http://svn.apache.org/viewvc?rev=765248&view=rev
Log:
basic tests for handling custom nodes and attributes

Added:
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-custom-nodes.xml
  (with props)
Modified:
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/ComponentMetadataImpl.java
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/LocalComponentMetadataImpl.java
    geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/ParserTest.java

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/ComponentMetadataImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/ComponentMetadataImpl.java?rev=765248&r1=765247&r2=765248&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/ComponentMetadataImpl.java
(original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/ComponentMetadataImpl.java
Wed Apr 15 15:53:44 2009
@@ -19,6 +19,7 @@
 package org.apache.felix.blueprint.reflect;
 
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
@@ -33,7 +34,15 @@
 
     private String name;
     private Set<String> explicitDependencies;
-
+    
+    protected ComponentMetadataImpl() {
+    }
+    
+    protected ComponentMetadataImpl(ComponentMetadata source) {
+        name = source.getName();
+        explicitDependencies = new HashSet<String>(source.getExplicitDependencies());
+    }
+    
     public String getName() {
         return name;
     }

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/LocalComponentMetadataImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/LocalComponentMetadataImpl.java?rev=765248&r1=765247&r2=765248&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/LocalComponentMetadataImpl.java
(original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/main/java/org/apache/felix/blueprint/reflect/LocalComponentMetadataImpl.java
Wed Apr 15 15:53:44 2009
@@ -53,6 +53,15 @@
         propertyInjectionMetadata = new ArrayList<PropertyInjectionMetadata>();
     }
 
+    public LocalComponentMetadataImpl(LocalComponentMetadata source) {
+        super(source);
+        initMethodName = source.getInitMethodName();
+        destroyMethodName = source.getDestroyMethodName();
+        className = source.getClassName();
+        scope = source.getScope();
+        isLazy = source.isLazy();
+    }
+    
     public String getClassName() {
         return className;
     }

Modified: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/ParserTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/ParserTest.java?rev=765248&r1=765247&r2=765248&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/ParserTest.java
(original)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/java/org/apache/felix/blueprint/ParserTest.java
Wed Apr 15 15:53:44 2009
@@ -18,6 +18,8 @@
  */
 package org.apache.felix.blueprint;
 
+import java.net.URI;
+import java.net.URL;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
@@ -25,7 +27,10 @@
 import junit.framework.TestCase;
 
 import org.apache.felix.blueprint.context.Parser;
+import org.apache.felix.blueprint.reflect.LocalComponentMetadataImpl;
 import org.osgi.service.blueprint.namespace.ComponentDefinitionRegistry;
+import org.osgi.service.blueprint.namespace.NamespaceHandler;
+import org.osgi.service.blueprint.namespace.ParserContext;
 import org.osgi.service.blueprint.reflect.ArrayValue;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
 import org.osgi.service.blueprint.reflect.ComponentValue;
@@ -33,8 +38,13 @@
 import org.osgi.service.blueprint.reflect.LocalComponentMetadata;
 import org.osgi.service.blueprint.reflect.NullValue;
 import org.osgi.service.blueprint.reflect.ParameterSpecification;
+import org.osgi.service.blueprint.reflect.PropertyInjectionMetadata;
 import org.osgi.service.blueprint.reflect.ReferenceValue;
 import org.osgi.service.blueprint.reflect.TypedStringValue;
+import org.osgi.service.blueprint.reflect.Value;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
 
 /**
  * TODO: constructor injection
@@ -118,13 +128,136 @@
         parser.parse(Arrays.asList( getClass().getResource("/test.xml") ));
         ComponentDefinitionRegistry registry = parser.getRegistry();
         assertNotNull(registry);
-
     }
 
+    public void testCustomNodes() throws Exception {
+        Parser parser = new Parser();
+        parser.setNamespaceHandlerRegistry(new TestNamespaceHandlerRegistry());
+        parser.parse(Arrays.asList( getClass().getResource("/test-custom-nodes.xml") ));
+        ComponentDefinitionRegistry registry = parser.getRegistry();
+        
+        ComponentMetadata metadata;
+        
+        metadata = registry.getComponentDefinition("fooService");
+        assertNotNull(metadata);
+        assertTrue(metadata instanceof MyLocalComponentMetadata);
+        MyLocalComponentMetadata comp1 = (MyLocalComponentMetadata) metadata;
+        assertEquals(true, comp1.getCacheReturnValues());
+        assertEquals("getVolatile", comp1.getOperation());
+        
+        metadata = registry.getComponentDefinition("barService");
+        assertNotNull(metadata);
+        assertTrue(metadata instanceof LocalComponentMetadata);
+        LocalComponentMetadata comp2 = (LocalComponentMetadata) metadata;
+        assertEquals(1, comp2.getPropertyInjectionMetadata().size());
+        PropertyInjectionMetadata propertyMetadata = (PropertyInjectionMetadata)comp2.getPropertyInjectionMetadata().iterator().next();
+        assertEquals("localCache", propertyMetadata.getName());
+        Value propertyValue = propertyMetadata.getValue();
+        assertTrue(propertyValue instanceof ComponentValue);
+        ComponentValue componentValue = (ComponentValue) propertyValue;
+        assertTrue(componentValue.getComponentMetadata() instanceof LocalComponentMetadata);
+        LocalComponentMetadata innerComp = (LocalComponentMetadata) componentValue.getComponentMetadata();
+        assertEquals("org.apache.geronimo.CacheProperty", innerComp.getClassName()); 
+        
+        metadata = registry.getComponentDefinition("myCache");
+        assertNotNull(metadata);
+        assertTrue(metadata instanceof LocalComponentMetadata);
+        LocalComponentMetadata comp3 = (LocalComponentMetadata) metadata;
+        assertEquals("org.apache.geronimo.Cache", comp3.getClassName());         
+    }
 
     protected Parser parse(String name) throws Exception {
         Parser parser = new Parser();
         parser.parse(Arrays.asList( getClass().getResource(name) ));
         return parser;
     }
+    
+    private static class TestNamespaceHandlerRegistry implements NamespaceHandlerRegistry
{
+        
+        public void destroy() {
+        }
+        
+        public NamespaceHandler getNamespaceHandler(URI uri) {
+            URI u = URI.create("http://cache.org");
+            if (u.equals(uri)) {
+                return new TestNamespaceHandler();
+            } else {
+                return null;
+            }        
+        }
+        
+    }
+    
+    private static class TestNamespaceHandler implements NamespaceHandler {
+
+        public ComponentMetadata decorate(Node node,
+                                          ComponentMetadata component,
+                                          ParserContext context) {
+            //System.out.println("decorate: " + node + " " + component + " " + context.getEnclosingComponent().getName());
+            
+            if (node instanceof Attr) {
+                Attr attr = (Attr) node;
+                MyLocalComponentMetadata decoratedComp = new MyLocalComponentMetadata((LocalComponentMetadata)component);
               
+                decoratedComp.setCacheReturnValues(Boolean.parseBoolean(attr.getValue()));
+                return decoratedComp;
+            } else if (node instanceof Element) {
+                Element element = (Element) node;                
+                MyLocalComponentMetadata decoratedComp = (MyLocalComponentMetadata) component;
+                decoratedComp.setOperation(element.getAttribute("name"));
+                return decoratedComp;
+            } else {
+                throw new RuntimeException("Unhandled node: " + node);
+            }
+        }
+
+        public URL getSchemaLocation(String namespace) {
+            return null;
+        }
+
+        public ComponentMetadata parse(Element element, ParserContext context) {
+            String comp = (context.getEnclosingComponent() == null) ? null : context.getEnclosingComponent().getName();
+            //System.out.println("parse: " + element.getLocalName() + " " + comp);
+            
+            String className;
+            if (context.getEnclosingComponent() == null) {
+                className = "org.apache.geronimo.Cache";
+            } else {
+                className = "org.apache.geronimo.CacheProperty";
+            }
+                        
+            LocalComponentMetadataImpl p = new LocalComponentMetadataImpl();
+            p.setName(element.getAttribute("id"));
+            p.setClassName(className);
+            
+            return p;
+        }
+        
+    }
+    
+    private static class MyLocalComponentMetadata extends LocalComponentMetadataImpl {
+        
+        private boolean cacheReturnValues;
+        private String operation;
+        
+        public MyLocalComponentMetadata(LocalComponentMetadata impl) {
+            super(impl);
+        }
+        
+        public boolean getCacheReturnValues() {
+            return cacheReturnValues;
+        }
+        
+        public void setCacheReturnValues(boolean value) {
+            cacheReturnValues = value;
+        }
+        
+        public void setOperation(String operation) {
+            this.operation = operation;
+        }
+        
+        public String getOperation() {
+            return this.operation;
+        }
+    }
+
 }

Added: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-custom-nodes.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-custom-nodes.xml?rev=765248&view=auto
==============================================================================
--- geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-custom-nodes.xml
(added)
+++ geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-custom-nodes.xml
Wed Apr 15 15:53:44 2009
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+-->
+<components xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xmlns:cache="http://cache.org"
+    xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0 ../../main/resources/org/apache/felix/blueprint/blueprint.xsd">
+
+    <cache:lru-cache id="myCache" />
+    
+    <component id="fooService" class="FooServiceImpl" cache:cache-return-values="true">
+        <cache:operation name="getVolatile" />
+
+        <property name="myProp" value="12" />
+    </component>
+    
+    <component id="barService" class="BarServiceImpl">
+        <property name="localCache">
+            <cache:lru-cache />
+        </property>
+    </component>
+
+</components>
\ No newline at end of file

Propchange: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-custom-nodes.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-custom-nodes.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/sandbox/blueprint/org.apache.felix.blueprint/src/test/resources/test-custom-nodes.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml



Mime
View raw message