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  package org.apache.jsieve.util;
20  
21  import java.io.ByteArrayInputStream;
22  
23  import org.apache.jsieve.ConfigurationManager;
24  import org.apache.jsieve.parser.generated.ASTarguments;
25  import org.apache.jsieve.parser.generated.ASTcommand;
26  import org.apache.jsieve.parser.generated.ASTcommands;
27  import org.apache.jsieve.parser.generated.ASTstart;
28  import org.apache.jsieve.parser.generated.Node;
29  import org.jmock.Mock;
30  import org.jmock.MockObjectTestCase;
31  
32  public class SimpleNodeTraverserTest extends MockObjectTestCase {
33  
34      Mock mock;
35      NodeHandler handler;
36      
37      NodeTraverser subject;
38      
39      protected void setUp() throws Exception {
40          super.setUp();
41          mock = mock(NodeHandler.class);
42          handler = (NodeHandler) mock.proxy();
43          
44          subject = new NodeTraverser();
45      }
46  
47      protected void tearDown() throws Exception {
48          super.tearDown();
49      }
50      
51      public void testTraverseSimpleScript() throws Exception {
52          mock.expects(once()).method("start").id("1");
53          mock.expects(once()).method("start").with(isA(ASTstart.class)).after("1").id("2");
54          mock.expects(once()).method("start").with(isA(ASTcommands.class)).after("2").id("3");
55          mock.expects(once()).method("start").with(isA(ASTcommand.class)).after("3").id("4");
56          mock.expects(once()).method("start").with(isA(ASTarguments.class)).after("4").id("5");
57          mock.expects(once()).method("end").with(isA(ASTarguments.class)).after("5").id("6");
58          mock.expects(once()).method("end").with(isA(ASTcommand.class)).after("6").id("7");
59          mock.expects(once()).method("end").with(isA(ASTcommands.class)).after("7").id("8");
60          mock.expects(once()).method("end").with(isA(ASTstart.class)).after("8").id("9");
61          mock.expects(once()).method("end").after("9");
62          traverse("Keep;");
63      }
64      
65      private void traverse(String script) throws Exception {
66          subject.traverse(handler, parse(script));
67      }
68  
69      private Node parse(String script) throws Exception {
70          return new ConfigurationManager().build().parse(
71                  new ByteArrayInputStream(script.getBytes()));
72      }
73  }