View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.felix.bundleplugin;
20  
21  import aQute.bnd.osgi.AbstractResource;
22  import aQute.bnd.osgi.Analyzer;
23  import aQute.bnd.osgi.Jar;
24  import aQute.bnd.osgi.Resource;
25  import org.junit.Test;
26  
27  import static org.junit.Assert.assertEquals;
28  
29  public class JpaPluginTest {
30  
31      @Test
32      public void testNamedJndi() throws Exception {
33          String xmlStr = "<persistence xmlns='http://java.sun.com/xml/ns/persistence'>\n" +
34                          "    <persistence-unit name='the-unit' transaction-type='JTA'>\n" +
35                          "        <jta-data-source>osgi:service/jdbc/h2DS</jta-data-source>\n" +
36                          "    </persistence-unit>\n" +
37                          "</persistence>";
38          String expectedReqs = "osgi.extender;osgi.extender=aries.jpa,osgi.service;effective:=active;objectClass=javax.transaction.TransactionManager";
39          assertTransformation(xmlStr, expectedReqs);
40      }
41  
42      @Test
43      public void testService() throws Exception {
44          String xmlStr = "<persistence xmlns='http://java.sun.com/xml/ns/persistence'>\n" +
45                          "    <persistence-unit name='the-unit' transaction-type='JTA'>\n" +
46                          "        <jta-data-source>osgi:service/javax.sql.DataSource/(&amp;(db=mydb)(version=3.1))</jta-data-source>\n" +
47                          "    </persistence-unit>\n" +
48                          "</persistence>";
49          String expectedReqs = "osgi.extender;osgi.extender=aries.jpa,osgi.service;effective:=active;objectClass=javax.transaction.TransactionManager,osgi.service;effective:=active;objectClass=javax.sql.DataSource;filter:=\"(&(db=mydb)(version=3.1))\"";
50          assertTransformation(xmlStr, expectedReqs);
51      }
52  
53      private void assertTransformation(final String xmlStr, String expectedReqs) throws Exception {
54          Analyzer analyzer = new Analyzer();
55          Jar jar = new Jar("the-jar");
56          Resource xml = new AbstractResource(0) {
57              @Override
58              protected byte[] getBytes() throws Exception {
59                  return xmlStr.getBytes();
60              }
61          };
62          JpaPlugin plugin = new JpaPlugin();
63  
64          jar.putResource("the-persistence-xml", xml);
65          analyzer.setJar(jar);
66          analyzer.setProperty("Meta-Persistence", "the-persistence-xml");
67  
68          plugin.analyzeJar(analyzer);
69  
70          assertEquals(expectedReqs, analyzer.getProperty("Require-Capability"));
71  
72      }
73  }