Debian Codegraph @

build-system/manifest-merger/src/test/java/com/android/manifmerger/XmlNodeTest.java

raw
1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.manifmerger;
18
19 import static org.mockito.Mockito.when;
20
21 import com.android.SdkConstants;
22
23 import junit.framework.TestCase;
24
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.MockitoAnnotations;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30
31 /**
32 * Tests for the {@link XmlNode} class
33 */
34 public class XmlNodeTest extends TestCase {
35
36 @Mock Node mNodeOne;
37 @Mock Node mNodeTwo;
38 @Mock Element mElement;
39
40 @Override
41 protected void setUp() throws Exception {
42 super.setUp();
43 MockitoAnnotations.initMocks(this);
44 }
45
46 public void testNoNamespace() {
47 when(mNodeOne.getNodeName()).thenReturn("my-name");
48
49 XmlNode.NodeName nodeName = XmlNode.unwrapName(mNodeOne);
50 assertFalse(nodeName.isInNamespace(SdkConstants.ANDROID_URI));
51 assertEquals("my-name", nodeName.toString());
52 }
53
54 public void testNoNamespaceEqualityAndHashCoding() {
55 when(mNodeOne.getNodeName()).thenReturn("my-name");
56 when(mNodeTwo.getNodeName()).thenReturn("my-name");
57
58 assertEquals(XmlNode.unwrapName(mNodeOne), XmlNode.unwrapName(mNodeTwo));
59 assertEquals(XmlNode.unwrapName(mNodeOne).hashCode(),
60 XmlNode.unwrapName(mNodeTwo).hashCode());
61 }
62
63 public void testNoNamespaceInequalityAndHashCoding() {
64 when(mNodeOne.getNodeName()).thenReturn("my-name");
65 when(mNodeTwo.getNodeName()).thenReturn("my-other-name");
66
67 assertNotSame(XmlNode.unwrapName(mNodeOne), XmlNode.unwrapName(mNodeTwo));
68 assertNotSame(XmlNode.unwrapName(mNodeOne).hashCode(),
69 XmlNode.unwrapName(mNodeTwo).hashCode());
70 }
71
72 public void testNamespace() {
73 when(mNodeOne.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
74 when(mNodeOne.getLocalName()).thenReturn("my-name");
75 when(mNodeOne.getPrefix()).thenReturn("android");
76
77 XmlNode.NodeName nodeName = XmlNode.unwrapName(mNodeOne);
78 assertTrue(nodeName.isInNamespace(SdkConstants.ANDROID_URI));
79 assertEquals("android:my-name", nodeName.toString());
80 }
81
82 public void testNamespaceEqualityAndHashCoding() {
83 // different local name.
84 when(mNodeOne.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
85 when(mNodeOne.getLocalName()).thenReturn("my-name");
86 when(mNodeOne.getPrefix()).thenReturn("android");
87
88 when(mNodeTwo.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
89 when(mNodeTwo.getLocalName()).thenReturn("my-name");
90 when(mNodeTwo.getPrefix()).thenReturn("android");
91
92 assertEquals(XmlNode.unwrapName(mNodeOne), XmlNode.unwrapName(mNodeTwo));
93 assertEquals(XmlNode.unwrapName(mNodeOne).hashCode(),
94 XmlNode.unwrapName(mNodeTwo).hashCode());
95
96 // different namespace.
97 when(mNodeOne.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
98 when(mNodeOne.getLocalName()).thenReturn("my-name");
99 when(mNodeOne.getPrefix()).thenReturn("android");
100
101 when(mNodeTwo.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
102 when(mNodeTwo.getLocalName()).thenReturn("my-name");
103 // another prefix should not matter, they are still the same xml names.
104 when(mNodeTwo.getPrefix()).thenReturn("y");
105
106 assertEquals(XmlNode.unwrapName(mNodeOne), XmlNode.unwrapName(mNodeTwo));
107 assertEquals(XmlNode.unwrapName(mNodeOne).hashCode(),
108 XmlNode.unwrapName(mNodeTwo).hashCode());
109 }
110
111 public void testNamespaceInequality() {
112 when(mNodeOne.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
113 when(mNodeOne.getLocalName()).thenReturn("my-name");
114 when(mNodeOne.getPrefix()).thenReturn("android");
115
116 when(mNodeTwo.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
117 when(mNodeTwo.getLocalName()).thenReturn("my-other-name");
118 when(mNodeTwo.getPrefix()).thenReturn("android");
119
120 assertNotSame(XmlNode.unwrapName(mNodeOne), XmlNode.unwrapName(mNodeTwo));
121 assertNotSame(XmlNode.unwrapName(mNodeOne).hashCode(),
122 XmlNode.unwrapName(mNodeTwo).hashCode());
123
124
125 when(mNodeOne.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
126 when(mNodeOne.getLocalName()).thenReturn("my-name");
127 when(mNodeOne.getPrefix()).thenReturn("android");
128
129 when(mNodeTwo.getNamespaceURI()).thenReturn(SdkConstants.TOOLS_URI);
130 when(mNodeTwo.getLocalName()).thenReturn("my-name");
131 when(mNodeTwo.getPrefix()).thenReturn("android");
132
133 assertNotSame(XmlNode.unwrapName(mNodeOne), XmlNode.unwrapName(mNodeTwo));
134 assertNotSame(XmlNode.unwrapName(mNodeOne).hashCode(),
135 XmlNode.unwrapName(mNodeTwo).hashCode());
136 }
137
138 public void testAddAttributeToNode() {
139 when(mNodeOne.getNodeName()).thenReturn("my-name");
140
141 XmlNode.NodeName nodeName = XmlNode.unwrapName(mNodeOne);
142 nodeName.addToNode(mElement, "my-value");
143 Mockito.verify(mElement).setAttribute("my-name", "my-value");
144 Mockito.verifyNoMoreInteractions(mElement);
145 }
146
147 public void testAddNamespaceAwareAttributeToNode() {
148 when(mNodeOne.getNamespaceURI()).thenReturn(SdkConstants.ANDROID_URI);
149 when(mNodeOne.getLocalName()).thenReturn("my-name");
150 when(mNodeOne.getPrefix()).thenReturn("android");
151
152 XmlNode.NodeName nodeName = XmlNode.unwrapName(mNodeOne);
153 nodeName.addToNode(mElement, "my-value");
154 Mockito.verify(mElement).setAttributeNS(
155 SdkConstants.ANDROID_URI, "android:my-name", "my-value");
156 Mockito.verifyNoMoreInteractions(mElement);
157 }
158 }