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