2012-04-17 21 views
6

Tôi đang sử dụng openEjb trong chế độ nhúng để kiểm tra một bean phiên không trạng thái, trong đó có một EntityManager được tiêm. Tuy nhiên, khi tôi chạy thử nghiệm, nó không thành công vì nó không thể khởi tạo ứng dụng. Khi tôi thấy bảng điều khiển, tôi cũng có thể thấy lỗi liên quan đến việc không thể tìm persistence.xmlopenejb nhúng container không thể tìm thấy persistence.xml

Lưu ý: Tôi đã thử đặt WebContent, cũng như META-INF trong đường dẫn lớp, nhưng điều đó cũng không giúp ích gì.

Cập nhật:

Có cái gì đó rất lạ về tra cứu tài nguyên khi sử dụng OpenEJB + eclipse.

  1. Tôi đã thêm WebContent vào thư mục nguồn. Tôi đã ngừng nhận lỗi persistence.xml, nhưng bây giờ tôi nhận được lỗi cho thấy openejb không thể tìm thấy bất kỳ tài nguyên được quản lý nào
  2. Tôi đã thêm WebContent vào thư mục nguồn, nhưng với một thư mục đầu ra khác (sử dụng thư mục đầu ra cho phép cho thư mục nguồn) và lỗi persistence.xml bắt đầu xảy ra lần nữa
  3. Tôi đã sao chép META-INF vào 'kiểm tra', là thư mục nguồn cho tất cả các thử nghiệm và mọi thứ đều chạy tốt
  4. Về điểm 1 và 2, vấn đề này dường như thực tế là WEB-INF cũng được đưa vào như một phần của WebContent. Nếu tôi loại trừ WEB-INF, thì openejb một lần nữa có thể tìm thấy persistence.xml trong thử nghiệm và thử nghiệm chạy tốt

Tôi thành thật không biết gì về những gì đang xảy ra.

Tôi đang dán tất cả các lỗi và mã có liên quan bên dưới.

Lỗi chồng từ thất bại thử JUnit trong Eclipse

org.apache.openejb.OpenEjbContainer$InvalidApplicationException: org.apache.openejb.config.ValidationFailedException: Module failed validation. AppModule(name=) 
    at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:273) 
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56) 
    at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:43) 
    at com.diycomputerscience.slides.service.SlideServiceTest.setUp(SlideServiceTest.java:45) 
    at junit.framework.TestCase.runBare(TestCase.java:128) 
    at junit.framework.TestResult$1.protect(TestResult.java:106) 
    at junit.framework.TestResult.runProtected(TestResult.java:124) 
    at junit.framework.TestResult.run(TestResult.java:109) 
    at junit.framework.TestCase.run(TestCase.java:120) 
    at junit.framework.TestSuite.runTest(TestSuite.java:230) 
    at junit.framework.TestSuite.run(TestSuite.java:225) 
    at junit.framework.TestSuite.runTest(TestSuite.java:230) 
    at junit.framework.TestSuite.run(TestSuite.java:225) 
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: org.apache.openejb.config.ValidationFailedException: Module failed validation. AppModule(name=) 
    at org.apache.openejb.config.ReportValidationResults.deploy(ReportValidationResults.java:82) 
    at org.apache.openejb.config.AppInfoBuilder.build(AppInfoBuilder.java:264) 
    at org.apache.openejb.config.ConfigurationFactory.configureApplication(ConfigurationFactory.java:696) 
    at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:267) 
    ... 18 more 

console đầu ra của tôi:

