/* Copyright (c) 2003-2004 Ricebridge. All Rights Reserved.
 *
 * This file is available under the terms and conditions of the
 * Ricebridge "Open Source API" policy; Ricebridge grants use of this
 * copyrighted work under the terms of a BSD-style license only. See
 * http://www.opensource.org/licenses/bsd-license.php for more
 * information.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *  - Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *
 *  - Redistributions in binary form must reproduce the above
 *  copyright notice, this list of conditions and the following
 *  disclaimer in the documentation and/or other materials provided
 *  with the distribution.
 *
 *  - Neither the name of the Ricebridge nor the names of its
 *  contributors may be used to endorse or promote products derived
 *  from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.  
 */

package com.ricebridge.csvman.test;


import com.ricebridge.csvman.*;

import org.jostraca.util.*;

import junit.framework.*;
import junit.textui.*;

import javax.swing.table.*;
import java.sql.ResultSet;
import java.util.*;
import java.io.*;


/** Test cases for I18N.
 *    <p>The <b><a href="I18NTest.java.html">Source Code</a></b> of this Java class 
 *    is available under a <a href="http://www.opensource.org/licenses/bsd-license.php">BSD-style license</a>.</p>
 */
public class I18NTest extends TestCase {

  // standard test methods
  
  public I18NTest( String pName ) {
    super( pName );
  }

  public static TestSuite suite() {
    return new TestSuite( I18NTest.class );
  }

  public static void main( String[] pArgs ) {
    TestRunner.run( suite() );
  }



  public static HashMap sFileEncodingMap = new HashMap();
  public static HashMap sFileDataMap     = new HashMap();
  public static HashMap sNotMatchMap     = new HashMap();

  static {
    sFileDataMap.put( "eu01", "1,a,\u00e1,\u00c1\u00e0\u00c0\u00e2\u00c2\u00e4\u00c4\u00e3\u00c3\u00e5\u00c5\n"
                      + "2,c,\u00e7,\u00c7\n" 
                      + "3,e,\u00e9,\u00c9\u00e8\u00c8\u00ea\u00ca\u00eb\u00cb\n" 
                      + "4,i,\u00ed,\u00cd\u00ec\u00cc\u00ee\u00ce\u00ef\u00cf\n"
                      + "5,n,\u00f1,\u00d1\n"
                      + "6,o,\u00f3,\u00d3\u00f2\u00d2\u00f4\u00d4\u00f6\u00d6\u00f5\u00d5\u00f8\u00d8\n"
                      + "7,u,\u00fa,\u00da\u00f9\u00d9\u00fb\u00db\u00fc\u00dc\n"
                      + "7,y,\u00fd,\u00dd\u00ff\n"
                      + "8,lig,\u00e6,\u00c6\u00df\n"
                      + "9,s,\u00f0,\u00d0\u00fe\u00de\u00aa\u00ba\n"
                      );
    sFileDataMap.put( "sym01", "1,html,\u00a0,\u00ad\n"
                      + "2,accent,\u00b4,\u00a8\u007e\u00b8\n"
                      + "3,maths,\u00bc,\u00bd\u00be\u00d7\u00f7\u00ac\u00b0\u00b1\u00b9\u00b2\u00b3\n"
                      + "4,currency,\u0344,\u00a5\u00a3\u00a2\u00a4\u00ae\u00a9\n"
                      + "5,punc,\u00b7,\u00a1\u00bf\u007b\u007d\n"
                      + "6,other,\u00a6,\u00a7\u00b6\u00ab\u00bb\u00af\u00b5\n"
                      );

    String[] enc01 = new String[] { "ASCII", "Cp1252", "UTF8", "UTF-16", 
                                    "UnicodeBigUnmarked", "UnicodeLittleUnmarked", "UnicodeBig", "UnicodeLittle" };

    sFileEncodingMap.put( "eu01",  enc01 );
    sFileEncodingMap.put( "sym01", enc01 );

    sNotMatchMap.put( makeFullPath("eu01","ASCII"), "" );
    sNotMatchMap.put( makeFullPath("sym01","ASCII"), "" );
    sNotMatchMap.put( makeFullPath("sym01","Cp1252"), "" );
  }




