001: /*
002: * Copyright (C) 2006, 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 15. March 2006 by Joerg Schaible
010: */
011: package com.thoughtworks.xstream.io;
012:
013: import com.thoughtworks.xstream.io.xml.CompactWriter;
014:
015: import junit.framework.TestCase;
016:
017: import java.io.IOException;
018: import java.io.StringWriter;
019:
020: /**
021: * @author Jörg Schaible
022: */
023: public class StatefulWriterTest extends TestCase {
024:
025: private StatefulWriter writer;
026: private StringWriter stringWriter;
027:
028: protected void setUp() throws Exception {
029: super .setUp();
030: stringWriter = new StringWriter();
031: writer = new StatefulWriter(new CompactWriter(stringWriter));
032: }
033:
034: public void testDelegatesAllCalls() {
035: writer.startNode("junit");
036: writer.addAttribute("test", "true");
037: writer.setValue("foo");
038: writer.endNode();
039: writer.close();
040: assertEquals("<junit test=\"true\">foo</junit>", stringWriter
041: .toString());
042: }
043:
044: public void testKeepsBlance() {
045: writer.startNode("junit");
046: writer.endNode();
047: try {
048: writer.endNode();
049: fail("Thrown " + StreamException.class.getName()
050: + " expected");
051: } catch (final StreamException e) {
052: assertTrue(e.getCause() instanceof IllegalStateException);
053: }
054: }
055:
056: public void testCanOnlyWriteAttributesToOpenNode() {
057: try {
058: writer.addAttribute("test", "true");
059: fail("Thrown " + StreamException.class.getName()
060: + " expected");
061: } catch (final StreamException e) {
062: assertTrue(e.getCause() instanceof IllegalStateException);
063: }
064: writer.startNode("junit");
065: writer.setValue("text");
066: try {
067: writer.addAttribute("test", "true");
068: fail("Thrown " + StreamException.class.getName()
069: + " expected");
070: } catch (final StreamException e) {
071: assertTrue(e.getCause() instanceof IllegalStateException);
072: }
073: writer.endNode();
074: try {
075: writer.addAttribute("test", "true");
076: fail("Thrown " + StreamException.class.getName()
077: + " expected");
078: } catch (final StreamException e) {
079: assertTrue(e.getCause() instanceof IllegalStateException);
080: }
081: }
082:
083: public void testCanWriteAttributesOnlyOnce() {
084: writer.startNode("junit");
085: writer.addAttribute("test", "true");
086: try {
087: writer.addAttribute("test", "true");
088: fail("Thrown " + StreamException.class.getName()
089: + " expected");
090: } catch (final StreamException e) {
091: assertTrue(e.getCause() instanceof IllegalStateException);
092: }
093: writer.endNode();
094: }
095:
096: public void testCanWriteValueOnlyToOpenNode() {
097: try {
098: writer.setValue("test");
099: fail("Thrown " + StreamException.class.getName()
100: + " expected");
101: } catch (final StreamException e) {
102: assertTrue(e.getCause() instanceof IllegalStateException);
103: }
104: writer.startNode("junit");
105: writer.endNode();
106: try {
107: writer.setValue("test");
108: fail("Thrown " + StreamException.class.getName()
109: + " expected");
110: } catch (final StreamException e) {
111: assertTrue(e.getCause() instanceof IllegalStateException);
112: }
113: }
114:
115: public void testCannotOpenNodeInValue() {
116: writer.startNode("junit");
117: writer.setValue("test");
118: try {
119: writer.startNode("junit");
120: fail("Thrown " + StreamException.class.getName()
121: + " expected");
122: } catch (final StreamException e) {
123: assertTrue(e.getCause() instanceof IllegalStateException);
124: }
125: }
126:
127: public void testCanCloseInFinally() {
128: try {
129: writer.endNode();
130: fail("Thrown " + StreamException.class.getName()
131: + " expected");
132: } catch (final StreamException e) {
133: writer.close();
134: }
135: }
136:
137: public void testCannotWriteAfterClose() {
138: writer.close();
139: try {
140: writer.startNode("junit");
141: fail("Thrown " + StreamException.class.getName()
142: + " expected");
143: } catch (final StreamException e) {
144: assertTrue(e.getCause() instanceof IOException);
145: }
146: try {
147: writer.addAttribute("junit", "test");
148: fail("Thrown " + StreamException.class.getName()
149: + " expected");
150: } catch (final StreamException e) {
151: assertTrue(e.getCause() instanceof IOException);
152: }
153: try {
154: writer.setValue("test");
155: fail("Thrown " + StreamException.class.getName()
156: + " expected");
157: } catch (final StreamException e) {
158: assertTrue(e.getCause() instanceof IOException);
159: }
160: try {
161: writer.endNode();
162: fail("Thrown " + StreamException.class.getName()
163: + " expected");
164: } catch (final StreamException e) {
165: assertTrue(e.getCause() instanceof IOException);
166: }
167: try {
168: writer.flush();
169: fail("Thrown " + StreamException.class.getName()
170: + " expected");
171: } catch (final StreamException e) {
172: assertTrue(e.getCause() instanceof IOException);
173: }
174: }
175:
176: public void testCanCloseTwice() {
177: writer.close();
178: writer.close();
179: }
180:
181: public void testCaresAboutNestingLevelWritingAttributes() {
182: writer.startNode("junit");
183: writer.addAttribute("test", "true");
184: writer.startNode("junit");
185: writer.addAttribute("test", "true");
186: writer.endNode();
187: writer.endNode();
188: }
189: }
|