1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.settings.xml;
17  
18  import java.io.IOException;
19  import java.io.StringReader;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  import javax.xml.parsers.DocumentBuilderFactory;
24  import javax.xml.parsers.FactoryConfigurationError;
25  import javax.xml.parsers.ParserConfigurationException;
26  
27  import org.springframework.richclient.settings.Settings;
28  import org.springframework.richclient.settings.SettingsAbstractTests;
29  import org.w3c.dom.Document;
30  import org.w3c.dom.Element;
31  import org.w3c.dom.Node;
32  import org.w3c.dom.NodeList;
33  import org.xml.sax.InputSource;
34  import org.xml.sax.SAXException;
35  
36  /**
37   * @author Peter De Bruycker
38   */
39  public class XmlSettingsTests extends SettingsAbstractTests {
40  
41  	protected Settings createSettings() throws Exception {
42  		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
43  		Element element = doc.createElement("settings");
44  		element.setAttribute("name", "root");
45  		doc.appendChild(element);
46  
47  		return new XmlSettings(element);
48  	}
49  
50  	public void testConstructor() throws ParserConfigurationException, FactoryConfigurationError, SAXException,
51  			IOException {
52  		StringBuffer sb = new StringBuffer();
53  		sb.append("<?xml version=\"1.0\"?>");
54  		sb.append("<settings name=\"test-settings\">");
55  		sb.append("  <entry key=\"key-1\" value=\"value-1\" />");
56  		sb.append("  <entry key=\"key-2\" value=\"false\" />");
57  		sb.append("  <entry key=\"key-3\" value=\"1.5\" />");
58  		sb.append("  <settings name=\"child-settings\">");
59  		sb.append("    <entry key=\"child-key\" value=\"value\" />");
60  		sb.append("  </settings>");
61  		sb.append("</settings>");
62  
63  		Element element = createElement(sb.toString());
64  
65  		XmlSettings settings = new XmlSettings(null, element);
66  		assertEquals("test-settings", settings.getName());
67  		assertEquals(element, settings.getElement());
68  
69  		List keys = Arrays.asList(settings.getKeys());
70  		assertEquals(3, keys.size());
71  		assertTrue(keys.contains("key-1"));
72  		assertTrue(keys.contains("key-2"));
73  		assertTrue(keys.contains("key-3"));
74  
75  		assertEquals("value-1", settings.getString("key-1"));
76  		assertFalse(settings.getBoolean("key-2"));
77  		assertEquals(1.5f, settings.getFloat("key-3"), 0.0f);
78  
79  		Settings childSettings = settings.getSettings("child-settings");
80  		assertTrue(childSettings instanceof XmlSettings);
81  		assertEquals(1, childSettings.getKeys().length);
82  		assertEquals("child-key", childSettings.getKeys()[0]);
83  	}
84  
85  	public void testRemove_RemovesElement() throws ParserConfigurationException, FactoryConfigurationError,
86  			SAXException, IOException {
87  		StringBuffer sb = new StringBuffer();
88  		sb.append("<?xml version=\"1.0\"?>");
89  		sb.append("<settings name=\"test-settings\">");
90  		sb.append("  <entry key=\"key-1\" value=\"value-1\" />");
91  		sb.append("  <entry key=\"key-2\" value=\"false\" />");
92  		sb.append("  <entry key=\"key-3\" value=\"1.5\" />");
93  		sb.append("</settings>");
94  
95  		Element element = createElement(sb.toString());
96  
97  		XmlSettings settings = new XmlSettings(null, element);
98  
99  		assertTrue(settings.contains("key-2"));
100 
101 		settings.remove("key-2");
102 
103 		Element settingsElement = settings.getElement();
104 		NodeList childNodes = settingsElement.getChildNodes();
105 		for (int i = 0; i < childNodes.getLength(); i++) {
106 			Node node = childNodes.item(i);
107 			if (node instanceof Element && node.getNodeName().equals("entry")) {
108 				Element tmp = (Element) node;
109 				assertFalse(tmp.getAttribute("key").equals("key-2"));
110 			}
111 		}
112 	}
113 
114 	public void testSetValue() throws ParserConfigurationException, FactoryConfigurationError {
115 		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
116 		Element element = doc.createElement("settings");
117 		element.setAttribute("name", "test-settings");
118 		doc.appendChild(element);
119 		Element entry = doc.createElement("entry");
120 		entry.setAttribute("key", "_key");
121 		entry.setAttribute("value", "_value");
122 		element.appendChild(entry);
123 
124 		XmlSettings settings = new XmlSettings(null, element);
125 
126 		settings.setString("_key", "new value");
127 		assertEquals("new value", entry.getAttribute("value"));
128 	}
129 
130 	public void testSave() throws ParserConfigurationException, FactoryConfigurationError, IOException {
131 		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
132 		Element parentElement = doc.createElement("settings");
133 		parentElement.setAttribute("name", "parent-settings");
134 		doc.appendChild(parentElement);
135 		Element childElement = doc.createElement("settings");
136 		childElement.setAttribute("name", "child-settings");
137 		parentElement.appendChild(childElement);
138 
139 		TestableXmlSettingsReaderWriter readerWriter = new TestableXmlSettingsReaderWriter();
140 		RootXmlSettings parentSettings = new RootXmlSettings(doc, readerWriter);
141 		Settings childSettings = parentSettings.getSettings("child-settings");
142 		childSettings.save();
143 
144 		assertEquals(parentSettings, readerWriter.lastWritten);
145 	}
146     
147     public void testChildSettings() throws ParserConfigurationException, FactoryConfigurationError, SAXException, IOException {
148         StringBuffer sb = new StringBuffer();
149         sb.append("<?xml version=\"1.0\"?>");
150         sb.append("<settings name=\"test-settings\">");
151         sb.append("  <entry key=\"key-1\" value=\"value-1\" />");
152         sb.append("  <entry key=\"key-2\" value=\"false\" />");
153         sb.append("  <entry key=\"key-3\" value=\"1.5\" />");
154         sb.append("  <settings name=\"child-settings\">");
155         sb.append("    <entry key=\"child-key\" value=\"value\" />");
156         sb.append("  </settings>");
157         sb.append("</settings>");
158 
159         XmlSettings settings = new XmlSettings(createElement(sb.toString()));
160         
161         assertEquals(Arrays.asList(new String[] {"child-settings"}), Arrays.asList(settings.getChildSettings()));
162     }
163 
164 	private static Element createElement(String xml) throws ParserConfigurationException, FactoryConfigurationError,
165 			SAXException, IOException {
166 		Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
167 				new InputSource(new StringReader(xml)));
168 		return doc.getDocumentElement();
169 	}
170 
171 }