1b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov/** 211a89b445f3bde56bf07e6a0d04f0b0256dcb215Andrey Somov * Copyright (c) 2008, http://www.snakeyaml.org 3b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * 4b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * Licensed under the Apache License, Version 2.0 (the "License"); 5b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * you may not use this file except in compliance with the License. 6b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * You may obtain a copy of the License at 7b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * 8b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * http://www.apache.org/licenses/LICENSE-2.0 9b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * 10b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * Unless required by applicable law or agreed to in writing, software 11b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * distributed under the License is distributed on an "AS IS" BASIS, 12b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * See the License for the specific language governing permissions and 14b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov * limitations under the License. 15b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov */ 16b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovpackage org.yaml.snakeyaml.issues.issue73; 17b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 18b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovimport java.util.Set; 19b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovimport java.util.TreeSet; 20b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 21b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovimport junit.framework.TestCase; 22b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 23b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovimport org.yaml.snakeyaml.DumperOptions; 242147e7b9c8ff8eecaf630793c68e0085531b1d53Andrey Somovimport org.yaml.snakeyaml.DumperOptions.FlowStyle; 25b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovimport org.yaml.snakeyaml.Util; 26def9635e03df5f547cda12693af495d24d1b0a5bAndrey Somovimport org.yaml.snakeyaml.Yaml; 27b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovimport org.yaml.snakeyaml.introspector.BeanAccess; 28fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somovimport org.yaml.snakeyaml.nodes.Node; 29fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somovimport org.yaml.snakeyaml.nodes.Tag; 30fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somovimport org.yaml.snakeyaml.representer.Represent; 31b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovimport org.yaml.snakeyaml.representer.Representer; 32b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 33b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somovpublic class DumpSetAsSequenceExampleTest extends TestCase { 34b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 35b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov public void testDumpFlow() { 36b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov DumperOptions options = new DumperOptions(); 37b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov options.setAllowReadOnlyProperties(true); 384efdcdc34840aa22eb58e536d4f111b17b13a4c0Andrey Somov Yaml yaml = new Yaml(new SetRepresenter(), options); 39b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov String output = yaml.dump(createBlog()); 40b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov // System.out.println(output); 41b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov assertEquals(Util.getLocalResource("issues/issue73-dump7.txt"), output); 42b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov // 43b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov check(output); 44b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov } 45b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 46b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov public void testDumpBlock() { 47b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov DumperOptions options = new DumperOptions(); 48b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov options.setAllowReadOnlyProperties(true); 49b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov options.setDefaultFlowStyle(FlowStyle.BLOCK); 504efdcdc34840aa22eb58e536d4f111b17b13a4c0Andrey Somov Yaml yaml = new Yaml(new SetRepresenter(), options); 51b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov String output = yaml.dump(createBlog()); 52b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov // System.out.println(output); 53b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov assertEquals(Util.getLocalResource("issues/issue73-dump8.txt"), output); 54b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov // 55b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov check(output); 56b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov } 57b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 58b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov private class SetRepresenter extends Representer { 59b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov public SetRepresenter() { 60fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov this.multiRepresenters.put(Set.class, new RepresentIterable()); 61fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov } 62fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov 63fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov private class RepresentIterable implements Represent { 64fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov @SuppressWarnings("unchecked") 65fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov public Node representData(Object data) { 66fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov return representSequence(getTag(data.getClass(), Tag.SEQ), (Iterable<Object>) data, 67fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov null); 68fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov 69fe23edf49ea1ddb80981ec1c300a8d41b08833edAndrey Somov } 70b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov } 71b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov } 72b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 73b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov private Blog createBlog() { 74b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov Blog blog = new Blog("Test Me!"); 75b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov blog.addPost(new Post("Title1", "text 1")); 76b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov blog.addPost(new Post("Title2", "text text 2")); 77b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov blog.numbers.add(19); 78b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov blog.numbers.add(17); 79b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov TreeSet<String> labels = new TreeSet<String>(); 80b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov labels.add("Java"); 81b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov labels.add("YAML"); 82b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov labels.add("SnakeYAML"); 83b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov blog.setLabels(labels); 84b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov return blog; 85b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov } 86b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov 87b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov private void check(String doc) { 88def9635e03df5f547cda12693af495d24d1b0a5bAndrey Somov Yaml yamlLoader = new Yaml(); 8953aac969dea8f57a705a75301a68f45ff1b53f93Andrey Somov yamlLoader.setBeanAccess(BeanAccess.FIELD); 90b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov Blog blog = (Blog) yamlLoader.load(doc); 91b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov assertEquals("Test Me!", blog.getName()); 92b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov assertEquals(2, blog.numbers.size()); 93b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov assertEquals(2, blog.getPosts().size()); 94b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov for (Post post : blog.getPosts()) { 95b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov assertEquals(Post.class, post.getClass()); 96b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov } 97b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov } 98b75dbcf70cc2506588ca1bf2fae0d212013e369fAndrey Somov} 99