1 package org.apache.archiva.xml;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.dom4j.Element;
27 import org.junit.Test;
34 public class XMLReaderTest
35 extends AbstractArchivaXmlTestCase
37 private void assertElementTexts( List<Element> elementList, String[] expectedTexts )
39 assertEquals( "Element List Size", expectedTexts.length, elementList.size() );
41 List<String> texts = new ArrayList<String>();
42 for ( Element element : elementList )
44 texts.add( element.getTextTrim() );
47 for ( int i = 0; i < expectedTexts.length; i++ )
49 String expectedText = expectedTexts[i];
50 assertTrue( "Contains [" + expectedText + "]", texts.contains( expectedText ) );
55 public void testNoPrologBasicRead()
58 File xmlFile = getExampleXml( "no-prolog-basic.xml" );
59 XMLReader reader = new XMLReader( "basic", xmlFile );
61 List<Element> fruits = reader.getElementList( "//basic/fruits/fruit" );
62 assertElementTexts( fruits, new String[] { "apple", "cherry", "pear", "peach" } );
66 public void testNoPrologEntitiesRead()
69 File xmlFile = getExampleXml( "no-prolog-with-entities.xml" );
70 XMLReader reader = new XMLReader( "basic", xmlFile );
72 List<Element> names = reader.getElementList( "//basic/names/name" );
73 assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
77 public void testNoPrologUtf8Read()
80 File xmlFile = getExampleXml( "no-prolog-with-utf8.xml" );
81 XMLReader reader = new XMLReader( "basic", xmlFile );
83 List<Element> names = reader.getElementList( "//basic/names/name" );
84 assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
88 public void testPrologUtf8Read()
91 File xmlFile = getExampleXml( "prolog-with-utf8.xml" );
92 XMLReader reader = new XMLReader( "basic", xmlFile );
94 List<Element> names = reader.getElementList( "//basic/names/name" );
95 assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
100 public void testProxiedMetadataRead()
103 File xmlFile = getExampleXml( "maven-metadata-codehaus-snapshots.xml" );
104 XMLReader reader = new XMLReader( "metadata", xmlFile );
105 reader.removeNamespaces();
107 Element groupId = reader.getElement( "//metadata/groupId" );
108 assertNotNull( groupId );
109 assertEquals( "org.codehaus.mojo", groupId.getTextTrim() );