Add FilterPropertyToDumpTest to show how to filter JavaBean properties
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index d59fa8b..b782d98 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -8,6 +8,9 @@
<body>
<release version="1.6-SNAPSHOT" date="in Mercurial" description="development">
<action dev="py4fun" type="add">
+ Add FilterPropertyToDumpTest to show how to filter JavaBean properties (2009-11-24)
+ </action>
+ <action dev="py4fun" type="add">
Add FilterClassesConstructorTest to show how to filter created classes (2009-11-16)
</action>
<action dev="py4fun" type="update" due-to="Stefan">
diff --git a/src/test/java/org/yaml/snakeyaml/representer/FilterPropertyToDumpTest.java b/src/test/java/org/yaml/snakeyaml/representer/FilterPropertyToDumpTest.java
new file mode 100644
index 0000000..d0bd9bd
--- /dev/null
+++ b/src/test/java/org/yaml/snakeyaml/representer/FilterPropertyToDumpTest.java
@@ -0,0 +1,81 @@
+/**
+ * Copyright (c) 2008-2009 Andrey Somov
+ *
+ * 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.Dumper;
+import org.yaml.snakeyaml.DumperOptions;
+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);
+ Dumper dumper = new Dumper(new MyRepresenter(), new DumperOptions());
+ Yaml yaml = new Yaml(dumper);
+ String dump = yaml.dump(bean);
+ // System.out.println(dump);
+ assertEquals(
+ "!!org.yaml.snakeyaml.representer.FilterPropertyToDumpTest$BeanToRemoveProperty {number: 24}\n",
+ dump);
+ // include by default
+ yaml = new Yaml();
+ 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)) {
+ for (Property prop : set) {
+ if (prop.getName().equals("setTestCase")) {
+ set.remove(prop);
+ break;
+ }
+ }
+ }
+ return set;
+ }
+ }
+}