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   * @author Benjamin Bentmann
40   */
41  class ExtensionDescriptorBuilderTest {
42  
43      private ExtensionDescriptorBuilder builder;
44  
45      @BeforeEach
46      void setUp() throws Exception {
47          builder = new ExtensionDescriptorBuilder();
48      }
49  
50      @AfterEach
51      void tearDown() throws Exception {
52          builder = null;
53      }
54  
55      private InputStream toStream(String xml) {
56          return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
57      }
58  
59      @Test
60      void testEmptyDescriptor() throws Exception {
61          String xml = "<extension></extension>";
62  
63          ExtensionDescriptor ed = builder.build(toStream(xml));
64  
65          assertNotNull(ed);
66          assertNotNull(ed.getExportedPackages());
67          assertThat(ed.getExportedPackages(), is(empty()));
68          assertNotNull(ed.getExportedArtifacts());
69          assertThat(ed.getExportedArtifacts(), is(empty()));
70      }
71  
72      @Test
73      void testCompleteDescriptor() throws Exception {
74          String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<extension>" + "<exportedPackages>"
75                  + "<exportedPackage>a</exportedPackage>" + "<exportedPackage>b</exportedPackage>"
76                  + "<exportedPackage>c</exportedPackage>" + "</exportedPackages>" + "<exportedArtifacts>"
77                  + "<exportedArtifact>x</exportedArtifact>" + "<exportedArtifact>y</exportedArtifact>"
78                  + "<exportedArtifact> z </exportedArtifact>" + "</exportedArtifacts>" + "</extension>";
79  
80          ExtensionDescriptor ed = builder.build(toStream(xml));
81  
82          assertNotNull(ed);
83          assertEquals(Arrays.asList("a", "b", "c"), ed.getExportedPackages());
84          assertEquals(Arrays.asList("x", "y", "z"), ed.getExportedArtifacts());
85      }
86  }