001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.settings.xml;
017    
018    import java.io.IOException;
019    import java.io.StringReader;
020    import java.util.Arrays;
021    import java.util.List;
022    
023    import javax.xml.parsers.DocumentBuilderFactory;
024    import javax.xml.parsers.FactoryConfigurationError;
025    import javax.xml.parsers.ParserConfigurationException;
026    
027    import org.springframework.richclient.settings.Settings;
028    import org.springframework.richclient.settings.SettingsAbstractTests;
029    import org.w3c.dom.Document;
030    import org.w3c.dom.Element;
031    import org.w3c.dom.Node;
032    import org.w3c.dom.NodeList;
033    import org.xml.sax.InputSource;
034    import org.xml.sax.SAXException;
035    
036    /**
037     * @author Peter De Bruycker
038     */
039    public class XmlSettingsTests extends SettingsAbstractTests {
040    
041            protected Settings createSettings() throws Exception {
042                    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
043                    Element element = doc.createElement("settings");
044                    element.setAttribute("name", "root");
045                    doc.appendChild(element);
046    
047                    return new XmlSettings(element);
048            }
049    
050            public void testConstructor() throws ParserConfigurationException, FactoryConfigurationError, SAXException,
051                            IOException {
052                    StringBuffer sb = new StringBuffer();
053                    sb.append("<?xml version=\"1.0\"?>");
054                    sb.append("<settings name=\"test-settings\">");
055                    sb.append("  <entry key=\"key-1\" value=\"value-1\" />");
056                    sb.append("  <entry key=\"key-2\" value=\"false\" />");
057                    sb.append("  <entry key=\"key-3\" value=\"1.5\" />");
058                    sb.append("  <settings name=\"child-settings\">");
059                    sb.append("    <entry key=\"child-key\" value=\"value\" />");
060                    sb.append("  </settings>");
061                    sb.append("</settings>");
062    
063                    Element element = createElement(sb.toString());
064    
065                    XmlSettings settings = new XmlSettings(null, element);
066                    assertEquals("test-settings", settings.getName());
067                    assertEquals(element, settings.getElement());
068    
069                    List keys = Arrays.asList(settings.getKeys());
070                    assertEquals(3, keys.size());
071                    assertTrue(keys.contains("key-1"));
072                    assertTrue(keys.contains("key-2"));
073                    assertTrue(keys.contains("key-3"));
074    
075                    assertEquals("value-1", settings.getString("key-1"));
076                    assertFalse(settings.getBoolean("key-2"));
077                    assertEquals(1.5f, settings.getFloat("key-3"), 0.0f);
078    
079                    Settings childSettings = settings.getSettings("child-settings");
080                    assertTrue(childSettings instanceof XmlSettings);
081                    assertEquals(1, childSettings.getKeys().length);
082                    assertEquals("child-key", childSettings.getKeys()[0]);
083            }
084    
085            public void testRemove_RemovesElement() throws ParserConfigurationException, FactoryConfigurationError,
086                            SAXException, IOException {
087                    StringBuffer sb = new StringBuffer();
088                    sb.append("<?xml version=\"1.0\"?>");
089                    sb.append("<settings name=\"test-settings\">");
090                    sb.append("  <entry key=\"key-1\" value=\"value-1\" />");
091                    sb.append("  <entry key=\"key-2\" value=\"false\" />");
092                    sb.append("  <entry key=\"key-3\" value=\"1.5\" />");
093                    sb.append("</settings>");
094    
095                    Element element = createElement(sb.toString());
096    
097                    XmlSettings settings = new XmlSettings(null, element);
098    
099                    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    }