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.plugins.shade.resource.properties;
20  
21  import java.io.BufferedWriter;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.nio.charset.StandardCharsets;
26  import java.util.Properties;
27  
28  import org.apache.maven.plugins.shade.resource.properties.io.NoCloseOutputStream;
29  import org.apache.maven.plugins.shade.resource.properties.io.SkipPropertiesDateLineWriter;
30  import org.apache.maven.plugins.shade.resource.rule.TransformerTesterRule;
31  import org.apache.maven.plugins.shade.resource.rule.TransformerTesterRule.Property;
32  import org.apache.maven.plugins.shade.resource.rule.TransformerTesterRule.Resource;
33  import org.apache.maven.plugins.shade.resource.rule.TransformerTesterRule.TransformerTest;
34  import org.junit.Rule;
35  import org.junit.Test;
36  import org.junit.rules.TestRule;
37  
38  import static org.junit.Assert.assertEquals;
39  import static org.junit.Assert.assertFalse;
40  import static org.junit.Assert.assertTrue;
41  
42  public class PropertiesTransformerTest {
43      @Rule
44      public final TestRule tester = new TransformerTesterRule();
45  
46      @Test
47      public void propertiesRewritingIsStable() throws IOException {
48          final Properties properties = new SortedProperties();
49          properties.setProperty("a", "1");
50          properties.setProperty("b", "2");
51  
52          final ByteArrayOutputStream os = new ByteArrayOutputStream();
53          final BufferedWriter writer = new SkipPropertiesDateLineWriter(
54                  new OutputStreamWriter(new NoCloseOutputStream(os), StandardCharsets.ISO_8859_1));
55          properties.store(writer, " Merged by maven-shade-plugin");
56          writer.close();
57          os.close();
58  
59          assertEquals(
60                  "# Merged by maven-shade-plugin\n" + "a=1\n" + "b=2\n",
61                  os.toString("UTF-8").replace(System.lineSeparator(), "\n"));
62      }
63  
64      @Test
65      public void canTransform() {
66          final PropertiesTransformer transformer = new PropertiesTransformer();
67          transformer.setResource("foo/bar/my.properties");
68          assertTrue(transformer.canTransformResource("foo/bar/my.properties"));
69          assertFalse(transformer.canTransformResource("whatever"));
70      }
71  
72      @Test
73      @TransformerTest(
74              transformer = PropertiesTransformer.class,
75              configuration = @Property(name = "resource", value = "foo/bar/my.properties"),
76              visited = {
77                  @Resource(path = "foo/bar/my.properties", content = "a=b"),
78                  @Resource(path = "foo/bar/my.properties", content = "c=d"),
79              },
80              expected = @Resource(path = "foo/bar/my.properties", content = "#.*\na=b\nc=d\n"))
81      public void mergeWithoutOverlap() {}
82  
83      @Test
84      @TransformerTest(
85              transformer = PropertiesTransformer.class,
86              configuration = {
87                  @Property(name = "resource", value = "foo/bar/my.properties"),
88                  @Property(name = "ordinalKey", value = "priority")
89              },
90              visited = {
91                  @Resource(path = "foo/bar/my.properties", content = "a=d\npriority=3"),
92                  @Resource(path = "foo/bar/my.properties", content = "a=b\npriority=1"),
93                  @Resource(path = "foo/bar/my.properties", content = "a=c\npriority=2"),
94              },
95              expected = @Resource(path = "foo/bar/my.properties", content = "#.*\na=d\n"))
96      public void mergeWithOverlap() {}
97  
98      @Test
99      @TransformerTest(
100             transformer = PropertiesTransformer.class,
101             configuration = {
102                 @Property(name = "resource", value = "foo/bar/my.properties"),
103                 @Property(name = "alreadyMergedKey", value = "complete")
104             },
105             visited = {
106                 @Resource(path = "foo/bar/my.properties", content = "a=b\ncomplete=true"),
107                 @Resource(path = "foo/bar/my.properties", content = "a=c\npriority=2"),
108             },
109             expected = @Resource(path = "foo/bar/my.properties", content = "#.*\na=b\n"))
110     public void mergeWithAlreadyMerged() {}
111 
112     @Test
113     @TransformerTest(
114             transformer = PropertiesTransformer.class,
115             configuration = {
116                 @Property(name = "resource", value = "foo/bar/my.properties"),
117                 @Property(name = "alreadyMergedKey", value = "complete")
118             },
119             visited = {
120                 @Resource(path = "foo/bar/my.properties", content = "a=b\ncomplete=true"),
121                 @Resource(path = "foo/bar/my.properties", content = "a=c\ncomplete=true"),
122             },
123             expected = {},
124             expectedException = IllegalStateException.class)
125     public void alreadyMergeConflict() {}
126 }