View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.net.URI;
26  import java.net.URISyntaxException;
27  import java.util.regex.Matcher;
28  import java.util.regex.Pattern;
29  
30  import org.apache.commons.vfs2.util.Messages;
31  
32  /**
33   * Provides utility methods for locating test resources.
34   */
35  public abstract class VfsTestUtils {
36  
37      public static final String TEST_BASE_DIR = "test.basedir";
38  
39      private static File baseDir;
40  
41      /** URL pattern */
42      private static final Pattern URL_PATTERN = Pattern.compile("[a-z]+://.*");
43  
44      /** Password pattern */
45      private static final Pattern PASSWORD_PATTERN = Pattern.compile(":(?:[^/]+)@");
46  
47      /**
48       * Asserts that an exception contains the expected message.
49       */
50      public static void assertSameMessage(final String code, final Object param, final Throwable throwable) {
51          assertSameMessage(code, new Object[] { param }, throwable);
52      }
53  
54      /**
55       * Asserts that an exception contains the expected message.
56       */
57      private static void assertSameMessage(final String code, final Object[] params, final Throwable throwable) {
58          Object[] parmArray = params;
59          if (throwable instanceof FileSystemException) {
60              final FileSystemException fse = (FileSystemException) throwable;
61  
62              // Compare message code and params
63              assertEquals(code, fse.getCode());
64              assertEquals(params.length, fse.getInfo().length);
65              parmArray = new Object[params.length];
66              for (int i = 0; i < params.length; i++) {
67                  String value = String.valueOf(params[i]);
68                  // mask passwords (VFS-169)
69                  final Matcher urlMatcher = URL_PATTERN.matcher(value);
70                  if (urlMatcher.find()) {
71                      final Matcher pwdMatcher = PASSWORD_PATTERN.matcher(value);
72                      value = pwdMatcher.replaceFirst(":***@");
73                  }
74                  assertEquals(value, fse.getInfo()[i]);
75                  parmArray[i] = value;
76              }
77          }
78  
79          // Compare formatted message
80          final String message = Messages.getString(code, parmArray);
81          assertEquals(message, throwable.getMessage());
82      }
83  
84      /**
85       * Gets a canonical file.
86       */
87      public static File getCanonicalFile(final File file) {
88          try {
89              return file.getCanonicalFile();
90          } catch (final IOException e) {
91              return file.getAbsoluteFile();
92          }
93      }
94  
95      public static String getResourceTestDirectory() {
96          return System.getProperty("test.basedir.res", "test-data");
97      }
98  
99      /**
100      * Gets the test directory as a String.
101      *
102      * {@link #getTestDirectoryFile()} should be preferred.
103      *
104      * @return the test directory as a String
105      */
106     public static String getTestDirectory() {
107         return System.getProperty(TEST_BASE_DIR, "target/test-classes/test-data");
108     }
109 
110     /**
111      * Gets a test directory, creating it if it does not exist.
112      *
113      * @param name path of the directory, relative to this test's base directory.
114      */
115     public static File getTestDirectory(final String name) {
116         File file = new File(getTestDirectoryFile(), name);
117         file = getCanonicalFile(file);
118         assertTrue(file.isDirectory() || file.mkdirs(), "Test directory \"" + file + "\" does not exist or is not a directory.");
119         return file;
120     }
121 
122     /**
123      * Gets the base directory for this test.
124      */
125     public static File getTestDirectoryFile() {
126         if (baseDir == null) {
127             final String baseDirProp = getTestDirectory();
128             // the directory maybe expressed as URI in certain environments
129             if (baseDirProp.startsWith("file://")) {
130                 try {
131                     baseDir = getCanonicalFile(new File(new URI(baseDirProp)));
132                 } catch (final URISyntaxException e) {
133                     baseDir = getCanonicalFile(new File(baseDirProp));
134                 }
135             } else {
136                 baseDir = getCanonicalFile(new File(baseDirProp));
137             }
138         }
139         return baseDir;
140     }
141 
142     /**
143      * Gets a test resource, and asserts that the resource exists.
144      *
145      * @param name path of the resource, relative to this test's base directory.
146      */
147     public static File getTestResource(final String name) {
148         return getTestResource(name, true);
149     }
150 
151     /**
152      * Gets a test resource.
153      *
154      * @param name path of the resource, relative to this test's base directory.
155      */
156     public static File getTestResource(final String name, final boolean mustExist) {
157         File file = new File(getTestDirectoryFile(), name);
158         file = getCanonicalFile(file);
159         if (mustExist) {
160             assertTrue(file.exists(), "Test file \"" + file + "\" does not exist.");
161         } else {
162             assertFalse(file.exists(), "Test file \"" + file + "\" should not exist.");
163         }
164 
165         return file;
166     }
167 
168 }