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.utils.io;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.nio.charset.StandardCharsets;
24  
25  import static org.apache.commons.io.FileUtils.write;
26  
27  public class SymlinkTestSetup {
28      /**
29       * Creates a standard directory layout with symlinks and files.
30       */
31      public static File createStandardSymlinkTestDir(File root) throws IOException {
32          File srcDir = new File(root, "/src");
33          srcDir.mkdirs();
34          File target = new File(srcDir, "targetDir");
35          target.mkdirs();
36          write(new File(target, "targetFile.txt"), "a regular File payload", StandardCharsets.UTF_8);
37          File aRegularDir = new File(srcDir, "aRegularDir");
38          aRegularDir.mkdirs();
39          write(new File(aRegularDir, "aRegularFile.txt"), "a regular File payload", StandardCharsets.UTF_8);
40  
41          File dirOnTheOutside = new File(root, "dirOnTheOutside");
42          dirOnTheOutside.mkdirs();
43          write(
44                  new File(dirOnTheOutside, "FileInDirOnTheOutside.txt"),
45                  "a file in dir on the outside",
46                  StandardCharsets.UTF_8);
47          write(new File(root, "onTheOutside.txt"), "A file on the outside", StandardCharsets.UTF_8);
48          write(new File(srcDir, "fileR.txt"), "FileR payload", StandardCharsets.UTF_8);
49          write(new File(srcDir, "fileW.txt"), "FileW payload", StandardCharsets.UTF_8);
50          write(new File(srcDir, "fileX.txt"), "FileX payload", StandardCharsets.UTF_8);
51          // todo: set file attributes (not used here)
52  
53          FileUtils.createSymbolicLink(new File(srcDir, "symDir"), new File("targetDir"));
54          FileUtils.createSymbolicLink(new File(srcDir, "symLinkToDirOnTheOutside"), new File("../dirOnTheOutside"));
55          FileUtils.createSymbolicLink(new File(srcDir, "symLinkToFileOnTheOutside"), new File("../onTheOutside.txt"));
56          FileUtils.createSymbolicLink(new File(srcDir, "symR"), new File("fileR.txt"));
57          FileUtils.createSymbolicLink(new File(srcDir, "symW"), new File("fileW.txt"));
58          FileUtils.createSymbolicLink(new File(srcDir, "symX"), new File("fileX.txt"));
59          return srcDir;
60      }
61  }