incubator-aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From timothyjw...@apache.org
Subject svn commit: r998981 - in /incubator/aries/trunk/jpa/jpa-blueprint-aries/src: main/java/org/apache/aries/jpa/blueprint/aries/impl/ main/resources/org/apache/aries/jpa/blueprint/namespace/ test/java/org/apache/aries/jpa/blueprint/aries/test/ test/resources/
Date Mon, 20 Sep 2010 16:14:50 GMT
Author: timothyjward
Date: Mon Sep 20 16:14:50 2010
New Revision: 998981

URL: http://svn.apache.org/viewvc?rev=998981&view=rev
Log:
ARIES-413 - Blueprint JPA integration: support for constructor/factory injection of JPA resources

Added:
    incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/resources/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd
    incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/resources/jpa_110.xml
Modified:
    incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java
    incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java

Modified: incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java?rev=998981&r1=998980&r2=998981&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java
(original)
+++ incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/java/org/apache/aries/jpa/blueprint/aries/impl/NSHandler.java
Mon Sep 20 16:14:50 2010
@@ -44,6 +44,7 @@ import org.apache.aries.jpa.container.Pe
 import org.apache.aries.jpa.container.context.PersistenceContextProvider;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.ServiceReference;
+import org.osgi.service.blueprint.reflect.BeanArgument;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
 import org.osgi.service.blueprint.reflect.BeanProperty;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
