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.tests;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.jsieve.Arguments;
24 import org.apache.jsieve.SieveContext;
25 import org.apache.jsieve.exception.SieveException;
26 import org.apache.jsieve.exception.SyntaxException;
27 import org.apache.jsieve.mail.MailAdapter;
28
29 /**
30 * Class | Interface Enter description here
31 *
32 * Creation Date: 13-Jan-04
33 *
34 * @author sbrewin
35 *
36 * Copyright 2003, Synergy Systems Limited
37 */
38 /**
39 * Abstract class AbstractTest defines a framework of common behavior for Sieve
40 * Tests.
41 */
42 public abstract class AbstractTest implements ExecutableTest {
43
44 /**
45 * Constructor for AbstractTest.
46 */
47 public AbstractTest() {
48 super();
49 }
50
51 /**
52 * <p>
53 * Method execute executes a basic Sieve Test after first invoking framework
54 * methods to validate the Command arguments.
55 * </p>
56 *
57 * <p>
58 * Also,
59 *
60 * @see org.apache.jsieve.tests.ExecutableTest#execute(MailAdapter,
61 * Arguments, SieveContext)
62 */
63 public boolean execute(MailAdapter mail, Arguments arguments,
64 SieveContext context) throws SieveException {
65 validateArguments(arguments, context);
66 return executeBasic(mail, arguments, context);
67 }
68
69 /**
70 * Abstract method executeBasic invokes a Sieve Test.
71 *
72 * @param mail
73 * @param arguments
74 * @param context
75 * <code>SieveContext</code> giving contextual information, not
76 * null
77 * @return boolean
78 * @throws SieveException
79 */
80 protected abstract boolean executeBasic(MailAdapter mail,
81 Arguments arguments, SieveContext context) throws SieveException;
82
83 /**
84 * Framework method validateArguments is invoked before a Sieve Test is
85 * executed to validate its arguments. Subclass methods are expected to
86 * override or extend this method to perform their own validation as
87 * appropriate.
88 *
89 * @param arguments
90 * @param context
91 * <code>SieveContext</code> giving comntextual information,
92 * not null
93 * @throws SieveException
94 */
95 protected void validateArguments(Arguments arguments, SieveContext context)
96 throws SieveException {
97 if (!arguments.getArgumentList().isEmpty()) {
98 final Log logger = context.getLog();
99 if (logger.isWarnEnabled()) {
100 logger.warn("Unexpected arguments for " + getClass().getName());
101 }
102 context.getCoordinate().logDiagnosticsInfo(logger);
103 logger.debug(arguments);
104 final String message = context.getCoordinate()
105 .addStartLineAndColumn("Found unexpected arguments.");
106 throw new SyntaxException(message);
107 }
108 }
109 }