db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kmars...@apache.org
Subject svn commit: r957580 - in /db/derby/code/trunk/java: drda/org/apache/derby/impl/drda/ testing/org/apache/derby/impl/drda/ testing/org/apache/derbyTesting/functionTests/tests/derbynet/
Date Thu, 24 Jun 2010 15:16:48 GMT
Author: kmarsden
Date: Thu Jun 24 15:16:41 2010
New Revision: 957580

URL: http://svn.apache.org/viewvc?rev=957580&view=rev
Log:
DERBY-728 Unable to create databases whose name containg Chinese characters through the client
driver

Incremental patches  DERBY-728_p2.diff, DERBY-728_p2-test.diff
Add server side UTf8CcsidManager and unit test. 
Change names of "convertXXXUCS2" methods to "convertXXXJavaString" for clarification.
UNICODEMGR level negotiation to use the the code has not yet been added.

contributed by Tiago R. Espinha tiago dot derby at yahoo dot co dot uk


Added:
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Utf8CcsidManager.java   (with
props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/Utf8CcsidManagerTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/CcsidManager.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMWriter.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAString.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/EbcdicCcsidManager.java
    db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/TestProto.java
    db/derby/code/trunk/java/testing/org/apache/derby/impl/drda/ProtocolTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/CcsidManager.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/CcsidManager.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/CcsidManager.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/CcsidManager.java Thu Jun 24
15:16:41 2010
@@ -44,7 +44,7 @@ abstract class CcsidManager
   //
   // @param sourceString A Java String to convert.
   // @return A new byte array representing the String in a particular ccsid.
-  abstract byte[] convertFromUCS2 (String sourceString);
+  abstract byte[] convertFromJavaString (String sourceString);
 
 
     /**
@@ -54,21 +54,26 @@ abstract class CcsidManager
      * @param sourceString  A Java String to convert.
      * @param buffer        The buffer to convert the String into.
      */
-    abstract void convertFromUCS2(String sourceString, ByteBuffer buffer);
+    abstract void convertFromJavaString(String sourceString, ByteBuffer buffer);
 
   // Convert a byte array representing characters in a particular ccsid into a Java String.
   //
   // @param sourceBytes An array of bytes to be converted.
   // @return String A new Java String Object created after conversion.
-  abstract String convertToUCS2 (byte[] sourceBytes);
+  abstract String convertToJavaString (byte[] sourceBytes);
 
 
-  // Convert a byte array representing characters in a particular ccsid into a Java String.
-  //
-  // @param sourceBytes An array of bytes to be converted.
-  // @param offset  An offset indicating first byte to convert.
-  // @param numToConvert The number of bytes to be converted.
-  // @return A new Java String Object created after conversion.
-  abstract String convertToUCS2 (byte[] sourceBytes, int offset, int numToConvert);
+  /**
+   * Convert a byte array representing characters in a particular ccsid into a Java String.
+   * 
+   * Mind the fact that for certain encodings (e.g. UTF8), the offset and numToConvert
+   * actually represent characters and 1 character does not always equal to 1 byte.
+   * 
+   * @param sourceBytes An array of bytes to be converted.
+   * @param offset An offset indicating first byte to convert.
+   * @param numToConvert The number of bytes to be converted.
+   * @return A new Java String Object created after conversion.
+   */
+  abstract String convertToJavaString (byte[] sourceBytes, int offset, int numToConvert);
 
 }

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMReader.java Thu Jun 24 15:16:41
2010
@@ -109,6 +109,8 @@ class DDMReader
     };
 
 	private DRDAConnThread agent;
+	private Utf8CcsidManager utf8CcsidManager;
+	private EbcdicCcsidManager ebcdicCcsidManager;
 	private CcsidManager ccsidManager;
 
 	// data buffer
@@ -175,11 +177,11 @@ class DDMReader
 	 * It is used by TestProto to read the protocol returned by the
 	 * server 
 	 */
