• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2006-2013 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.objenesis.tck;
17 
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.Serializable;
21 
22 import javax.xml.parsers.DocumentBuilder;
23 import javax.xml.parsers.DocumentBuilderFactory;
24 import javax.xml.xpath.XPath;
25 import javax.xml.xpath.XPathExpression;
26 import javax.xml.xpath.XPathExpressionException;
27 import javax.xml.xpath.XPathFactory;
28 
29 import org.objenesis.Objenesis;
30 import org.objenesis.ObjenesisHelper;
31 import org.springframework.osgi.test.AbstractConfigurableBundleCreatorTests;
32 import org.w3c.dom.Document;
33 
34 /**
35  * @author Henri Tremblay
36  */
37 public class OsgiTest extends AbstractConfigurableBundleCreatorTests implements Serializable {
38 
39    private static final long serialVersionUID = 1L;
40 
getTestBundlesNames()41    protected String[] getTestBundlesNames() {
42       String version = getImplementationVersion(Objenesis.class);
43       // Null means we are an IDE, not in Maven. So we have an IDE project dependency instead
44       // of a Maven dependency with the jar. Which explains why the version is null (no Manifest
45       // since there's no jar. In that case we get the version from the pom.xml and hope the Maven
46       // jar is up-to-date in the local repository
47       if(version == null) {
48          try {
49             XPathFactory xPathFactory = XPathFactory.newInstance();
50             final XPath xPath = xPathFactory.newXPath();
51             XPathExpression xPathExpression;
52             try {
53                xPathExpression = xPath.compile("/project/parent/version");
54             }
55             catch(final XPathExpressionException e) {
56                throw new RuntimeException(e);
57             }
58 
59             final DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance();
60             xmlFact.setNamespaceAware(false);
61             final DocumentBuilder builder = xmlFact.newDocumentBuilder();
62             final Document doc = builder.parse(new File("pom.xml"));
63             version = xPathExpression.evaluate(doc);
64          }
65          catch(final Exception e) {
66             throw new RuntimeException(e);
67          }
68       }
69       return new String[] {"org.objenesis, objenesis, " + version};
70    }
71 
testCanInstantiate()72    public void testCanInstantiate() throws IOException {
73       assertSame(OsgiTest.class, ObjenesisHelper.newInstance(getClass()).getClass());
74    }
75 
testCanInstantiateSerialize()76    public void testCanInstantiateSerialize() throws IOException {
77       assertSame(OsgiTest.class, ObjenesisHelper.newSerializableInstance(getClass()).getClass());
78    }
79 
getImplementationVersion(final Class c)80    protected String getImplementationVersion(final Class c) {
81       return c.getPackage().getImplementationVersion();
82    }
83 }
84