1   ////////////////////////////////////////////////////////////////////////////////
2   //
3   //                  ObjectLab is sponsoring QALab
4   // 
5   // Based in London, we are world leaders in the design and development 
6   // of bespoke applications for the Securities Financing markets.
7   // 
8   // <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
9   //           ___  _     _           _   _          _
10  //          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
11  //         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
12  //         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
13  //          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
14  //                   |__/
15  //
16  //                   http://www.ObjectLab.co.uk
17  // ---------------------------------------------------------------------------
18  //
19  //QALab is released under the GNU General Public License.
20  //
21  //QALab: Collects QA Statistics from your build over time.
22  //2005+, ObjectLab Ltd
23  //
24  //This library is free software; you can redistribute it and/or
25  //modify it under the terms of the GNU General Public
26  //License as published by the Free Software Foundation; either
27  //version 2.1 of the License, or (at your option) any later version.
28  //
29  //This library is distributed in the hope that it will be useful,
30  //but WITHOUT ANY WARRANTY; without even the implied warranty of
31  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
32  //General Public License for more details.
33  //
34  //You should have received a copy of the GNU General Public
35  //License along with this library; if not, write to the Free Software
36  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
37  //
38  ////////////////////////////////////////////////////////////////////////////////
39  package net.objectlab.qalab.util;
40  
41  import junit.framework.TestCase;
42  
43  /**
44   * Test for Utility class for file name manipulations.
45   *
46   * @author Dave Sag
47   * @author Benoit Xhenseval
48   */
49  public class FilenameUtilTest extends TestCase {
50      /**
51       * Test trimFilename with a unix style path and relative src dir ending
52       * in a slash.
53       */
54      public void testTrimFilenameUnixPathsRelativeSrcEndsWithSlash() {
55          final String theSrcDir = "tester/src/";
56          final String theFilePath = "/Users/tester/src/main/java/com/test/SomeTest.java";
57          validate(theSrcDir, theFilePath);
58      }
59  
60      /**
61       * Test trimFilename with a unix style path and relative src dir not
62       * ending in a slash.
63       */
64      public void testTrimFilenameUnixPathsRelativeSrcEndsNoSlash() {
65          final String theSrcDir = "tester/src";
66          final String theFilePath = "/Users/tester/src/main/java/com/test/SomeTest.java";
67          validate(theSrcDir, theFilePath);
68      }
69  
70      /**
71       * Test trimFilename with a unix style path and src dir ending in a slash.
72       */
73      public void testTrimFilenameUnixPathsSrcEndsWithSlash() {
74          final String theSrcDir = "/Users/tester/src/";
75          final String theFilePath = "/Users/tester/src/main/java/com/test/SomeTest.java";
76          validate(theSrcDir, theFilePath);
77      }
78  
79      /**
80       * Test trimFilename with a unix style path and src dir 
81       * that is not ending in a slash.
82       */
83      public void testTrimFilenameUnixPathsSrcNotEndsWithSlash() {
84          final String theSrcDir = "/Users/tester/src";
85          final String theFilePath = "/Users/tester/src/main/java/com/test/SomeTest.java";
86          validate(theSrcDir, theFilePath);
87      }
88  
89      /**
90       * Test trimFilename with a windows style path and src dir
91       * ending in a slash.
92       */
93      public void testTrimFilenameWindozePathsSrcEndsWithSlash() {
94          final String theSrcDir = "C:\\src\\";
95          final String theFilePath = "C:\\src\\main\\java\\com\\test\\SomeTest.java";
96          validate(theSrcDir, theFilePath);
97      }
98  
99      /**
100      * Test trimFilename with a windows style path and src dir
101      * that is not ending in a slash.
102      */
103     public void testTrimFilenameWindozePathsSrcNotEndsWithSlash() {
104         final String theSrcDir = "C:\\src\\";
105         final String theFilePath = "C:\\src\\main\\java\\com\\test\\SomeTest.java";
106         validate(theSrcDir, theFilePath);
107     }
108     
109     /**
110      * Test trimFilename with a path that it should leave untouched.
111      */
112     public void testTrimFilenameNoChange() {
113         final String theSrcDir = "src/main/java";
114         final String theFilePath = "com/test/SomeTest.java";
115         assertEquals("no change",theFilePath,FilenameUtil.trimFilename(theFilePath, theSrcDir));
116     }
117     
118     private void validate(final String srcdir, final String file) {
119         final String expected = "main/java/com/test/SomeTest.java";
120         final String actual = FilenameUtil.trimFilename(file, srcdir);
121         assertEquals("trimmed filename was wrong.\noriginal path '"
122                 + file
123                 + "',\nsrc dir '"
124                 + srcdir
125                 + "',\nactual result '"
126                 + actual + "'.",
127                 expected, actual);    
128     }
129 }
130 /*
131  *                   ObjectLab is sponsoring QALab
132  * 
133  * Based in London, we are world leaders in the design and development 
134  * of bespoke applications for the securities financing markets.
135  * 
136  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
137  *           ___  _     _           _   _          _
138  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
139  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
140  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
141  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
142  *                   |__/
143  *
144  *                     www.ObjectLab.co.uk
145  */