Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a0093197669bb7e24d4a8dfba7cc758e28a24cd0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.util.tests;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

/**
 * @author Eike Stepper
 */
public class UTFTest extends AbstractOMTest
{
  private static final int UNSIGNED_SHORT_MAX = (1 << 16) - 1;

  private static final int MAX = UNSIGNED_SHORT_MAX / 10 + 1;

  public void testUTF8_OneOctet() throws Exception
  {
    String part = "0123456789";
    assertEquals(10, part.length());

    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < MAX; i++)
    {
      builder.append(part);
    }

    String str = builder.toString();
    assertTrue(str.length() > UNSIGNED_SHORT_MAX);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeUTF(str);

    String received = baos.toString("UTF-8");
    assertEquals(str, received);
  }

  public void testUTF8_ThreeOctets() throws Exception
  {
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < UNSIGNED_SHORT_MAX; i++)
    {
      builder.append("\u6771");
    }

    String str = builder.toString();
    assertEquals(UNSIGNED_SHORT_MAX, str.length());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeUTF(str);

    String received = baos.toString("UTF-8");
    assertEquals(str, received);
  }
}

Back to the top