  public void testLoad() throws Exception {
    writeFiles();

    CsvManager cm = new CsvManager();
    List       lines;

    lines = cm.loadAsListsFromString( (String)sFileDataMap.get("eu01") );
    assertEquals( (String)sFileDataMap.get("eu01"), TestUtil.normalizeAsLists(lines) );

    for( Iterator feT = sFileEncodingMap.keySet().iterator(); feT.hasNext(); ) {
      String   fn        = (String) feT.next();
      String[] encodings = (String[]) sFileEncodingMap.get(fn);
      for( int eI = 0; eI < encodings.length; eI++ ) {
        String enc = encodings[eI];
        String ffp = makeFullPath( fn, enc );
        //System.out.println( "load:"+ffp );
        cm.getCsvSpec().setEncoding( enc );
        lines = cm.loadAsLists( ffp );
        if( sNotMatchMap.containsKey( ffp ) ) {
          assertTrue( !((String)sFileDataMap.get(fn)).equals(TestUtil.normalizeAsLists(lines)) );
        }
        else {
          assertEquals( (String)sFileDataMap.get(fn), TestUtil.normalizeAsLists(lines) );
        }
      }
    }
  }


  public void testSave() throws Exception {
    CsvManager cm = new CsvManager();
    List       lines;

    lines = cm.loadAsListsFromString( (String)sFileDataMap.get("eu01") );
    assertEquals( (String)sFileDataMap.get("eu01"), TestUtil.normalizeAsLists(lines) );

    for( Iterator feT = sFileEncodingMap.keySet().iterator(); feT.hasNext(); ) {
      String   fn        = (String) feT.next();
      String[] encodings = (String[]) sFileEncodingMap.get(fn);
      for( int eI = 0; eI < encodings.length; eI++ ) {
        String enc = encodings[eI];
        String ffp = makeFullPath( fn, enc );
        //System.out.println( "save:"+ffp );
        cm.getCsvSpec().setEncoding( enc );
        lines = cm.loadAsListsFromString( (String)sFileDataMap.get(fn) );
        cm.saveAsLists( ffp, lines );
        lines = cm.loadAsLists( ffp );
        if( sNotMatchMap.containsKey( ffp ) ) {
          assertTrue( !((String)sFileDataMap.get(fn)).equals(TestUtil.normalizeAsLists(lines)) );
        }
        else {
          assertEquals( (String)sFileDataMap.get(fn), TestUtil.normalizeAsLists(lines) );
        }
      }
    }
  }


  public void writeFiles() throws Exception {
    for( Iterator feT = sFileEncodingMap.keySet().iterator(); feT.hasNext(); ) {
      String   fn        = (String) feT.next();
      String[] encodings = (String[]) sFileEncodingMap.get(fn);
      for( int eI = 0; eI < encodings.length; eI++ ) {
        String enc = encodings[eI];
        String ffp = makeFullPath( fn, enc );
        //System.out.println( "write: "+ffp );
        writeFile( fn,(String)sFileDataMap.get(fn),enc );
      }
    }
  }



  public void writeFile( String pName, String pData, String pEncoding ) throws Exception {
    FileOutputStream fos = new FileOutputStream( makeFullPath(pName,pEncoding) );
    Writer out = new OutputStreamWriter(fos, pEncoding);
    out.write( pData );
    out.close();
  }

  public static String makeFullPath( String pName, String pEncoding ) {
    String fullPath = "/tmp/"+pName+"-"+pEncoding+".csv";
    return fullPath;
  }

}





Syntax Highlighting created using the com.Ostermiller.Syntax package.
Friday, October 13 2006 at 22:45