Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

StringToolTest.java

Go to the documentation of this file.
00001 package net.threebit.utils.sosc.test;
00002 
00003 /*
00004         Copyright 2003 Shawn Deleurme
00005         Copyright 2003 Kevin O'Donnell
00006 
00007         This program is free software; you can redistribute it and/or modify
00008         it under the terms of the GNU General Public License as published by
00009         the Free Software Foundation; either version 2 of the License, or
00010         (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016 
00017         You should have received a copy of the GNU General Public License
00018         along with this program; if not, write to the Free Software
00019         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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                                 // System.out.println(c + " " + Character.getType(c));
00056                                 System.out.println(c + " " + (byte)c);
00057                         }
00058                         baseEncoder.reset();
00059                         CoderResult cr = baseEncoder.encode (tmpcb, bb, true);
00060                         // If error or output overflow, we need to adjust
00061                         // the position of the input buffer to match what
00062                         // was really consumed from the temp buffer.  If
00063                         // underflow (all input consumed) this is a no-op.
00064                         cb.position (cb.position() - tmpcb.remaining());
00065                         return (cr);
00066                         /*
00067                         System.out.println("CharBuffer in: " + in);
00068                         try {
00069                                 while (true) { 
00070                                         char c = in.get();
00071                                         System.out.println("char: " + c + ('Î'==c?" is ?":" NOT ?"));
00072                                         out.putChar(c);
00073                                 }
00074                         }
00075                         catch (BufferUnderflowException e) {
00076                                 return CoderResult.UNDERFLOW;
00077                         }
00078                         catch (BufferOverflowException e) {
00079                                 return CoderResult.OVERFLOW;
00080                         }
00081                         finally {
00082                                 System.out.println("ByteBuffer out: " + out);
00083                         }
00084                         */
00085                 }
00086         }
00087 
00088         public void test2() throws Exception {
00089                 String s = "Île-à-La-Crosse";
00090                 // s = "test";
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                 String encoding = "US-ASCII";
00098                 byte[ ] b = new byte [ 256 ];
00099                 for ( int i=0 ; i < 256 ; i++ ) b[ i ]= (byte )i;
00100                 String x = new String ( b , encoding );
00101                 System.out.println(x);
00102                 for ( int i=0 ; i < x.length (); i++ ) {
00103                         if ( x.charAt ( i ) != i ) System.out.println (i + " -> " + (int)x.charAt ( i ) );
00104                 }
00105 
00106                 String in = "Île-à-La-Crosse";
00107                 String out = "?le-?-La-Crosse";
00108                 String z = StringTool.anglosize(in);
00109                 assertTrue(out.equals(z));
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 }

Generated on Mon Jul 14 17:19:20 2003 for SOSC by doxygen1.2.15