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