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    *     https://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  
37      /** The parameters object to be tested. */
38      private XMLBuilderParametersImpl params;
39  
40      @BeforeEach
41      public void setUp() throws Exception {
42          params = new XMLBuilderParametersImpl();
43      }
44  
45      /**
46       * Tests whether properties can be set through BeanUtils.
47       */
48      @Test
49      void testBeanPropertiesAccess() throws Exception {
50          final EntityResolver resolver = mock(EntityResolver.class);
51          final DocumentBuilder builder = mock(DocumentBuilder.class);
52          BeanHelper.setProperty(params, "throwExceptionOnMissing", Boolean.TRUE);
53          BeanHelper.setProperty(params, "fileName", "test.xml");
54          BeanHelper.setProperty(params, "entityResolver", resolver);
55          BeanHelper.setProperty(params, "documentBuilder", builder);
56          assertEquals("test.xml", params.getFileHandler().getFileName());
57          final Map<String, Object> paramsMap = params.getParameters();
58          assertEquals(Boolean.TRUE, paramsMap.get("throwExceptionOnMissing"));
59          assertSame(resolver, paramsMap.get("entityResolver"));
60          assertSame(builder, paramsMap.get("documentBuilder"));
61      }
62  
63      /**
64       * Tests whether properties can be inherited.
65       */
66      @Test
67      void testInheritFrom() {
68          final EntityResolver resolver = mock(EntityResolver.class);
69          final DocumentBuilder builder = mock(DocumentBuilder.class);
70          params.setDocumentBuilder(builder).setEntityResolver(resolver).setSchemaValidation(true).setValidating(true);
71          params.setThrowExceptionOnMissing(true);
72          final XMLBuilderParametersImpl params2 = new XMLBuilderParametersImpl();
73  
74          params2.inheritFrom(params.getParameters());
75          final Map<String, Object> parameters = params2.getParameters();
76          assertEquals(Boolean.TRUE, parameters.get("throwExceptionOnMissing"));
77          assertEquals(resolver, parameters.get("entityResolver"));
78          assertEquals(builder, parameters.get("documentBuilder"));
79          assertEquals(Boolean.TRUE, parameters.get("validating"));
80          assertEquals(Boolean.TRUE, parameters.get("schemaValidation"));
81      }
82  
83      /**
84       * Tests whether a document builder can be set.
85       */
86      @Test
87      void testSetDocumentBuilder() {
88          final DocumentBuilder builder = mock(DocumentBuilder.class);
89          assertSame(params, params.setDocumentBuilder(builder));
90          assertSame(builder, params.getParameters().get("documentBuilder"));
91      }
92  
93      /**
94       * Tests whether an entity resolver can be set.
95       */
96      @Test
97      void testSetEntityResolver() {
98          final EntityResolver resolver = mock(EntityResolver.class);
99          assertSame(params, params.setEntityResolver(resolver));
100         assertSame(resolver, params.getEntityResolver());
101         assertSame(resolver, params.getParameters().get("entityResolver"));
102     }
103 
104     /**
105      * Tests whether a public ID can be set.
106      */
107     @Test
108     void testSetPublicID() {
109         final String pubID = "testPublicID";
110         assertSame(params, params.setPublicID(pubID));
111         assertEquals(pubID, params.getParameters().get("publicID"));
112     }
113 
114     /**
115      * Tests whether the schema validation flag can be set.
116      */
117     @Test
118     void testSetSchemaValidation() {
119         assertSame(params, params.setSchemaValidation(false));
120         assertEquals(Boolean.FALSE, params.getParameters().get("schemaValidation"));
121     }
122 
123     /**
124      * Tests whether a system ID can be set.
125      */
126     @Test
127     void testSetSystemID() {
128         final String sysID = "testSystemID";
129         assertSame(params, params.setSystemID(sysID));
130         assertEquals(sysID, params.getParameters().get("systemID"));
131     }
132 
133     /**
134      * Tests whether validating property can be set.
135      */
136     @Test
137     void testSetValidating() {
138         assertSame(params, params.setValidating(true));
139         assertEquals(Boolean.TRUE, params.getParameters().get("validating"));
140     }
141 }