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 java.util.Set; 17 import java.util.TreeSet; 18 import junit.framework.TestCase; 19 import org.yaml.snakeyaml.DumperOptions; 20 import org.yaml.snakeyaml.TypeDescription; 21 import org.yaml.snakeyaml.Yaml; 22 import org.yaml.snakeyaml.introspector.Property; 23 24 public class FilterPropertyToDumpTest extends TestCase { 25 testFilterPropertyInJavaBeanDumper()26 public void testFilterPropertyInJavaBeanDumper() { 27 BeanToRemoveProperty bean = new BeanToRemoveProperty(); 28 bean.setNumber(24); 29 bean.setId("ID124"); 30 Yaml d = new Yaml(); 31 String dump = d.dumpAsMap(bean); 32 // System.out.println(dump); 33 assertEquals("id: ID124\nnumber: 24\n", dump); 34 } 35 testFilterPropertyInYaml()36 public void testFilterPropertyInYaml() { 37 BeanToRemoveProperty bean = new BeanToRemoveProperty(); 38 bean.setNumber(25); 39 bean.setId("ID125"); 40 Yaml yaml = new Yaml(new MyRepresenter()); 41 String dump = yaml.dumpAsMap(bean); 42 // System.out.println(dump); 43 assertEquals("number: 25\n", dump); 44 } 45 testDoNotFilterPropertyIncludeReadOnly()46 public void testDoNotFilterPropertyIncludeReadOnly() { 47 BeanToRemoveProperty bean = new BeanToRemoveProperty(); 48 bean.setNumber(26); 49 bean.setId("ID126"); 50 DumperOptions options = new DumperOptions(); 51 options.setAllowReadOnlyProperties(true); 52 Yaml yaml = new Yaml(options); 53 String dump = yaml.dump(bean); 54 // System.out.println(dump); 55 assertEquals( 56 "!!org.yaml.snakeyaml.representer.FilterPropertyToDumpTest$BeanToRemoveProperty {id: ID126,\n number: 26, something: true}\n", 57 dump); 58 } 59 testFilterPropertyWithTypeDesciptionIncludes()60 public void testFilterPropertyWithTypeDesciptionIncludes() { 61 BeanToRemoveProperty bean = new BeanToRemoveProperty(); 62 bean.setNumber(27); 63 bean.setId("ID127"); 64 Yaml yaml = new Yaml(); 65 TypeDescription td = new TypeDescription(BeanToRemoveProperty.class); 66 td.setIncludes("number"); 67 yaml.addTypeDescription(td); 68 String dump = yaml.dump(bean); 69 // System.out.println(dump); 70 assertEquals( 71 "!!org.yaml.snakeyaml.representer.FilterPropertyToDumpTest$BeanToRemoveProperty {number: 27}\n", 72 dump); 73 } 74 testFilterPropertyWithTypeDesciptionExcludes()75 public void testFilterPropertyWithTypeDesciptionExcludes() { 76 BeanToRemoveProperty bean = new BeanToRemoveProperty(); 77 bean.setNumber(28); 78 bean.setId("ID128"); 79 Yaml yaml = new Yaml(); 80 TypeDescription td = new TypeDescription(BeanToRemoveProperty.class); 81 td.setExcludes("id"); 82 yaml.addTypeDescription(td); 83 String dump = yaml.dump(bean); 84 // System.out.println(dump); 85 assertEquals( 86 "!!org.yaml.snakeyaml.representer.FilterPropertyToDumpTest$BeanToRemoveProperty {number: 28}\n", 87 dump); 88 } 89 90 91 public class BeanToRemoveProperty { 92 93 private int number; 94 private String id; 95 isSomething()96 public boolean isSomething() { 97 return true; 98 } 99 getNumber()100 public int getNumber() { 101 return number; 102 } 103 setNumber(int number)104 public void setNumber(int number) { 105 this.number = number; 106 } 107 setId(String id)108 public void setId(String id) { 109 this.id = id; 110 } 111 getId()112 public String getId() { 113 return id; 114 } 115 } 116 117 private class MyRepresenter extends Representer { 118 119 @Override getProperties(Class<? extends Object> type)120 protected Set<Property> getProperties(Class<? extends Object> type) { 121 Set<Property> set = super.getProperties(type); 122 Set<Property> filtered = new TreeSet<Property>(); 123 if (type.equals(BeanToRemoveProperty.class)) { 124 // filter properties 125 for (Property prop : set) { 126 String name = prop.getName(); 127 if (!name.equals("id")) { 128 filtered.add(prop); 129 } 130 } 131 } 132 return filtered; 133 } 134 } 135 } 136