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