使用Arquillian时,如何引用放在WEB-INF文件夹中的文件? [英] How do I reference a file that is placed in the WEB-INF folder when using Arquillian?

查看:88
本文介绍了使用Arquillian时,如何引用放在WEB-INF文件夹中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用maven和标准目录布局。所以我在src / test / resources文件夹中添加了一个testdata.xml文件,我还将其添加为:

I am using maven and the standard directory layout. So I have added a testdata.xml file in the src/test/resources folder, and I also added it as:

.addAsWebInfResource("testdata.xml", "testdata.xml")

在部署方法中,我已经确认它在那里。这将使文件显示在 /WEB-INF/testdata.xml 中。现在我需要在我的代码中引用这个文件,我尝试了几个不同的 getClass()。getResourceAsStream(...)并且一次又一次失败所以我需要一些现在建议。

in the deployment method, and I have confirmed that it is there. This will make the file appear in /WEB-INF/testdata.xml. Now I need to have a reference to this file in my code and I tried several different getClass().getResourceAsStream(...) and failing again and again so I need some advise now.

我的DBUnit集成测试需要它。这是不可能的吗?

I need it for my DBUnit integration test. Is this not possible?

推荐答案

选项A)使用ServletContext.getResourceXXX()

你应该有一个Aquillarian MockHttpSession和一个MockServletContext。例如:

You should have a Aquillarian MockHttpSession and a MockServletContext. E.g.:

@Test
   public void myTest()
   {
      HttpSession session = new MockHttpSession(new MockServletContext());
      ServletLifecycle.beginSession(session);

      ..testCode..

      // You can obtain a ServletContext (will actually be a MockServletContext 
      // implementation):
      ServletContext sc = session.getServletContext();
      URL url = sc.getResource("/WEB-INF/testdata.xml")
      Path resPath = new Path(url);
      File resFile = new File(url);
      FileReader resRdr = new FileReader(resFile);
      etc...

      ..testCode..

      ServletLifecycle.endSession(session);
   }

您可以创建资源文件&子目录:

You can create resource files & subdirectories in:


  1. 网络模块文档根目录 - 可以从浏览器和类中访问资源

  2. WEB-INF / classes / - 类可访问资源

  3. WEB-INF / lib / * .jar source jar - 类可访问

  4. WEB-INF / lib / * .jar专用资源专用jar - 类可访问

  5. WEB-INF /直接在目录中 - 可访问类。这就是你要求的。

  1. the web module document root - resources are accessible from the browser and from classes
  2. WEB-INF/classes/ - resources are accessible to classes
  3. WEB-INF/lib/*.jar source jar - accessible to classes
  4. WEB-INF/lib/*.jar dedicated resource-only jar - accessible to classes
  5. WEB-INF/ directly within directory - accessible to classes. This is what you are asking for.

在所有情况下都可以通过以下方式访问资源:

In all cases the resource can be accessed via:

URL url = sc.getResource("/<path from web doc root>/<resourceFileName>");
OR    
InputStream resIS = sc.getResourceAsStream("/<path from web doc root>/<resourceFileName>");

>

这些将打包成WAR文件可以展开到已部署的应用服务器上的目录中,也可以保留在应用服务器上的WAR文件中。无论哪种方式 - 访问资源的行为相同:使用ServletContext.getResourceXXX()。

These will be packaged into the WAR file and may be exploded into directories on the deployed app server OR they may stay within the WAR file on the app server. Either way - same behaviour for accessing resources: use ServletContext.getResourceXXX().

注意,作为一般原则,(5)顶级WEB-INF目录本身供服务器使用。不礼貌地将您的网络资源直接放在这里或直接在这里创建您自己的目录是礼貌的。相反,最好使用上面的(2)。

Note that as a general principle, (5) the top-level WEB-INF directory itself is intended for use by the server. It is 'polite' not to put your web resources directly in here or create your own directory directly in here. Instead, better to use (2) above.

JEE5教程网络模块
JEE6教程网络模块

选项B):使用Class.getResourceXXX()

首先将资源从WEB-INF文件夹移到WEB-INF / classes(或在jar-WEB / INF / *。jar中)。

First move the resource out of WEB-INF folder into WEB-INF/classes (or inside a jar WEB-INF/lib/*.jar).

如果您的测试类是:


  • com.abc.pkg.test.MyTest在文件WEB-INF /中classes / com / abc / pkg / test / MyTest.class

您的资源文件是


  • WEB-INF / classes / com / abc / pkg / test / resources / testdata.xml(或jar文件中的等效文件)

使用相对文件位置访问文件,通过Java ClassLoader - 找到相对于Classpath的文件夹/ Jars:

Access File using Relative File Location, via the Java ClassLoader - finds Folders/Jars relative to Classpath:

java.net.URL resFileURL = MyTest.class.getResource("resources/testdata.xml");
File resFile = new File(fileURL);    
OR
InputStream resFileIS = 
     MyTedy.class.getResourceAsStream("resources/testdata.xml");

访问文件使用完全类似于Package的资格,使用Java ClassLoader - 查找相对于Classpath的文件夹/ Jars :

Access File Using full Package-like Qualification, Using the Java ClassLoader - finds Folders/Jars relative to Classpath:

java.net.URL resFileURL = MyTest.class.getResource("/com/abc/pkg/test/resources/testdata.xml");
File resFile = new File(fileURL);        
OR 
InputStream resFileIS = 
     MyTest.class.getResourceAsStream("/com/abc/pkg/test/resources/testdata.xml");   
OR 
java.net.URL resFileURL = MyTest.class.getClassLoader().getResource("com/abc/pkg/test/resources/testdata.xml");
File resFile = new File(fileURL);       
OR 
InputStream resFileIS = 
     MyTest.class.getClassLoader().getResourceAsStream("com/abc/pkg/test/resources/testdata.xml");

希望指甲吧! @B)

这篇关于使用Arquillian时,如何引用放在WEB-INF文件夹中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