View Javadoc

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