View Javadoc

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