001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020 package org.apache.james.jspf.tester;
021
022 import org.jvyaml.Constructor;
023 import org.jvyaml.DefaultYAMLFactory;
024 import org.jvyaml.YAMLFactory;
025
026 import java.io.BufferedReader;
027 import java.io.FileNotFoundException;
028 import java.io.IOException;
029 import java.io.InputStream;
030 import java.io.InputStreamReader;
031 import java.io.Reader;
032 import java.util.ArrayList;
033 import java.util.HashMap;
034 import java.util.Iterator;
035 import java.util.List;
036 import java.util.Locale;
037 import java.util.Map;
038 import java.util.Set;
039
040 /**
041 * Describe a test loaded from a YAML file using the format
042 * described in the OpenSPF testsuite.
043 */
044 public class SPFYamlTestDescriptor {
045 private String comment;
046 private Map<String, Map<String, ?>> tests;
047 private Map<String, Object> zonedata;
048
049 @SuppressWarnings("unchecked")
050 public SPFYamlTestDescriptor(Map<String, ?> source, int i) {
051 this.setComment((String) source.get("description"));
052 if (this.getComment() == null) {
053 this.setComment("Test #"+i);
054 }
055 this.setTests((Map) source.get("tests"));
056 this.setZonedata((Map) source.get("zonedata"));
057 }
058
059 public String getComment() {
060 return comment;
061 }
062 public void setComment(String comment) {
063 this.comment = comment;
064 }
065 public Map<String,Map<String,?>> getTests() {
066 return tests;
067 }
068 public void setTests(Map<String, Map<String,?>> tests) {
069 this.tests = tests;
070 }
071 public Map<String, ?> getZonedata() {
072 return zonedata;
073 }
074 public void setZonedata(Map<String, Map<?, ?>> zonedata) {
075 this.zonedata = new HashMap<String, Object>();
076 Set<String> keys = zonedata.keySet();
077 for (Iterator<String> i = keys.iterator(); i.hasNext(); ) {
078 String hostname = (String) i.next();
079 String lowercase = hostname.toLowerCase(Locale.US);
080 this.zonedata.put(lowercase, zonedata.get(hostname));
081 }
082 }
083
084 @SuppressWarnings("unchecked")
085 public static List<SPFYamlTestDescriptor> loadTests(String filename) throws IOException {
086 List<SPFYamlTestDescriptor> tests = new ArrayList<SPFYamlTestDescriptor>();
087
088 InputStream is = SPFYamlTestDescriptor.class.getResourceAsStream(filename);
089 System.out.println(filename+": "+is);
090
091 if (is != null) {
092 Reader br = new BufferedReader(new InputStreamReader(is));
093 YAMLFactory fact = new DefaultYAMLFactory();
094
095 Constructor ctor = fact.createConstructor(fact.createComposer(fact.createParser(fact.createScanner(br)),fact.createResolver()));
096 int i = 1;
097 while(ctor.checkData()) {
098 Object o = ctor.getData();
099 if (o instanceof Map<?, ?>) {
100 Map<String, ?> m = (Map<String, ?>) o;
101 SPFYamlTestDescriptor ts = new SPFYamlTestDescriptor(m, i);
102 tests.add(ts);
103 }
104 i++;
105 }
106
107 return tests;
108 } else {
109 throw new FileNotFoundException("Unable to load the file");
110 }
111 }
112
113 }