@@ -67,12 +68,16 @@ import org.w3c.dom.NodeList;
  * the {@link GlobalPersistenceManager}.
  */
 public class NSHandler implements NamespaceHandler {
+    private static final String ATTR_INDEX = "index";
+
     /** Logger */
     private static final Logger _logger = LoggerFactory
             .getLogger("org.apache.aries.jpa.blueprint.aries");
 
-    /** The JPA namespace */
-    public static final String NS_URI = "http://aries.apache.org/xmlns/jpa/v1.0.0";
+    /** The JPA 1.0.0 namespace */
+    public static final String NS_URI_100 = "http://aries.apache.org/xmlns/jpa/v1.0.0";
+    /** The JPA 1.0.0 namespace */
+    public static final String NS_URI_110 = "http://aries.apache.org/xmlns/jpa/v1.1.0";
     /** The standard blueprint namespace */
     private static final String BLUEPRINT_NS = "http://www.osgi.org/xmlns/blueprint/v1.0.0";
 
@@ -128,8 +133,8 @@ public class NSHandler implements Namesp
         Element element = (Element) node;
         // The surrounding component should always be a bean
         if (!(component instanceof BeanMetadata)) {
-            _logger.error("The JPA namespace should only be used to inject properties into
a bean. The surrounding component was {}.",
-                            new Object[] { component });
+            _logger.error("The JPA namespace should only be used to inject properties or
constuctor arguments into a bean." +
+            		" The surrounding component was {}.", new Object[] { component });
             throw new IllegalArgumentException(component.toString());
         }
         
@@ -142,27 +147,45 @@ public class NSHandler implements Namesp
 
         MutableBeanMetadata bean = (MutableBeanMetadata) component;
 
-        if (!NS_URI.equals(element.getNamespaceURI())) {
-            _logger
-                    .error(
-                            "The JPA namespace handler should not be called for the namespace
{}.",
+        if (!NS_URI_100.equals(element.getNamespaceURI())
+            && !NS_URI_110.equals(element.getNamespaceURI())) {
+            _logger.error("The JPA namespace handler should not be called for the namespace
{}.",
                             new Object[] { element.getNamespaceURI() });
-            throw new IllegalArgumentException();
+            throw new IllegalArgumentException("The JPA namespace handler should not be called
for the namespace " 
+                   + element.getNamespaceURI());
         }
 
         if (!TAG_UNIT.equals(element.getLocalName())
                 && !TAG_CONTEXT.equals(element.getLocalName())) {
-            _logger
-                    .error(
-                            "The JPA namespace handler did not recognize the element named
{}.",
+            _logger.error("The JPA namespace handler did not recognize the element named
{}.",
                             new Object[] { element.getLocalName() });
-            throw new IllegalArgumentException();
+            throw new IllegalArgumentException("The JPA namespace handler did not recognize
the element named " 
+                   + element.getLocalName());
         }
 
-        // Create an injection point for the JPA resource (a blueprint property)
-        final BeanProperty beanProperty = createInjectMetadata(element,
-                TAG_UNIT.equals(element.getLocalName()), context);
-
+        String property = element.getAttribute(ATTR_PROPERTY);
+        property = property.isEmpty() ? null : property;
+        String index = element.getAttribute(ATTR_INDEX);
+        index = index.isEmpty() ? null : index;
+        if(property != null && index != null) {
+          _logger.error("It is invalid to specify a bean property and an index") ;
+        } else if (property != null) {
+            
+                
+            // Create an injection point for the JPA resource (a blueprint property)
+            BeanProperty beanProperty = createBeanProperty(element, property,
+                    TAG_UNIT.equals(element.getLocalName()), context);
+
+            bean.addProperty(beanProperty);
+        } else {
+          
+          //Create a constructor argument for the JPA resource
+          BeanArgument argument = createArgument(element, index, 
+              TAG_UNIT.equals(element.getLocalName()), context);
+          
+          bean.addArgument(argument);
+        }
+        
         // If this is a persistence context then register it with the manager
         if (TAG_CONTEXT.equals(element.getLocalName())) {
             Bundle client = getBlueprintBundle(context);
@@ -188,8 +211,6 @@ public class NSHandler implements Namesp
             }
         }
 
-        bean.addProperty(beanProperty);
-        
         return bean;
     }
 
@@ -200,8 +221,10 @@ public class NSHandler implements Namesp
     }
 
     public URL getSchemaLocation(String namespace) {
-        if(NS_URI.equals(namespace))
+        if(NS_URI_100.equals(namespace))
             return getClass().getResource("/org/apache/aries/jpa/blueprint/namespace/jpa.xsd");
+        else if (NS_URI_110.equals(namespace))
+            return getClass().getResource("/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd");
         else
             return null;
     }
@@ -246,10 +269,9 @@ public class NSHandler implements Namesp
      *            The current parser context
      * @return
      */
-    private BeanProperty createInjectMetadata(Element element,
+    private BeanProperty createBeanProperty(Element element, final String property,
             boolean isPersistenceUnit, ParserContext ctx) {
         String unitName = parseUnitName(element);
-        final String property = element.getAttribute(ATTR_PROPERTY);
 
         if (_logger.isDebugEnabled()) {
             if (isPersistenceUnit)
@@ -260,45 +282,8 @@ public class NSHandler implements Namesp
                                 new Object[] { unitName, property });
         }
 
-        // Create a service reference for the EMF (it is an EMF for persistence
-        // contexts and units)
-        final MutableReferenceMetadata refMetadata = (MutableReferenceMetadata) ctx
-                .createMetadata(ReferenceMetadata.class);
-        refMetadata.setActivation(ACTIVATION_EAGER.equalsIgnoreCase(ctx
-                .getDefaultActivation()) ? ReferenceMetadata.ACTIVATION_EAGER
-                : ReferenceMetadata.ACTIVATION_LAZY);
-        refMetadata.setAvailability(ReferenceMetadata.AVAILABILITY_MANDATORY);
-        refMetadata.setInterface(EntityManagerFactory.class.getName());
-
-        // Pick the right EMF by looking for the presence, or absence, of the
-        // PROXY_FACTORY service property
-        StringBuilder filter = new StringBuilder("(&");
-        // Persistence units do not have the property, persistence contexts do
-        if (isPersistenceUnit)
-            filter.append("(!(").append(PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE)
-            .append("=*))");
-        else
-            filter.append("(").append(PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE)
-                    .append("=*)");
-
-        // Add the empty name filter if necessary
-        if (!"".equals(unitName))
-            filter.append("(" + PersistenceUnitConstants.OSGI_UNIT_NAME + "="
-                    + unitName + ")");
-        else
-            filter.append(EMPTY_UNIT_NAME_FILTER);
-
-        filter.append(")");
-
-        refMetadata.setFilter(filter.toString());
-        refMetadata.setTimeout(Integer.parseInt(ctx.getDefaultTimeout()));
-        refMetadata.setDependsOn((List<String>) Collections.EMPTY_LIST);
-        refMetadata.setId(ctx.generateId());
-
-        // Finally, if this is a persistence context we need to create the
-        // entity manager as the Target
-        final Metadata target = isPersistenceUnit ? refMetadata
-                : createInjectionBeanMetedata(ctx, refMetadata);
+        final Metadata target = createTargetMetadata(isPersistenceUnit, ctx,
+            unitName);
 
         return new BeanProperty() {
             public Metadata getValue() {
@@ -310,6 +295,116 @@ public class NSHandler implements Namesp
             }
         };
     }
+    
+    /**
+     * Create a BeanProperty that will inject a JPA resource into a bean
+     * 
+     * @param element
+     *            The element being parsed
+     * @param isPersistenceUnit
+     *            true if this is a persistence unit
+     * @param ctx
+     *            The current parser context
+     * @return
+     */
+    private BeanArgument createArgument(Element element, final String index,
+            final boolean isPersistenceUnit, ParserContext ctx) {
+        
+        String unitName = parseUnitName(element);
+        if (_logger.isDebugEnabled()) {
+          if (isPersistenceUnit) {
+              if (index == null)
+                  _logger.debug("Creating blueprint injection metadata to inject the unit
{} as a constructor argument",
+                              new Object[] { unitName });
+              else
+                  _logger.debug("Creating blueprint injection metadata to inject the unit
{} as a constructor argument" +
+                      " with index {}", new Object[] { unitName, index });
+          } else {
+              if (index == null)
+                  _logger.debug("Creating blueprint injection metadata to inject the context
{} as a constructor argument",
+                              new Object[] { unitName });
+              else
+                  _logger.debug("Creating blueprint injection metadata to inject the context
{} as a constructor argument" +
+                      " with index {}", new Object[] { unitName, index });
+          }
+      }
+        
+        final int i;
+        
+        if(index == null) {
+            i = -1;
+        } else {
+            try {
+                i = Integer.parseInt(index);
+            } catch (NumberFormatException nfe) {
+                throw new IllegalArgumentException("The string " + index + " could not be
parsed as an index.", nfe);
+            }
+        }
+        
+        final Metadata target = createTargetMetadata(isPersistenceUnit, ctx,
+            unitName);
+
+        return new BeanArgument() {
+            public Metadata getValue() {
+                return target;
+            }
+
+            @Override
+            public String getValueType() {
+              return isPersistenceUnit ? "javax.persistence.EntityManagerFactory" 
+                                       : "javax.persistence.EntityManager";
+            }
+
+            @Override
+            public int getIndex() {
+              return i;
+            }
+        };
+    }
+
+    private Metadata createTargetMetadata(boolean isPersistenceUnit,
+        ParserContext ctx, String unitName) {
+      // Create a service reference for the EMF (it is an EMF for persistence
+      // contexts and units)
+      final MutableReferenceMetadata refMetadata = (MutableReferenceMetadata) ctx
+              .createMetadata(ReferenceMetadata.class);
+      refMetadata.setActivation(ACTIVATION_EAGER.equalsIgnoreCase(ctx
+              .getDefaultActivation()) ? ReferenceMetadata.ACTIVATION_EAGER
+              : ReferenceMetadata.ACTIVATION_LAZY);
+      refMetadata.setAvailability(ReferenceMetadata.AVAILABILITY_MANDATORY);
+      refMetadata.setInterface(EntityManagerFactory.class.getName());
+
+      // Pick the right EMF by looking for the presence, or absence, of the
+      // PROXY_FACTORY service property
+      StringBuilder filter = new StringBuilder("(&");
+      // Persistence units do not have the property, persistence contexts do
+      if (isPersistenceUnit)
+          filter.append("(!(").append(PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE)
+          .append("=*))");
+      else
+          filter.append("(").append(PersistenceContextProvider.PROXY_FACTORY_EMF_ATTRIBUTE)
+                  .append("=*)");
+
+      // Add the empty name filter if necessary
+      if (!"".equals(unitName))
+          filter.append("(" + PersistenceUnitConstants.OSGI_UNIT_NAME + "="
+                  + unitName + ")");
+      else
+          filter.append(EMPTY_UNIT_NAME_FILTER);
+
+      filter.append(")");
+
+      refMetadata.setFilter(filter.toString());
+      refMetadata.setTimeout(Integer.parseInt(ctx.getDefaultTimeout()));
+      refMetadata.setDependsOn((List<String>) Collections.EMPTY_LIST);
+      refMetadata.setId(ctx.generateId());
+
+      // Finally, if this is a persistence context we need to create the
+      // entity manager as the Target
+      final Metadata target = isPersistenceUnit ? refMetadata
+              : createInjectionBeanMetedata(ctx, refMetadata);
+      return target;
+    }
 
     /**
      * This method turns a persistence context factory into an

Added: incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/resources/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/resources/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd?rev=998981&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/resources/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd
(added)
+++ incubator/aries/trunk/jpa/jpa-blueprint-aries/src/main/resources/org/apache/aries/jpa/blueprint/namespace/jpa_110.xsd
Mon Sep 20 16:14:50 2010
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!--
+
+    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.
+
+-->
+<xsd:schema xmlns="http://aries.apache.org/xmlns/jpa/v1.0.0"
+            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+            xmlns:osgi="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+            targetNamespace="http://aries.apache.org/xmlns/jpa/v1.1.0"
+            elementFormDefault="qualified"
+            attributeFormDefault="unqualified"
+            version="1.0.0">
+
+    <xsd:import namespace="http://www.osgi.org/xmlns/blueprint/v1.0.0"/>
+
+  <xsd:element name="context" type="TcontextType" />
+  <xsd:element name="unit" type="TunitType" />
+  
+  <xsd:complexType name="TcontextType">
+    <xsd:sequence>
+      <xsd:element name="map" type="osgi:Tmap" minOccurs="0" maxOccurs="1" />
+    </xsd:sequence>
+    <xsd:attribute name="property" type="xsd:string" use="optional" />
+    <xsd:attribute name="index" type="xsd:string" use="optional" />
+    <xsd:attribute name="unitname" type="xsd:string" use="optional" />
+    <xsd:attribute name="type" type="TcontextScopeType" use="optional" />
+  </xsd:complexType>
+
+  <xsd:complexType name="TunitType">
+    <xsd:attribute name="property" type="xsd:string" use="optional" />
+    <xsd:attribute name="index" type="xsd:string" use="optional" />
+    <xsd:attribute name="unitname" type="xsd:string" use="optional" />
+  </xsd:complexType>
+
+   <xsd:simpleType name="TcontextScopeType">
+        <xsd:restriction base="xsd:token">
+            <xsd:enumeration value="TRANSACTION"/>
+            <xsd:enumeration value="EXTENDED"/>
+        </xsd:restriction>
+   </xsd:simpleType>
+
+</xsd:schema>
+  

Modified: incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java?rev=998981&r1=998980&r2=998981&view=diff
==============================================================================
--- incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java
(original)
+++ incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/java/org/apache/aries/jpa/blueprint/aries/test/NSHandlerTest.java
Mon Sep 20 16:14:50 2010
@@ -19,9 +19,11 @@
 package org.apache.aries.jpa.blueprint.aries.test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -49,6 +51,7 @@ import org.apache.aries.unittest.mocks.S
 import org.junit.Before;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
+import org.osgi.service.blueprint.reflect.BeanArgument;
 import org.osgi.service.blueprint.reflect.BeanMetadata;
 import org.osgi.service.blueprint.reflect.BeanProperty;
 import org.osgi.service.blueprint.reflect.ComponentMetadata;
@@ -62,6 +65,7 @@ import org.w3c.dom.NodeList;
 
 public class NSHandlerTest {
   private Element root;
+  private Element root_110;
   private NSHandler sut;
   private PersistenceContextProvider manager;
   private ParserContext parserCtx;
@@ -78,6 +82,10 @@ public class NSHandlerTest {
     Document doc = builder.parse(getClass().getClassLoader().getResourceAsStream("jpa.xml"));
     root = doc.getDocumentElement();
     
+    builder = factory.newDocumentBuilder();
+    doc = builder.parse(getClass().getClassLoader().getResourceAsStream("jpa_110.xml"));
+    root_110 = doc.getDocumentElement();
+    
     sut = new NSHandler();
     manager = Skeleton.newMock(PersistenceContextProvider.class);
     sut.setManager(manager);
@@ -127,7 +135,24 @@ public class NSHandlerTest {
   
   @Test
   public void testUnit() {
-    Element e = getTestElement("unit");
+    Element e = getTestElement("unit", root);
+    BeanMetadata bean = 
+      (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
+    BeanProperty property = (BeanProperty) bean.getProperties().get(0);
+    ReferenceMetadata reference = (ReferenceMetadata) property.getValue();
+    
+    assertEquals("emf", property.getName());
+    assertEquals(EntityManagerFactory.class.getName(), reference.getInterface());
+    assertEquals("(&(!(org.apache.aries.jpa.proxy.factory=*))(osgi.unit.name=myUnit))",
reference.getFilter());
+    
+    Skeleton.getSkeleton(manager).assertSkeletonNotCalled();
+    
+    assertTrue(registeredComponents.isEmpty());
+  }
+  
+  @Test
+  public void testUnit_110() {
+    Element e = getTestElement("unit", root_110);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
     BeanProperty property = (BeanProperty) bean.getProperties().get(0);
@@ -144,7 +169,22 @@ public class NSHandlerTest {
   
   @Test
   public void testUnitNoName() {
-    Element e = getTestElement("unitNoName");
+    Element e = getTestElement("unitNoName", root);
+    BeanMetadata bean = 
+      (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
+    BeanProperty property = (BeanProperty) bean.getProperties().get(0);
+    ReferenceMetadata reference = (ReferenceMetadata) property.getValue();
+    
+    assertEquals("emf2", property.getName());
+    assertEquals("(&(!(org.apache.aries.jpa.proxy.factory=*))"+NSHandler.EMPTY_UNIT_NAME_FILTER+")",

+        reference.getFilter());
+
+    assertTrue(registeredComponents.isEmpty());
+  }
+  
+  @Test
+  public void testUnitNoName_110() {
+    Element e = getTestElement("unitNoName", root_110);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
     BeanProperty property = (BeanProperty) bean.getProperties().get(0);
@@ -159,7 +199,22 @@ public class NSHandlerTest {
   
   @Test
   public void testEmptyUnitName() {
-    Element e = getTestElement("emptyUnitName");
+    Element e = getTestElement("emptyUnitName", root);
+    BeanMetadata bean = 
+      (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
+    BeanProperty property = (BeanProperty) bean.getProperties().get(0);
+    ReferenceMetadata reference = (ReferenceMetadata) property.getValue();
+    
+    assertEquals("emf3", property.getName());
+    assertEquals("(&(!(org.apache.aries.jpa.proxy.factory=*))"+NSHandler.EMPTY_UNIT_NAME_FILTER+")",
+        reference.getFilter());
+    
+    assertTrue(registeredComponents.isEmpty());
+  }
+  
+  @Test
+  public void testEmptyUnitName_110() {
+    Element e = getTestElement("emptyUnitName", root_110);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
     BeanProperty property = (BeanProperty) bean.getProperties().get(0);
@@ -174,7 +229,22 @@ public class NSHandlerTest {
   
   @Test 
   public void testBeanMetadataOverwrite() {
-    Element e = getTestElement("unit");
+    Element e = getTestElement("unit", root);
+    BeanMetadataImpl oldBean = new BeanMetadataImpl();
+    oldBean.setId("myid");
+    oldBean.setProperties(Arrays.asList(Skeleton.newMock(BeanProperty.class)));
+    
+    BeanMetadata bean = (BeanMetadata) sut.decorate(e, oldBean, parserCtx);
+
+    assertEquals("myid", bean.getId());
+    assertEquals(2, bean.getProperties().size());
+    
+    assertTrue(registeredComponents.isEmpty());
+  }
+  
+  @Test 
+  public void testBeanMetadataOverwrite_110() {
+    Element e = getTestElement("unit", root);
     BeanMetadataImpl oldBean = new BeanMetadataImpl();
     oldBean.setId("myid");
     oldBean.setProperties(Arrays.asList(Skeleton.newMock(BeanProperty.class)));
@@ -189,7 +259,29 @@ public class NSHandlerTest {
 
   @Test
   public void testDefaultContext() {
-    Element e = getTestElement("context");
+    Element e = getTestElement("context", root);
+    BeanMetadata bean = 
+      (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
+    BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
+
+    assertEquals("createEntityManager", innerBean.getFactoryMethod());
+    assertEquals("internalClose", innerBean.getDestroyMethod());
+
+    assertEquals(1, registeredComponents.size());
+    ReferenceMetadata reference = (ReferenceMetadata) registeredComponents.get(0);
+    
+    assertEquals(EntityManagerFactory.class.getName(), reference.getInterface());
+    assertEquals("(&(org.apache.aries.jpa.proxy.factory=*)(osgi.unit.name=myUnit))",
reference.getFilter());
+    
+    Map<String,Object> props = new HashMap<String, Object>();
+    props.put(PersistenceContextProvider.PERSISTENCE_CONTEXT_TYPE, PersistenceContextType.TRANSACTION);
+    Skeleton.getSkeleton(manager).assertCalled(
+        new MethodCall(PersistenceContextProvider.class, "registerContext", "myUnit", clientBundle,
props));
+  }
+  
+  @Test
+  public void testDefaultContext_110() {
+    Element e = getTestElement("context", root_110);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
     BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
@@ -213,7 +305,31 @@ public class NSHandlerTest {
   public void testContextNoPersistenceContextProvider() {
     
     sut.contextUnavailable(null);
-    Element e = getTestElement("context");
+    Element e = getTestElement("context", root);
+    BeanMetadata bean = 
+      (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
+    BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
+
+    assertEquals("createEntityManager", innerBean.getFactoryMethod());
+    assertEquals("internalClose", innerBean.getDestroyMethod());
+
+    assertEquals(1, registeredComponents.size());
+    ReferenceMetadata reference = (ReferenceMetadata) registeredComponents.get(0);
+    
+    assertEquals(EntityManagerFactory.class.getName(), reference.getInterface());
+    assertEquals("(&(org.apache.aries.jpa.proxy.factory=*)(osgi.unit.name=myUnit))",
reference.getFilter());
+    
+    Map<String,Object> props = new HashMap<String, Object>();
+    props.put(PersistenceContextProvider.PERSISTENCE_CONTEXT_TYPE, PersistenceContextType.TRANSACTION);
+    Skeleton.getSkeleton(manager).assertNotCalled(
+        new MethodCall(PersistenceContextProvider.class, "registerContext", String.class,
Bundle.class, Map.class));
+  }
+  
+  @Test
+  public void testContextNoPersistenceContextProvider_110() {
+    
+    sut.contextUnavailable(null);
+    Element e = getTestElement("context", root_110);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
     BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
@@ -235,7 +351,31 @@ public class NSHandlerTest {
   
   @Test
   public void testContextWithProps() {
-    Element e = getTestElement("contextWithProps");
+    Element e = getTestElement("contextWithProps", root);
+    BeanMetadata bean = 
+      (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
+    BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
+    
+    assertEquals("createEntityManager", innerBean.getFactoryMethod());
+    
+    assertEquals(1, registeredComponents.size());
+    ReferenceMetadata reference = (ReferenceMetadata) registeredComponents.get(0);
+    
+    assertEquals(EntityManagerFactory.class.getName(), reference.getInterface());
+    assertEquals("(&(org.apache.aries.jpa.proxy.factory=*)"+NSHandler.EMPTY_UNIT_NAME_FILTER+")",

+        reference.getFilter());
+    
+    Map<String,Object> props = new HashMap<String, Object>();
+    props.put(PersistenceContextProvider.PERSISTENCE_CONTEXT_TYPE, PersistenceContextType.EXTENDED);
+    props.put("one", "eins");
+    props.put("two", "zwo");
+    Skeleton.getSkeleton(manager).assertCalled(
+        new MethodCall(PersistenceContextProvider.class, "registerContext", "", clientBundle,
props));    
+  }
+  
+  @Test
+  public void testContextWithProps_110() {
+    Element e = getTestElement("contextWithProps", root_110);
     BeanMetadata bean = 
       (BeanMetadata) sut.decorate(e, new BeanMetadataImpl(), parserCtx);
     BeanMetadata innerBean = (BeanMetadata) ((BeanProperty) bean.getProperties().get(0)).getValue();
@@ -259,13 +399,127 @@ public class NSHandlerTest {
   
   @Test
   public void testNoMoreProxying() {
-      Element e = getTestElement("contextWithProps");
+      Element e = getTestElement("contextWithProps", root);
       BeanMetadata input = new BeanMetadataImpl();
       Object output = sut.decorate(e, input, parserCtx);
       assertTrue(input == output);
   }
   
-  private Element getTestElement(String beanName) {
+  @Test
+  public void testNoMoreProxying_110() {
+      Element e = getTestElement("contextWithProps", root_110);
+      BeanMetadata input = new BeanMetadataImpl();
+      Object output = sut.decorate(e, input, parserCtx);
+      assertTrue(input == output);
+  }
+  
+  @Test
+  public void testNonIndexedArgs_110() {
+      Element e = getTestElement("withUnitArg", root_110);
+      BeanMetadata input = new BeanMetadataImpl();
+      Object output = sut.decorate(e, input, parserCtx);
+      assertEquals("Wrong number of arguments",
+          1 ,input.getArguments().size());
+      assertEquals("Wrong class type", "javax.persistence.EntityManagerFactory",
+          ((BeanArgument)input.getArguments().get(0)).getValueType());
+      assertEquals("Wrong index", -1,
+          ((BeanArgument)input.getArguments().get(0)).getIndex());
+      
+      ReferenceMetadata reference = (ReferenceMetadata) ((BeanArgument)input.getArguments().get(0)).getValue();
+      assertEquals(EntityManagerFactory.class.getName(), reference.getInterface());
+      assertEquals("(&(!(org.apache.aries.jpa.proxy.factory=*))(osgi.unit.name=myUnit))",
reference.getFilter());
+      
+      Skeleton.getSkeleton(manager).assertSkeletonNotCalled();
+      assertTrue(registeredComponents.isEmpty());
+      
+      e = getTestElement("withContextArg", root_110);
+      input = new BeanMetadataImpl();
+      output = sut.decorate(e, input, parserCtx);
+      
+      assertEquals("Wrong number of arguments",
+          1 ,input.getArguments().size());
+      assertEquals("Wrong type", "javax.persistence.EntityManager",
+          ((BeanArgument)input.getArguments().get(0)).getValueType());
+      assertEquals("Wrong index", -1,
+          ((BeanArgument)input.getArguments().get(0)).getIndex());
+      
+      BeanMetadata innerBean = (BeanMetadata) ((BeanArgument)input.getArguments().get(0)).getValue();
+
+      assertEquals("createEntityManager", innerBean.getFactoryMethod());
+      assertEquals("internalClose", innerBean.getDestroyMethod());
+
+      assertEquals(1, registeredComponents.size());
+      reference = (ReferenceMetadata) registeredComponents.get(0);
+      
+      assertEquals(EntityManagerFactory.class.getName(), reference.getInterface());
+      assertEquals("(&(org.apache.aries.jpa.proxy.factory=*)(osgi.unit.name=myUnit))",
reference.getFilter());
+      
+      Map<String,Object> props = new HashMap<String, Object>();
+      props.put(PersistenceContextProvider.PERSISTENCE_CONTEXT_TYPE, PersistenceContextType.TRANSACTION);
+      Skeleton.getSkeleton(manager).assertCalled(
+          new MethodCall(PersistenceContextProvider.class, "registerContext", "myUnit", clientBundle,
Map.class));
+  }
+  
+  @Test
+  public void testIndexedArgs_110() {
+      Element e = getTestElement("withIndexedUnitArg", root_110);
+      BeanMetadata input = new BeanMetadataImpl();
+      Object output = sut.decorate(e, input, parserCtx);
+      assertEquals("Wrong number of arguments",
+          1 ,input.getArguments().size());
+      assertEquals("Wrong class type", "javax.persistence.EntityManagerFactory",
+          ((BeanArgument)input.getArguments().get(0)).getValueType());
+      assertEquals("Wrong index", 0,
+          ((BeanArgument)input.getArguments().get(0)).getIndex());
+      
+      ReferenceMetadata reference = (ReferenceMetadata) ((BeanArgument)input.getArguments().get(0)).getValue();
+      assertEquals(EntityManagerFactory.class.getName(), reference.getInterface());
+      assertEquals("(&(!(org.apache.aries.jpa.proxy.factory=*))(osgi.unit.name=myUnit))",
reference.getFilter());
+      
+      Skeleton.getSkeleton(manager).assertSkeletonNotCalled();
+      assertTrue(registeredComponents.isEmpty());
+      
+      e = getTestElement("withIndexedContextArg", root_110);
+      input = new BeanMetadataImpl();
+      output = sut.decorate(e, input, parserCtx);
+      
+      assertEquals("Wrong number of arguments",
+          1 ,input.getArguments().size());
+      assertEquals("Wrong type", "javax.persistence.EntityManager",
+          ((BeanArgument)input.getArguments().get(0)).getValueType());
+      assertEquals("Wrong index", 1,
+          ((BeanArgument)input.getArguments().get(0)).getIndex());
+      
+      BeanMetadata innerBean = (BeanMetadata) ((BeanArgument)input.getArguments().get(0)).getValue();
+
+      assertEquals("createEntityManager", innerBean.getFactoryMethod());
+      assertEquals("internalClose", innerBean.getDestroyMethod());
+
+      assertEquals(1, registeredComponents.size());
+      reference = (ReferenceMetadata) registeredComponents.get(0);
+      
+      assertEquals(EntityManagerFactory.class.getName(), reference.getInterface());
+      assertEquals("(&(org.apache.aries.jpa.proxy.factory=*)(osgi.unit.name=myUnit))",
reference.getFilter());
+      
+      Map<String,Object> props = new HashMap<String, Object>();
+      props.put(PersistenceContextProvider.PERSISTENCE_CONTEXT_TYPE, PersistenceContextType.TRANSACTION);
+      Skeleton.getSkeleton(manager).assertCalled(
+          new MethodCall(PersistenceContextProvider.class, "registerContext", "myUnit", clientBundle,
Map.class));
+  }
+  
+  @Test
+  public void testInvalidIndex_110() {
+      Element e = getTestElement("withInvalidIndexArg", root_110);
+      BeanMetadata input = new BeanMetadataImpl();
+      try {
+          Object output = sut.decorate(e, input, parserCtx);
+          fail("Should throw an exception");
+      } catch (IllegalArgumentException iae) {
+          assertTrue("Wrong cause type", iae.getCause() instanceof NumberFormatException);
+      }
+  }
+  
+  private Element getTestElement(String beanName, Element root) {
     NodeList ns = root.getElementsByTagName("bean");
     
     Element bean = null;
@@ -279,7 +533,8 @@ public class NSHandlerTest {
     for (int i=0; i<ns.getLength(); i++) {
       if (ns.item(i).getNodeType() == Node.ELEMENT_NODE) {
         Element e = (Element) ns.item(i);
-        if (NSHandler.NS_URI.equals(e.getNamespaceURI()))
+        if (NSHandler.NS_URI_100.equals(e.getNamespaceURI())
+            || NSHandler.NS_URI_110.equals(e.getNamespaceURI()))
           return e;
       }
     }
@@ -290,7 +545,10 @@ public class NSHandlerTest {
   @Test
   public void testgetSchemaLocation()
   {
-    assertNotNull("No schema found", sut.getSchemaLocation(NSHandler.NS_URI));
+    assertNotNull("No schema found", sut.getSchemaLocation(NSHandler.NS_URI_100));
+    assertNotNull("No schema found", sut.getSchemaLocation(NSHandler.NS_URI_110));
+    assertFalse("Should not be the same schema", sut.getSchemaLocation(NSHandler.NS_URI_100)
+                  .equals(sut.getSchemaLocation(NSHandler.NS_URI_110)));
     assertNull("No schema expected", sut.getSchemaLocation("http://maven.apache.org/POM/4.0.0"));
   }
 }

Added: incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/resources/jpa_110.xml
URL: http://svn.apache.org/viewvc/incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/resources/jpa_110.xml?rev=998981&view=auto
==============================================================================
--- incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/resources/jpa_110.xml (added)
+++ incubator/aries/trunk/jpa/jpa-blueprint-aries/src/test/resources/jpa_110.xml Mon Sep 20
16:14:50 2010
@@ -0,0 +1,65 @@
+<?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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" 
+  xmlns:jpa="http://aries.apache.org/xmlns/jpa/v1.1.0">
+
+  <bean id="unit">
+    <jpa:unit property="emf" unitname="myUnit" />
+  </bean>
+  
+  <bean id="unitNoName">
+    <jpa:unit property="emf2" />
+  </bean>
+  
+  <bean id="emptyUnitName">
+    <jpa:unit property="emf3" unitname="" />
+  </bean>
+  
+  <bean id="context">
+    <jpa:context property="em" unitname="myUnit"/>
+  </bean>
+  
+  <bean id="contextWithProps">
+    <jpa:context property="em" type="EXTENDED">
+      <map>
+        <entry key="one" value="eins" />
+        <entry key="two" value="zwo" />
+      </map>
+    </jpa:context>
+  </bean>
+
+  <bean id="withUnitArg">
+    <jpa:unit unitname="myUnit"/>
+  </bean>
+  
+  <bean id="withContextArg">
+    <jpa:context unitname="myUnit"/>
+  </bean>
+
+  <bean id="withIndexedUnitArg">
+    <jpa:unit index="0" unitname="myUnit"/>
+  </bean>
+  
+  <bean id="withIndexedContextArg">
+    <jpa:context index="1" unitname="myUnit"/>
+  </bean>
+
+  <bean id="withInvalidIndexArg">
+    <jpa:unit index="1A" unitname="myUnit"/>
+  </bean>
+</blueprint>
\ No newline at end of file



Mime
View raw message