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