View Javadoc
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.creadur.whisker.fromxml;
20  
21  import org.apache.creadur.whisker.model.Resource;
22  
23  import junit.framework.TestCase;
24  import org.jdom2.Element;
25  
26  /**
27   * 
28   */
29  public class JDomBuilderResourceTest extends TestCase {
30  
31      private JDomBuilder subject;
32      
33      @Override
34      protected void setUp() throws Exception {
35          super.setUp();
36          subject = new JDomBuilder();
37      }
38  
39      @Override
40      protected void tearDown() throws Exception {
41          super.tearDown();
42      }
43  
44      public void testBuildResourceWithNameNoticeAndSourceTrimsSpacesBeforeValue() throws Exception {
45          checkResourceBuild(" name", "name", 
46                  "  notice", "notice",
47                  "   source", "source");        
48      }
49  
50  
51      public void testBuildResourceWithNameNoticeAndSourceTrimsSpaces() throws Exception {        
52          checkResourceBuild(" name   ", "name", 
53                  "  notice    ", "notice", "  " +
54                  "   source   ", "source");   
55          
56      }
57  
58  
59      public void testBuildResourceWithNameNoticeAndSourceTrimsSpacesAfterValue() throws Exception {
60          checkResourceBuild("name   ", "name", 
61                  "notice    ", "notice", "  " +
62                  "source   ", "source");  
63      }
64  
65  
66      public void testBuildResourceWithNameNoticeAndSourceTrimsWithSpacesInValue() throws Exception {
67          checkResourceBuild(" n  ame   ", "n  ame", 
68                  "  not ic e    ", "not ic e", "  " +
69                  "sour  ce   ", "sour  ce");  
70      }
71  
72      public void testBuildResourceWithNameNoticeAndSourceSpacesInVaule() throws Exception {
73          checkResourceBuildWithoutSurroundingSpace("a name", "a notice", "some source");
74      }
75      
76      public void testBuildResourceWithNameNoticeAndSourceLowers() throws Exception {
77          checkResourceBuildWithoutSurroundingSpace("name", "notice", "source");
78      }
79  
80      public void testBuildResourceWithNameNoticeAndSourceCaps() throws Exception {
81          checkResourceBuildWithoutSurroundingSpace("NOTICE", "NOTICE", "SOURCE");
82      }
83  
84      public void testThrowsIllegalArgumentWhenElementIsNotResource() throws Exception {
85          try {
86              subject.resource(
87                  new Element("bogus")
88                      .setAttribute("name", "name")
89                      .setAttribute("notice", "notice")
90                      .setAttribute("source", "source"));
91              fail("Expected IllegalArgument throw when elements is not named 'resource'");  
92          } catch (UnexpectedElementException e) {
93              //expected
94          } catch (Throwable t) {
95              fail("Expected IllegalArgument throw when elements is not named 'resource' but " + t + " was instead");
96          }
97      }
98      
99      /**
100      * @param nameValue
101      * @param noticeValue
102      * @param sourceValue
103      */
104     private void checkResourceBuildWithoutSurroundingSpace(
105             final String nameValue, final String noticeValue,
106             final String sourceValue) {
107         final String expectedNameValue = nameValue;
108         final String expectedNoticeValue = noticeValue;
109         final String expectedSourceValue = sourceValue;
110         
111         checkResourceBuild(nameValue, expectedNameValue, noticeValue,
112                 expectedNoticeValue, sourceValue, expectedSourceValue);
113     }
114 
115     /**
116      * @param nameValue
117      * @param expectedNameValue
118      * @param noticeValue
119      * @param expectedNoticeValue
120      * @param sourceValue
121      * @param expectedSourceValue
122      */
123     private void checkResourceBuild(final String nameValue,
124             final String expectedNameValue, final String noticeValue,
125             final String expectedNoticeValue, final String sourceValue,
126             final String expectedSourceValue) {
127         final Resource result = subject.resource(
128                 new Element("resource")
129                     .setAttribute("name", nameValue)
130                     .setAttribute("notice", noticeValue)
131                     .setAttribute("source", sourceValue));
132         assertNotNull("Expected builder to build", result);
133         assertEquals("Name value set from xml attribute", expectedNameValue, result.getName());
134         assertEquals("Notice value set from xml attribute", expectedNoticeValue, result.getNoticeId());
135         assertEquals("Source value set from xml attribute", expectedSourceValue, result.getSource());
136     }
137 }