~gabriel1984sibiu/jackson-core/trunk

« back to all changes in this revision

Viewing changes to src/test/java/com/fasterxml/jackson/core/io/TestUTF8Writer.java

  • Committer: Package Import Robot
  • Author(s): Wolodja Wentland
  • Date: 2013-08-10 19:27:10 UTC
  • Revision ID: package-import@ubuntu.com-20130810192710-euj21chefjiec90i
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.fasterxml.jackson.core.io;
 
2
 
 
3
import java.io.*;
 
4
 
 
5
import com.fasterxml.jackson.core.io.IOContext;
 
6
import com.fasterxml.jackson.core.io.UTF8Writer;
 
7
import com.fasterxml.jackson.core.util.BufferRecycler;
 
8
 
 
9
public class TestUTF8Writer
 
10
    extends com.fasterxml.jackson.test.BaseTest
 
11
{
 
12
    public void testSimple() throws Exception
 
13
    {
 
14
        BufferRecycler rec = new BufferRecycler();
 
15
        IOContext ctxt = new IOContext(rec, null, false);
 
16
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
17
        UTF8Writer w = new UTF8Writer(ctxt, out);
 
18
 
 
19
        String str = "AB\u00A0\u1AE9\uFFFC";
 
20
        char[] ch = str.toCharArray();
 
21
 
 
22
        // Let's write 3 times, using different methods
 
23
        w.write(str);
 
24
 
 
25
        w.append(ch[0]);
 
26
        w.write(ch[1]);
 
27
        w.write(ch, 2, 3);
 
28
 
 
29
        w.write(str, 0, str.length());
 
30
        w.close();
 
31
 
 
32
        // and thus should have 3 times contents
 
33
        byte[] data = out.toByteArray();
 
34
        assertEquals(3*10, data.length);
 
35
        String act = out.toString("UTF-8");
 
36
        assertEquals(15, act.length());
 
37
 
 
38
        assertEquals(3 * str.length(), act.length());
 
39
        assertEquals(str+str+str, act);
 
40
    }
 
41
 
 
42
    public void testFlushAfterClose() throws Exception
 
43
    {
 
44
        BufferRecycler rec = new BufferRecycler();
 
45
        IOContext ctxt = new IOContext(rec, null, false);
 
46
        ByteArrayOutputStream out = new ByteArrayOutputStream();
 
47
        UTF8Writer w = new UTF8Writer(ctxt, out);
 
48
        
 
49
        w.write('X');
 
50
        
 
51
        w.close();
 
52
        assertEquals(1, out.size());
 
53
 
 
54
        // and this ought to be fine...
 
55
        w.flush();
 
56
        // as well as some more...
 
57
        w.close();
 
58
        w.flush();
 
59
    }
 
60
}