View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
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   * Describe a test loaded from a YAML file using the format
41   * described in the OpenSPF testsuite.
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 }