1   /**
2    *  MicroEmulator
3    *  Copyright (C) 2006-2007 Bartek Teodorczyk <barteo@barteo.net>
4    *  Copyright (C) 2006-2007 Vlad Skarzhevskyy
5    *
6    *  It is licensed under the following two licenses as alternatives:
7    *    1. GNU Lesser General Public License (the "LGPL") version 2.1 or any newer version
8    *    2. Apache License (the "AL") Version 2.0
9    *
10   *  You may not use this file except in compliance with at least one of
11   *  the above two licenses.
12   *
13   *  You may obtain a copy of the LGPL at
14   *      http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
15   *
16   *  You may obtain a copy of the AL at
17   *      http://www.apache.org/licenses/LICENSE-2.0
18   *
19   *  Unless required by applicable law or agreed to in writing, software
20   *  distributed under the License is distributed on an "AS IS" BASIS,
21   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   *  See the LGPL or the AL for the specific language governing permissions and
23   *  limitations.
24   *
25   *  @version $Id: DeviceXMLInheritTest.java 1717 2008-05-20 03:44:05Z vlads $
26   */
27  package org.microemu.device.impl;
28  
29  import junit.framework.TestCase;
30  import nanoxml.XMLElement;
31  
32  /**
33   * @author vlads
34   * 
35   */
36  public class DeviceXMLInheritTest extends TestCase {
37  
38  	public void verifyXML(String parentXML, String childXML, String expectedXML) {
39  		XMLElement child = new XMLElement();
40  		child.parseString(childXML);
41  		XMLElement parent = new XMLElement();
42  		parent.parseString(parentXML);
43  		XMLElement expected = new XMLElement();
44  		expected.parseString(expectedXML);
45  
46  		XMLElement result = DeviceImpl.inheritXML(parent, child, "/");
47  
48  		try {
49  			assertEquals("xml compare", expected.toString(), result.toString());
50  		} catch (Error e) {
51  			System.out.println("expected:" + expected.toString());
52  			System.out.println("  result:" + result.toString());
53  			throw e;
54  		}
55  	}
56  
57  	public void testValueOverride() {
58  		verifyXML("<device><display><numcolors>2</numcolors><vp>vpv</vp></display></device>",
59  				"<device><display><numcolors>65536</numcolors><v>v1</v></display></device>",
60  				"<device><display><numcolors>65536</numcolors><vp>vpv</vp><v>v1</v></display></device>");
61  	}
62  
63  	public void testValueOverrideByName() {
64  		verifyXML("<device><v>v1</v><v name=\"2\">v2</v><v name=\"3\">v3</v></device>",
65  				"<device><v>v1m</v><v name=\"3\">v3m</v></device>",
66  				"<device><v>v1m</v><v name=\"2\">v2</v><v name=\"3\">v3m</v></device>");
67  
68  		verifyXML("<device><v>v1</v><v name=\"2\">v2</v><v name=\"3\">v3</v></device>",
69  				"<device><v name=\"3\">v3m</v><v>v1m</v></device>",
70  				"<device><v>v1m</v><v name=\"2\">v2</v><v name=\"3\">v3m</v></device>");
71  
72  		verifyXML("<device><v>v1</v><v name=\"2\">v2</v><v name=\"3\">v3</v></device>",
73  				"<device><v>v1m</v><v name=\"3\" attrm=\"am\">v3m</v></device>",
74  				"<device><v>v1m</v><v name=\"2\">v2</v><v name=\"3\" attrm=\"am\">v3m</v></device>");
75  
76  		verifyXML("<device><v attrm=\"am\">v1</v><v name=\"2\">v2</v><v name=\"3\">v3</v></device>",
77  				"<device><v>v1m</v><v name=\"3\">v3m</v></device>",
78  				"<device><v attrm=\"am\">v1m</v><v name=\"2\">v2</v><v name=\"3\">v3m</v></device>");
79  	}
80  
81  	public void testChildElementsOverride() {
82  		verifyXML("<device><v name=\"2\"><cc><c>a</c></cc></v></device>",
83  				"<device><v name=\"2\"><cc override=\"true\"><c>b</c><c>c</c></cc></v></device>",
84  				"<device><v name=\"2\"><cc><c>b</c><c>c</c></cc></v></device>");
85  	}
86  
87  	public void testValueOverrideByNameWithCase() {
88  		verifyXML("<device><v>v1</v><v name=\"a\">v2</v><v name=\"b\">v3</v></device>",
89  				"<device><v>v1m</v><v name=\"B\">v3m</v></device>",
90  				"<device><v>v1m</v><v name=\"a\">v2</v><v name=\"b\">v3</v><v name=\"B\">v3m</v></device>");
91  
92  	}
93  
94  	public void testValueOverrideNoName() {
95  		verifyXML("<device><v name=\"A\">v1</v></device>", "<device><v name=\"B\">v2</v></device>",
96  				"<device><v name=\"A\">v1</v><v name=\"B\">v2</v></device>");
97  
98  	}
99  
100 	public void testValueOverrideFonts() {
101 		verifyXML("<device><fonts>" + "<font face=\"system\" style=\"plain\" size=\"small\">F1</font>"
102 				+ "<font face=\"system\" style=\"plain\" size=\"medium\">F2</font>" + "</fonts></device>",
103 
104 		"<device><fonts>" + "<font face=\"system\" style=\"plain\" size=\"medium\">F2m</font>" + "</fonts></device>",
105 
106 		"<device><fonts>" + "<font face=\"system\" style=\"plain\" size=\"small\">F1</font>"
107 				+ "<font face=\"system\" style=\"plain\" size=\"medium\">F2m</font>" + "</fonts></device>");
108 	}
109 
110 }