-	DDMReader(CcsidManager ccsidManager, InputStream inputStream)
+	DDMReader(InputStream inputStream)
 	{
 		buffer = new byte[DEFAULT_BUFFER_SIZE];
 		ddmCollectionLenStack = new long[MAX_MARKS_NESTING];
-		this.ccsidManager = ccsidManager;
+		
 		this.inputStream = inputStream;
 		initialize(null, null);
 		// turn off tracing
@@ -202,9 +204,11 @@ class DDMReader
 	protected void initialize(DRDAConnThread agent, DssTrace dssTrace)
   	{
 		this.agent = agent;
+		this.utf8CcsidManager = new Utf8CcsidManager();
+        this.ebcdicCcsidManager = new EbcdicCcsidManager();
+        this.ccsidManager = ebcdicCcsidManager;
 		if (agent != null)
 		{
-			ccsidManager = agent.ccsidManager;
 			inputStream = agent.getInputStream();
 		}
 		topDdmCollectionStack = EMPTY_STACK;
@@ -218,6 +222,21 @@ class DDMReader
 		this.dssTrace = dssTrace;
 	}
 
+	// Switch the ccsidManager to the UTF-8 instance
+    protected void setUtf8Ccsid() {
+        ccsidManager = utf8CcsidManager;
+    }
+    
+    // Switch the ccsidManager to the EBCDIC instance
+    protected void setEbcdicCcsid() {
+        ccsidManager = ebcdicCcsidManager;
+    }
+    
+    // Get the current ccsidManager
+    protected CcsidManager getCurrentCcsidManager() {
+        return ccsidManager;
+    }
+    
 	protected boolean terminateChainOnErr()
 	{
 		return terminateChainOnErr;
@@ -1378,7 +1397,7 @@ class DDMReader
 		if (plainText == null)
 			return null;
 		else
-			return ccsidManager.convertToUCS2(plainText);
+			return ccsidManager.convertToJavaString(plainText);
 	}
 
 	/**
@@ -1394,7 +1413,7 @@ class DDMReader
 	{
 		ensureBLayerDataInBuffer (length, ADJUST_LENGTHS);
 
-		String result = ccsidManager.convertToUCS2 (buffer, pos, length);
+		String result = ccsidManager.convertToJavaString(buffer, pos, length);
 		pos += length;
 		return result;
 	}
@@ -1587,7 +1606,7 @@ class DDMReader
 	 */
 	protected String convertBytes(byte[] buf)
 	{
-		return ccsidManager.convertToUCS2 (buf, 0, buf.length);
+		return ccsidManager.convertToJavaString (buf, 0, buf.length);
 	}
 
 	// Private methods

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMWriter.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMWriter.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMWriter.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DDMWriter.java Thu Jun 24 15:16:41
2010
@@ -69,7 +69,11 @@ class DDMWriter
 	// top of the stack
 	private int top;
 
-	// CCSID manager for translation of strings in the protocol to EBCDIC
+	// CCSID manager for translation of strings in the protocol to UTF-8 and EBCDIC
+	private EbcdicCcsidManager ebcdicCcsidManager;
+	private Utf8CcsidManager utf8CcsidManager;
+	
+	// Current CCSID manager
 	private CcsidManager ccsidManager;
 
 	// DRDA connection thread for this writer
@@ -120,10 +124,14 @@ class DDMWriter
     
     volatile long totalByteCount = 0;
     
-	DDMWriter (CcsidManager ccsidManager, DRDAConnThread agent, DssTrace dssTrace)
+	DDMWriter (DRDAConnThread agent, DssTrace dssTrace)
 	{
+	    // Create instances of the two ccsid managers and default to EBCDIC
+	    this.ebcdicCcsidManager = new EbcdicCcsidManager();
+	    this.utf8CcsidManager = new Utf8CcsidManager();
+	    this.ccsidManager = this.ebcdicCcsidManager;
+	    
 		this.buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
-		this.ccsidManager = ccsidManager;
 		this.agent = agent;
 		this.prevHdrLocation = -1;
 		this.previousCorrId = DssConstants.CORRELATION_ID_UNKNOWN;
@@ -138,6 +146,21 @@ class DDMWriter
 			.onUnmappableCharacter(CodingErrorAction.REPLACE);
 	}
 
+	// Switch the ccsidManager to the UTF-8 instance
+	protected void setUtf8Ccsid() {
+	    ccsidManager = utf8CcsidManager;
+	}
+	
+	// Switch the ccsidManager to the EBCDIC instance
+	protected void setEbcdicCcsid() {
+	    ccsidManager = ebcdicCcsidManager;
+	}
+	
+	// Get the current ccsidManager
+	protected CcsidManager getCurrentCcsidManager() {
+	    return ccsidManager;
+	}
+	
 	/**
 	 * reset values for sending next message
 	 *
@@ -883,7 +906,7 @@ class DDMWriter
 		ensureLength ((stringLength * 2)  + 4);
 		buffer.putShort((short) (stringLength + 4));
 		buffer.putShort((short) codePoint);
-		ccsidManager.convertFromUCS2(string, buffer);
+		ccsidManager.convertFromJavaString(string, buffer);
 	}
 
 	/**
@@ -901,7 +924,7 @@ class DDMWriter
 		ensureLength (paddedLength + 4);
 		buffer.putShort((short) (paddedLength + 4));
 		buffer.putShort((short) codePoint);
-		ccsidManager.convertFromUCS2(string, buffer);
+		ccsidManager.convertFromJavaString(string, buffer);
 		padBytes(ccsidManager.space, fillLength);
 	}
 
@@ -918,7 +941,7 @@ class DDMWriter
 
 		int fillLength = paddedLength -stringLength;
 		ensureLength (paddedLength);
-		ccsidManager.convertFromUCS2(string, buffer);
+		ccsidManager.convertFromJavaString(string, buffer);
 		padBytes(ccsidManager.space, fillLength);
 	}
 

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAConnThread.java Thu Jun 24
15:16:41 2010
@@ -102,7 +102,6 @@ class DRDAConnThread extends Thread {
 	private static final int ROLLBACK = 2;
 
 
-	protected CcsidManager ccsidManager = new EbcdicCcsidManager();
 	private int correlationID;
 	private InputStream sockis;
 	private OutputStream sockos;
@@ -155,13 +154,13 @@ class DRDAConnThread extends Thread {
 	 * <code>parsePKGNAMCSN()</code>. */
 	private Pkgnamcsn prevPkgnamcsn = null;
 	/** Current RDB Package Name. */
-	private DRDAString rdbnam = new DRDAString(ccsidManager);
+	private DRDAString rdbnam = null;
 	/** Current RDB Collection Identifier. */
-	private DRDAString rdbcolid = new DRDAString(ccsidManager);
+	private DRDAString rdbcolid = null;
 	/** Current RDB Package Identifier. */
-	private DRDAString pkgid = new DRDAString(ccsidManager);
+	private DRDAString pkgid = null;
 	/** Current RDB Package Consistency Token. */
-	private DRDAString pkgcnstkn = new DRDAString(ccsidManager);
+	private DRDAString pkgcnstkn = null;
 	/** Current RDB Package Section Number. */
 	private int pkgsn;
 
@@ -605,7 +604,14 @@ class DRDAConnThread extends Thread {
 		sockos = session.sessionOutput;
 
 		reader = new DDMReader(this, session.dssTrace);
-		writer = new DDMWriter(ccsidManager, this, session.dssTrace);
+		writer = new DDMWriter(this, session.dssTrace);
+		
+		/* At this stage we can initialize the strings as we have
+		 * the CcsidManager for the DDMWriter. */
+		rdbnam = new DRDAString(writer.getCurrentCcsidManager());
+	    rdbcolid = new DRDAString(writer.getCurrentCcsidManager());
+	    pkgid = new DRDAString(writer.getCurrentCcsidManager());
+	    pkgcnstkn = new DRDAString(writer.getCurrentCcsidManager());
 	}
 
 	/**

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAString.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAString.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAString.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/DRDAString.java Thu Jun 24 15:16:41
2010
@@ -112,7 +112,7 @@ final class DRDAString {
     public String toString() {
         if (cachedString == null) {
             cachedString =
-                ccsidManager.convertToUCS2(buffer);
+                ccsidManager.convertToJavaString(buffer);
         }
         return cachedString;
     }

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/EbcdicCcsidManager.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/EbcdicCcsidManager.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/EbcdicCcsidManager.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/EbcdicCcsidManager.java Thu Jun
24 15:16:41 2010
@@ -124,14 +124,14 @@ class EbcdicCcsidManager extends CcsidMa
 	  );
 	}
 
-	byte[] convertFromUCS2 (String sourceString)
+	byte[] convertFromJavaString (String sourceString)
 	{
 		ByteBuffer buf = ByteBuffer.allocate(sourceString.length());
-		convertFromUCS2(sourceString, buf);
+		convertFromJavaString(sourceString, buf);
 		return buf.array();
 	}
 
-	void convertFromUCS2 (String sourceString, ByteBuffer buffer)
+	void convertFromJavaString (String sourceString, ByteBuffer buffer)
 	{
 		for (int i=0; i < sourceString.length(); i++) {
 			char c = sourceString.charAt (i);
@@ -142,7 +142,7 @@ class EbcdicCcsidManager extends CcsidMa
 		}
 	}
 
-	String convertToUCS2 (byte[] sourceBytes)
+	String convertToJavaString (byte[] sourceBytes)
 	{
 		int i = 0;
 		char[] theChars = new char[sourceBytes.length];
@@ -156,7 +156,7 @@ class EbcdicCcsidManager extends CcsidMa
 		return new String (theChars);
 	}
 
-	String convertToUCS2 (byte[] sourceBytes, int offset, int numToConvert)
+	String convertToJavaString (byte[] sourceBytes, int offset, int numToConvert)
 	{
 		int i = 0,j = 0;
 		char[] theChars = new char[numToConvert];

Modified: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/TestProto.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/TestProto.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/TestProto.java (original)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/TestProto.java Thu Jun 24 15:16:41
2010
@@ -52,7 +52,6 @@ public class TestProto {
 	private static final CodePointNameTable codePointNameTable = new CodePointNameTable();
 	private static final Hashtable codePointValueTable = new Hashtable();
 	private static final Hashtable commandTable = new Hashtable();
-	private	static final CcsidManager ccsidManager = new EbcdicCcsidManager();
 	//commands
 	private static final int CREATE_DSS_REQUEST = 1;
 	private static final int CREATE_DSS_OBJECT = 2;
@@ -124,7 +123,7 @@ public class TestProto {
 	private Socket monitorSocket = null;
 	private InputStream monitorIs = null;
 	private OutputStream monitorOs = null;
-	private DDMWriter writer = new DDMWriter(ccsidManager, null, null);
+	private DDMWriter writer = new DDMWriter(null, null);
 	private DDMReader reader;
 	private boolean failed = false;
 	private StreamTokenizer tkn;
@@ -140,7 +139,7 @@ public class TestProto {
 
 		try 
 		{
-			reader = new DDMReader(ccsidManager, monitorIs);
+			reader = new DDMReader(monitorIs);
 			processFile(filename);
 		}
 		catch (Exception e)
@@ -402,7 +401,7 @@ public class TestProto {
 				break;
 			case WRITE_SCALAR_PADDED_BYTES:
 				writer.writeScalarPaddedBytes(getCP(), getBytes(), getInt(),
-					ccsidManager.space);
+					writer.getCurrentCcsidManager().space);
 				break;
 			case WRITE_BYTE:
 				writer.writeByte(getInt());
@@ -435,7 +434,8 @@ public class TestProto {
 				int reqLen = getInt();
 				int strLen = str.length();
 				if (strLen < reqLen)
-					writer.padBytes(ccsidManager.space, reqLen-strLen);
+					writer.padBytes(writer.getCurrentCcsidManager().space, 
+					                reqLen-strLen);
 				break;
 			case READ_REPLY_DSS:
 				reader.readReplyDss();
@@ -680,7 +680,7 @@ public class TestProto {
 		if (!str.startsWith("0x"))
 		{
 			//just convert the string to ebcdic byte array
-			return ccsidManager.convertFromUCS2(str);
+			return writer.getCurrentCcsidManager().convertFromJavaString(str);
 		}
 		else
 		{
@@ -953,7 +953,7 @@ public class TestProto {
 	 */
 	private byte[] getEBCDIC(String str)
 	{
-		return ccsidManager.convertFromUCS2(str);
+		return writer.getCurrentCcsidManager().convertFromJavaString(str);
 	}
 	/**
 	 * Write an encoded string

Added: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Utf8CcsidManager.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Utf8CcsidManager.java?rev=957580&view=auto
==============================================================================
--- db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Utf8CcsidManager.java (added)
+++ db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Utf8CcsidManager.java Thu Jun
24 15:16:41 2010
@@ -0,0 +1,87 @@
+/*
+
+   Derby - Class org.apache.derby.impl.drda.Utf8CcsidManager
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+package org.apache.derby.impl.drda;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.ByteBuffer;
+import org.apache.derby.iapi.services.sanity.SanityManager;
+
+public class Utf8CcsidManager extends CcsidManager {
+
+    public Utf8CcsidManager() {
+        super((byte)' ',
+              (byte)'.',
+              new byte[] {
+              //      '0',       '1',      '2',        '3',      '4',
+                (byte)0xf0,(byte)0xf1,(byte)0xf2,(byte)0xf3,(byte)0xf4,
+              //      '5',       '6',       '7',       '8',      '9',
+                (byte)0xf5,(byte)0xf6,(byte)0xf7,(byte)0xf8,(byte)0xf9,
+              //      'A',       'B',       'C',       'D',      'E',
+                (byte)0xc1,(byte)0xc2,(byte)0xc3,(byte)0xc4,(byte)0xc5,
+              //      'F',       'G',       'H',      'I',       'J',
+                (byte)0xc6,(byte)0xc7,(byte)0xc8,(byte)0xc9,(byte)0xd1,
+              //     'K',        'L',       'M',       'N',      'O',
+                (byte)0xd2,(byte)0xd3,(byte)0xd4,(byte)0xd5,(byte)0xd6,
+              //     'P'
+                (byte)0xd7
+              });
+    }
+    
+    public byte[] convertFromJavaString(String sourceString){
+        try {
+            return sourceString.getBytes("UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            SanityManager.THROWASSERT("Could not convert UCS2 (String) to UTF-8 (byte[])",
e);
+        }
+        return null;
+    }
+    
+    public String convertToJavaString(byte[] sourceBytes) {
+       try {
+           return new String(sourceBytes,"UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            SanityManager.THROWASSERT("Could not convert UCS2 (byte[]) to UTF-8 (String)",
e);
+        }
+        return null;
+    }
+
+    public String convertToJavaString(byte[] sourceBytes, int offset, int numToConvert) {
+        try {
+            return new String(sourceBytes,"UTF-8").substring(offset, offset+numToConvert);
+        } catch (UnsupportedEncodingException e) {
+            SanityManager.THROWASSERT("Could not convert UCS2 (byte[]) to UTF-8 (String)
with offset",e);
+        }
+        return null;
+    }
+
+    /* Keeping this method out for now.
+     * The CcsidManager for **client** has this method as abstract
+     * but the one for the server doesn't seem to have it.
+     */
+    /*int maxBytesPerChar() {
+        return 4;
+    }*/
+
+    public void convertFromJavaString(String sourceString, ByteBuffer buffer) {
+        buffer.put(convertFromJavaString(sourceString));
+    }
+
+}

