01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // Unless required by applicable law or agreed to in writing, software
10: // distributed under the License is distributed on an "AS IS" BASIS,
11: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: // See the License for the specific language governing permissions and
13: // limitations under the License.
14:
15: package org.apache.tapestry.ioc.internal.util;
16:
17: import java.util.Random;
18:
19: import org.apache.tapestry.ioc.Location;
20: import org.apache.tapestry.ioc.Resource;
21: import org.apache.tapestry.ioc.test.IOCTestCase;
22: import org.testng.annotations.Test;
23:
24: public class LocationImplTest extends IOCTestCase {
25: private final Random _random = new Random();
26:
27: private final Resource _resource = new ClasspathResource(
28: "/foo/Bar.xml");
29:
30: @Test
31: public void all_three_parameters() {
32:
33: int line = _random.nextInt();
34: int column = _random.nextInt();
35:
36: Location l = new LocationImpl(_resource, line, column);
37:
38: assertSame(l.getResource(), _resource);
39: assertEquals(l.getLine(), line);
40: assertEquals(l.getColumn(), column);
41:
42: assertEquals(l.toString(), String.format(
43: "%s, line %d, column %d", _resource, line, column));
44: }
45:
46: @Test
47: public void unknown_column() {
48: int line = _random.nextInt();
49:
50: Location l = new LocationImpl(_resource, line);
51:
52: assertSame(l.getResource(), _resource);
53: assertEquals(l.getLine(), line);
54: assertEquals(l.getColumn(), -1);
55:
56: assertEquals(l.toString(), String.format("%s, line %d",
57: _resource, line));
58: }
59:
60: @Test
61: public void unknown_line_and_column() {
62: Location l = new LocationImpl(_resource);
63:
64: assertSame(l.getResource(), _resource);
65: assertEquals(l.getLine(), -1);
66: assertEquals(l.getColumn(), -1);
67:
68: assertEquals(l.toString(), _resource.toString());
69: }
70:
71: @Test
72: public void equality() {
73: Location l1 = new LocationImpl(_resource, 22, 7);
74: Location l2 = new LocationImpl(_resource, 22, 7);
75: Location l3 = new LocationImpl(null, 22, 7);
76: Location l4 = new LocationImpl(_resource, 99, 7);
77: Location l5 = new LocationImpl(_resource, 22, 99);
78: Location l6 = new LocationImpl(new ClasspathResource(
79: "/baz/Biff.txt"), 22, 7);
80:
81: assertEquals(l1, l1);
82: assertFalse(l1.equals(null));
83:
84: assertEquals(l1, l2);
85: assertEquals(l2.hashCode(), l1.hashCode());
86:
87: assertFalse(l3.equals(l1));
88: assertFalse(l3.hashCode() == l1.hashCode());
89:
90: assertFalse(l4.equals(l1));
91: assertFalse(l4.hashCode() == l1.hashCode());
92:
93: assertFalse(l5.equals(l1));
94: assertFalse(l5.hashCode() == l1.hashCode());
95:
96: assertFalse(l6.equals(l1));
97: assertFalse(l6.hashCode() == l1.hashCode());
98: }
99: }
|