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;
21  
22  import org.apache.james.jspf.core.Logger;
23  import org.apache.james.jspf.impl.Log4JLogger;
24  import org.apache.james.jspf.tester.SPFYamlTestDescriptor;
25  
26  import java.io.IOException;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  import junit.textui.TestRunner;
33  
34  public class RFC4408YamlTest extends AbstractYamlTest {
35  
36      private static final String YAMLFILE2 = "rfc4408-tests-2008.08.yml";
37  
38      /**
39       * @param name
40       * @throws IOException
41       */
42      public RFC4408YamlTest(String name) throws IOException {
43          super(name);
44      }
45  
46      protected RFC4408YamlTest(SPFYamlTestDescriptor def) {
47          super(def);
48      }
49  
50      protected RFC4408YamlTest(SPFYamlTestDescriptor def, String test) {
51          super(def, test);
52      }
53  
54      protected String getFilename() {
55          return YAMLFILE2;
56      }
57  
58      public static Test suite() throws IOException {
59          return new RFC4408Suite();
60      }
61  
62      static class RFC4408Suite extends TestSuite {
63  
64          public RFC4408Suite() throws IOException {
65              super();
66              try {
67                  List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2);
68                  Iterator i = tests.iterator();
69                  while (i.hasNext()) {
70                      SPFYamlTestDescriptor o = (SPFYamlTestDescriptor) i.next();
71                      Iterator ttt = o.getTests().keySet().iterator();
72                      while (ttt.hasNext()) {
73                          addTest(new RFC4408YamlTest(o,(String) ttt.next()));
74                      }
75                  }
76              } catch (RuntimeException e) {
77                  if ("Unable to load the file".equals(e.getMessage())) {
78                      System.err.println("WARNING: RFC4408 tests disabled.");
79                      System.err.println("The RFC4408 test-suite is not bundled with jspf due to licensing issues.");
80                      System.err.println("You can download the yaml testsuite at the following url:");
81                      System.err.println("  http://www.openspf.org/source/project/test-suite/");
82                      System.err.println("and place an rfc4408-tests.yml file in the /src/test/resources/org/apache/james/jspf folder.");
83                  }
84              }
85          }
86  
87      }
88      
89      protected void setLogger(Logger logger) {
90          this.log = logger;
91      }
92      
93      /**
94       * This method has been created for spf spec people to let them better read the
95       * output of our tests against their yaml file
96       * 
97       * @param args
98       * @throws Throwable 
99       */
100     public static void main(String[] args) throws Throwable {
101         Logger l = new Log4JLogger(org.apache.log4j.Logger.getLogger("ROOT"));
102 
103         List tests = SPFYamlTestDescriptor.loadTests(YAMLFILE2);
104         Iterator i = tests.iterator();
105         while (i.hasNext()) {
106             SPFYamlTestDescriptor o = (SPFYamlTestDescriptor) i.next();
107             Iterator ttt = o.getTests().keySet().iterator();
108             while (ttt.hasNext()) {
109                 RFC4408YamlTest t = new RFC4408YamlTest(o,(String) ttt.next());
110                 t.setLogger(l);
111                 TestRunner.run(t);
112             }
113         }
114     }
115 
116 }