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 java.io.File;
20  import java.io.IOException;
21  import java.net.URI;
22  import java.net.URISyntaxException;
23  import java.util.regex.Matcher;
24  import java.util.regex.Pattern;
25  
26  import org.apache.commons.vfs2.util.Messages;
27  
28  import junit.framework.TestCase;
29  
30  /**
31   * A base class for VFS tests. Provides utility methods for locating test resources.
32   */
33  public abstract class AbstractVfsTestCase extends TestCase {
34  
35      private static File baseDir;
36  
37      /** URL pattern */
38      private static final Pattern URL_PATTERN = Pattern.compile("[a-z]+://.*");
39  
40      /** Password pattern */
41      private static final Pattern PASSWORD_PATTERN = Pattern.compile(":(?:[^/]+)@");
42  
43      /**
44       * Asserts that an exception contains the expected message.
45       */
46      public static void assertSameMessage(final String code, final Object param, final Throwable throwable) {
47          assertSameMessage(code, new Object[] { param }, throwable);
48      }
49  
50      /**
51       * Asserts that an exception contains the expected message.
52       */
53      private static void assertSameMessage(final String code, final Object[] params, final Throwable throwable) {
54          Object[] parmArray = params;
55          if (throwable instanceof FileSystemException) {
56              final FileSystemException fse = (FileSystemException) throwable;
57  
58              // Compare message code and params
59              assertEquals(code, fse.getCode());
60              assertEquals(params.length, fse.getInfo().length);
61              parmArray = new Object[params.length];
62              for (int i = 0; i < params.length; i++) {
63                  String value = String.valueOf(params[i]);
64                  // mask passwords (VFS-169)
65                  final Matcher urlMatcher = URL_PATTERN.matcher(value);
66                  if (urlMatcher.find()) {
67                      final Matcher pwdMatcher = PASSWORD_PATTERN.matcher(value);
68                      value = pwdMatcher.replaceFirst(":***@");
69                  }
70                  assertEquals(value, fse.getInfo()[i]);
71                  parmArray[i] = value;
72              }
73          }
74  
75          // Compare formatted message
76          final String message = Messages.getString(code, parmArray);
77          assertEquals(message, throwable.getMessage());
78      }
79  
80      /**
81       * Compares 2 objects for equality, nulls are equal. Used by the test classes' equals() methods.
82       */
83      public static boolean equals(final Object o1, final Object o2) {
84          if (o1 == null && o2 == null) {
85              return true;
86          }
87          if (o1 == null || o2 == null) {
88              return false;
89          }
90          return o1.equals(o2);
91      }
92  
93      /**
94       * Makes a file canonical
95       */
96      public static File getCanonicalFile(final File file) {
97          try {
98              return file.getCanonicalFile();
99          } catch (final IOException e) {
100             return file.getAbsoluteFile();
101         }
102     }
103 
104     public static String getResourceTestDirectory() {
105         return System.getProperty("test.basedir.res", "test-data");
106     }
107 
108     /**
109      * Returns the test directory as a String.
110      * <p>
111      * {@link #getTestDirectoryFile()} should be preferred.
112      *
113      * @return the test directory as a String
114      */
115     public static String getTestDirectory() {
116         return System.getProperty("test.basedir", "target/test-classes/test-data");
117     }
118 
119     /**
120      * Locates a test directory, creating it if it does not exist.
121      *
122      * @param name path of the directory, relative to this test's base directory.
123      */
124     public static File getTestDirectory(final String name) {
125         File file = new File(getTestDirectoryFile(), name);
126         file = getCanonicalFile(file);
127         assertTrue("Test directory \"" + file + "\" does not exist or is not a directory.",
128                 file.isDirectory() || file.mkdirs());
129         return file;
130     }
131 
132     /**
133      * Locates the base directory for this test.
134      */
135     public static File getTestDirectoryFile() {
136         if (baseDir == null) {
137             final String baseDirProp = getTestDirectory();
138             // the directory maybe expressed as URI in certain environments
139             if (baseDirProp.startsWith("file://")) {
140                 try {
141                     baseDir = getCanonicalFile(new File(new URI(baseDirProp)));
142                 } catch (final URISyntaxException e) {
143                     baseDir = getCanonicalFile(new File(baseDirProp));
144                 }
145             } else {
146                 baseDir = getCanonicalFile(new File(baseDirProp));
147             }
148         }
149         return baseDir;
150     }
151 
152     /**
153      * Locates a test resource, and asserts that the resource exists
154      *
155      * @param name path of the resource, relative to this test's base directory.
156      */
157     public static File getTestResource(final String name) {
158         return getTestResource(name, true);
159     }
160 
161     /**
162      * Locates a test resource.
163      *
164      * @param name path of the resource, relative to this test's base directory.
165      */
166     public static File getTestResource(final String name, final boolean mustExist) {
167         File file = new File(getTestDirectoryFile(), name);
168         file = getCanonicalFile(file);
169         if (mustExist) {
170             assertTrue("Test file \"" + file + "\" does not exist.", file.exists());
171         } else {
172             assertFalse("Test file \"" + file + "\" should not exist.", file.exists());
173         }
174 
175         return file;
176     }
177 }