1   /*
2    * Copyright 2004,2005 The Apache Software Foundation.
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.apache.neethi;
17  
18  import java.io.File;
19  import java.io.FileInputStream;
20  import java.io.FileNotFoundException;
21  import java.io.InputStream;
22  
23  import javax.xml.stream.XMLInputFactory;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.axiom.om.OMAbstractFactory;
28  import org.apache.axiom.om.OMElement;
29  import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
30  
31  
32  public class PolicyTestCase extends TestCase{
33      
34      protected String baseDir = System.getProperty("basedir");
35      protected String testResourceDir = "src" + File.separator + "test" + File.separator + "test-resources";
36      
37      public PolicyTestCase(String name) {
38          super(name);
39          if (baseDir == null) {
40              baseDir = (String) new File(".").getAbsolutePath();
41          }
42      }
43  
44      public InputStream getResource(String name) {
45          String filePath = new File(testResourceDir, name).getAbsolutePath(); 
46  
47          try {
48              FileInputStream fis = new FileInputStream(filePath);
49              return fis;
50          } catch (FileNotFoundException e) {
51              fail("Cannot get resource: " + e.getMessage());
52              throw new RuntimeException();
53          }
54      }
55      
56      public OMElement getResourceAsElement(String name) {
57          try {
58              InputStream in = getResource(name);
59              OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(
60                      OMAbstractFactory.getOMFactory(),
61                      XMLInputFactory.newInstance().createXMLStreamReader(in)).getDocumentElement();
62              return element;
63  
64          } catch (Exception e) {
65              fail("Cannot get resource: " + e.getMessage());
66              throw new RuntimeException();
67          }
68      }
69  }
70