View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.context;
21  
22  import org.apache.myfaces.tobago.internal.config.AbstractTobagoTestBase;
23  import org.apache.myfaces.tobago.internal.webapp.XmlResponseWriter;
24  import org.junit.Assert;
25  import org.junit.Test;
26  
27  import javax.faces.context.FacesContext;
28  import javax.swing.tree.DefaultMutableTreeNode;
29  import javax.swing.tree.TreeNode;
30  import java.io.IOException;
31  import java.io.StringWriter;
32  
33  public class ResponseWriterDividerUnitTest extends AbstractTobagoTestBase {
34  
35    @Test
36    public void test() throws IOException {
37  
38      final StringWriter stringWriter = new StringWriter();
39      getFacesContext().setResponseWriter(new XmlResponseWriter(stringWriter, "text/xml", "ISO-8859-1"));
40  
41      final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
42      final DefaultMutableTreeNode colors = new DefaultMutableTreeNode("Colors");
43      final DefaultMutableTreeNode numbers = new DefaultMutableTreeNode("Numbers");
44      final DefaultMutableTreeNode integers = new DefaultMutableTreeNode("Integers");
45      final DefaultMutableTreeNode doubles = new DefaultMutableTreeNode("Doubles");
46      root.add(colors);
47      root.add(numbers);
48      numbers.add(integers);
49      numbers.add(doubles);
50      integers.add(new DefaultMutableTreeNode("1"));
51      integers.add(new DefaultMutableTreeNode("2"));
52      doubles.add(new DefaultMutableTreeNode("2.7182"));
53      doubles.add(new DefaultMutableTreeNode("3.1415"));
54  
55      render(getFacesContext(), root);
56  
57      final ResponseWriterDivider divider = ResponseWriterDivider.getInstance(getFacesContext(), "unit test");
58      divider.writeOutAndCleanUp(getFacesContext());
59  
60      final String expected
61          = "(Root)\n"
62          + "Colors\n"
63          + "Numbers\n"
64          + "(/Root)\n"
65          + "(Numbers)\n"
66          + "Integers\n"
67          + "Doubles\n"
68          + "(/Numbers)\n"
69          + "(Integers)\n"
70          + "1\n"
71          + "2\n"
72          + "(/Integers)\n"
73          + "(Doubles)\n"
74          + "2.7182\n"
75          + "3.1415\n"
76          + "(/Doubles)\n";
77      
78      Assert.assertEquals(expected, stringWriter.toString());
79    }
80  
81    private void render(final FacesContext facesContext, final DefaultMutableTreeNode node) throws IOException {
82      final ResponseWriterDivider divider = ResponseWriterDivider.getInstance(facesContext, "unit test");
83  
84      final String label = (String) node.getUserObject();
85  
86      // label
87      if (!node.isRoot()) {
88        facesContext.getResponseWriter().write(label + "\n");
89      }
90  
91      // tag
92      if (node.getChildCount() > 0) {
93        divider.activateBranch(facesContext);
94        facesContext.getResponseWriter().write("(" + label + ")\n");
95        for (int i = 0; i < node.getChildCount(); i++) {
96          final TreeNode sub = node.getChildAt(i);
97          render(facesContext, (DefaultMutableTreeNode) sub);
98        }
99        facesContext.getResponseWriter().write("(/" + label + ")\n");
100       divider.passivateBranch(facesContext);
101     }
102   }
103 }