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 java.util.ArrayList;
23  import java.util.List;
24  
25  import junit.framework.TestCase;
26  
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.jsieve.parser.generated.ASTstring;
29  import org.apache.jsieve.parser.generated.Node;
30  import org.apache.jsieve.utils.JUnitUtils;
31  
32  public class SieveParserVisitorImplQuoteTest extends TestCase {
33  
34      
35      SieveParserVisitorImpl visitor;
36  
37      List data;
38  
39      ASTstring node;
40  
41      protected void setUp() throws Exception {
42          super.setUp();
43          final ConfigurationManager configurationManager = new ConfigurationManager();
44          visitor = new SieveParserVisitorImpl(new BaseSieveContext(
45                  configurationManager.getCommandManager(), configurationManager
46                          .getComparatorManager(), configurationManager
47                          .getTestManager(), LogFactory
48                          .getLog(SieveParserVisitorImplQuoteTest.class)));
49          data = new ArrayList();
50  
51      }
52  
53      private ASTstring stringNode(String value) throws Exception {
54          Node node = JUnitUtils.parse("fileinto " + value + ";");
55          return (ASTstring) node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
56      }
57      
58      public void testVisitASTstringObjectQuoted() throws Exception {
59          node = stringNode("\"value\"");
60          visitor.visit(node, data);
61          assertEquals("Data value added after quotes stripped", 1, data.size());
62          assertEquals("Data value added after quotes stripped", "value", data
63                  .get(0));
64      }
65  
66      public void testVisitASTstringObjectQuoteInQuoted() throws Exception {
67          
68          node = stringNode("\"val\\\"ue\"");
69          visitor.visit(node, data);
70          assertEquals("Data value added after quotes stripped", 1, data.size());
71          assertEquals("Data value added after quotes stripped", "val\"ue", data
72                  .get(0));
73      }
74  
75      public void testVisitASTstringObjectDoubleSlashQuoted() throws Exception {
76  
77          node = stringNode("\"val\\\\ue\"");
78          visitor.visit(node, data);
79          assertEquals("Data value added after quotes stripped", 1, data.size());
80          assertEquals("Data value added after quotes stripped", "val\\ue", data
81                  .get(0));
82      }
83  
84      public void testVisitASTstringObjectSlashQuoted() throws Exception {
85  
86          node = stringNode("\"value\"");
87          visitor.visit(node, data);
88          assertEquals("Data value added after quotes stripped", 1, data.size());
89          assertEquals("Data value added after quotes stripped", "value", data
90                  .get(0));
91      }
92  
93      public void testVisitASTstringEmptyQuoted() throws Exception {
94  
95          node = stringNode("\"\"");
96          visitor.visit(node, data);
97          assertEquals("Data value added after quotes stripped", 1, data.size());
98          assertEquals("Data value added after quotes stripped", "", data.get(0));
99      }
100 
101     public void testVisitASTstringObjectMultiSlashQuoted() throws Exception {
102 
103         node = stringNode("\"v\\\\al\\\\u\\e\\\\\"");
104         visitor.visit(node, data);
105         assertEquals("Data value added after quotes stripped", 1, data.size());
106         assertEquals("Data value added after quotes stripped", "v\\al\\ue\\",
107                 data.get(0));
108     }
109 }