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.apache.commons.configuration2.ConfigurationAssert.checkEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import org.junit.jupiter.api.BeforeAll;
26  import org.junit.jupiter.api.Test;
27  
28  /**
29   * Test class for {@code QueryResult}.
30   */
31  public class TestQueryResult {
32      /** Constant for an attribute name. */
33      private static final String ATTR = "testAttribute";
34  
35      /** Constant for an attribute value. */
36      private static final Object VALUE = "Value of my attribute";
37  
38      /** A test result node. */
39      private static ImmutableNode resultNode;
40  
41      /** A test parent node for an attribute. */
42      private static ImmutableNode attributeNode;
43  
44      @BeforeAll
45      public static void setUpBeforeClass() {
46          resultNode = new ImmutableNode.Builder().name("resultNode").value(42).create();
47          attributeNode = new ImmutableNode.Builder().name("attributeNode").addAttribute(ATTR, VALUE).create();
48      }
49  
50      /**
51       * Tests equals() if the expected result is false.
52       */
53      @Test
54      void testEqualsFalse() {
55          final QueryResult<ImmutableNode> nodeRes = QueryResult.createNodeResult(resultNode);
56          final QueryResult<ImmutableNode> attrRes = QueryResult.createAttributeResult(attributeNode, ATTR);
57          checkEquals(nodeRes, attrRes, false);
58          QueryResult<ImmutableNode> res = QueryResult.createNodeResult(attributeNode);
59          checkEquals(nodeRes, res, false);
60          res = QueryResult.createAttributeResult(attributeNode, "otherAttr");
61          checkEquals(attrRes, res, false);
62          res = QueryResult.createAttributeResult(resultNode, ATTR);
63          checkEquals(attrRes, res, false);
64      }
65  
66      /**
67       * Tests equals() with other objects.
68       */
69      @Test
70      void testEqualsOtherObjects() {
71          final QueryResult<ImmutableNode> result = QueryResult.createNodeResult(resultNode);
72          checkEquals(result, null, false);
73          checkEquals(result, this, false);
74      }
75  
76      /**
77       * Tests equals() if the expected result is true.
78       */
79      @Test
80      void testEqualsTrue() {
81          QueryResult<ImmutableNode> r1 = QueryResult.createNodeResult(resultNode);
82          checkEquals(r1, r1, true);
83          QueryResult<ImmutableNode> r2 = QueryResult.createNodeResult(resultNode);
84          checkEquals(r1, r2, true);
85          r1 = QueryResult.createAttributeResult(attributeNode, ATTR);
86          r2 = QueryResult.createAttributeResult(attributeNode, ATTR);
87          checkEquals(r1, r2, true);
88      }
89  
90      /**
91       * Tests whether the attribute's value can be queried.
92       */
93      @Test
94      void testGetAttributeValue() {
95          final QueryResult<ImmutableNode> result = QueryResult.createAttributeResult(attributeNode, ATTR);
96          assertEquals(VALUE, result.getAttributeValue(new InMemoryNodeModel().getNodeHandler()));
97      }
98  
99      /**
100      * Tries to query an attribute value for a non-attribute result.
101      */
102     @Test
103     void testGetAttributeValueNoAttributeResult() {
104         final QueryResult<ImmutableNode> result = QueryResult.createNodeResult(resultNode);
105         final NodeHandler<ImmutableNode> nodeHandler = new InMemoryNodeModel().getNodeHandler();
106         assertThrows(IllegalStateException.class, () -> result.getAttributeValue(nodeHandler));
107     }
108 
109     /**
110      * Tests is attributeResult() if the expected result is false.
111      */
112     @Test
113     void testIsAttributeResultFalse() {
114         final QueryResult<ImmutableNode> result = QueryResult.createNodeResult(resultNode);
115         assertFalse(result.isAttributeResult());
116     }
117 
118     /**
119      * Tests isAttributeResult() if the expected result is true.
120      */
121     @Test
122     void testIsAttributeResultTrue() {
123         final QueryResult<ImmutableNode> result = QueryResult.createAttributeResult(attributeNode, ATTR);
124         assertTrue(result.isAttributeResult());
125     }
126 
127     /**
128      * Tests the string representation of an attribute result.
129      */
130     @Test
131     void testToStringAttributeResult() {
132         final QueryResult<ImmutableNode> result = QueryResult.createAttributeResult(attributeNode, ATTR);
133         final String s = result.toString();
134         assertTrue(s.contains("attribute=" + ATTR));
135         assertTrue(s.contains("parentNode=" + attributeNode));
136     }
137 
138     /**
139      * Tests the string representation of a node result.
140      */
141     @Test
142     void testToStringNodeResult() {
143         final QueryResult<ImmutableNode> result = QueryResult.createNodeResult(resultNode);
144         assertTrue(result.toString().contains("resultNode=" + resultNode));
145     }
146 }