View Javadoc

1   package cz.cuni.amis.pogamut.sposh.elements;
2   
3   import java.io.BufferedReader;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.InputStreamReader;
7   import java.io.StringReader;
8   import org.junit.Assert;
9   
10  /**
11   * Contains various methods that are useful in other tests of POSH, especially loading of
12   * plans.
13   *
14   * @author Honza
15   */
16  public abstract class PlanTest extends Assert {
17  
18      public String loadPlan(String relativeResourcePath) throws IOException {
19          String resourcePath = this.getClass().getPackage().getName().replace('.', '/') + '/' + relativeResourcePath;
20  
21          InputStream is = getClass().getClassLoader().getResourceAsStream(resourcePath);
22  
23          if (is == null) {
24              fail("Unable to open resource \"" + resourcePath + "\"");
25          }
26  
27          BufferedReader reader = new BufferedReader(new InputStreamReader(is));
28          StringBuilder sb = new StringBuilder();
29          String line;
30  
31          while ((line = reader.readLine()) != null) {
32              sb.append(line);
33              sb.append('\n');
34          }
35  
36          reader.close();
37  
38          return sb.toString();
39      }
40  
41      public PoshPlan parsePlan(String relativeResourcePath) throws IOException, ParseException {
42          String plan = loadPlan(relativeResourcePath);
43          PoshParser parser = new PoshParser(new StringReader(plan));
44          return parser.parsePlan();
45      }
46      
47      public String getMethodName() {
48          return Thread.currentThread().getStackTrace()[2].getMethodName();
49      }
50      
51  }