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 
19 package org.apache.hadoop.yarn.webapp;
20 
21 import static org.junit.Assert.assertTrue;
22 
23 import org.w3c.dom.Attr;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.Node;
26 import org.w3c.dom.NodeList;
27 
28 public class WebServicesTestUtils {
29 
getXmlLong(Element element, String name)30   public static long getXmlLong(Element element, String name) {
31     String val = getXmlString(element, name);
32     return Long.parseLong(val);
33   }
34 
getXmlInt(Element element, String name)35   public static int getXmlInt(Element element, String name) {
36     String val = getXmlString(element, name);
37     return Integer.parseInt(val);
38   }
39 
getXmlBoolean(Element element, String name)40   public static Boolean getXmlBoolean(Element element, String name) {
41     String val = getXmlString(element, name);
42     return Boolean.parseBoolean(val);
43   }
44 
getXmlFloat(Element element, String name)45   public static float getXmlFloat(Element element, String name) {
46     String val = getXmlString(element, name);
47     return Float.parseFloat(val);
48   }
49 
getXmlString(Element element, String name)50   public static String getXmlString(Element element, String name) {
51     NodeList id = element.getElementsByTagName(name);
52     Element line = (Element) id.item(0);
53     if (line == null) {
54       return null;
55     }
56     Node first = line.getFirstChild();
57     // handle empty <key></key>
58     if (first == null) {
59       return "";
60     }
61     String val = first.getNodeValue();
62     if (val == null) {
63       return "";
64     }
65     return val;
66   }
67 
getXmlAttrString(Element element, String name)68   public static String getXmlAttrString(Element element, String name) {
69     Attr at = element.getAttributeNode(name);
70     if (at != null) {
71       return at.getValue();
72     }
73     return null;
74   }
75 
checkStringMatch(String print, String expected, String got)76   public static void checkStringMatch(String print, String expected, String got) {
77     assertTrue(
78         print + " doesn't match, got: " + got + " expected: " + expected,
79         got.matches(expected));
80   }
81 
checkStringContains(String print, String expected, String got)82   public static void checkStringContains(String print, String expected, String got) {
83     assertTrue(
84         print + " doesn't contain expected string, got: " + got + " expected: " + expected,
85         got.contains(expected));
86   }
87 
checkStringEqual(String print, String expected, String got)88   public static void checkStringEqual(String print, String expected, String got) {
89     assertTrue(
90         print + " is not equal, got: " + got + " expected: " + expected,
91         got.equals(expected));
92   }
93 
94 }
95