/* * Copyright 2003-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package groovy.xml.dom import org.custommonkey.xmlunit.Diff import org.custommonkey.xmlunit.XMLUnit import groovy.xml.* /* This isn't inherently doing anything requiring jdk1.5 but there is a bug in JDK 1.4 VMs which causes the test to have problems, so it is here. */ class NamespaceDOMTest extends TestXmlSupport { def expected1 = ''' ''' def expected2 = ''' 123ABCD 557 Short-Sleeved Woolen Blouse 10 ''' void testXsdSchemaWithBuilderHavingAutoPrefix() { def builder = DOMBuilder.newInstance() def xsd = NamespaceBuilder.newInstance(builder, 'http://www.w3.org/2001/XMLSchema', 'xsd') def root = xsd.schema { element(name: 'purchaseOrder', type: 'PurchaseOrderType') element(name: 'comment', type: 'xsd:string') complexType(name: 'PurchaseOrderType') { sequence { element(name: 'shipTo', type: 'USAddress') element(name: 'billTo', type: 'USAddress') element(minOccurs: '0', ref: 'comment') element(name: 'items', type: 'Items') } attribute(name: 'orderDate', type: 'xsd:date') } } assertXmlEqual(expected1, XmlUtil.serialize(root)) } void testXsdSchemaWithBuilderHavingMultipleNamespaces() { def builder = DOMBuilder.newInstance() def multi = NamespaceBuilder.newInstance(builder) multi.namespace('http://example.org/ord') multi.namespace('http://example.org/prod', 'prod') checkXml(multi) } void testXsdSchemaWithBuilderHavingDeclareNamespace() { def builder = DOMBuilder.newInstance() def multi = NamespaceBuilder.newInstance(builder) multi.declareNamespace( '':'http://example.org/ord', prod:'http://example.org/prod' ) checkXml(multi) } private checkXml(multi) { def root = multi.envelope(xmlns: '') { order { number('123ABCD') items { 'prod:product' { 'prod:number'('prod:id': 'prod557', '557') 'prod:name'('Short-Sleeved Woolen Blouse') 'prod:size'(system: 'UK-DRESS', '10') 'prod:colour'(value: 'red') } } } } assertXmlEqual(expected2, XmlUtil.serialize(root)) } private assertXmlEqual(expected, actual) { XMLUnit.ignoreWhitespace = true def xmlDiff = new Diff(expected, actual) assert xmlDiff.similar(), xmlDiff.toString() } }