1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.jspf.tester;
21
22 import org.jvyaml.Constructor;
23 import org.jvyaml.DefaultYAMLFactory;
24 import org.jvyaml.YAMLFactory;
25
26 import java.io.BufferedReader;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.io.InputStreamReader;
31 import java.io.Reader;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
36 import java.util.Locale;
37 import java.util.Set;
38
39
40
41
42
43 public class SPFYamlTestDescriptor {
44 private String comment;
45 private HashMap tests;
46 private HashMap zonedata;
47
48 public SPFYamlTestDescriptor(HashMap source, int i) {
49 this.setComment((String) source.get("description"));
50 if (this.getComment() == null) {
51 this.setComment("Test #"+i);
52 }
53 this.setTests((HashMap) source.get("tests"));
54 this.setZonedata((HashMap) source.get("zonedata"));
55 }
56
57 public String getComment() {
58 return comment;
59 }
60 public void setComment(String comment) {
61 this.comment = comment;
62 }
63 public HashMap getTests() {
64 return tests;
65 }
66 public void setTests(HashMap tests) {
67 this.tests = tests;
68 }
69 public HashMap getZonedata() {
70 return zonedata;
71 }
72 public void setZonedata(HashMap zonedata) {
73 this.zonedata = new HashMap();
74 Set keys = zonedata.keySet();
75 for (Iterator i = keys.iterator(); i.hasNext(); ) {
76 String hostname = (String) i.next();
77 String lowercase = hostname.toLowerCase(Locale.US);
78 this.zonedata.put(lowercase, zonedata.get(hostname));
79 }
80 }
81
82 public static List loadTests(String filename) throws IOException {
83 List tests = new ArrayList();
84
85 InputStream is = SPFYamlTestDescriptor.class.getResourceAsStream(filename);
86
87 if (is != null) {
88 Reader br = new BufferedReader(new InputStreamReader(is));
89 YAMLFactory fact = new DefaultYAMLFactory();
90
91 Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(br)),fact.createResolver()));
92 int i = 1;
93 while(ctor.checkData()) {
94 Object o = ctor.getData();
95 if (o instanceof HashMap) {
96 HashMap m = (HashMap) o;
97 SPFYamlTestDescriptor ts = new SPFYamlTestDescriptor(m, i);
98 tests.add(ts);
99 }
100 i++;
101 }
102
103 return tests;
104 } else {
105 throw new FileNotFoundException("Unable to load the file");
106 }
107 }
108
109 }