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.File;
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.nio.charset.StandardCharsets;
25  import java.nio.file.Files;
26  import java.nio.file.StandardCopyOption;
27  
28  import org.codehaus.plexus.testing.PlexusTest;
29  import org.junit.jupiter.api.Test;
30  
31  import static org.codehaus.plexus.testing.PlexusExtension.getBasedir;
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertFalse;
34  
35  /**
36   * @author John Casey
37   * @author Dennis Lundberg
38   * @since 1.0
39   *
40   */
41  @PlexusTest
42  class FilteringUtilsTest {
43      private static File testDirectory = new File(getBasedir(), "target/test-classes/");
44  
45      @Test
46      void mSHARED1213CopyWithTargetAlreadyExisting0ByteFile() throws IOException {
47          File fromFile = new File(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
48          File toFile = new File(testDirectory, "MSHARED-1213-enunciate.xml");
49          Files.write(toFile.toPath(), "".getBytes(StandardCharsets.UTF_8));
50          FilteringUtils.copyFile(
51                  fromFile,
52                  toFile,
53                  "UTF-8",
54                  new FilterWrapper[] {
55                      new FilterWrapper() {
56                          @Override
57                          public Reader getReader(Reader fileReader) {
58                              return fileReader;
59                          }
60                      }
61                  },
62                  false);
63          assertEquals(
64                  Files.readAllLines(fromFile.toPath(), StandardCharsets.UTF_8),
65                  Files.readAllLines(toFile.toPath(), StandardCharsets.UTF_8));
66      }
67  
68      @Test
69      void mSHARED1213CopyWithTargetAlreadyExistingJunkFile() throws IOException {
70          File fromFile = new File(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
71          File toFile = new File(testDirectory, "MSHARED-1213-enunciate.xml");
72          Files.write(toFile.toPath(), "junk".getBytes(StandardCharsets.UTF_8));
73          FilteringUtils.copyFile(
74                  fromFile,
75                  toFile,
76                  "UTF-8",
77                  new FilterWrapper[] {
78                      new FilterWrapper() {
79                          @Override
80                          public Reader getReader(Reader fileReader) {
81                              return fileReader;
82                          }
83                      }
84                  },
85                  false);
86          assertEquals(
87                  Files.readAllLines(fromFile.toPath(), StandardCharsets.UTF_8),
88                  Files.readAllLines(toFile.toPath(), StandardCharsets.UTF_8));
89      }
90  
91      @Test
92      void mSHARED1213CopyWithTargetAlreadyExistingSameFile() throws IOException {
93          File fromFile = new File(getBasedir() + "/src/test/units-files/MSHARED-1213/enunciate.xml");
94          File toFile = new File(testDirectory, "MSHARED-1213-enunciate.xml");
95          Files.copy(fromFile.toPath(), toFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
96          FilteringUtils.copyFile(
97                  fromFile,
98                  toFile,
99                  "UTF-8",
100                 new FilterWrapper[] {
101                     new FilterWrapper() {
102                         @Override
103                         public Reader getReader(Reader fileReader) {
104                             return fileReader;
105                         }
106                     }
107                 },
108                 false);
109         assertEquals(
110                 Files.readAllLines(fromFile.toPath(), StandardCharsets.UTF_8),
111                 Files.readAllLines(toFile.toPath(), StandardCharsets.UTF_8));
112     }
113 
114     @Test
115     void escapeWindowsPathStartingWithDrive() {
116         assertEquals("C:\\\\Users\\\\Administrator", FilteringUtils.escapeWindowsPath("C:\\Users\\Administrator"));
117     }
118 
119     @Test
120     void escapeWindowsPathMissingDriveLetter() {
121         assertEquals(":\\Users\\Administrator", FilteringUtils.escapeWindowsPath(":\\Users\\Administrator"));
122     }
123 
124     @Test
125     void escapeWindowsPathInvalidDriveLetter() {
126         assertEquals("4:\\Users\\Administrator", FilteringUtils.escapeWindowsPath("4:\\Users\\Administrator"));
127     }
128 
129     // This doesn't work, see MSHARED-121
130     /*
131      * public void testEscapeWindowsPathStartingWithDrivelessAbsolutePath()
132      * {
133      * assertEquals( "\\\\Users\\\\Administrator", FilteringUtils.escapeWindowsPath( "\\Users\\Administrator" ) );
134      * }
135      */
136 
137     // This doesn't work, see MSHARED-121
138     /*
139      * public void testEscapeWindowsPathStartingWithExpression()
140      * {
141      * assertEquals( "${pathExpr}\\\\Documents", FilteringUtils.escapeWindowsPath( "${pathExpr}\\Documents" ) );
142      * }
143      */
144 
145     // MSHARED-179
146     @Test
147     void escapeWindowsPathNotAtBeginning() throws Exception {
148         assertEquals(
149                 "jdbc:derby:C:\\\\Users\\\\Administrator/test;create=true",
150                 FilteringUtils.escapeWindowsPath("jdbc:derby:C:\\Users\\Administrator/test;create=true"));
151     }
152 
153     // MSHARED-1330
154     @Test
155     void copyReadOnlyFileTwice() throws Exception {
156         File temp = File.createTempFile("pre-", ".txt");
157         temp.setReadOnly();
158 
159         File out = File.createTempFile("out-", ".txt");
160         out.delete();
161 
162         FilteringUtils.copyFile(temp, out, "UTF-8", new FilterWrapper[0]);
163         assertFalse(out.canWrite());
164 
165         FilteringUtils.copyFile(temp, out, "UTF-8", new FilterWrapper[0]);
166         assertFalse(out.canWrite());
167     }
168 }