Propchange: db/derby/code/trunk/java/drda/org/apache/derby/impl/drda/Utf8CcsidManager.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derby/impl/drda/ProtocolTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derby/impl/drda/ProtocolTest.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derby/impl/drda/ProtocolTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derby/impl/drda/ProtocolTest.java Thu Jun
24 15:16:41 2010
@@ -152,8 +152,8 @@ public class ProtocolTest
         try {
             Reader cmdStream = new StringReader(this.commandSequence);
             processCommands(cmdStream,
-                            new DDMReader(ccsidManager, monitorIs),
-                            new DDMWriter(ccsidManager, null, null),
+                            new DDMReader(monitorIs),
+                            new DDMWriter(null, null),
                             monitorOs);
         } finally {
             monitorIs.close();
@@ -554,7 +554,7 @@ public class ProtocolTest
         if (!str.startsWith("0x"))
         {
             //just convert the string to ebcdic byte array
-            return ccsidManager.convertFromUCS2(str);
+            return ccsidManager.convertFromJavaString(str);
         }
         else
         {
@@ -768,7 +768,7 @@ public class ProtocolTest
      */
     private byte[] getEBCDIC(String str)
     {
-        return ccsidManager.convertFromUCS2(str);
+        return ccsidManager.convertFromJavaString(str);
     }
 
     /**

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/Utf8CcsidManagerTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/Utf8CcsidManagerTest.java?rev=957580&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/Utf8CcsidManagerTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/Utf8CcsidManagerTest.java
Thu Jun 24 15:16:41 2010
@@ -0,0 +1,98 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.Utf8CcsidManagerTest
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+package org.apache.derbyTesting.functionTests.tests.derbynet;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import junit.framework.Test;
+
+import org.apache.derby.impl.drda.Utf8CcsidManager;
+import org.apache.derbyTesting.junit.BaseTestCase;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+/* This test uses internal APIs which might change over consequent releases. */
+public class Utf8CcsidManagerTest extends BaseTestCase {
+    private Utf8CcsidManager ccsidManager;
+    
+    public Utf8CcsidManagerTest(String name) {
+        super(name);
+        
+        ccsidManager = new Utf8CcsidManager();
+    }
+    
+    /**
+     * Use the Utf8CcsidManager to convert strings from UCS2/UTF-16 into UTF-8
+     */
+    public void testConvertFromUCS2() throws Exception {
+        // Get the UTF-16 representation of "Hello World" in Chinese
+        String ucs2String = new String(new String("\u4f60\u597d\u4e16\u754c").getBytes("UTF-16"),"UTF-16");
+        
+        // Get the same as above but in UTF-8
+        byte[] utf8Bytes = new String("\u4f60\u597d\u4e16\u754c").getBytes("UTF-8");
+        
+        // Use the CcsidManager to convert the UTF-16 string to UTF-8 bytes
+        byte[] utf8Converted = ccsidManager.convertFromJavaString(ucs2String);
+        
+        // Compare the bytes
+        assertTrue("UTF-8 conversion isn't equal to bytes",
+                Arrays.equals(utf8Bytes, utf8Converted));
+        
+        // Repeat the process for the conversion using a buffer
+        ByteBuffer buffer = ByteBuffer.allocate(utf8Bytes.length);
+        
+        ccsidManager.convertFromJavaString(ucs2String, buffer);
+        if (buffer.hasArray()) {
+            utf8Converted = buffer.array();
+            
+            assertTrue("UTF-8 conversion isn't equal to bytes (with buffer)",
+                    Arrays.equals(utf8Bytes, utf8Converted));
+        } else {
+            fail("Could not convert from UCS2 to UTF-8 using a buffer");
+        }
+    }
+    
+    /**
+     * Use the Utf8CcsidManager to convert strings from UTF-8 into UCS2/UTF-16
+     */
+    public void testConvertToUCS2() throws Exception {
+        // Get the UTF-8 bytes for "Hello World" in Chinese
+        byte[] utf8Bytes = new String("\u4f60\u597d\u4e16\u754c").getBytes("UTF-8");
+        
+        // Get the UTF-16 string for "Hello World" in Chinese
+        String ucs2String = new String(new String("\u4f60\u597d\u4e16\u754c").getBytes("UTF-16"),"UTF-16");
+        
+        // Get the 2nd and 3rd Chinese characters in UTF-16
+        String offsetUcs2String = new String(new String("\u597d\u4e16").getBytes("UTF-16"),"UTF-16");
+        
+        // Convert our UTF-8 bytes to UTF-16 using the CcsidManager and compare
+        String convertedString = ccsidManager.convertToJavaString(utf8Bytes);
+        assertEquals(ucs2String, convertedString);
+        
+        // Convert just the two characters as offset above and compare
+        String convertedOffset = ccsidManager.convertToJavaString(utf8Bytes, 1, 2);
+        assertEquals(offsetUcs2String, convertedOffset);
+    }
+    
+    public static Test suite() {
+        return TestConfiguration.clientServerSuite(Utf8CcsidManagerTest.class);
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/Utf8CcsidManagerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java?rev=957580&r1=957579&r2=957580&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/_Suite.java
Thu Jun 24 15:16:41 2010
@@ -63,7 +63,7 @@ public class _Suite extends BaseTestCase
         suite.addTest(LOBLocatorReleaseTest.suite());
         suite.addTest(OutBufferedStreamTest.suite());
         suite.addTest(GetCurrentPropertiesTest.suite());
-
+        suite.addTest(Utf8CcsidManagerTest.suite());
 
         // Disabled due to "java.sql.SQLSyntaxErrorException: The class
         // 'org.apache.derbyTesting.functionTests.tests.derbynet.checkSecMgr'



Mime
View raw message