1b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov/** 211a89b445f3bde56bf07e6a0d04f0b0256dcb215Andrey Somov * Copyright (c) 2008, http://www.snakeyaml.org 3b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 4b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Licensed under the Apache License, Version 2.0 (the "License"); 5b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * you may not use this file except in compliance with the License. 6b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * You may obtain a copy of the License at 7b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 8b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * http://www.apache.org/licenses/LICENSE-2.0 9b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 10b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Unless required by applicable law or agreed to in writing, software 11b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * distributed under the License is distributed on an "AS IS" BASIS, 12b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * See the License for the specific language governing permissions and 14b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * limitations under the License. 15b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */ 16b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpackage org.yaml.snakeyaml.issues.issue133; 17b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 18b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport java.awt.Point; 19b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 20b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport junit.framework.TestCase; 21b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 22b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.Yaml; 23b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.introspector.Property; 24b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.nodes.NodeTuple; 25b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.nodes.Tag; 26b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovimport org.yaml.snakeyaml.representer.Representer; 27b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 28b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov/** 29b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * to test http://code.google.com/p/snakeyaml/issues/detail?id=133 30b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */ 31b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somovpublic class StackOverflowTest extends TestCase { 32b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public void testDumpRecursiveObject() { 33b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov try { 34b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov Yaml yaml = new Yaml(); 35b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov // by default it must fail with StackOverflow 36b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov yaml.dump(new Point()); 37b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov fail("getLocation() is recursive."); 38b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } catch (Throwable e) { 3961d96e546f0ba3593015dbcb73ad733c254a0188Andrey Somov String message = e.getMessage(); 4061d96e546f0ba3593015dbcb73ad733c254a0188Andrey Somov assertTrue("StackOverflow has no message: " + e.getMessage(), message == null 4161d96e546f0ba3593015dbcb73ad733c254a0188Andrey Somov || message.contains("Unable to find getter for property 'location'")); 42b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 43b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 44b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 45b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov /** 46b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Since Point.getLocation() creates a new instance of Point class, 47b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * SnakeYAML will fail to dump an instance of Point if 'getLocation()' is 48b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * also included. 49b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * 50b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * Since Point is not really a JavaBean, we can safely skip the recursive 51b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov * property when we dump the instance of Point. 52b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov */ 53b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov private class PointRepresenter extends Representer { 54b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 55b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov @Override 56b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, 57b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov Object propertyValue, Tag customTag) { 58b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov if (javaBean instanceof Point && "location".equals(property.getName())) { 59b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return null; 60b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } else { 61b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov return super 62b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov .representJavaBeanProperty(javaBean, property, propertyValue, customTag); 63b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 64b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 65b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 66b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov 67b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov public void testDump() { 68b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov Yaml yaml = new Yaml(new PointRepresenter()); 69b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov String output = yaml.dump(new Point()); 70b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov assertEquals("!!java.awt.Point {x: 0, y: 0}\n", output); 71b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov } 72b5f4ec3dfb1a49968ebcc1243da10af9a2dc54a2Andrey Somov} 73