View Javadoc
1   package org.apache.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  import org.junit.Test;
28  
29  /**
30   * XMLReaderTest 
31   *
32   *
33   */
34  public class XMLReaderTest
35      extends AbstractArchivaXmlTestCase
36  {
37      private void assertElementTexts( List<Element> elementList, String[] expectedTexts )
38      {
39          assertEquals( "Element List Size", expectedTexts.length, elementList.size() );
40  
41          List<String> texts = new ArrayList<>();
42          for ( Element element : elementList )
43          {
44              texts.add( element.getTextTrim() );
45          }
46  
47          for ( int i = 0; i < expectedTexts.length; i++ )
48          {
49              String expectedText = expectedTexts[i];
50              assertTrue( "Contains [" + expectedText + "]", texts.contains( expectedText ) );
51          }
52      }
53      
54      @Test
55      public void testNoPrologBasicRead()
56          throws XMLException
57      {
58          File xmlFile = getExampleXml( "no-prolog-basic.xml" );
59          XMLReader reader = new XMLReader( "basic", xmlFile );
60  
61          List<Element> fruits = reader.getElementList( "//basic/fruits/fruit" );
62          assertElementTexts( fruits, new String[] { "apple", "cherry", "pear", "peach" } );
63      }
64  
65      @Test
66      public void testNoPrologEntitiesRead()
67          throws XMLException
68      {
69          File xmlFile = getExampleXml( "no-prolog-with-entities.xml" );
70          XMLReader reader = new XMLReader( "basic", xmlFile );
71  
72          List<Element> names = reader.getElementList( "//basic/names/name" );
73          assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
74      }
75  
76      @Test
77      public void testNoPrologUtf8Read()
78          throws XMLException
79      {
80          File xmlFile = getExampleXml( "no-prolog-with-utf8.xml" );
81          XMLReader reader = new XMLReader( "basic", xmlFile );
82  
83          List<Element> names = reader.getElementList( "//basic/names/name" );
84          assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
85      }
86  
87      @Test
88      public void testPrologUtf8Read()
89          throws XMLException
90      {
91          File xmlFile = getExampleXml( "prolog-with-utf8.xml" );
92          XMLReader reader = new XMLReader( "basic", xmlFile );
93  
94          List<Element> names = reader.getElementList( "//basic/names/name" );
95          assertElementTexts( names, new String[] { TRYGVIS, INFINITE_ARCHIVA } );
96      }
97      
98      // MRM-1136
99      @Test
100     public void testProxiedMetadataRead()
101         throws XMLException
102     {
103         File xmlFile = getExampleXml( "maven-metadata-codehaus-snapshots.xml" );
104         XMLReader reader = new XMLReader( "metadata", xmlFile );        
105         reader.removeNamespaces();
106         
107         Element groupId = reader.getElement( "//metadata/groupId" );        
108         assertNotNull( groupId );
109         assertEquals( "org.codehaus.mojo", groupId.getTextTrim() );   
110     }
111 
112 }