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.tree;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.HashMap;
26  import java.util.LinkedList;
27  import java.util.Map;
28  
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Test class for {@code NodeUpdateData}.
33   */
34  public class TestNodeUpdateData {
35  
36      /**
37       * Convenience method for creating a query result object.
38       *
39       * @param value the value of this result
40       * @return the result object
41       */
42      private static QueryResult<Object> result(final Object value) {
43          return QueryResult.createNodeResult(value);
44      }
45  
46      /**
47       * Tests that the map with changed values cannot be modified.
48       */
49      @Test
50      void testGetChangedValuesModify() {
51          final Map<QueryResult<Object>, Object> map = new HashMap<>();
52          map.put(result("n1"), 42);
53          final NodeUpdateData<Object> data = new NodeUpdateData<>(map, null, null, null);
54          final Map<QueryResult<Object>, Object> changedValues = data.getChangedValues();
55          final QueryResult<Object> result = result("n2");
56          assertThrows(UnsupportedOperationException.class, () -> changedValues.put(result, 43));
57      }
58  
59      /**
60       * Tests that the collection with new values cannot be modified.
61       */
62      @Test
63      void testGetNewValuesModify() {
64          final Collection<Object> col = new LinkedList<>();
65          col.add(42);
66          final NodeUpdateData<Object> data = new NodeUpdateData<>(null, col, null, null);
67          final Collection<Object> newValues = data.getNewValues();
68          assertThrows(UnsupportedOperationException.class, () -> newValues.add(43));
69      }
70  
71      /**
72       * Tests that the collection with removed nodes cannot be modified.
73       */
74      @Test
75      void testGetRemovedNodesModify() {
76          final Collection<QueryResult<Object>> col = new LinkedList<>();
77          col.add(result("n1"));
78          final NodeUpdateData<Object> data = new NodeUpdateData<>(null, null, col, null);
79          final Collection<QueryResult<Object>> removedNodes = data.getRemovedNodes();
80          final QueryResult<Object> result = result("newNode");
81          assertThrows(UnsupportedOperationException.class, () -> removedNodes.add(result));
82      }
83  
84      /**
85       * Tests whether a defensive copy is created from the changed values.
86       */
87      @Test
88      void testInitChangedValuesDefensiveCopy() {
89          final Map<QueryResult<Object>, Object> map = new HashMap<>();
90          map.put(result("test"), "value");
91          final NodeUpdateData<Object> data = new NodeUpdateData<>(map, null, null, null);
92          map.put(result("anotherTest"), "anotherValue");
93          final Map<QueryResult<Object>, Object> changedValues = data.getChangedValues();
94          assertEquals(Collections.singletonMap(result("test"), "value"), changedValues);
95      }
96  
97      /**
98       * Tests whether a defensive copy is created from the new values.
99       */
100     @Test
101     void testInitNewValuesDefensiveCopy() {
102         final Collection<Object> col = new LinkedList<>();
103         col.add(42);
104         final NodeUpdateData<Object> data = new NodeUpdateData<>(null, col, null, null);
105         col.add("anotherValue");
106         final Collection<Object> newValues = data.getNewValues();
107         assertEquals(1, newValues.size());
108         assertEquals(42, newValues.iterator().next());
109     }
110 
111     /**
112      * Tests whether null parameters for collections are converted to empty collections.
113      */
114     @Test
115     void testInitNoData() {
116         final NodeUpdateData<Object> data = new NodeUpdateData<>(null, null, null, null);
117         assertTrue(data.getChangedValues().isEmpty());
118         assertTrue(data.getNewValues().isEmpty());
119         assertTrue(data.getRemovedNodes().isEmpty());
120     }
121 
122     /**
123      * Tests whether a defensive copy is created from the removed nodes.
124      */
125     @Test
126     void testInitRemovedNodesDefensiveCopy() {
127         final Collection<QueryResult<Object>> col = new LinkedList<>();
128         col.add(result("n1"));
129         final NodeUpdateData<Object> data = new NodeUpdateData<>(null, null, col, null);
130         col.add(result("n2"));
131         final Collection<QueryResult<Object>> removedNodes = data.getRemovedNodes();
132         assertEquals(1, removedNodes.size());
133         assertEquals(result("n1"), removedNodes.iterator().next());
134     }
135 }