View Javadoc

1   package org.apache.maven.archiva.xml;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.dom4j.Element;
27  
28  /**
29   * XMLReaderTest 
30   *
31   * @version $Id: XMLReaderTest.java 756559 2009-03-20 16:05:05Z oching $
32   */
33  public class XMLReaderTest
34      extends AbstractArchivaXmlTestCase
35  {
36      private void assertElementTexts( List<Element> elementList, String[] expectedTexts )
37      {
38          assertEquals( "Element List Size", expectedTexts.length, elementList.size() );
39  
40          List<String> texts = new ArrayList<String>();
41          for ( Element element : elementList )
42          {
43              texts.add( element.getTextTrim() );
44          }
45  
46          for ( int i = 0; i < expectedTexts.length; i++ )
47          {
48              String expectedText = expectedTexts[i];
49              assertTrue( "Contains [" + expectedText + "]", texts.contains( expectedText ) );
50          }
51      }
52  
53      public void testNoPrologBasicRead()
54          throws XMLException
55      {
56          File xmlFile = getExampleXml( "no-prolog-basic.xml" );
57          XMLReader reader = new XMLReader( "basic", xmlFile );
58  
59          List<Element> fruits = reader.getElementList( "//basic/fruits/fruit" );
60          assertElementTexts( fruits, new String[] { "apple", "cherry", "pear", "peach" } );
61      }
62  
63      public void testNoPrologEntitiesRead()
64          throws XMLException
65      {
66          File xmlFile = getExampleXml( "no-prolog-with-entities.xml" );
67          XMLReader reader = new XMLReader( "basic", xmlFile );
68  
69          List<Element> names = reader.getElementList( "//basic/names/name" );
70          assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
71      }
72  
73      public void testNoPrologUtf8Read()
74          throws XMLException
75      {
76          File xmlFile = getExampleXml( "no-prolog-with-utf8.xml" );
77          XMLReader reader = new XMLReader( "basic", xmlFile );
78  
79          List<Element> names = reader.getElementList( "//basic/names/name" );
80          assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
81      }
82  
83      public void testPrologUtf8Read()
84          throws XMLException
85      {
86          File xmlFile = getExampleXml( "prolog-with-utf8.xml" );
87          XMLReader reader = new XMLReader( "basic", xmlFile );
88  
89          List<Element> names = reader.getElementList( "//basic/names/name" );
90          assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
91      }
92      
93      // MRM-1136
94      public void testProxiedMetadataRead()
95          throws XMLException
96      {
97          File xmlFile = getExampleXml( "maven-metadata-codehaus-snapshots.xml" );
98          XMLReader reader = new XMLReader( "metadata", xmlFile );        
99          reader.removeNamespaces();
100         
101         Element groupId = reader.getElement( "//metadata/groupId" );        
102         assertNotNull( groupId );
103         assertEquals( "org.codehaus.mojo", groupId.getTextTrim() );   
104     }
105 
106 }