View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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  package org.yaml.snakeyaml.nodes;
17  
18  import java.net.URI;
19  import java.net.URISyntaxException;
20  
21  import junit.framework.TestCase;
22  
23  public class TagTest extends TestCase {
24  
25      public void testCreate() {
26          try {
27              new Tag((String) null);
28              fail();
29          } catch (Exception e) {
30              assertEquals("Tag must be provided.", e.getMessage());
31          }
32          try {
33              new Tag("");
34              fail();
35          } catch (Exception e) {
36              assertEquals("Tag must not be empty.", e.getMessage());
37          }
38          try {
39              new Tag("!Dice ");
40              fail();
41          } catch (Exception e) {
42              assertEquals("Tag must not contain leading or trailing spaces.", e.getMessage());
43          }
44          Tag tag = new Tag(TagTest.class);
45          assertEquals(Tag.PREFIX + "org.yaml.snakeyaml.nodes.TagTest", tag.getValue());
46      }
47  
48      public void testCreate2() {
49          try {
50              new Tag((Class<?>) null);
51              fail();
52          } catch (Exception e) {
53              assertEquals("Class for tag must be provided.", e.getMessage());
54          }
55      }
56  
57      public void testGetClassName() {
58          Tag tag = new Tag(Tag.PREFIX + "org.yaml.snakeyaml.nodes.TagTest");
59          assertEquals("org.yaml.snakeyaml.nodes.TagTest", tag.getClassName());
60      }
61  
62      public void testGetClassNameError() {
63          try {
64              Tag tag = new Tag("!TagTest");
65              tag.getClassName();
66              fail("Class name is only available for global tag");
67          } catch (Exception e) {
68              assertEquals("Invalid tag: !TagTest", e.getMessage());
69          }
70      }
71  
72      public void testLength() {
73          String t = Tag.PREFIX + "org.yaml.snakeyaml.nodes.TagTest";
74          Tag tag = new Tag(t);
75          assertEquals(t.length(), tag.getLength());
76      }
77  
78      public void testToString() {
79          Tag tag = new Tag("!car");
80          assertEquals("!car", tag.toString());
81      }
82  
83      public void testUri1() {
84          Tag tag = new Tag("!Académico");
85          assertEquals("!Acad%C3%A9mico", tag.toString());
86      }
87  
88      public void testUri2() {
89          Tag tag = new Tag("!ruby/object:Test::Module::Sub2");
90          assertEquals("!ruby/object:Test::Module::Sub2", tag.getValue());
91      }
92  
93      public void testUri3() throws URISyntaxException {
94          Tag tag = new Tag(new URI("!!java/javabean:foo.Bar"));
95          assertEquals("!!java/javabean:foo.Bar", tag.toString());
96      }
97  
98      public void testNullUri() throws URISyntaxException {
99          try {
100             new Tag((URI) null);
101             fail("URI for tag must not be null.");
102         } catch (Exception e) {
103             assertEquals("URI for tag must be provided.", e.getMessage());
104         }
105     }
106 
107     public void testCompare() {
108         Tag tag = new Tag("!car");
109         assertEquals(0, tag.compareTo(new Tag("!car")));
110     }
111 
112     public void testEqualsObject() {
113         Tag tag = new Tag("!car");
114         assertEquals(tag, tag);
115         // TODO should be removed when tags as Strings are not used (2.0?)
116         assertTrue("Temporarily  allow compare Tag and String.", tag.equals("!car"));
117         assertFalse("Temporarily  allow compare Tag and String.", tag.equals("!foo"));
118         assertEquals(tag, new Tag("!car"));
119         assertFalse(tag.equals(new Tag("!!str")));
120         assertFalse(tag.equals(null));
121         assertFalse(tag.equals(25));
122     }
123 }