2013-09-30 3 views
1

문제점을 설명하기 위해 매우 간단한 "HelloWorld"서비스를 설정했습니다. maven-scr-plugin을 사용하여 서비스 디스크립터를 생성하고 pax-exam 유닛 테스트를 수행합니다. 그러나 나는 나에게이 오류를주기 전에 'MVN에게 깨끗한 테스트'잠시 동안 차단을 실행하려고하면pax-exam을 사용할 때 maven-scr-plugin으로 생성 된 서비스 테스트 서비스

org.ops4j.pax.swissbox.tracker.ServiceLookupException: gave up waiting for service com.liveops.examples.osgi.helloworld.HelloWorldService 

나는 'MVN -DskipTests = 진정한 패키지'를 실행 한 다음 깨끗한없이 'MVN 테스트'(실행하는 경우) 그것은 작동합니다. 차이점은 내 META-INF에서이 라인이 추가 될 것으로 보인다/M ANIFEST.MF 파일 :이 라인은 이전 빌드 프로세스에 추가되어 있는지 확인하는 방법이 있는지

Service-Component: OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml 

사람이 알고 있나요 그래서 'mvn clean test'가 통과 할 것인가? 아니면 제가 잘못하고있는 다른 것이 있습니까?


다음은 pom.xml, 서비스 및 단위 테스트입니다.

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.liveops.examples</groupId> 
    <artifactId>HelloWorldService</artifactId> 
    <version>1.0.0-SNAPSHOT</version> 

    <packaging>bundle</packaging> 

    <dependencies> 
     <dependency> 
      <groupId>org.osgi</groupId> 
      <artifactId>org.osgi.core</artifactId> 
      <version>4.3.1</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.inject</groupId> 
      <artifactId>javax.inject</artifactId> 
      <version>1</version> 
     </dependency> 
     <dependency> 
      <groupId>org.ops4j.pax.exam</groupId> 
      <artifactId>pax-exam</artifactId> 
      <version>3.3.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.ops4j.pax.exam</groupId> 
      <artifactId>pax-exam-container-native</artifactId> 
      <version>3.3.0</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.ops4j.pax.exam</groupId> 
      <artifactId>pax-exam-junit4</artifactId> 
      <version>3.3.0</version> 
     </dependency> 
     <dependency> 
      <groupId>org.ops4j.pax.url</groupId> 
      <artifactId>pax-url-aether</artifactId> 
      <version>1.6.0</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.ops4j.pax.exam</groupId> 
      <artifactId>pax-exam-link-mvn</artifactId> 
      <version>3.3.0</version> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.slf4j</groupId> 
      <artifactId>slf4j-simple</artifactId> 
      <version>1.5.8</version> 
     </dependency> 
     <dependency> 
      <groupId>org.apache.felix</groupId> 
      <artifactId>org.apache.felix.framework</artifactId> 
      <version>4.0.2</version> 
      <scope>test</scope> 
     </dependency>  
     <dependency> 
      <groupId>org.apache.felix</groupId> 
      <artifactId>org.apache.felix.scr.annotations</artifactId> 
      <version>1.9.0</version> 
     </dependency> 
    </dependencies> 

    <properties> 
     <namespace>com.liveops.examples.osgi.helloworld</namespace> 
    </properties> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.0</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 

      <plugin> 
       <!-- 
       | the following instructions build a simple set of public/private classes into an OSGi bundle 
       --> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-bundle-plugin</artifactId> 
       <version>2.3.7</version> 
       <extensions>true</extensions>     
       <configuration> 
        <instructions> 
         <Bundle-SymbolicName>${project.name}</Bundle-SymbolicName> 
         <Bundle-Version>${project.version}</Bundle-Version> 
         <!-- Bundle-Activator>${namespace}.internal.HelloActivator</Bundle-Activator --> 
         <!-- 
         | assume public classes are in the top package, and private classes are under ".internal" 
         --> 
         <Export-Package>!${namespace}.internal.*,${namespace}.*;version="${project.version}"</Export-Package> 
         <Private-Package>${namespace}.internal.*</Private-Package> 
         <!-- 
         | each module can override these defaults in their osgi.bnd file 
    --> 
         <!--_include>-osgi.bnd</_include--> 
        </instructions> 
       </configuration> 
       <executions> 
        <execution> 
         <id>generate-manifest</id> 
         <phase>process-classes</phase> 
         <goals> 
          <goal>manifest</goal> 
         </goals> 
        </execution> 
       </executions>   
      </plugin> 
      <plugin> 
       <groupId>org.apache.felix</groupId> 
       <artifactId>maven-scr-plugin</artifactId> 
       <version>1.9.0</version> 
       <configuration> 
        <supportedProjectTypes> 
         <supportedProjectType>jar</supportedProjectType> 
         <supportedProjectType>bundle</supportedProjectType> 
         <supportedProjectType>war</supportedProjectType> 
        </supportedProjectTypes> 
        <generateAccessors>true</generateAccessors> 
        <strictMode>true</strictMode> 
        <specVersion>1.1</specVersion> 
        <outputDirectory>target/classes</outputDirectory> 
       </configuration> 
       <executions> 
        <execution> 
         <id>generate-scr-scrdescriptor</id> 
         <goals> 
          <goal>scr</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 

</project> 

하여 HelloWorld 구현 클래스

package com.liveops.examples.osgi.helloworld.internal; 

import com.liveops.examples.osgi.helloworld.HelloWorldService; 
import org.apache.felix.scr.annotations.Service; 
import org.apache.felix.scr.annotations.Component; 


@Component 
@Service(HelloWorldService.class) 
public class HelloImpl implements HelloWorldService 
{ 
    public String helloWorld(String personalization) 
    { 
     return "Hello " + personalization + "!"; 
    } 
} 

