View Javadoc
1   package org.apache.maven.project;
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 static org.hamcrest.Matchers.empty;
23  import static org.hamcrest.Matchers.is;
24  import static org.junit.Assert.assertThat;
25  
26  import java.io.ByteArrayInputStream;
27  import java.io.InputStream;
28  import java.io.UnsupportedEncodingException;
29  import java.util.Arrays;
30  
31  import junit.framework.TestCase;
32  
33  /**
34   * Tests {@link ExtensionDescriptorBuilder}.
35   *
36   * @author Benjamin Bentmann
37   */
38  public class ExtensionDescriptorBuilderTest
39      extends TestCase
40  {
41  
42      private ExtensionDescriptorBuilder builder;
43  
44      @Override
45      protected void setUp()
46          throws Exception
47      {
48          super.setUp();
49  
50          builder = new ExtensionDescriptorBuilder();
51      }
52  
53      @Override
54      protected void tearDown()
55          throws Exception
56      {
57          builder = null;
58  
59          super.tearDown();
60      }
61  
62      private InputStream toStream( String xml )
63      {
64          try
65          {
66              return new ByteArrayInputStream( xml.getBytes( "UTF-8" ) );
67          }
68          catch ( UnsupportedEncodingException e )
69          {
70              throw new IllegalStateException( e );
71          }
72      }
73  
74      public void testEmptyDescriptor()
75          throws Exception
76      {
77          String xml = "<extension></extension>";
78  
79          ExtensionDescriptor ed = builder.build( toStream( xml ) );
80  
81          assertNotNull( ed );
82          assertNotNull( ed.getExportedPackages() );
83          assertThat( ed.getExportedPackages(), is( empty() ) );
84          assertNotNull( ed.getExportedArtifacts() );
85          assertThat( ed.getExportedArtifacts(), is( empty() ) );
86      }
87  
88      public void testCompleteDescriptor()
89          throws Exception
90      {
91          String xml =
92              "<?xml version='1.0' encoding='UTF-8'?>" + "<extension>" + "<exportedPackages>"
93                  + "<exportedPackage>a</exportedPackage>" + "<exportedPackage>b</exportedPackage>"
94                  + "<exportedPackage>c</exportedPackage>" + "</exportedPackages>" + "<exportedArtifacts>"
95                  + "<exportedArtifact>x</exportedArtifact>" + "<exportedArtifact>y</exportedArtifact>"
96                  + "<exportedArtifact> z </exportedArtifact>" + "</exportedArtifacts>" + "</extension>";
97  
98          ExtensionDescriptor ed = builder.build( toStream( xml ) );
99  
100         assertNotNull( ed );
101         assertEquals( Arrays.asList( "a", "b", "c" ), ed.getExportedPackages() );
102         assertEquals( Arrays.asList( "x", "y", "z" ), ed.getExportedArtifacts() );
103     }
104 
105 }