1
2
3
4
5
6
7
8
9
10
11
12
13
14
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