단위 테스트

package com.liveops.examples.osgi.helloworld.internal; 

import com.liveops.examples.osgi.helloworld.HelloWorldService; 
import junit.framework.Assert; 
import org.junit.Test; 
import org.junit.runner.RunWith; 

import org.ops4j.pax.exam.Option; 
import org.ops4j.pax.exam.Configuration; 
import org.ops4j.pax.exam.junit.PaxExam; 
import org.ops4j.pax.exam.util.PathUtils; 

import javax.inject.Inject; 

import static org.ops4j.pax.exam.CoreOptions.*; 

@RunWith(PaxExam.class) 
public class HelloImplTest 
{ 
    @Inject 
    HelloWorldService hws; 

    @Configuration 

    public static Option[] configuration() throws Exception{ 
      return options(
        systemProperty("org.ops4j.pax.logging.DefaultServiceLog.level").value("WARN"), 
        mavenBundle("org.apache.felix", "org.apache.felix.scr", "1.6.2"), 
        bundle("reference:file:" + PathUtils.getBaseDir() + "/target/classes"), 
          junitBundles()); 
    } 

    @Test 
    public void testInjection() 
    { 
     Assert.assertNotNull(hws); 
    } 

    @Test 
    public void testHelloWorld() throws Exception 
    { 
     Assert.assertNotNull(hws); 
     Assert.assertEquals("Hello UnitTest!", hws.helloWorld("UnitTest")); 

    } 
} 

답변

1

사용 ProbeBuilder는 테스트 번들을 향상 : 그 실종 모든

@ProbeBuilder 
public TestProbeBuilder probeConfiguration(TestProbeBuilder probe) { 
    probe.setHeader("Service-Component", "OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml"); 
    return probe; 
} 

이 가능성이 가장 높은이 .

편집 :

당신이 당신의 구성 방법에 특정 작업을 수행해야하는 동일한 번들에 팍스 - 시험에 사용하려는 경우를 대비

: 완전한 샘플을 찾을 수

streamBundle(bundle() 
.add(SomceClass.class).add("OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml", new File("src/main/resources/OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml") 
.toURL()) 
.set("Service-Component", "OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml") 
.build()).start() 

here

+0

답장을 보내 주셔서 감사합니다. 그러나 이것은 효과가없는 것 같습니다.분명히'@ ProbeBuilder'는 더 이상 사용되지 않습니다.이 방법을 사용하면이 오류가 발생합니다 : 오류 : PAXEXAM-PROBE-c57393ab-7878-4dd3-b533-cbbfb90411d4 (18) : 구성 요소 설명자 항목 'OSGI-INF/com .liveops.examples.osgi.helloworld.internal.HelloImpl.xml 'not found' – peterc

+0

이것은 확실히 사용되지 않습니다.이 메소드를 testclass에 추가 했습니까? 내 샘플 중 하나를보십시오 : https://github.com/ANierbeck/Camel-Pax-Exam-Demo/blob/master/route-control/src/test/java/de/nierbeck/camel/exam/demo /control/route/KarafRoutingTest.java#L168 –

+0

'org.ops4j.pax.exam.junit.ProbeBuilder'는 더 이상 사용되지 않으며'org.ops4j.pax.exam.ProbeBuilder'에 의해 대체되었습니다. –

0

약간의 우아함이 있지만이 문제가있을 수 있습니다. pom 파일의 서비스 구성 요소maven-bundle-plugin 섹션에 명시 적으로 추가 할 수 있습니다.

<plugin> 
     <groupId>org.apache.felix</groupId> 
     <artifactId>maven-bundle-plugin</artifactId> 
     <version>2.3.7</version> 
     <extensions>true</extensions>     
     <configuration> 
      <instructions> 
       <Bundle-SymbolicName>${project.name}</Bundle-SymbolicName> 
       <Bundle-Version>${project.version}</Bundle-Version> 
       <Export-Package>!${namespace}.internal.*,${namespace}.*;version="${project.version}"</Export-Package> 
       <Private-Package>${namespace}.internal.*</Private-Package> 

       <!--Explicitly add the components no that they can be found in the test phase --> 
       <Service-Component>OSGI-INF/com.liveops.examples.osgi.helloworld.internal.HelloImpl.xml</Service-Component> 
      </instructions> 
     </configuration> 
     <executions> 
      <execution> 
       <id>generate-manifest</id> 
       <phase>process-classes</phase> 
       <goals> 
        <goal>manifest</goal> 
       </goals> 
      </execution> 
     </executions>   
    </plugin> 

더 좋은 방법을 생각해 볼 수 있는지 알려 주시기 바랍니다.

0

Maven SCR Plugin은 서비스 구성 요소 설명자 만 생성하지만 자동으로 매니페스트에 포함하지 않습니다.

Maven Bundle Plugin 구성에 <Service-Component> 명령어를 포함하는 것에 대해 부끄럽지 않은 점은 documented usage입니다.

매니페스트 헤더가 없기 때문에 SCR은 번들 대신 서비스를 등록하지 않으므로 Pax Exam에서 필요한 서비스를 찾을 수 없습니다.

+0

답변에 감사드립니다. 이것이 내가가는 길이라고 생각하지만 링크 된 문서에서는'maven-scr-plugin'과'maven-bundle-plugin'을 모두 사용할 때' 직접 구성 할 필요가 없습니다. " 그리고 실제로 MANIFEST.MF 파일에서이 항목을 생성합니다. 테스트 단계에서 너무 늦은 것 뿐이며, 말했듯이 서비스는 등록되어 있지 않으므로 pax-exam에서 찾을 수 없습니다. – peterc