Apache OpenEJB 4.0.0-beta-2 build: 20120115-08:26 
http://openejb.apache.org/ 
INFO - openejb.home = /home/user/workspace/wwald/slides 
INFO - openejb.base = /home/user/workspace/wwald/slides 
INFO - Using 'javax.ejb.embeddable.EJBContainer=true' 
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service) 
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager) 
INFO - Inspecting classpath for applications: 38 urls. Consider adjusting your exclude/include. Current settings: openejb.deployments.classpath.exclude='', openejb.deployments.classpath.include='.*' 
INFO - Found EjbModule in classpath: /home/user/workspace/wwald/slides/build/classes 
INFO - Searched 38 classpath urls in 2000 milliseconds. Average 52 milliseconds per url. 
INFO - Beginning load: /home/user/workspace/wwald/slides/build/classes 
INFO - Configuring enterprise application: /home/user/workspace/wwald/slides 
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container) 
INFO - Auto-creating a container for bean SlideService: Container(type=STATELESS, id=Default Stateless Container) 
INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container) 
INFO - Auto-creating a container for bean com.diycomputerscience.slides.service.SlideServiceTest: Container(type=MANAGED, id=Default Managed Container) 
INFO - Dumping Generated ejb-jar.xml to: /tmp/ejb-jar-5804778531295096416slides.xml 
INFO - Dumping Generated openejb-jar.xml to: /tmp/openejb-jar-2921830618491817127slides.xml 
ERROR - FAIL ... SlideService: Missing required persistence.xml for @PersistenceContext ref "em" to unit "entities" 
ERROR - Invalid EjbModule(name=slides, path=/home/user/workspace/wwald/slides/build/classes) 
INFO - Set the 'openejb.validation.output.level' system property to VERBOSE for increased validation details. 
WARN - configureApplication.loadFailed 

Các Stateless Session Bean:

@Stateless 
public class SlideService { 
    @PersistenceContext(unitName="entities", type=PersistenceContextType.TRANSACTION) 
    private EntityManager em; 

    //various business methods not shown for brevity 
} 

persistence.xml

<persistence version="1.0" 
    xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> 
    <persistence-unit name="entities"> 
     <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider> 
     <jta-data-source>myds</jta-data-source> 
     <non-jta-data-source>myds</non-jta-data-source> 
     <properties> 
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema" /> 
     </properties> 
    </persistence-unit> 
</persistence> 

Các trường hợp thử nghiệm JUnit:

public class SlideServiceTest extends TestCase { 

    private SlideService slideService; 

    public SlideServiceTest(String name) { 
     super(name); 
    } 

    protected void setUp() throws Exception { 
     super.setUp(); 

     final Properties p = new Properties(); 
     p.put("myds", "new://Resource?type=DataSource"); 
     p.put("myds.JdbcDriver", "org.hsqldb.jdbcDriver"); 
     p.put("myds.JdbcUrl", "jdbc:hsqldb:mem:slidedb"); 

     EJBContainer ejbContainer = EJBContainer.createEJBContainer(); 
     Object oSlideService = ejbContainer.getContext().lookup("java:global/slides/SlideService"); 
     assertNotNull(oSlideService); 
     this.slideService = (SlideService)oSlideService; 
    } 

    //not showing test methods for brevity, since the code fails in setUp itself 
} 

Trả lời

7

tôi nhận được cùng một ngoại lệ và vấn đề là hai persistence.xml là trên classpath của OpenEJB.

Khi tôi xóa một trong số chúng, sự cố đã được giải quyết.

Thông điệp ngoại lệ không phải là hữu ích ở đây trên một phần của OpenEJB, nhưng nếu bạn gỡ lỗi vào hasFailures() hoặc hasErrors() phương pháp org.apache.openejb.config.Appmodule 's, bạn có thể thấy thông tin hữu ích hơn trong EjbModule.getValidation() cuộc gọi! (Phiên bản 4.0.0).

1

Vấn đề chính ở đây là khi chạy junit, JUnit bắt đầu nghĩ tệp WAR là tệp JAR do đó cố gắng tìm tệp persistance.xml trong META-INF tại thư mục gốc, trong khi tệp WAR trong META-INF được tạo bên trong Thư mục WebContent. Tôi chỉ đơn giản là tạo ra một liên kết tượng trưng trong thư mục src trỏ đến META-INF ban đầu. Và voilla ............... nó hoạt động. :)

2

Maven tạo cấu trúc thư mục cho các dự án web như hình dưới đây:

src->main-+->java 
      +->resources->META-INF 
      +->webapp->WEB-INF 

Để giải quyết vấn đề của bạn, bạn nên tạo dir [META-INF] dưới thư mục [nguồn] và nơi persistence.xml đó.

Các vấn đề liên quan