1 /** 2 * Copyright 2006-2017 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 org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.objenesis.Objenesis; 25 import org.objenesis.ObjenesisHelper; 26 import org.ops4j.pax.exam.Configuration; 27 import org.ops4j.pax.exam.Option; 28 import org.ops4j.pax.exam.junit.PaxExam; 29 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; 30 import org.ops4j.pax.exam.spi.reactors.PerMethod; 31 import org.w3c.dom.Document; 32 33 import javax.xml.parsers.DocumentBuilder; 34 import javax.xml.parsers.DocumentBuilderFactory; 35 import javax.xml.xpath.XPath; 36 import javax.xml.xpath.XPathExpression; 37 import javax.xml.xpath.XPathExpressionException; 38 import javax.xml.xpath.XPathFactory; 39 40 import static org.junit.Assert.*; 41 import static org.ops4j.pax.exam.CoreOptions.*; 42 43 /** 44 * @author Henri Tremblay 45 */ 46 @RunWith(PaxExam.class) 47 @ExamReactorStrategy(PerMethod.class) 48 public class OsgiTest implements Serializable{ 49 50 private static final long serialVersionUID = 1L; 51 52 @Configuration config()53 public Option[] config() { 54 String version = getImplementationVersion(Objenesis.class); 55 return options( 56 bundle("file:../main/target/objenesis-" + version + ".jar"), 57 junitBundles() 58 ); 59 } 60 61 @Test testCanInstantiate()62 public void testCanInstantiate() throws IOException { 63 assertSame(OsgiTest.class, ObjenesisHelper.newInstance(getClass()).getClass()); 64 } 65 66 @Test testCanInstantiateSerialize()67 public void testCanInstantiateSerialize() throws IOException { 68 assertSame(OsgiTest.class, ObjenesisHelper.newSerializableInstance(getClass()).getClass()); 69 } 70 getImplementationVersion(final Class<?> c)71 private String getImplementationVersion(final Class<?> c) { 72 String version = c.getPackage().getImplementationVersion(); 73 // Null means we are an IDE, not in Maven. So we have an IDE project dependency instead 74 // of a Maven dependency with the jar. Which explains why the version is null (no Manifest 75 // since there's no jar). In that case we get the version from the pom.xml. 76 if(version == null) { 77 try { 78 XPathFactory xPathFactory = XPathFactory.newInstance(); 79 final XPath xPath = xPathFactory.newXPath(); 80 XPathExpression xPathExpression; 81 try { 82 xPathExpression = xPath.compile("/project/parent/version"); 83 } 84 catch(final XPathExpressionException e) { 85 throw new RuntimeException(e); 86 } 87 88 final DocumentBuilderFactory xmlFact = DocumentBuilderFactory.newInstance(); 89 xmlFact.setNamespaceAware(false); 90 final DocumentBuilder builder = xmlFact.newDocumentBuilder(); 91 final Document doc = builder.parse(new File("pom.xml")); 92 version = xPathExpression.evaluate(doc); 93 } 94 catch(final Exception e) { 95 throw new RuntimeException(e); 96 } 97 } 98 return version; 99 } 100 } 101