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.jsieve;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.jsieve.exception.LookupException;
24  import org.apache.jsieve.exception.SieveException;
25  import org.apache.jsieve.mail.MailAdapter;
26  import org.apache.jsieve.tests.ExecutableTest;
27  
28  /**
29   * <p>
30   * A parsed representation of an RFC3028 test argument...
31   * </p>
32   * 
33   * <code>test = identifier arguments</code>
34   */
35  public class Test implements Executable {
36  
37      /** The name of this Test */
38      private String fieldName;
39  
40      /** The arguments for this Test */
41      private Arguments fieldArguments;
42  
43      /**
44       * @see org.apache.jsieve.Executable#execute(MailAdapter, SieveContext)
45       */
46      public Object execute(MailAdapter mail, SieveContext context)
47              throws SieveException {
48          return new Boolean(isTestPassed(mail, context));
49      }
50  
51      /**
52       * Is this test passed for the given mail?
53       * @param mail not null
54       * @param context not null
55       * @return true when the test passes, false otherwise
56       * @throws LookupException
57       * @throws SieveException
58       */
59      public boolean isTestPassed(MailAdapter mail, SieveContext context) throws LookupException, SieveException {
60          Log log = context.getLog();
61          if (log.isDebugEnabled()) {
62              log.debug(toString());
63          }
64          final String name = getName();
65          final ExecutableTest test = context.getTestManager().getTest(name);
66          return test.execute(mail, getArguments(), context);
67      }
68  
69      /**
70       * Constructor for Test.
71       */
72      private Test() {
73          super();
74      }
75  
76      /**
77       * Constructor for Test.
78       * 
79       * @param name
80       * @param arguments
81       */
82      public Test(String name, Arguments arguments) {
83          this();
84          setName(name);
85          setArguments(arguments);
86      }
87  
88      /**
89       * Returns the arguments.
90       * 
91       * @return Arguments
92       */
93      public Arguments getArguments() {
94          return fieldArguments;
95      }
96  
97      /**
98       * Returns the name.
99       * 
100      * @return String
101      */
102     public String getName() {
103         return fieldName;
104     }
105 
106     /**
107      * Sets the arguments.
108      * 
109      * @param arguments
110      *            The arguments to set
111      */
112     protected void setArguments(Arguments arguments) {
113         fieldArguments = arguments;
114     }
115 
116     /**
117      * Sets the name.
118      * 
119      * @param name
120      *            The name to set
121      */
122     protected void setName(String name) {
123         fieldName = name;
124     }
125 
126     /**
127      * @see java.lang.Object#toString()
128      */
129     public String toString() {
130         return "Test name: " + getName() + " "
131                 + (getArguments() == null ? "null" : getArguments().toString());
132     }
133 
134 }