View Javadoc

1   /**
2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.yaml.snakeyaml.nodes;
18  
19  import java.net.URI;
20  import java.net.URISyntaxException;
21  
22  import junit.framework.TestCase;
23  
24  public class TagTest extends TestCase {
25  
26      public void testCreate() {
27          try {
28              new Tag((String) null);
29              fail();
30          } catch (Exception e) {
31              assertEquals("Tag must be provided.", e.getMessage());
32          }
33          try {
34              new Tag("");
35              fail();
36          } catch (Exception e) {
37              assertEquals("Tag must not be empty.", e.getMessage());
38          }
39          try {
40              new Tag("!Dice ");
41              fail();
42          } catch (Exception e) {
43              assertEquals("Tag must not contain leading or trailing spaces.", e.getMessage());
44          }
45          Tag tag = new Tag(TagTest.class);
46          assertEquals(Tag.PREFIX + "org.yaml.snakeyaml.nodes.TagTest", tag.getValue());
47      }
48  
49      public void testCreate2() {
50          try {
51              new Tag((Class<?>) null);
52              fail();
53          } catch (Exception e) {
54              assertEquals("Class for tag must be provided.", e.getMessage());
55          }
56      }
57  
58      public void testGetClassName() {
59          Tag tag = new Tag(Tag.PREFIX + "org.yaml.snakeyaml.nodes.TagTest");
60          assertEquals("org.yaml.snakeyaml.nodes.TagTest", tag.getClassName());
61      }
62  
63      public void testGetClassNameError() {
64          try {
65              Tag tag = new Tag("!TagTest");
66              tag.getClassName();
67              fail("Class name is only available for global tag");
68          } catch (Exception e) {
69              assertEquals("Invalid tag: !TagTest", e.getMessage());
70          }
71      }
72  
73      public void testLength() {
74          String t = Tag.PREFIX + "org.yaml.snakeyaml.nodes.TagTest";
75          Tag tag = new Tag(t);
76          assertEquals(t.length(), tag.getLength());
77      }
78  
79      public void testToString() {
80          Tag tag = new Tag("!car");
81          assertEquals("!car", tag.toString());
82      }
83  
84      public void testUri1() {
85          Tag tag = new Tag("!Académico");
86          assertEquals("!Acad%C3%A9mico", tag.toString());
87      }
88  
89      public void testUri2() {
90          Tag tag = new Tag("!ruby/object:Test::Module::Sub2");
91          assertEquals("!ruby/object:Test::Module::Sub2", tag.getValue());
92      }
93  
94      public void testUri3() throws URISyntaxException {
95          Tag tag = new Tag(new URI("!!java/javabean:foo.Bar"));
96          assertEquals("!!java/javabean:foo.Bar", tag.toString());
97      }
98  
99      public void testNullUri() throws URISyntaxException {
100         try {
101             new Tag((URI) null);
102             fail("URI for tag must not be null.");
103         } catch (Exception e) {
104             assertEquals("URI for tag must be provided.", e.getMessage());
105         }
106     }
107 
108     public void testCompare() {
109         Tag tag = new Tag("!car");
110         assertEquals(0, tag.compareTo(new Tag("!car")));
111     }
112 
113     public void testEqualsObject() {
114         Tag tag = new Tag("!car");
115         assertEquals(tag, tag);
116         // TODO should be removed when tags as Strings are not used (2.0?)
117         assertTrue("Temporarily  allow compare Tag and String.", tag.equals("!car"));
118         assertFalse("Temporarily  allow compare Tag and String.", tag.equals("!foo"));
119         assertEquals(tag, new Tag("!car"));
120         assertFalse(tag.equals(new Tag("!!str")));
121         assertFalse(tag.equals(null));
122         assertFalse(tag.equals(25));
123     }
124 }