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.issues.issue133; 17 18 import java.awt.Point; 19 20 import junit.framework.TestCase; 21 22 import org.yaml.snakeyaml.Yaml; 23 import org.yaml.snakeyaml.introspector.Property; 24 import org.yaml.snakeyaml.nodes.NodeTuple; 25 import org.yaml.snakeyaml.nodes.Tag; 26 import org.yaml.snakeyaml.representer.Representer; 27 28 /** 29 * to test http://code.google.com/p/snakeyaml/issues/detail?id=133 30 */ 31 public class StackOverflowTest extends TestCase { 32 public void testDumpRecursiveObject() { 33 try { 34 Yaml yaml = new Yaml(); 35 // by default it must fail with StackOverflow 36 yaml.dump(new Point()); 37 fail("getLocation() is recursive."); 38 } catch (Throwable e) { 39 String message = e.getMessage(); 40 assertTrue("StackOverflow has no message: " + e.getMessage(), message == null 41 || message.contains("Unable to find getter for property 'location'")); 42 } 43 } 44 45 /** 46 * Since Point.getLocation() creates a new instance of Point class, 47 * SnakeYAML will fail to dump an instance of Point if 'getLocation()' is 48 * also included. 49 * 50 * Since Point is not really a JavaBean, we can safely skip the recursive 51 * property when we dump the instance of Point. 52 */ 53 private class PointRepresenter extends Representer { 54 55 @Override 56 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, 57 Object propertyValue, Tag customTag) { 58 if (javaBean instanceof Point && "location".equals(property.getName())) { 59 return null; 60 } else { 61 return super 62 .representJavaBeanProperty(javaBean, property, propertyValue, customTag); 63 } 64 } 65 } 66 67 public void testDump() { 68 Yaml yaml = new Yaml(new PointRepresenter()); 69 String output = yaml.dump(new Point()); 70 assertEquals("!!java.awt.Point {x: 0, y: 0}\n", output); 71 } 72 } 73