00001 package net.threebit.utils.sosc.test;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 import java.io.*;
00023 import java.sql.*;
00024 import java.util.*;
00025 import java.util.logging.*;
00026 import junit.framework.*;
00027 import net.threebit.utils.sosc.*;
00028 import java.nio.*;
00029 import java.nio.charset.*;
00030
00035 public class StringToolTest extends TestCase {
00036
00037 class AsciiEncoder extends CharsetEncoder {
00038 private CharsetEncoder baseEncoder;
00042 AsciiEncoder (Charset cs, CharsetEncoder baseEncoder) {
00043 super(cs, baseEncoder.averageBytesPerChar(), baseEncoder.maxBytesPerChar());
00044 this.baseEncoder = baseEncoder;
00045 }
00049 protected CoderResult encodeLoop (CharBuffer cb, ByteBuffer bb) {
00050 CharBuffer tmpcb = CharBuffer.allocate (cb.remaining());
00051 while (cb.hasRemaining()) { tmpcb.put (cb.get()); }
00052 tmpcb.rewind();
00053 for (int pos = tmpcb.position(); pos < tmpcb.limit(); pos++) {
00054 char c = tmpcb.get(pos);
00055
00056 System.out.println(c + " " + (byte)c);
00057 }
00058 baseEncoder.reset();
00059 CoderResult cr = baseEncoder.encode (tmpcb, bb, true);
00060
00061
00062
00063
00064 cb.position (cb.position() - tmpcb.remaining());
00065 return (cr);
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 }
00086 }
00087
00088 public void test2() throws Exception {
00089 String s = "Île-à-La-Crosse";
00090
00091 AsciiEncoder ae = new AsciiEncoder(Charset.forName("UTF-8"), Charset.forName("UTF-8").newEncoder());
00092 ByteBuffer bb = ae.encode(CharBuffer.wrap(s));
00093 System.out.println( new String(bb.array() ));
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 }
00112
00113 public void test1() throws Exception {
00114 String s = "";
00115 String p = " ";
00116 String r = " ";
00117 assertTrue(r.equals( StringTool.appendPadding(s,1) ));
00118 assertTrue(r.equals( StringTool.appendPadding(s,p,1) ));
00119
00120 s = "Another test";
00121 p = "Z";
00122 r = StringTool.appendPadding(s,p,s.length() + 3);
00123 assertTrue(r.equals(s + p + p + p));
00124
00125 s = "12345";
00126 p = " ";
00127 assertTrue("12".equals( StringTool.appendPadding(s,2) ));
00128
00129 }
00130
00131 }