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 org.jmock.Mock;
22  import org.jmock.MockObjectTestCase;
23  
24  public class SieveToXmlNullNameTest extends MockObjectTestCase {
25  
26      SieveToXml subject;
27      Mock mockOut;
28      SieveHandler instance;
29      
30      protected void setUp() throws Exception {
31          super.setUp();
32          subject = new SieveToXml();
33          subject.setNameAttributeName(null);
34          mockOut = mock(SieveToXml.Out.class);
35          instance = subject.build((SieveToXml.Out) mockOut.proxy());
36      }
37  
38      protected void tearDown() throws Exception {
39          super.tearDown();
40      }
41  
42      private void assertElementIsOutput(final String commandName, final String elementName) throws HaltTraversalException {
43          mockOut.expects(once()).method("openElement").with(eq(elementName), 
44                  eq(SieveToXml.DEFAULT_NAMESPACE), eq(SieveToXml.DEFAULT_PREFIX)).id("1");
45          mockOut.expects(once()).method("closeElement").after("1");
46          assertBuilderIsReturned(instance.startCommand(commandName));
47          assertBuilderIsReturned(instance.endCommand(commandName));
48      }
49  
50      private void assertBuilderIsReturned(SieveHandler handler) {
51          assertEquals("Builder pattern so instance should be returned", instance, handler);
52      }
53      
54      public void testBuildIsNotNull() {
55          assertNotNull(instance);
56      }
57          
58      public void testControlCommandShouldOutputElement() throws Exception {
59          assertElementIsOutput("if", SieveToXml.DEFAULT_NAME_CONTROL_COMMAND);
60      }
61  
62      public void testActionCommandShouldOutputElement() throws Exception {
63          assertElementIsOutput("other", SieveToXml.DEFAULT_NAME_ACTION_COMMAND);
64      }
65      
66      public void testTestShouldOutputElement() throws Exception {
67          final String testName = "is";
68          mockOut.expects(once()).method("openElement").with(eq(SieveToXml.DEFAULT_NAME_TEST), 
69                  eq(SieveToXml.DEFAULT_NAMESPACE), eq(SieveToXml.DEFAULT_PREFIX)).id("1");
70          mockOut.expects(once()).method("closeElement").after("1");
71          assertBuilderIsReturned(instance.startTest(testName));
72          assertBuilderIsReturned(instance.endTest(testName));
73      }
74     
75  }