1 /** 2 * Copyright (c) 2008, SnakeYAML 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package org.yaml.snakeyaml.serializer; 15 16 import java.io.IOException; 17 import java.io.StringWriter; 18 import java.text.NumberFormat; 19 import junit.framework.TestCase; 20 import org.yaml.snakeyaml.DumperOptions; 21 import org.yaml.snakeyaml.emitter.Emitter; 22 import org.yaml.snakeyaml.nodes.ScalarNode; 23 import org.yaml.snakeyaml.nodes.Tag; 24 import org.yaml.snakeyaml.resolver.Resolver; 25 26 public class SerializerTest extends TestCase { 27 28 private Serializer serializer; 29 30 @Override setUp()31 protected void setUp() { 32 DumperOptions config = new DumperOptions(); 33 StringWriter writer = new StringWriter(); 34 serializer = new Serializer(new Emitter(writer, config), new Resolver(), config, null); 35 } 36 testSerializerIsAlreadyOpened()37 public void testSerializerIsAlreadyOpened() throws IOException { 38 serializer.open(); 39 try { 40 serializer.open(); 41 fail(); 42 } catch (RuntimeException e) { 43 assertEquals("serializer is already opened", e.getMessage()); 44 } 45 } 46 testSerializerIsClosed1()47 public void testSerializerIsClosed1() throws IOException { 48 serializer.open(); 49 serializer.close(); 50 try { 51 serializer.open(); 52 fail(); 53 } catch (RuntimeException e) { 54 assertEquals("serializer is closed", e.getMessage()); 55 } 56 } 57 testSerializerIsClosed2()58 public void testSerializerIsClosed2() throws IOException { 59 serializer.open(); 60 serializer.close(); 61 try { 62 serializer.serialize( 63 new ScalarNode(new Tag("!foo"), "bar", null, null, DumperOptions.ScalarStyle.PLAIN)); 64 fail(); 65 } catch (RuntimeException e) { 66 assertEquals("serializer is closed", e.getMessage()); 67 } 68 } 69 testSerializerIsClosed3()70 public void testSerializerIsClosed3() throws IOException { 71 serializer.open(); 72 serializer.close(); 73 serializer.close();// no problem to close twice 74 } 75 testSerializerIsNotOpened1()76 public void testSerializerIsNotOpened1() throws IOException { 77 try { 78 serializer.close(); 79 fail(); 80 } catch (RuntimeException e) { 81 assertEquals("serializer is not opened", e.getMessage()); 82 } 83 } 84 testSerializerIsNotOpened2()85 public void testSerializerIsNotOpened2() throws IOException { 86 try { 87 serializer.serialize( 88 new ScalarNode(new Tag("!foo"), "bar", null, null, DumperOptions.ScalarStyle.PLAIN)); 89 fail(); 90 } catch (RuntimeException e) { 91 assertEquals("serializer is not opened", e.getMessage()); 92 } 93 } 94 testGenerateAnchor()95 public void testGenerateAnchor() { 96 NumberFormat format = NumberFormat.getNumberInstance(); 97 format.setMinimumIntegerDigits(3); 98 String anchor = format.format(3L); 99 assertEquals("003", anchor); 100 } 101 } 102