View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration2.builder;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertSame;
21  import static org.mockito.Mockito.mock;
22  
23  import java.util.Map;
24  
25  import javax.xml.parsers.DocumentBuilder;
26  
27  import org.apache.commons.configuration2.beanutils.BeanHelper;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  import org.xml.sax.EntityResolver;
31  
32  /**
33   * Test class for {@code XMLBuilderParametersImpl}.
34   */
35  public class TestXMLBuilderParametersImpl {
36      /** The parameters object to be tested. */
37      private XMLBuilderParametersImpl params;
38  
39      @BeforeEach
40      public void setUp() throws Exception {
41          params = new XMLBuilderParametersImpl();
42      }
43  
44      /**
45       * Tests whether properties can be set through BeanUtils.
46       */
47      @Test
48      public void testBeanPropertiesAccess() throws Exception {
49          final EntityResolver resolver = mock(EntityResolver.class);
50          final DocumentBuilder builder = mock(DocumentBuilder.class);
51          BeanHelper.setProperty(params, "throwExceptionOnMissing", Boolean.TRUE);
52          BeanHelper.setProperty(params, "fileName", "test.xml");
53          BeanHelper.setProperty(params, "entityResolver", resolver);
54          BeanHelper.setProperty(params, "documentBuilder", builder);
55          assertEquals("test.xml", params.getFileHandler().getFileName());
56          final Map<String, Object> paramsMap = params.getParameters();
57          assertEquals(Boolean.TRUE, paramsMap.get("throwExceptionOnMissing"));
58          assertSame(resolver, paramsMap.get("entityResolver"));
59          assertSame(builder, paramsMap.get("documentBuilder"));
60      }
61  
62      /**
63       * Tests whether properties can be inherited.
64       */
65      @Test
66      public void testInheritFrom() {
67          final EntityResolver resolver = mock(EntityResolver.class);
68          final DocumentBuilder builder = mock(DocumentBuilder.class);
69          params.setDocumentBuilder(builder).setEntityResolver(resolver).setSchemaValidation(true).setValidating(true);
70          params.setThrowExceptionOnMissing(true);
71          final XMLBuilderParametersImpl params2 = new XMLBuilderParametersImpl();
72  
73          params2.inheritFrom(params.getParameters());
74          final Map<String, Object> parameters = params2.getParameters();
75          assertEquals(Boolean.TRUE, parameters.get("throwExceptionOnMissing"));
76          assertEquals(resolver, parameters.get("entityResolver"));
77          assertEquals(builder, parameters.get("documentBuilder"));
78          assertEquals(Boolean.TRUE, parameters.get("validating"));
79          assertEquals(Boolean.TRUE, parameters.get("schemaValidation"));
80      }
81  
82      /**
83       * Tests whether a document builder can be set.
84       */
85      @Test
86      public void testSetDocumentBuilder() {
87          final DocumentBuilder builder = mock(DocumentBuilder.class);
88          assertSame(params, params.setDocumentBuilder(builder));
89          assertSame(builder, params.getParameters().get("documentBuilder"));
90      }
91  
92      /**
93       * Tests whether an entity resolver can be set.
94       */
95      @Test
96      public void testSetEntityResolver() {
97          final EntityResolver resolver = mock(EntityResolver.class);
98          assertSame(params, params.setEntityResolver(resolver));
99          assertSame(resolver, params.getEntityResolver());
100         assertSame(resolver, params.getParameters().get("entityResolver"));
101     }
102 
103     /**
104      * Tests whether a public ID can be set.
105      */
106     @Test
107     public void testSetPublicID() {
108         final String pubID = "testPublicID";
109         assertSame(params, params.setPublicID(pubID));
110         assertEquals(pubID, params.getParameters().get("publicID"));
111     }
112 
113     /**
114      * Tests whether the schema validation flag can be set.
115      */
116     @Test
117     public void testSetSchemaValidation() {
118         assertSame(params, params.setSchemaValidation(false));
119         assertEquals(Boolean.FALSE, params.getParameters().get("schemaValidation"));
120     }
121 
122     /**
123      * Tests whether a system ID can be set.
124      */
125     @Test
126     public void testSetSystemID() {
127         final String sysID = "testSystemID";
128         assertSame(params, params.setSystemID(sysID));
129         assertEquals(sysID, params.getParameters().get("systemID"));
130     }
131 
132     /**
133      * Tests whether validating property can be set.
134      */
135     @Test
136     public void testSetValidating() {
137         assertSame(params, params.setValidating(true));
138         assertEquals(Boolean.TRUE, params.getParameters().get("validating"));
139     }
140 }