/**
 * Copyright (c) 2008-2011, http://www.snakeyaml.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yaml.snakeyaml.representer;

import java.beans.IntrospectionException;
import java.util.Set;

import junit.framework.TestCase;

import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.JavaBeanDumper;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.introspector.Property;

public class FilterPropertyToDumpTest extends TestCase {

    public void testFilterProperty() {
        BeanToRemoveProperty bean = new BeanToRemoveProperty();
        bean.setNumber(24);
        JavaBeanDumper d = new JavaBeanDumper(new MyRepresenter(), new DumperOptions());
        String dump = d.dump(bean);
        // System.out.println(dump);
        assertEquals(
                "!!org.yaml.snakeyaml.representer.FilterPropertyToDumpTest$BeanToRemoveProperty {number: 24}\n",
                dump);
    }

    public void testFilterProperty2() {
        BeanToRemoveProperty bean = new BeanToRemoveProperty();
        bean.setNumber(24);
        Yaml yaml = new Yaml(new MyRepresenter());
        String dump = yaml.dump(bean);
        // System.out.println(dump);
        assertEquals(
                "!!org.yaml.snakeyaml.representer.FilterPropertyToDumpTest$BeanToRemoveProperty {number: 24}\n",
                dump);
        // include by default
        DumperOptions options = new DumperOptions();
        options.setAllowReadOnlyProperties(true);
        yaml = new Yaml(options);
        dump = yaml.dump(bean);
        // System.out.println(dump);
        assertEquals(
                "!!org.yaml.snakeyaml.representer.FilterPropertyToDumpTest$BeanToRemoveProperty {number: 24,\n  setTestCase: true}\n",
                dump);
    }

    public class BeanToRemoveProperty {
        private int number;

        public boolean isSetTestCase() {
            return true;
        }

        public int getNumber() {
            return number;
        }

        public void setNumber(int number) {
            this.number = number;
        }
    }

    private class MyRepresenter extends Representer {
        @Override
        protected Set<Property> getProperties(Class<? extends Object> type)
                throws IntrospectionException {
            Set<Property> set = super.getProperties(type);
            if (type.equals(BeanToRemoveProperty.class)) {
                // drop setTestCase property
                for (Property prop : set) {
                    if (prop.getName().equals("setTestCase")) {
                        set.remove(prop);
                        break;
                    }
                }
            }
            return set;
        }
    }
}

@@ -737,16 +737,43 @@
 il.Set;%0D
+%0Aimport java.util.TreeSet;%0D
 %0A%0D%0Aimpor
@@ -1067,16 +1067,32 @@
 Property
+InJavaBeanDumper
 () %7B%0D%0A  
@@ -1175,32 +1175,62 @@
 setNumber(24);%0D%0A
+        bean.setId(%22ID124%22);%0D%0A
         JavaBean
@@ -1582,17 +1582,22 @@
 Property
-2
+InYaml
 () %7B%0D%0A  
@@ -1675,33 +1675,63 @@
 bean.setNumber(2
-4
+5);%0D%0A        bean.setId(%22ID125%22
 );%0D%0A        Yaml
@@ -1969,33 +1969,33 @@
 perty %7Bnumber: 2
-4
+5
 %7D%5Cn%22,%0D%0A         
@@ -2017,33 +2017,195 @@
     
-    // include by default
+%7D%0D%0A%0D%0A    public void testDoNotFilterPropertyIncludeReadOnly() %7B%0D%0A        BeanToRemoveProperty bean = new BeanToRemoveProperty();%0D%0A        bean.setNumber(26);%0D%0A        bean.setId(%22ID126%22);
 %0D%0A  
@@ -2315,16 +2315,21 @@
         
+Yaml 
 yaml = n
@@ -2351,24 +2351,31 @@
 );%0D%0A        
+String 
 dump = yaml.
@@ -2549,34 +2549,43 @@
 ty %7B
-number: 24,%5Cn  setTestCase
+id: ID126,%5Cn  number: 26, something
 : tr
@@ -2695,16 +2695,44 @@
 number;%0D
+%0A        private String id;%0D
 %0A%0D%0A     
@@ -2752,26 +2752,24 @@
 lean isS
-etTestCase
+omething
 () %7B%0D%0A  
@@ -2962,32 +2962,182 @@
 ber;%0D%0A        %7D%0D
+%0A%0D%0A        public void setId(String id) %7B%0D%0A            this.id = id;%0D%0A        %7D%0D%0A%0D%0A        public String getId() %7B%0D%0A            return id;%0D%0A        %7D%0D
 %0A    %7D%0D%0A%0D%0A    pr
@@ -3386,24 +3386,87 @@
 ies(type);%0D%0A
+            Set%3CProperty%3E filtered = new TreeSet%3CProperty%3E();%0D%0A
             
@@ -3536,32 +3536,22 @@
  // 
-drop setTestCase
+filter
  propert
 y%0D%0A 
@@ -3546,17 +3546,19 @@
  propert
-y
+ies
 %0D%0A      
@@ -3620,94 +3620,84 @@
     
-if (prop.getName().equals(%22setTestCase%22)) %7B%0D%0A                        set.remove(prop);
+String name = prop.getName();%0D%0A                    if (!name.equals(%22id%22)) %7B
 %0D%0A  
@@ -3722,13 +3722,26 @@
     
-break
+filtered.add(prop)
 ;%0D%0A 
@@ -3815,19 +3815,24 @@
  return 
-set
+filtered
 ;%0D%0A     

Refactor test