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