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  
18  package org.apache.commons.jxpath.ri.model;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import org.apache.commons.jxpath.AbstractJXPathTest;
23  import org.apache.commons.jxpath.JXPathContext;
24  import org.apache.commons.jxpath.xml.DocumentContainer;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Test for text trimming from JXPATH-83.
29   */
30  public class XMLSpaceTest extends AbstractJXPathTest {
31  
32      protected JXPathContext context;
33  
34      protected JXPathContext createContext(final String model) {
35          final JXPathContext context = JXPathContext.newContext(createDocumentContainer(model));
36          return context;
37      }
38  
39      protected DocumentContainer createDocumentContainer(final String model) {
40          return new DocumentContainer(AbstractJXPathTest.class.getResource("XmlSpace.xml"), model);
41      }
42  
43      protected void doTest(final String id, final String model, final String expectedValue) {
44          final JXPathContext context = JXPathContext.newContext(createDocumentContainer(model));
45          assertEquals(expectedValue, context.getValue("test/text[@id='" + id + "']"));
46      }
47  
48      @Test
49      public void testDefaultDOM() {
50          doTest("default", DocumentContainer.MODEL_DOM, "foo");
51      }
52  
53      @Test
54      public void testDefaultJDOM() {
55          doTest("default", DocumentContainer.MODEL_JDOM, "foo");
56      }
57  
58      @Test
59      public void testNestedDOM() {
60          doTest("nested", DocumentContainer.MODEL_DOM, "foo;bar; baz ");
61      }
62  
63      @Test
64      public void testNestedJDOM() {
65          doTest("nested", DocumentContainer.MODEL_JDOM, "foo;bar; baz ");
66      }
67  
68      @Test
69      public void testNestedWithCommentsDOM() {
70          doTest("nested-with-comments", DocumentContainer.MODEL_DOM, "foo;bar; baz ");
71      }
72  
73      @Test
74      public void testNestedWithCommentsJDOM() {
75          doTest("nested-with-comments", DocumentContainer.MODEL_JDOM, "foo;bar; baz ");
76      }
77  
78      @Test
79      public void testPreserveDOM() {
80          doTest("preserve", DocumentContainer.MODEL_DOM, " foo ");
81      }
82  
83      @Test
84      public void testPreserveJDOM() {
85          doTest("preserve", DocumentContainer.MODEL_JDOM, " foo ");
86      }
87  
88      @Test
89      public void testUnspecifiedDOM() {
90          doTest("unspecified", DocumentContainer.MODEL_DOM, "foo");
91      }
92  
93      @Test
94      public void testUnspecifiedJDOM() {
95          doTest("unspecified", DocumentContainer.MODEL_JDOM, "foo");
96      }
97  }