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.shared.filtering;
20  
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.nio.charset.StandardCharsets;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.Paths;
27  import java.nio.file.StandardCopyOption;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import static org.apache.maven.api.di.testing.MavenDIExtension.getBasedir;
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  
34  /**
35   * @author John Casey
36   * @author Dennis Lundberg
37   * @since 1.0
38   *
39   */
40  public class FilteringUtilsTest {
41      private static Path testDirectory = Paths.get(getBasedir(), "target/test-classes/");
42  
43      @Test
44      public void testMSHARED1213CopyWithTargetAlreadyExisting0ByteFile() throws IOException {
45          Path fromFile = Paths.get(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
46          Path toFile = testDirectory.resolve("MSHARED-1213-enunciate.xml");
47          Files.write(toFile, "".getBytes(StandardCharsets.UTF_8));
48          FilteringUtils.copyFile(
49                  fromFile,
50                  toFile,
51                  "UTF-8",
52                  new FilterWrapper[] {
53                      new FilterWrapper() {
54                          @Override
55                          public Reader getReader(Reader fileReader) {
56                              return fileReader;
57                          }
58                      }
59                  },
60                  false);
61          assertEquals(
62                  Files.readAllLines(fromFile, StandardCharsets.UTF_8),
63                  Files.readAllLines(toFile, StandardCharsets.UTF_8));
64      }
65  
66      @Test
67      public void testMSHARED1213CopyWithTargetAlreadyExistingJunkFile() throws IOException {
68          Path fromFile = Paths.get(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
69          Path toFile = testDirectory.resolve("MSHARED-1213-enunciate.xml");
70          Files.write(toFile, "junk".getBytes(StandardCharsets.UTF_8));
71          FilteringUtils.copyFile(
72                  fromFile,
73                  toFile,
74                  "UTF-8",
75                  new FilterWrapper[] {
76                      new FilterWrapper() {
77                          @Override
78                          public Reader getReader(Reader fileReader) {
79                              return fileReader;
80                          }
81                      }
82                  },
83                  false);
84          assertEquals(
85                  Files.readAllLines(fromFile, StandardCharsets.UTF_8),
86                  Files.readAllLines(toFile, StandardCharsets.UTF_8));
87      }
88  
89      @Test
90      public void testMSHARED1213CopyWithTargetAlreadyExistingSameFile() throws IOException {
91          Path fromFile = Paths.get(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
92          Path toFile = testDirectory.resolve("MSHARED-1213-enunciate.xml");
93          Files.copy(fromFile, toFile, StandardCopyOption.REPLACE_EXISTING);
94          FilteringUtils.copyFile(
95                  fromFile,
96                  toFile,
97                  "UTF-8",
98                  new FilterWrapper[] {
99                      new FilterWrapper() {
100                         @Override
101                         public Reader getReader(Reader fileReader) {
102                             return fileReader;
103                         }
104                     }
105                 },
106                 false);
107         assertEquals(
108                 Files.readAllLines(fromFile, StandardCharsets.UTF_8),
109                 Files.readAllLines(toFile, StandardCharsets.UTF_8));
110     }
111 
112     @Test
113     public void testEscapeWindowsPathStartingWithDrive() {
114         assertEquals("C:\\\\Users\\\\Administrator", FilteringUtils.escapeWindowsPath("C:\\Users\\Administrator"));
115     }
116 
117     @Test
118     public void testEscapeWindowsPathMissingDriveLetter() {
119         assertEquals(":\\Users\\Administrator", FilteringUtils.escapeWindowsPath(":\\Users\\Administrator"));
120     }
121 
122     @Test
123     public void testEscapeWindowsPathInvalidDriveLetter() {
124         assertEquals("4:\\Users\\Administrator", FilteringUtils.escapeWindowsPath("4:\\Users\\Administrator"));
125     }
126 
127     // This doesn't work, see MSHARED-121
128     /*
129      * public void testEscapeWindowsPathStartingWithDrivelessAbsolutePath()
130      * {
131      * assertEquals( "\\\\Users\\\\Administrator", FilteringUtils.escapeWindowsPath( "\\Users\\Administrator" ) );
132      * }
133      */
134 
135     // This doesn't work, see MSHARED-121
136     /*
137      * public void testEscapeWindowsPathStartingWithExpression()
138      * {
139      * assertEquals( "${pathExpr}\\\\Documents", FilteringUtils.escapeWindowsPath( "${pathExpr}\\Documents" ) );
140      * }
141      */
142 
143     // MSHARED-179
144     @Test
145     public void testEscapeWindowsPathNotAtBeginning() throws Exception {
146         assertEquals(
147                 "jdbc:derby:C:\\\\Users\\\\Administrator/test;create=true",
148                 FilteringUtils.escapeWindowsPath("jdbc:derby:C:\\Users\\Administrator/test;create=true"));
149     }
150 }