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  
18  package org.apache.commons.configuration2;
19  
20  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertSame;
23  
24  import org.apache.commons.configuration2.ex.ConfigurationException;
25  import org.apache.commons.configuration2.io.ConfigurationLogger;
26  import org.apache.commons.configuration2.io.FileHandler;
27  import org.apache.commons.configuration2.resolver.CatalogResolver;
28  import org.junit.jupiter.api.AfterEach;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Test class for CatalogResolver.
34   */
35  public class TestCatalogResolver {
36      private static final String CATALOG_FILES = "catalog.xml";
37      private static final String PUBLIC_FILE = "testResolver.xml";
38      private static final String REWRITE_SYSTEM_FILE = "test.properties.xml";
39      private static final String REWRITE_SCHEMA_FILE = "sample.xml";
40  
41      private CatalogResolver resolver;
42      private XMLConfiguration config;
43  
44      /**
45       * Loads the test configuration from the specified file.
46       *
47       * @param fileName the file name
48       * @throws ConfigurationException if an error occurs
49       */
50      private void load(final String fileName) throws ConfigurationException {
51          final FileHandler handler = new FileHandler(config);
52          handler.load(fileName);
53      }
54  
55      @BeforeEach
56      public void setUp() throws Exception {
57          resolver = new CatalogResolver();
58          resolver.setCatalogFiles(CATALOG_FILES);
59          // resolver.setDebug(true);
60          config = new XMLConfiguration();
61          config.setEntityResolver(resolver);
62      }
63  
64      @AfterEach
65      public void tearDown() throws Exception {
66          resolver = null;
67          config = null;
68      }
69  
70      @Test
71      public void testDebug() throws Exception {
72          resolver.setDebug(true);
73          // There is no really good way to check this except to do something
74          // that causes debug output.
75      }
76  
77      @Test
78      public void testLogger() throws Exception {
79          final ConfigurationLogger log = new ConfigurationLogger(this.getClass());
80          resolver.setLogger(log);
81          assertNotNull(resolver.getLogger());
82          assertSame(log, resolver.getLogger());
83      }
84  
85      @Test
86      public void testPublic() {
87          assertDoesNotThrow(() -> load(PUBLIC_FILE));
88      }
89  
90      @Test
91      public void testRewriteSystem() {
92          assertDoesNotThrow(() -> load(REWRITE_SYSTEM_FILE));
93      }
94  
95      /**
96       * Tests that the schema can be resolved and that XMLConfiguration will validate the file using the schema.
97       */
98      @Test
99      public void testSchemaResolver() {
100         assertDoesNotThrow(() -> load(REWRITE_SCHEMA_FILE));
101     }
102 }