|
| 1 | +/* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. */ |
| 2 | + |
| 3 | +/****************************************************************************** |
| 4 | + * |
| 5 | + * You may not use the identified files except in compliance with the Apache |
| 6 | + * License, Version 2.0 (the "License.") |
| 7 | + * |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0. |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * The node-oracledb test suite uses 'mocha', 'should' and 'async'. |
| 19 | + * See LICENSE.md for relevant licenses. |
| 20 | + * |
| 21 | + * NAME |
| 22 | + * 67. utf8MultibyteCharacter.js |
| 23 | + * |
| 24 | + * DESCRIPTION |
| 25 | + * Testing UTF-8 multibyte characters. |
| 26 | + * |
| 27 | + * NUMBERING RULE |
| 28 | + * Test numbers follow this numbering rule: |
| 29 | + * 1 - 20 are reserved for basic functional tests |
| 30 | + * 21 - 50 are reserved for data type supporting tests |
| 31 | + * 51 onwards are for other tests |
| 32 | + * |
| 33 | + *****************************************************************************/ |
| 34 | +'use strict'; |
| 35 | + |
| 36 | +var oracledb = require('oracledb'); |
| 37 | +var should = require('should'); |
| 38 | +var async = require('async'); |
| 39 | +var dbConfig = require('./dbconfig.js'); |
| 40 | + |
| 41 | +describe('67. utf8MultibyteCharacter.js', function() { |
| 42 | + |
| 43 | + var connection = null; |
| 44 | + var strLength = 10; |
| 45 | + |
| 46 | + before('get one connection', function(done) { |
| 47 | + oracledb.getConnection(dbConfig, function(err, conn) { |
| 48 | + should.not.exist(err); |
| 49 | + connection = conn; |
| 50 | + done(); |
| 51 | + }); |
| 52 | + }) |
| 53 | + |
| 54 | + after('release connection', function(done) { |
| 55 | + connection.release( function(err) { |
| 56 | + should.not.exist(err); |
| 57 | + done(); |
| 58 | + }); |
| 59 | + }) |
| 60 | + |
| 61 | + it('67.1 works with UTF-8 multibyte characters', function(done) { |
| 62 | + async.series([ |
| 63 | + function doCreate(cb) { |
| 64 | + var proc = "BEGIN \n" + |
| 65 | + " DECLARE \n" + |
| 66 | + " e_table_missing EXCEPTION; \n" + |
| 67 | + " PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \n" + |
| 68 | + " BEGIN \n" + |
| 69 | + " EXECUTE IMMEDIATE('DROP TABLE nodb_testutf8'); \n" + |
| 70 | + " EXCEPTION \n" + |
| 71 | + " WHEN e_table_missing \n" + |
| 72 | + " THEN NULL; \n" + |
| 73 | + " END; \n" + |
| 74 | + " EXECUTE IMMEDIATE (' \n" + |
| 75 | + " CREATE TABLE nodb_testutf8 ( \n" + |
| 76 | + " id NUMBER(9), \n" + |
| 77 | + " name VARCHAR2(30) \n" + |
| 78 | + " ) \n" + |
| 79 | + " '); \n" + |
| 80 | + "END; "; |
| 81 | + |
| 82 | + connection.execute( |
| 83 | + proc, |
| 84 | + function(err) { |
| 85 | + should.not.exist(err); |
| 86 | + return cb(); |
| 87 | + } |
| 88 | + ); |
| 89 | + }, |
| 90 | + function doInsert(cb) { |
| 91 | + var sql = "INSERT INTO nodb_testutf8 \n" + |
| 92 | + " SELECT 1, rpad( unistr('\\20ac'), " + strLength + ", unistr('\\20ac') ) FROM dual"; |
| 93 | + |
| 94 | + connection.execute( |
| 95 | + sql, |
| 96 | + function(err) { |
| 97 | + should.not.exist(err); |
| 98 | + return cb(); |
| 99 | + } |
| 100 | + ); |
| 101 | + }, |
| 102 | + function doCommit(cb) { |
| 103 | + connection.commit(function(err) { |
| 104 | + should.not.exist(err); |
| 105 | + return cb(); |
| 106 | + }); |
| 107 | + }, |
| 108 | + function doSelect(cb) { |
| 109 | + connection.execute( |
| 110 | + "SELECT name FROM nodb_testutf8", |
| 111 | + function(err, result) { |
| 112 | + should.not.exist(err); |
| 113 | + var byteLen = getByteLen(result.rows[0][0]); |
| 114 | + byteLen.should.be.exactly(strLength * 3); |
| 115 | + (result.rows[0][0]).should.eql('€€€€€€€€€€'); |
| 116 | + |
| 117 | + return cb(); |
| 118 | + } |
| 119 | + ); |
| 120 | + }, |
| 121 | + function doplsql(cb) { |
| 122 | + var proc = "BEGIN \n" + |
| 123 | + " SELECT name INTO :o FROM nodb_testutf8; \n" + |
| 124 | + "END;"; |
| 125 | + var bindVar = { o: { type: oracledb.STRING, dir: oracledb.BIND_OUT, maxSize: 30 } }; |
| 126 | + |
| 127 | + connection.execute( |
| 128 | + proc, |
| 129 | + bindVar, |
| 130 | + function(err, result) { |
| 131 | + should.not.exist(err); |
| 132 | + var byteLen = getByteLen(result.outBinds.o); |
| 133 | + byteLen.should.be.exactly(strLength * 3); |
| 134 | + (result.outBinds.o).should.eql('€€€€€€€€€€'); |
| 135 | + |
| 136 | + return cb(); |
| 137 | + } |
| 138 | + ); |
| 139 | + }, |
| 140 | + function doDrop(cb) { |
| 141 | + connection.execute( |
| 142 | + "DROP TABLE nodb_testutf8", |
| 143 | + function(err) { |
| 144 | + should.not.exist(err); |
| 145 | + return cb(); |
| 146 | + } |
| 147 | + ); |
| 148 | + } |
| 149 | + ], done); |
| 150 | + }) |
| 151 | + |
| 152 | +}) |
| 153 | + |
| 154 | +/* |
| 155 | +* Count bytes of a utf-8 String |
| 156 | +*/ |
| 157 | +var getByteLen = function(str) { |
| 158 | + // String type conversion |
| 159 | + str = String(str); |
| 160 | + |
| 161 | + var byteLen = 0; |
| 162 | + for (var i = 0; i < str.length; i++) { |
| 163 | + var ch = str.charCodeAt(i); |
| 164 | + byteLen += ch < (1 << 7) ? 1 : |
| 165 | + ch < (1 << 11) ? 2 : |
| 166 | + ch < (1 << 16) ? 3 : |
| 167 | + ch < (1 << 21) ? 4 : |
| 168 | + ch < (1 << 26) ? 5 : |
| 169 | + ch < (1 << 31) ? 6 : |
| 170 | + Number.NaN; |
| 171 | + } |
| 172 | + |
| 173 | + return byteLen; |
| 174 | +} |
0 commit comments