View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.InputStream;
23  import java.nio.charset.StandardCharsets;
24  import java.util.Arrays;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.hamcrest.MatcherAssert.assertThat;
31  import static org.hamcrest.Matchers.empty;
32  import static org.hamcrest.Matchers.is;
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertNotNull;
35  
36  /**
37   * Tests {@link ExtensionDescriptorBuilder}.
38   *
39   */
40  class ExtensionDescriptorBuilderTest {
41  
42      private ExtensionDescriptorBuilder builder;
43  
44      @BeforeEach
45      void setUp() throws Exception {
46          builder = new ExtensionDescriptorBuilder();
47      }
48  
49      @AfterEach
50      void tearDown() throws Exception {
51          builder = null;
52      }
53  
54      private InputStream toStream(String xml) {
55          return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
56      }
57  
58      @Test
59      void testEmptyDescriptor() throws Exception {
60          String xml = "<extension></extension>";
61  
62          ExtensionDescriptor ed = builder.build(toStream(xml));
63  
64          assertNotNull(ed);
65          assertNotNull(ed.getExportedPackages());
66          assertThat(ed.getExportedPackages(), is(empty()));
67          assertNotNull(ed.getExportedArtifacts());
68          assertThat(ed.getExportedArtifacts(), is(empty()));
69      }
70  
71      @Test
72      void testCompleteDescriptor() throws Exception {
73          String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<extension>" + "<exportedPackages>"
74                  + "<exportedPackage>a</exportedPackage>" + "<exportedPackage>b</exportedPackage>"
75                  + "<exportedPackage>c</exportedPackage>" + "</exportedPackages>" + "<exportedArtifacts>"
76                  + "<exportedArtifact>x</exportedArtifact>" + "<exportedArtifact>y</exportedArtifact>"
77                  + "<exportedArtifact> z </exportedArtifact>" + "</exportedArtifacts>" + "</extension>";
78  
79          ExtensionDescriptor ed = builder.build(toStream(xml));
80  
81          assertNotNull(ed);
82          assertEquals(Arrays.asList("a", "b", "c"), ed.getExportedPackages());
83          assertEquals(Arrays.asList("x", "y", "z"), ed.getExportedArtifacts());
84      }
85  }