2013-06-25 2 views
0

배포를 위해 여러 프로필이 필요합니다. 나는 그것을 필터링 이야기하기 위해 받는다는 - EJB-플러그인에 filterDeploymentDescriptor을 활성화Enterprise Bean 컨테이너 (GlassFish)가 내장 된 단위 테스트를 위해 ebj-jar.xml에서 env-entry 값을 필터링 (대체)하는 방법은 무엇입니까?

<profiles> 
    <profile> 
    <id>dev</id> 
    <activation> 
     <activeByDefault>true</activeByDefault> <!-- use dev profile by default --> 
    </activation> 
    <build> 
    </build> 
    <properties> 
     <theHost>localhost</theHost> 
    </properties> 
    </profile> 
... 

(대체 : 메이븐 POM에서 나는 프로필 "DEV"와 (로컬 호스트와 같은) 속성 "theHost"를 정의 EJB-jar.xml의 단위) 값 :

<session> 
    <ejb-name>MongoDao</ejb-name> 
    <ejb-class>com.coolcorp.MongoDao</ejb-class> 
    <session-type>Stateless</session-type> 
    <env-entry> 
    <env-entry-name>host</env-entry-name> 
    <env-entry-type>java.lang.String</env-entry-type> 
    <env-entry-value>${theHost}</env-entry-value> 
    </env-entry> 
... 
:

<plugins> 
    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-ejb-plugin</artifactId> 
    <version>2.3</version> 
    <configuration> 
     <ejbVersion>3.1</ejbVersion> 
--> <filterDeploymentDescriptor>true</filterDeploymentDescriptor> 
    </configuration> 
</plugin 

마지막에 EJB-jar.xml의 I는 @Resource 속성 "호스트"에 대한 원하는 프로파일 특정 값을 구하는 참조 ${theHost}

이 모든 것이 일반적인 Maven 빌드와 잘 맞습니다. 그러나 GlassFish의 임베디드 엔터프라이즈 빈 컨테이너 [EJBContainer.createEJBContainer()]를 사용하여 EJB 유닛 테스트를 실행하면 maven-ejb-plugin은 filterDeploymentDescriptor = true를 무시하는 것으로 보입니다. 동일한 "dev"프로파일을 가진 maven을 실행하더라도 EJB는 "localhost"대신 "$ {theHost}"를 봅니다.

mvn.bat -Pdev test 

단위 테스트를 실행할 때 대체가 작동하지 않는 이유는 누구나 알 수 있습니까? ejb-jar.xml을 필터링 할 수 있도록 단위 테스트를 위해 특별히 정의해야 할 것이 있습니까? 또는 다른 프로파일이 존재하는 경우 단위 테스트 EJB에 대한 더 나은 접근 방법?

답변

0

이상적으로 env-entry에 대해 외부 "바인딩"을 지정할 수 있습니다. 나는 WebSphere Application Server (EnvEntry.Value properties을 통해)와 할 수 있음을 알고 있지만 Glassfish가 가능한지 여부는 알지 못합니다.

해결 방법으로 주입을 위해 env-entry를 선언 한 다음 PostConstruct에서 컨테이너가 값을 주입했는지 여부를 확인할 수 있습니다 (예 : 컨테이너에 배치 할 때까지 env-entry-value를 지정하지 마십시오. 섬기는 사람). JNDI 만 사용하는 경우 try/catch (NameNotFoundException)를 사용하여 동일한 작업을 수행 할 수 있습니다. bkail의 제안에 따라

@Resource(name="host") 
private String host; 

@PostConstruct 
public void postConstruct() { 
    if (host == null) { 
    // Not configured at deployment time. 
    host = System.getProperty("test.host"); 
    } 
} 
+0

제안 해 주셔서 감사합니다. 우리는 앞으로 전환 할 수 있기 때문에 컨테이너의 독점 메커니즘을 피하려고 노력합니다. 그러나 Maven 플러그인 maven-surefire-plugin을 사용하여 시스템 속성을 설정하여 해결 방법을 구현했습니다. 주석의 공간 제한으로 인한 답변. – StaticNoiseLog

0

해결 방법 :

// Override values that were not substituted in ejb-jar.xml 
    if (Boolean.getBoolean("is.unittest")) { 
     host = "localhost"; 
     port = "27017"; 
     authenticationRequired = false; 
    } 
: @PostConstruct 주석 Java 메소드에 다음

 <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.14.1</version> 
      <configuration> 
       <skip>false</skip> 
       <argLine>-Xmx1g -XX:MaxPermSize=128m</argLine> 
       <reuseForks>false</reuseForks> <!-- with reuse the EJB timer service would fail --> 
       <systemPropertyVariables> 
        <is.unittest>true</is.unittest> 
       </systemPropertyVariables> 
      </configuration> 
     </plugin> 

을 그리고 : 만 단위 테스트를위한 시스템 속성을 설정하고 PostConstruct의 그것을 발견

관련 문제