1 | // Copyright (C) 2009 Google Inc. |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // 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 com.google.caja.util; |
16 | |
17 | import java.util.Arrays; |
18 | import java.util.Collections; |
19 | import java.util.HashSet; |
20 | |
21 | import junit.framework.TestCase; |
22 | |
23 | public class AbbreviatorTest extends TestCase { |
24 | public final void testOne() { |
25 | Abbreviator a = new Abbreviator( |
26 | Collections.singleton("test:///foo/bar"), "/"); |
27 | assertEquals("bar", a.unambiguousAbbreviationFor("test:///foo/bar")); |
28 | } |
29 | |
30 | public final void testAbbreviator() { |
31 | Abbreviator a = new Abbreviator( |
32 | new HashSet<String>(Arrays.asList( |
33 | "/a/b/c", "/a/d/e", "/a/d/f", "/a/g/h/c", |
34 | "/h/i", "/h/j/", "/", "/c", "/d", "x", "")), |
35 | "/"); |
36 | assertEquals("b/c", a.unambiguousAbbreviationFor("/a/b/c")); |
37 | assertEquals("h/c", a.unambiguousAbbreviationFor("/a/g/h/c")); |
38 | assertEquals("e", a.unambiguousAbbreviationFor("/a/d/e")); |
39 | assertEquals("f", a.unambiguousAbbreviationFor("/a/d/f")); |
40 | assertEquals("i", a.unambiguousAbbreviationFor("/h/i")); |
41 | assertEquals("j/", a.unambiguousAbbreviationFor("/h/j/")); |
42 | assertEquals("/", a.unambiguousAbbreviationFor("/")); |
43 | assertEquals("/c", a.unambiguousAbbreviationFor("/c")); |
44 | assertEquals("d", a.unambiguousAbbreviationFor("/d")); |
45 | assertEquals("x", a.unambiguousAbbreviationFor("x")); |
46 | assertEquals("", a.unambiguousAbbreviationFor("")); |
47 | assertEquals("/notpresent", a.unambiguousAbbreviationFor("/notpresent")); |
48 | } |
49 | |
50 | public final void testSetContainsSuffixOfOtherMember() { |
51 | Abbreviator a = new Abbreviator( |
52 | new HashSet<String>(Arrays.asList( |
53 | "foo:bar/z", "foo:foo:baz/z", "foo:baz/z", "foo:bar:far/z")), |
54 | ":"); |
55 | assertEquals("bar/z", a.unambiguousAbbreviationFor("foo:bar/z")); |
56 | assertEquals("foo:baz/z", a.unambiguousAbbreviationFor("foo:baz/z")); |
57 | assertEquals( |
58 | "foo:foo:baz/z", a.unambiguousAbbreviationFor("foo:foo:baz/z")); |
59 | assertEquals("far/z", a.unambiguousAbbreviationFor("foo:bar:far/z")); |
60 | } |
61 | } |