1/** 2 * Copyright (c) 2008, 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 */ 16package org.yaml.snakeyaml.nodes; 17 18import java.net.URI; 19import java.net.URISyntaxException; 20 21import junit.framework.TestCase; 22 23public 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 testCompare() { 94 Tag tag = new Tag("!car"); 95 assertEquals(0, tag.compareTo(new Tag("!car"))); 96 } 97 98 public void testEqualsObject() { 99 Tag tag = new Tag("!car"); 100 assertEquals(tag, tag); 101 assertEquals(tag, new Tag("!car")); 102 assertFalse(tag.equals(new Tag("!!str"))); 103 assertFalse(tag.equals(null)); 104 assertFalse(tag.equals(25)); 105 } 106} 107