View Javadoc

1   // Copyright (C) 2007 - 2009 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
21  
22  package net.grinder.engine.common;
23  
24  import java.io.File;
25  
26  import net.grinder.testutility.AbstractFileTestCase;
27  import net.grinder.testutility.Serializer;
28  import net.grinder.util.Directory;
29  
30  
31  /**
32   * Unit tests for {@link  ScriptLocation}.
33   *
34   * @author Philip Aston
35   */
36  public class TestScriptLocation extends AbstractFileTestCase {
37  
38    public void testScriptLocation() throws Exception {
39  
40      final Directory directory = new Directory(new File("abc"));
41      final File file1 = new File("def");
42      final File file2 = new File("blah");
43      final File file3 = new File("lah/dah");
44  
45      final ScriptLocation sl1 = new ScriptLocation(directory, file1);
46      final ScriptLocation sl2 = new ScriptLocation(directory, file1);
47      final ScriptLocation sl3 = new ScriptLocation(directory, file2);
48  
49      assertEquals(sl1, sl1);
50      assertEquals(sl1, sl2);
51      assertTrue(!sl1.equals(sl3));
52      assertTrue(!sl1.equals(this));
53      assertEquals(sl1.hashCode(), sl2.hashCode());
54      assertTrue(sl1.hashCode() != sl3.hashCode());
55  
56      final ScriptLocation sl4 = Serializer.serialize(sl1);
57      assertEquals(sl1, sl4);
58  
59      final ScriptLocation sl5 = new ScriptLocation(file1);
60      assertEquals(new File("."),
61                   sl5.getDirectory().getFile());
62      assertEquals(new File(".", file1.getPath()), sl5.getFile());
63  
64      final ScriptLocation sl6 = new ScriptLocation(file3);
65      assertEquals(new File("."),
66                   sl6.getDirectory().getFile());
67      assertEquals(new File(".", file3.getPath()), sl6.getFile());
68    }
69  
70    public void testNameShortening() throws Exception {
71      final Directory directory = new Directory(getDirectory());
72      final File existentFile = new File(getDirectory(), "hello");
73      assertTrue(existentFile.createNewFile());
74      final File nonExistentFile = new File(getDirectory(), "world");
75  
76      final ScriptLocation s1 = new ScriptLocation(directory, existentFile);
77      assertEquals("hello", s1.toString());
78  
79      final ScriptLocation s2 = new ScriptLocation(directory, nonExistentFile);
80      assertEquals("world", s2.toString());
81    }
82  }