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 */ 16 package org.yaml.snakeyaml.representer; 17 18 import junit.framework.TestCase; 19 20 import org.yaml.snakeyaml.Yaml; 21 22 public class RepresentFieldTest extends TestCase { 23 24 public void testRepresent1() { 25 Yaml yaml = new Yaml(); 26 WrongJavaBean bean = new WrongJavaBean(); 27 bean.packageField = "Value";// the field is present 28 bean.publicField = "Michael Jackson"; 29 WrongJavaBean.staticField = "Another value"; 30 String output = yaml.dump(bean); 31 assertEquals( 32 "!!org.yaml.snakeyaml.representer.WrongJavaBean {publicField: Michael Jackson}\n", 33 output); 34 } 35 36 public void testWrongNotPublicField() { 37 Yaml yaml = new Yaml(); 38 WrongJavaBean bean = new WrongJavaBean(); 39 bean.packageField = "Value";// the field is present 40 try { 41 yaml.load("!!org.yaml.snakeyaml.representer.WrongJavaBean {packageField: Gnome}\n"); 42 fail("Only public fields can be used."); 43 } catch (Exception e) { 44 // TODO improve the error message - the pointer should be at the 45 // property name, not value 46 assertTrue(e.getMessage().startsWith( 47 "Cannot create property=packageField for JavaBean=WrongJavaBean")); 48 assertEquals( 49 "Unable to find property 'packageField' on class: org.yaml.snakeyaml.representer.WrongJavaBean", 50 e.getCause().getMessage()); 51 } 52 } 53 54 public void testStaticField() { 55 Yaml yaml = new Yaml(); 56 WrongJavaBean.staticField = "Value";// the field is present 57 try { 58 yaml.load("!!org.yaml.snakeyaml.representer.WrongJavaBean {staticField: Gnome}\n"); 59 fail("Static fields cannot be used."); 60 } catch (Exception e) { 61 // TODO improve the error message - the pointer should be at the 62 // property name, not value 63 assertTrue(e.getMessage().startsWith( 64 "Cannot create property=staticField for JavaBean=WrongJavaBean")); 65 assertEquals( 66 "Unable to find property 'staticField' on class: org.yaml.snakeyaml.representer.WrongJavaBean", 67 e.getCause().getMessage()); 68 } 69 } 70 } 71