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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.http.conf;
19  
20  import java.io.StringReader;
21  import java.io.StringWriter;
22  import java.util.Map;
23  
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.testclassification.SmallTests;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  import org.mortbay.util.ajax.JSON;
34  import org.w3c.dom.Document;
35  import org.w3c.dom.Element;
36  import org.w3c.dom.Node;
37  import org.w3c.dom.NodeList;
38  import org.xml.sax.InputSource;
39  
40  /**
41   * Basic test case that the ConfServlet can write configuration
42   * to its output in XML and JSON format.
43   */
44  @Category(SmallTests.class)
45  public class TestConfServlet extends TestCase {
46    private static final String TEST_KEY = "testconfservlet.key";
47    private static final String TEST_VAL = "testval";
48  
49    private Configuration getTestConf() {
50      Configuration testConf = new Configuration();
51      testConf.set(TEST_KEY, TEST_VAL);
52      return testConf;
53    }
54  
55    @Test
56    @SuppressWarnings("unchecked")
57    public void testWriteJson() throws Exception {
58      StringWriter sw = new StringWriter();
59      ConfServlet.writeResponse(getTestConf(), sw, "json");
60      String json = sw.toString();
61      boolean foundSetting = false;
62      Object parsed = JSON.parse(json);
63      Object[] properties = ((Map<String, Object[]>)parsed).get("properties");
64      for (Object o : properties) {
65        Map<String, Object> propertyInfo = (Map<String, Object>)o;
66        String key = (String)propertyInfo.get("key");
67        String val = (String)propertyInfo.get("value");
68        String resource = (String)propertyInfo.get("resource");
69        System.err.println("k: " + key + " v: " + val + " r: " + resource);
70        if (TEST_KEY.equals(key) && TEST_VAL.equals(val)
71            && "programatically".equals(resource)) {
72          foundSetting = true;
73        }
74      }
75      assertTrue(foundSetting);
76    }
77  
78    @Test
79    public void testWriteXml() throws Exception {
80      StringWriter sw = new StringWriter();
81      ConfServlet.writeResponse(getTestConf(), sw, "xml");
82      String xml = sw.toString();
83  
84      DocumentBuilderFactory docBuilderFactory 
85        = DocumentBuilderFactory.newInstance();
86      DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
87      Document doc = builder.parse(new InputSource(new StringReader(xml)));
88      NodeList nameNodes = doc.getElementsByTagName("name");
89      boolean foundSetting = false;
90      for (int i = 0; i < nameNodes.getLength(); i++) {
91        Node nameNode = nameNodes.item(i);
92        String key = nameNode.getTextContent();
93        System.err.println("xml key: " + key);
94        if (TEST_KEY.equals(key)) {
95          foundSetting = true;
96          Element propertyElem = (Element)nameNode.getParentNode();
97          String val = propertyElem.getElementsByTagName("value").item(0).getTextContent();
98          assertEquals(TEST_VAL, val);
99        }
100     }
101     assertTrue(foundSetting);
102   }
103 
104   @Test
105   public void testBadFormat() throws Exception {
106     StringWriter sw = new StringWriter();
107     try {
108       ConfServlet.writeResponse(getTestConf(), sw, "not a format");
109       fail("writeResponse with bad format didn't throw!");
110     } catch (ConfServlet.BadFormatException bfe) {
111       // expected
112     }
113     assertEquals("", sw.toString());
114   }
115 }