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.naming.Context;
26  
27  import org.apache.commons.configuration2.beanutils.BeanHelper;
28  import org.junit.jupiter.api.BeforeEach;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test class for {@code JndiBuilderParametersImpl}.
33   */
34  public class TestJndiBuilderParametersImpl {
35  
36      /** The parameters object to be tested. */
37      private JndiBuilderParametersImpl params;
38  
39      @BeforeEach
40      public void setUp() throws Exception {
41          params = new JndiBuilderParametersImpl();
42      }
43  
44      /**
45       * Tests whether the parameters map contains inherited properties, too.
46       */
47      @Test
48      void testGetParametersBaseProperties() {
49          params.setPrefix("somePrefix");
50          params.setThrowExceptionOnMissing(true);
51          final Map<String, Object> paramsMap = params.getParameters();
52          assertEquals(Boolean.TRUE, paramsMap.get("throwExceptionOnMissing"));
53      }
54  
55      /**
56       * Tests whether properties can be set through BeanUtils.
57       */
58      @Test
59      void testSetBeanProperties() throws Exception {
60          final Context ctx = mock(Context.class);
61          final String prefix = "testJndiPrefix";
62          BeanHelper.setProperty(params, "context", ctx);
63          BeanHelper.setProperty(params, "prefix", prefix);
64          final Map<String, Object> paramsMap = params.getParameters();
65          assertSame(ctx, paramsMap.get("context"));
66          assertEquals(prefix, paramsMap.get("prefix"));
67      }
68  
69      /**
70       * Tests whether a JNDI context can be set.
71       */
72      @Test
73      void testSetContext() {
74          final Context ctx = mock(Context.class);
75          assertSame(params, params.setContext(ctx));
76          final Map<String, Object> paramsMap = params.getParameters();
77          assertSame(ctx, paramsMap.get("context"));
78      }
79  
80      /**
81       * Tests whether a prefix can be set.
82       */
83      @Test
84      void testSetPrefix() {
85          final String prefix = "testJndiPrefix";
86          assertSame(params, params.setPrefix(prefix));
87          final Map<String, Object> paramsMap = params.getParameters();
88          assertEquals(prefix, paramsMap.get("prefix"));
89      }
90  }