• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.serializer;
17 
18 import java.io.IOException;
19 import java.io.StringWriter;
20 import java.text.NumberFormat;
21 
22 import junit.framework.TestCase;
23 
24 import org.yaml.snakeyaml.DumperOptions;
25 import org.yaml.snakeyaml.emitter.Emitter;
26 import org.yaml.snakeyaml.nodes.ScalarNode;
27 import org.yaml.snakeyaml.nodes.Tag;
28 import org.yaml.snakeyaml.resolver.Resolver;
29 
30 public class SerializerTest extends TestCase {
31     private Serializer serializer;
32 
33     @Override
setUp()34     protected void setUp() {
35         DumperOptions config = new DumperOptions();
36         StringWriter writer = new StringWriter();
37         serializer = new Serializer(new Emitter(writer, config), new Resolver(), config, null);
38     }
39 
testSerializerIsAlreadyOpened()40     public void testSerializerIsAlreadyOpened() throws IOException {
41         serializer.open();
42         try {
43             serializer.open();
44             fail();
45         } catch (RuntimeException e) {
46             assertEquals("serializer is already opened", e.getMessage());
47         }
48     }
49 
testSerializerIsClosed1()50     public void testSerializerIsClosed1() throws IOException {
51         serializer.open();
52         serializer.close();
53         try {
54             serializer.open();
55             fail();
56         } catch (RuntimeException e) {
57             assertEquals("serializer is closed", e.getMessage());
58         }
59     }
60 
testSerializerIsClosed2()61     public void testSerializerIsClosed2() throws IOException {
62         serializer.open();
63         serializer.close();
64         try {
65             serializer.serialize(new ScalarNode(new Tag("!foo"), "bar", null, null, (char) 0));
66             fail();
67         } catch (RuntimeException e) {
68             assertEquals("serializer is closed", e.getMessage());
69         }
70     }
71 
testSerializerIsClosed3()72     public void testSerializerIsClosed3() throws IOException {
73         serializer.open();
74         serializer.close();
75         serializer.close();// no problem to close twice
76     }
77 
testSerializerIsNotOpened1()78     public void testSerializerIsNotOpened1() throws IOException {
79         try {
80             serializer.close();
81             fail();
82         } catch (RuntimeException e) {
83             assertEquals("serializer is not opened", e.getMessage());
84         }
85     }
86 
testSerializerIsNotOpened2()87     public void testSerializerIsNotOpened2() throws IOException {
88         try {
89             serializer.serialize(new ScalarNode(new Tag("!foo"), "bar", null, null, (char) 0));
90             fail();
91         } catch (RuntimeException e) {
92             assertEquals("serializer is not opened", e.getMessage());
93         }
94     }
95 
testGenerateAnchor()96     public void testGenerateAnchor() {
97         NumberFormat format = NumberFormat.getNumberInstance();
98         format.setMinimumIntegerDigits(3);
99         String anchor = format.format(3L);
100         assertEquals("003", anchor);
101     }
102 }
103