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 javax.mail.MessagingException;
23  import javax.mail.internet.MimeMultipart;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.jsieve.commands.ThrowTestException;
28  import org.apache.jsieve.exception.SieveException;
29  import org.apache.jsieve.parser.generated.ParseException;
30  import org.apache.jsieve.utils.JUnitUtils;
31  import org.apache.jsieve.utils.SieveMailAdapter;
32  
33  /**
34   * Class BodyTest
35   */
36  public class BodyTest extends TestCase {
37  
38      protected SieveMailAdapter textMail() throws MessagingException {
39          SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
40          mail.getMessage().setContent("Wibble\n\n" + "Wibble\n", "text/plain");
41          return mail;
42      }
43  
44      protected SieveMailAdapter nonTextMail() throws MessagingException,
45              SieveException {
46          SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
47          // FIXME: This doesn't work
48          mail.getMessage().setContent(new MimeMultipart("image/png"));
49          return mail;
50      }
51  
52      /**
53       * Test for Test 'header'
54       */
55      public void testBasic() {
56          boolean isTestPassed = false;
57          String script = "if body :contains [\"Wibble\"] {throwTestException;}";
58          try {
59              JUnitUtils.interpret(textMail(), script);
60          } catch (MessagingException e) {
61          } catch (ThrowTestException.TestException e) {
62              isTestPassed = true;
63          } catch (ParseException e) {
64          } catch (SieveException e) {
65          }
66          assertTrue(isTestPassed);
67      }
68  
69      /**
70       * Test for Test 'body'
71       */
72      public void testBodyCaseInsensitivity() {
73          boolean isTestPassed = false;
74          String script = "if body :contains [\"wibble\"] {throwTestException;}";
75          try {
76              JUnitUtils.interpret(textMail(), script);
77          } catch (MessagingException e) {
78          } catch (ThrowTestException.TestException e) {
79              isTestPassed = true;
80          } catch (ParseException e) {
81          } catch (SieveException e) {
82          }
83          assertTrue(isTestPassed);
84      }
85  
86      /**
87       * Test for Test 'body'
88       */
89      public void testBodyNoContains() {
90          boolean isTestPassed = false;
91          String script = "if body [\"wibble\"] {throwTestException;}";
92          try {
93              JUnitUtils.interpret(textMail(), script);
94          } catch (MessagingException e) {
95          } catch (ThrowTestException.TestException e) {
96          } catch (ParseException e) {
97          } catch (SieveException e) {
98              isTestPassed = true;
99          }
100         assertTrue(isTestPassed);
101     }
102 
103     /**
104      * Test for Test 'body'
105      */
106     // FIXME: I can't find a method of forcing the mime type, so this test
107     // always fails ...
108     // public void testBodyNonText()
109     // {
110     // boolean isTestPassed = false;
111     // String script = "if body :contains [\"wibble\"] {throwTestException;}";
112     // try
113     // {
114     // JUnitUtils.interpret(nonTextMail(), script);
115     // }
116     // catch (MessagingException e)
117     // {
118     // }
119     // catch (ThrowTestException.TestException e)
120     // {
121     // }
122     // catch (ParseException e)
123     // {
124     // }
125     // catch (SieveException e)
126     // {
127     // isTestPassed = true;
128     // }
129     // assertTrue(isTestPassed);
130     // }
131 }