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.License;
22  
23  import junit.framework.TestCase;
24  import org.jdom2.CDATA;
25  import org.jdom2.Element;
26  
27  /**
28   * 
29   */
30  public class JDomBuilderLicenseTest extends TestCase {
31  
32      private JDomBuilder subject;
33      
34      @Override
35      protected void setUp() throws Exception {
36          super.setUp();
37          subject = new JDomBuilder();
38      }
39  
40      @Override
41      protected void tearDown() throws Exception {
42          super.tearDown();
43      }
44      
45      public void testLicenseWithNameUrl() throws Exception {
46          checkSetNameUrlId("a name", "some url", "some id");
47          checkSetNameUrlId("name", "url", "id");
48          checkSetNameUrlId("NAME", "URL", "ID");
49          checkSetNameUrlId("a name", "  some url  ", "  some id  ");        
50      }
51  
52      /**
53       * @param nameValue
54       * @param urlValue
55       * @param idValue
56       */
57      private void checkSetNameUrlId(final String nameValue,
58              final String urlValue, final String idValue) {
59          final License result = subject.license(new Element("license")
60                      .setAttribute("name", nameValue)
61                      .setAttribute("url", urlValue)
62                      .setAttribute("id", idValue));
63          assertNotNull("Builder should build", result);
64          assertEquals("Set id", idValue, result.getId());
65          assertEquals("Set url", urlValue, result.getURL());
66          assertEquals("Set name", nameValue, result.getName());
67      }
68      
69      public void testLicenseRequiresSourceTrue() throws Exception {
70          assertTrue("Expected required source to be set", licenseWithSource("yes").isSourceRequired());
71      }
72      
73      public void testLicenseRequiresSourceTrueAllCaps() throws Exception {
74          assertTrue("Expected required source to be set even when yes is in all caps case. This is against the schema.", 
75                  licenseWithSource("YES").isSourceRequired());
76      }
77      
78      public void testLicenseRequiresSourceTrueMixedCase() throws Exception {
79          assertTrue("Expected required source to be set even when yes is in mixed case. This is against the schema.", 
80                  licenseWithSource("Yes").isSourceRequired());
81      }
82  
83      public void testLicenseRequiresSourceFalse() throws Exception {
84          assertFalse("Expected required source to be set", licenseWithSource("no").isSourceRequired());
85      }
86      
87      public void testLicenseRequiresSourceDefault() throws Exception {
88          final License result = subject.license(new Element("license")
89              .setAttribute("name", "name Value")
90              .setAttribute("url", "urlValue")
91              .setAttribute("id", "idValue"));
92          assertNotNull("Builder should build", result);
93          assertFalse("Expected to default to false", result.isSourceRequired());
94      }
95      
96      /**
97       * @param requireSourceValue
98       * @return
99       */
100     private License licenseWithSource(final String requireSourceValue) {
101         final License result = subject.license(new Element("license")
102             .setAttribute("name", "name Value")
103             .setAttribute("url", "urlValue")
104             .setAttribute("id", "idValue")
105             .setAttribute("requires-source", requireSourceValue));
106         assertNotNull("Builder should build", result);
107         return result;
108     }
109     
110     public void testLicenseNoParameters() throws Exception {
111         final License result = subject.license(new Element("license")
112             .setAttribute("name", "name Value")
113             .setAttribute("url", "urlValue")
114             .setAttribute("id", "idValue")
115             .setContent(new Element("template")));
116         assertNotNull("Builder should build", result);
117         assertNotNull("Builder should always set parameters even when empty", result.getExpectedParameters());
118         assertTrue("No parameters in template so collection should be empty", result.getExpectedParameters().isEmpty());
119     }
120 
121     public void testLicenseOneParameter() throws Exception {
122         final String parameterName = "whatever";
123         final License result = subject.license(new Element("license")
124             .setAttribute("name", "name Value")
125             .setAttribute("url", "urlValue")
126             .setAttribute("id", "idValue")
127             .setContent(new Element("template").setContent(new Element("parameter-name").setContent(new CDATA(parameterName)))));
128         assertNotNull("Builder should build", result);
129         assertNotNull("Builder should always set parameters even when empty", result.getExpectedParameters());
130         assertEquals("One parameters in template", 1, result.getExpectedParameters().size());
131         assertEquals("Parameter name should be set from xml", parameterName, result.getExpectedParameters().iterator().next());
132     }
133 
134     public void testLicenseTwoParameters() throws Exception {
135         final String parameterName = "whatever";
136         final License result = subject.license(new Element("license")
137             .setAttribute("name", "name Value")
138             .setAttribute("url", "urlValue")
139             .setAttribute("id", "idValue")
140             .setContent(new Element("template")
141                 .addContent(new Element("parameter-name").setContent(new CDATA(parameterName)))
142                 .addContent(new Element("parameter-name").setContent(new CDATA(parameterName + "2")))
143                 ));
144         assertNotNull("Builder should build", result);
145         assertNotNull("Builder should always set parameters even when empty", result.getExpectedParameters());
146         assertEquals("One parameters in template", 2, result.getExpectedParameters().size());
147     }
148 
149     
150     public void testLicenseBaseText() throws Exception {
151         final String text = "Some text";
152         final License result = subject.license(new Element("license")
153             .setAttribute("name", "name Value")
154             .setAttribute("url", "urlValue")
155             .setAttribute("id", "idValue")
156             .setContent(new Element("text").setContent(new CDATA(text))));
157         assertNotNull("Builder should build", result);
158         assertEquals("Expected base text to be set", text, result.getText());
159     }
160 
161 }