1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. 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 distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.yaml.snakeyaml.representer; 15 16 import junit.framework.TestCase; 17 import org.yaml.snakeyaml.DumperOptions; 18 import org.yaml.snakeyaml.Yaml; 19 20 public class RepresenterTest extends TestCase { 21 testRepresenter()22 public void testRepresenter() { 23 MyBean bean = new MyBean(); 24 bean.setName("Gnome"); 25 bean.setValid(true); 26 bean.setPrimitive(true); 27 Yaml yaml = new Yaml(); 28 assertEquals( 29 "!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean {name: Gnome, primitive: true}\n", 30 yaml.dump(bean)); 31 } 32 33 public static class MyBean { 34 35 private String name; 36 private Boolean valid; 37 private boolean primitive; 38 getName()39 public String getName() { 40 return name; 41 } 42 setName(String name)43 public void setName(String name) { 44 this.name = name; 45 } 46 isValid()47 public Boolean isValid() { 48 return valid; 49 } 50 setValid(Boolean valid)51 public void setValid(Boolean valid) { 52 this.valid = valid; 53 } 54 isPrimitive()55 public boolean isPrimitive() { 56 return primitive; 57 } 58 setPrimitive(boolean primitive)59 public void setPrimitive(boolean primitive) { 60 this.primitive = primitive; 61 } 62 } 63 testRepresenterNoConstructorAvailable()64 public void testRepresenterNoConstructorAvailable() { 65 MyBean2 bean = new MyBean2("Gnome", true); 66 DumperOptions options = new DumperOptions(); 67 options.setAllowReadOnlyProperties(true); 68 Yaml yaml = new Yaml(options); 69 assertEquals("!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean2 {valid: true}\n", 70 yaml.dump(bean)); 71 } 72 73 public static class MyBean2 { 74 75 private String name; 76 private Boolean valid; 77 MyBean2(String name, Boolean valid)78 public MyBean2(String name, Boolean valid) { 79 this(); 80 this.name = name; 81 this.valid = valid; 82 } 83 MyBean2()84 private MyBean2() { 85 super(); 86 } 87 getName()88 private String getName() { 89 return name; 90 } 91 getValid()92 public Boolean getValid() { 93 return valid; 94 } 95 96 @Override toString()97 public String toString() { 98 return getName() + " " + getValid(); 99 } 100 } 101 testRepresenterGetterWithException()102 public void testRepresenterGetterWithException() { 103 MyBean3 bean = new MyBean3("Gnome", false); 104 DumperOptions options = new DumperOptions(); 105 options.setAllowReadOnlyProperties(true); 106 Yaml yaml = new Yaml(options); 107 try { 108 String str = yaml.dump(bean); 109 fail("Exception must be reported: " + str); 110 } catch (Exception e) { 111 assertTrue(true); 112 } 113 // no exception 114 MyBean3 bean2 = new MyBean3("Gnome", true); 115 String str = yaml.dump(bean2); 116 // isValid is no JavaBean property (it must be a primitive then) 117 assertEquals("isValid property must not be dumped.", 118 "!!org.yaml.snakeyaml.representer.RepresenterTest$MyBean3 {boolProperty: true, name: Gnome}\n", 119 str); 120 } 121 122 public static class MyBean3 { 123 124 private final String name; 125 private final Boolean valid; 126 private final boolean boolProperty; 127 MyBean3(String name, Boolean valid)128 public MyBean3(String name, Boolean valid) { 129 this.name = name; 130 this.valid = valid; 131 boolProperty = true; 132 } 133 getName()134 public String getName() { 135 if (valid) { 136 return name; 137 } else { 138 throw new UnsupportedOperationException("Test."); 139 } 140 } 141 isValid()142 public Boolean isValid() { 143 return valid; 144 } 145 isBoolProperty()146 public boolean isBoolProperty() { 147 return boolProperty; 148 } 149 150 @Override toString()151 public String toString() { 152 return "MyBean3<" + name + ", " + isValid() + ">"; 153 } 154 } 155 testRepresenterAddNull()156 public void testRepresenterAddNull() { 157 Representer representer = new Representer(); 158 try { 159 representer.addClassTag(EmptyBean.class, null); 160 fail("Tag must be provided."); 161 } catch (Exception e) { 162 assertEquals("Tag must be provided.", e.getMessage()); 163 } 164 } 165 testRepresenterEmptyBean()166 public void testRepresenterEmptyBean() { 167 EmptyBean bean = new EmptyBean(); 168 Yaml yaml = new Yaml(); 169 try { 170 yaml.dump(bean); 171 fail("EmptyBean has empty representation."); 172 } catch (Exception e) { 173 assertEquals( 174 "No JavaBean properties found in org.yaml.snakeyaml.representer.RepresenterTest$EmptyBean", 175 e.getMessage()); 176 } 177 } 178 179 public static class EmptyBean { 180 181 private int number; 182 process()183 public void process() { 184 number += 1; 185 } 186 obtain()187 public int obtain() { 188 return number; 189 } 190 } 191 } 192