KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > drda > TestProto


1 /*
2
3    Derby - Class org.apache.derby.impl.drda.TestProto
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.drda;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.io.StreamTokenizer JavaDoc;
32 import java.io.UnsupportedEncodingException JavaDoc;
33 import java.net.Socket JavaDoc;
34 import java.net.UnknownHostException JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.Hashtable JavaDoc;
37 import java.util.Locale JavaDoc;
38 import java.util.Vector JavaDoc;
39
40 /**
41     This class is used to test error conditions in the protocol.
42     The protocol to send to the Net server is contained in a file encoded
43     as calls to routines in ddmreader and ddmwriter.
44     Additional commands have been added for testing purposes.
45     To add tests, modify the file protocol.tests.
46     Tests can also be done as separate files and given as an argument to
47     this class.
48 */

49
50 public class TestProto {
51
52     private static final CodePointNameTable codePointNameTable = new CodePointNameTable();
53     private static final Hashtable JavaDoc codePointValueTable = new Hashtable JavaDoc();
54     private static final Hashtable JavaDoc commandTable = new Hashtable JavaDoc();
55     private static final CcsidManager ccsidManager = new EbcdicCcsidManager();
56     //commands
57
private static final int CREATE_DSS_REQUEST = 1;
58     private static final int CREATE_DSS_OBJECT = 2;
59     private static final int END_DSS = 3;
60     private static final int END_DDM_AND_DSS = 4;
61     private static final int START_DDM = 5;
62     private static final int END_DDM = 6;
63     private static final int WRITE_BYTE = 7;
64     private static final int WRITE_NETWORK_SHORT = 8;
65     private static final int WRITE_NETWORK_INT = 9;
66     private static final int WRITE_BYTES = 10;
67     private static final int WRITE_CODEPOINT_4BYTES = 11;
68     private static final int WRITE_SCALAR_1BYTE = 12;
69     private static final int WRITE_SCALAR_2BYTES = 13;
70     private static final int WRITE_SCALAR_BYTES = 14;
71     private static final int WRITE_SCALAR_HEADER = 15;
72     private static final int WRITE_SCALAR_STRING = 16;
73     private static final int WRITE_SCALAR_PADDED_STRING = 17;
74     private static final int WRITE_SCALAR_PADDED_BYTES = 18;
75     private static final int WRITE_SHORT = 19;
76     private static final int WRITE_INT = 20;
77     private static final int WRITE_LONG = 21;
78     private static final int WRITE_FLOAT = 22;
79     private static final int WRITE_DOUBLE = 23;
80     private static final int READ_REPLY_DSS = 24;
81     private static final int READ_LENGTH_AND_CODEPOINT = 25;
82     private static final int READ_CODEPOINT = 26;
83     private static final int MARK_COLLECTION = 27;
84     private static final int GET_CODEPOINT = 28;
85     private static final int READ_BYTE = 29;
86     private static final int READ_NETWORK_SHORT = 30;
87     private static final int READ_SHORT = 31;
88     private static final int READ_NETWORK_INT = 32;
89     private static final int READ_INT = 33;
90     private static final int READ_LONG = 34;
91     private static final int READ_BOOLEAN = 35;
92     private static final int READ_STRING = 36;
93     private static final int READ_BYTES = 37;
94     private static final int FLUSH = 38;
95     private static final int DISPLAY = 39;
96     private static final int CHECKERROR = 40;
97     private static final int RESET = 41;
98     private static final int CREATE_DSS_REPLY = 42;
99     private static final int SKIP_DSS = 43;
100     private static final int READ_SCALAR_2BYTES = 44;
101     private static final int READ_SCALAR_1BYTE = 45;
102     private static final int END_TEST = 46;
103     private static final int SKIP_DDM = 47;
104     private static final int INCLUDE = 48;
105     private static final int SKIP_BYTES = 49;
106     private static final int WRITE_PADDED_STRING = 50;
107     private static final int WRITE_STRING = 51;
108     private static final int WRITE_ENCODED_STRING = 52;
109     private static final int WRITE_ENCODED_LDSTRING = 53;
110     private static final int CHECK_SQLCARD = 54;
111     private static final int MORE_DATA = 55;
112     private static final int COMPLETE_TEST = 56;
113     private static final int READ_SECMEC_SECCHKCD = 57;
114
115     private static final String JavaDoc MULTIVAL_START = "MULTIVALSTART";
116     private static final String JavaDoc MULTIVAL_SEP = "SEP";
117     private static final String JavaDoc MULTIVAL_END = "MULTIVALEND";
118     // initialize hash tables
119
static {
120             init();
121             }
122
123
124     private Socket JavaDoc monitorSocket = null;
125     private InputStream JavaDoc monitorIs = null;
126     private OutputStream JavaDoc monitorOs = null;
127     private DDMWriter writer = new DDMWriter(ccsidManager, null, null);
128     private DDMReader reader;
129     private boolean failed = false;
130     private StreamTokenizer JavaDoc tkn;
131     private String JavaDoc current_filename;
132
133     // constructor
134
public TestProto(String JavaDoc filename)
135     {
136         current_filename = filename;
137         getConnection();
138
139         try
140         {
141             reader = new DDMReader(ccsidManager, monitorIs);
142             processFile(filename);
143         }
144         catch (Exception JavaDoc e)
145         {
146             int line = 0;
147             if (tkn != null)
148                 line = tkn.lineno();
149             System.err.println("Unexpected exception in line " + line + " file: " + current_filename);
150             e.printStackTrace();
151         }
152         finally
153         {
154             closeConnection();
155         }
156
157     }
158     /**
159      * Process include file
160      *
161      * @exception IOException, DRDAProtocolException error reading file or protocol
162      */

163     private void processIncludeFile()
164         throws IOException JavaDoc, DRDAProtocolException
165     {
166         String JavaDoc fileName = getString();
167         StreamTokenizer JavaDoc saveTkn = tkn;
168         processFile(fileName);
169         tkn = saveTkn;
170     }
171     /**
172      * Process a command file
173      *
174      * @param filename
175      * @exception IOException, DRDAProtocolException error reading file or protocol
176      */

177     private void processFile(String JavaDoc filename)
178         throws IOException JavaDoc, DRDAProtocolException
179     {
180         String JavaDoc prev_filename = current_filename;
181         current_filename = filename;
182             String JavaDoc hostName=getHostName();
183         BufferedReader JavaDoc fr;
184                 try
185         {
186             fr = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(filename),"UTF-8"));
187         } catch (FileNotFoundException JavaDoc fnfe) {
188             // if useprocess=false & we're running in a suite,
189
// the location is different, try it
190
String JavaDoc userdir = System.getProperty("user.dir");
191             String JavaDoc sep = System.getProperty("file.separator");
192             fr = new BufferedReader JavaDoc (new InputStreamReader JavaDoc(new FileInputStream JavaDoc(userdir + sep + ".." + sep + filename),"UTF-8"));
193         }
194         tkn = new StreamTokenizer JavaDoc(fr);
195         int val;
196         while ( (val = tkn.nextToken()) != StreamTokenizer.TT_EOF)
197         {
198             switch(val)
199             {
200                 case StreamTokenizer.TT_NUMBER:
201                     break;
202                 case StreamTokenizer.TT_WORD:
203                     processCommand();
204                     break;
205                 case StreamTokenizer.TT_EOL:
206                     break;
207             }
208         }
209         current_filename = prev_filename;
210     }
211     /**
212      * Set up a connection to the Network server
213      */

214     private void getConnection()
215     {
216         String JavaDoc hostName=getHostName();
217         try {
218             monitorSocket = new Socket JavaDoc(hostName,1527);
219         } catch (UnknownHostException JavaDoc e) {
220             System.err.println("Don't know about host: " + hostName);
221             System.exit(1);
222         } catch (IOException JavaDoc e) {
223             System.err.println("Couldn't get I/O for the connection to: " + hostName);
224             System.exit(1);
225         }
226         try
227         {
228            monitorIs = monitorSocket.getInputStream();
229            monitorOs = monitorSocket.getOutputStream();
230         }
231         catch (IOException JavaDoc e)
232         {
233             System.err.println("Couldn't get I/O for the connection to: " + hostName);
234             System.exit(1);
235         }
236     }
237     /**
238      * Close connection to the network server
239      */

240     private void closeConnection()
241     {
242         try {
243             monitorIs.close();
244             monitorOs.close();
245             monitorSocket.close();
246         }
247         catch (Exception JavaDoc e) {} //Ignore exceptions when closing the connection
248
}
249     /**
250      * Reset connection for another test
251      */

252     private void reset()
253     {
254         closeConnection();
255         getConnection();
256         reader.initialize(monitorIs);
257         writer.reset(null);
258     }
259     /**
260      * finish by cleaning up the last connection
261      */

262     private void completeTest()
263     {
264         closeConnection();
265     }
266     /**
267      * Initialize hashtable for commands and set up a table to translate from
268      * the codepoint name to the codepoint value
269      */

270     private static void init()
271     {
272         commandTable.put("createdssrequest", new Integer JavaDoc(CREATE_DSS_REQUEST));
273         commandTable.put("createdssobject", new Integer JavaDoc(CREATE_DSS_OBJECT));
274         commandTable.put("createdssreply", new Integer JavaDoc(CREATE_DSS_REPLY));
275         commandTable.put("enddss", new Integer JavaDoc(END_DSS));
276         commandTable.put("enddss", new Integer JavaDoc(END_DSS));
277         commandTable.put("endddmanddss", new Integer JavaDoc(END_DDM_AND_DSS));
278         commandTable.put("startddm", new Integer JavaDoc(START_DDM));
279         commandTable.put("endddm", new Integer JavaDoc(END_DDM));
280         commandTable.put("writebyte", new Integer JavaDoc(WRITE_BYTE));
281         commandTable.put("writenetworkshort", new Integer JavaDoc(WRITE_NETWORK_SHORT));
282         commandTable.put("writenetworkint", new Integer JavaDoc(WRITE_NETWORK_INT));
283         commandTable.put("writebytes", new Integer JavaDoc(WRITE_BYTES));
284         commandTable.put("writecodepoint4bytes", new Integer JavaDoc(WRITE_CODEPOINT_4BYTES));
285         commandTable.put("writescalar1byte", new Integer JavaDoc(WRITE_SCALAR_1BYTE));
286         commandTable.put("writescalar2bytes", new Integer JavaDoc(WRITE_SCALAR_2BYTES));
287         commandTable.put("writescalarbytes", new Integer JavaDoc(WRITE_SCALAR_BYTES));
288         commandTable.put("writescalarheader", new Integer JavaDoc(WRITE_SCALAR_HEADER));
289         commandTable.put("writescalarstring", new Integer JavaDoc(WRITE_SCALAR_STRING));
290         commandTable.put("writescalarpaddedstring", new Integer JavaDoc(WRITE_SCALAR_PADDED_STRING));
291         commandTable.put("writescalarpaddedbytes", new Integer JavaDoc(WRITE_SCALAR_PADDED_BYTES));
292         commandTable.put("writeshort", new Integer JavaDoc(WRITE_SHORT));
293         commandTable.put("writeint", new Integer JavaDoc(WRITE_INT));
294         commandTable.put("writelong", new Integer JavaDoc(WRITE_LONG));
295         commandTable.put("writefloat", new Integer JavaDoc(WRITE_FLOAT));
296         commandTable.put("writedouble", new Integer JavaDoc(WRITE_DOUBLE));
297         commandTable.put("readreplydss", new Integer JavaDoc(READ_REPLY_DSS));
298         commandTable.put("readlengthandcodepoint", new Integer JavaDoc(READ_LENGTH_AND_CODEPOINT));
299         commandTable.put("readcodepoint", new Integer JavaDoc(READ_CODEPOINT));
300         commandTable.put("markcollection", new Integer JavaDoc(MARK_COLLECTION));
301         commandTable.put("getcodepoint", new Integer JavaDoc(GET_CODEPOINT));
302         commandTable.put("readbyte", new Integer JavaDoc(READ_BYTE));
303         commandTable.put("readnetworkshort", new Integer JavaDoc(READ_NETWORK_SHORT));
304         commandTable.put("readshort", new Integer JavaDoc(READ_SHORT));
305         commandTable.put("readint", new Integer JavaDoc(READ_INT));
306         commandTable.put("readlong", new Integer JavaDoc(READ_LONG));
307         commandTable.put("readboolean", new Integer JavaDoc(READ_BOOLEAN));
308         commandTable.put("readstring", new Integer JavaDoc(READ_STRING));
309         commandTable.put("readbytes", new Integer JavaDoc(READ_BYTES));
310         commandTable.put("flush", new Integer JavaDoc(FLUSH));
311         commandTable.put("display", new Integer JavaDoc(DISPLAY));
312         commandTable.put("checkerror", new Integer JavaDoc(CHECKERROR));
313         commandTable.put("reset", new Integer JavaDoc(RESET));
314         commandTable.put("skipdss", new Integer JavaDoc(SKIP_DSS));
315         commandTable.put("skipddm", new Integer JavaDoc(SKIP_DDM));
316         commandTable.put("readscalar2bytes", new Integer JavaDoc(READ_SCALAR_2BYTES));
317         commandTable.put("readscalar1byte", new Integer JavaDoc(READ_SCALAR_1BYTE));
318         commandTable.put("endtest", new Integer JavaDoc(END_TEST));
319         commandTable.put("include", new Integer JavaDoc(INCLUDE));
320         commandTable.put("skipbytes", new Integer JavaDoc(SKIP_BYTES));
321         commandTable.put("writepaddedstring", new Integer JavaDoc(WRITE_PADDED_STRING));
322         commandTable.put("writestring", new Integer JavaDoc(WRITE_STRING));
323         commandTable.put("writeencodedstring", new Integer JavaDoc(WRITE_ENCODED_STRING));
324         commandTable.put("writeencodedldstring", new Integer JavaDoc(WRITE_ENCODED_LDSTRING));
325         commandTable.put("checksqlcard", new Integer JavaDoc(CHECK_SQLCARD));
326         commandTable.put("moredata", new Integer JavaDoc(MORE_DATA));
327         commandTable.put("completetest", new Integer JavaDoc(COMPLETE_TEST));
328         commandTable.put("readsecmecandsecchkcd", new Integer JavaDoc(READ_SECMEC_SECCHKCD));
329         
330         Integer JavaDoc key;
331         for (Enumeration JavaDoc e = codePointNameTable.keys(); e.hasMoreElements(); )
332         {
333             key = (Integer JavaDoc)e.nextElement();
334             codePointValueTable.put(codePointNameTable.get(key), key);
335         }
336         
337     }
338     /**
339      * Process a command
340      */

341     private void processCommand()
342         throws IOException JavaDoc, DRDAProtocolException
343     {
344         Integer JavaDoc icmd = (Integer JavaDoc)commandTable.get(tkn.sval.toLowerCase(Locale.ENGLISH));
345         if (icmd == null)
346         {
347             System.err.println("Unknown command, " + tkn.sval + " in line " +
348                 tkn.lineno());
349             System.exit(1);
350         }
351         int cmd = icmd.intValue();
352         int codepoint;
353         int val;
354         int reqVal;
355         String JavaDoc str;
356
357         switch (cmd)
358         {
359             case INCLUDE:
360                 processIncludeFile();
361                 break;
362             case CREATE_DSS_REQUEST:
363                 writer.createDssRequest();
364                 break;
365             case CREATE_DSS_OBJECT:
366                 writer.createDssObject();
367                 break;
368             case CREATE_DSS_REPLY:
369                 writer.createDssReply();
370                 break;
371             case END_DSS:
372                 tkn.nextToken();
373                 tkn.pushBack();
374                 if ((tkn.sval != null) && tkn.sval.startsWith("0x"))
375                 // use specified chaining.
376
writer.endDss((getBytes())[0]);
377                 else
378                 // use default chaining
379
writer.endDss();
380                 break;
381             case END_DDM:
382                 writer.endDdm();
383                 break;
384             case END_DDM_AND_DSS:
385                 writer.endDdmAndDss();
386                 break;
387             case START_DDM:
388                 writer.startDdm(getCP());
389                 break;
390             case WRITE_SCALAR_STRING:
391                 writer.writeScalarString(getCP(), getString());
392                 break;
393             case WRITE_SCALAR_2BYTES:
394                 writer.writeScalar2Bytes(getCP(),getIntOrCP());
395                 break;
396             case WRITE_SCALAR_1BYTE:
397                 writer.writeScalar1Byte(getCP(),getInt());
398                 break;
399             case WRITE_SCALAR_BYTES:
400                 writer.writeScalarBytes(getCP(),getBytes());
401                 break;
402             case WRITE_SCALAR_PADDED_BYTES:
403                 writer.writeScalarPaddedBytes(getCP(), getBytes(), getInt(),
404                     ccsidManager.space);
405                 break;
406             case WRITE_BYTE:
407                 writer.writeByte(getInt());
408                 break;
409             case WRITE_BYTES:
410                 writer.writeBytes(getBytes());
411                 break;
412             case WRITE_SHORT:
413                 writer.writeShort(getInt());
414                 break;
415             case WRITE_INT:
416                 writer.writeInt(getInt());
417                 break;
418             case WRITE_CODEPOINT_4BYTES:
419                 writer.writeCodePoint4Bytes(getCP(), getInt());
420                 break;
421             case WRITE_STRING:
422                 str = getString();
423                 writer.writeBytes(getEBCDIC(str));
424                 break;
425             case WRITE_ENCODED_STRING:
426                 writeEncodedString(getString(), getString());
427                 break;
428             case WRITE_ENCODED_LDSTRING:
429                 writeEncodedLDString(getString(), getString(), getInt());
430                 break;
431             case WRITE_PADDED_STRING:
432                 str = getString();
433                 writer.writeBytes(getEBCDIC(str));
434                 int reqLen = getInt();
435                 int strLen = str.length();
436                 if (strLen < reqLen)
437                     writer.padBytes(ccsidManager.space, reqLen-strLen);
438                 break;
439             case READ_REPLY_DSS:
440                 reader.readReplyDss();
441                 break;
442             case SKIP_DSS:
443                 skipDss();
444                 break;
445             case SKIP_DDM:
446                 skipDdm();
447                 break;
448             case MORE_DATA:
449                 boolean expbool;
450                 str = getString();
451                 if (str.equalsIgnoreCase("true"))
452                     expbool = true;
453                 else
454                     expbool = false;
455                 if (reader.moreData() && expbool == false )
456                     fail("Failed - more data left");
457                 if (!reader.moreData() && expbool == true )
458                     fail("Failed - no data left");
459                 break;
460             case READ_LENGTH_AND_CODEPOINT:
461                 readLengthAndCodePoint();
462                 break;
463             case READ_SCALAR_2BYTES:
464                 readLengthAndCodePoint();
465                 val = reader.readNetworkShort();
466                 checkIntOrCP(val);
467                 break;
468             case READ_SCALAR_1BYTE:
469                 readLengthAndCodePoint();
470                 val = reader.readByte();
471                 checkIntOrCP(val);
472                 break;
473             case READ_SECMEC_SECCHKCD:
474                 readSecMecAndSECCHKCD();
475                 break;
476             case READ_BYTES:
477                 byte[] byteArray = reader.readBytes();
478                 byte[] reqArray = getBytes();
479                 if (byteArray.length != reqArray.length)
480                         fail("Failed - byte array didn't match");
481                 for (int i = 0; i < byteArray.length; i++)
482                     if (byteArray[i] != reqArray[i])
483                         fail("Failed - byte array didn't match");
484                 break;
485             case READ_NETWORK_SHORT:
486                 val = reader.readNetworkShort();
487                 checkIntOrCP(val);
488                 break;
489             case FLUSH:
490                 writer.finalizeChain(reader.getCurrChainState(), monitorOs);
491                 writer.reset(null);
492                 break;
493             case DISPLAY:
494                 System.out.println(getString());
495                 break;
496             case CHECKERROR:
497                 checkError();
498                 break;
499             case CHECK_SQLCARD:
500                 checkSQLCARD(getInt(), getString());
501                 break;
502             case COMPLETE_TEST:
503                 completeTest();
504                 break;
505             case END_TEST:
506                 // print that we passed the test if we haven't failed
507
if (failed == false)
508                     System.out.println("PASSED");
509                 failed = false;
510                 reset();
511                 break;
512             case RESET:
513                 reset();
514                 break;
515             case SKIP_BYTES:
516                 reader.skipBytes();
517                 break;
518             default:
519                 System.out.println("unknown command in line " + tkn.lineno());
520                 // skip remainder of line
521
while (tkn.nextToken() != StreamTokenizer.TT_EOL)
522                         ;
523     
524         }
525     }
526     /**
527      * Skip a DSS communication
528      */

529     private void skipDss() throws DRDAProtocolException
530     {
531         reader.readReplyDss();
532         reader.skipDss();
533     }
534     /**
535      * Skip the a Ddm communication
536      */

537     private void skipDdm() throws DRDAProtocolException
538     {
539         reader.readLengthAndCodePoint();
540         reader.skipBytes();
541     }
542     /**
543      * Read an int from the command file
544      * Negative numbers are preceded by "-"
545      */

546     private int getInt() throws IOException JavaDoc
547     {
548         int mult = 1;
549         int val = tkn.nextToken();
550         if (tkn.sval != null && tkn.sval.equals("-"))
551         {
552             mult = -1;
553             val = tkn.nextToken();
554         }
555
556         if (val != StreamTokenizer.TT_NUMBER)
557         {
558             if (tkn.sval == null)
559             {
560                 System.err.println("Invalid string on line " + tkn.lineno());
561                 System.exit(1);
562             }
563             String JavaDoc str = tkn.sval.toLowerCase(Locale.ENGLISH);
564             if (!str.startsWith("0x"))
565             {
566                 System.err.println("Expecting number, got " + tkn.sval + " on line " + tkn.lineno());
567                 System.exit(1);
568             }
569             else
570                 return convertHex(str);
571         }
572         return (new Double JavaDoc(tkn.nval).intValue() * mult);
573     }
574     /**
575      * Convert a token in hex format to int from the command file
576      */

577     private int convertHex(String JavaDoc str) throws IOException JavaDoc
578     {
579         int retval = 0;
580         int len = str.length();
581         if ((len % 2) == 1 || len > 10)
582         {
583             System.err.println("Invalid length for byte string, " + len +
584             " on line " + tkn.lineno());
585             System.exit(1);
586         }
587         for (int i = 2; i < len; i++)
588         {
589             retval = retval << 4;
590             retval += Byte.valueOf(str.substring(i, i+1), 16).byteValue();
591         }
592         return retval;
593     }
594
595     /**
596      * checks if value matches next int or cp.
597      * Handles multiple legal values in protocol test file
598      * FORMAT for Multiple Values
599      * MULTIVALSTART 10 SEP 32 SEP 40 MULTIVALEND
600      **/

601     private boolean checkIntOrCP(int val) throws IOException JavaDoc
602     {
603         boolean rval = false;
604         int tknType = tkn.nextToken();
605         String JavaDoc reqVal = " ";
606
607         
608         if (tknType == StreamTokenizer.TT_WORD && tkn.sval.trim().equals(MULTIVAL_START))
609         {
610             do {
611                 int nextVal = getIntOrCP();
612                 reqVal = reqVal + nextVal + " ";
613                 // System.out.println("Checking MULTIVAL (" + val + "==" + nextVal + ")");
614
rval = rval || (val == nextVal);
615                 tkn.nextToken();
616             }
617             while(tkn.sval.trim().equals(MULTIVAL_SEP));
618             
619             if (! (tkn.sval.trim().equals(MULTIVAL_END)))
620                 fail("Invalid test file format requires " + MULTIVAL_END +
621                      " got: " + tkn.sval);
622             
623         }
624         else
625         {
626             tkn.pushBack();
627             int nextVal = getIntOrCP();
628             reqVal = " " + nextVal;
629             // System.out.println("Checking Single Value (" + val + "==" + nextVal + ")");
630
rval = (val == nextVal);
631         }
632         if (rval == false)
633             fail("Failed - wrong val = " + val + " Required Value: " + reqVal);
634
635         return rval;
636     }
637
638
639     /**
640      * Read an int or codepoint - codepoint is given as a string
641      */

642     private int getIntOrCP() throws IOException JavaDoc
643     {
644         int val = tkn.nextToken();
645         if (val == StreamTokenizer.TT_NUMBER)
646         {
647             return new Double JavaDoc(tkn.nval).intValue();
648         }
649         else if (val == StreamTokenizer.TT_WORD)
650         {
651             return decodeCP(tkn.sval);
652         }
653         else
654         {
655             fail("Expecting number, got " + tkn.sval + " on line "
656                                + tkn.lineno());
657             System.exit(1);
658         }
659         return 0;
660     }
661     /**
662      * Read an array of bytes from the command file
663      * A byte string can start with 0x in which case the bytes are interpreted
664      * in hex format or it can just be a string, in which case each char is
665      * interpreted as 2 byte UNICODE
666      *
667      * @return byte array
668      */

669     private byte [] getBytes() throws IOException JavaDoc
670     {
671         byte[] retval = null;
672         int val = tkn.nextToken();
673         if (tkn.sval == null)
674         {
675             System.err.println("Invalid string on line " + tkn.lineno());
676             System.exit(1);
677         }
678         String JavaDoc str = tkn.sval.toLowerCase(Locale.ENGLISH);
679         if (!str.startsWith("0x"))
680         {
681             //just convert the string to ebcdic byte array
682
return ccsidManager.convertFromUCS2(str);
683         }
684         else
685         {
686             int len = str.length();
687             if ((len % 2) == 1)
688             {
689                 System.err.println("Invalid length for byte string, " + len +
690                 " on line " + tkn.lineno());
691                 System.exit(1);
692             }
693             retval = new byte[(len-2)/2];
694             int j = 0;
695             for (int i = 2; i < len; i+=2, j++)
696             {
697                 retval[j] = (byte)(Byte.valueOf(str.substring(i, i+1), 16).byteValue() << 4);
698                 retval[j] += Byte.valueOf(str.substring(i+1, i+2), 16).byteValue();
699             }
700         }
701         return retval;
702     }
703     /**
704      * Read a string from the command file
705      *
706      * @return string found in file
707      * @exception IOException error reading file
708      */

709     private String JavaDoc getString() throws IOException JavaDoc
710     {
711         int val = tkn.nextToken();
712         if (val == StreamTokenizer.TT_NUMBER)
713         {
714             System.err.println("Expecting word, got " + tkn.nval + " on line " + tkn.lineno());
715             System.exit(1);
716         }
717         return tkn.sval;
718     }
719     /**
720      * Read the string version of a CodePoint
721      *
722      * @exception IOException error reading file
723      */

724     private int getCP() throws IOException JavaDoc
725     {
726         String JavaDoc strval = getString();
727         return decodeCP(strval);
728     }
729     /**
730      * Translate a string codepoint such as ACCSEC to the equivalent int value
731      *
732      * @param strval string codepoint
733      * @return integer value of codepoint
734      */

735     private int decodeCP(String JavaDoc strval)
736     {
737         Integer JavaDoc cp = (Integer JavaDoc)codePointValueTable.get(strval);
738         if (cp == null)
739         {
740            System.err.println("Unknown codepoint, "+ strval + " in line "
741                 + tkn.lineno());
742            Exception JavaDoc e = new Exception JavaDoc();
743            e.printStackTrace();
744             System.exit(1);
745         }
746         return cp.intValue();
747     }
748     /**
749      * Print failure message and skip to the next test
750      *
751      * @exception IOException error reading file
752      */

753     private void fail(String JavaDoc msg) throws IOException JavaDoc
754     {
755         System.out.println("FAILED - " + msg + " in line " + tkn.lineno());
756         // skip remainder of the test look for endtest or end of file
757
int val = tkn.nextToken();
758         while (val != StreamTokenizer.TT_EOF)
759         {
760             if (val == StreamTokenizer.TT_WORD && tkn.sval.toLowerCase(Locale.ENGLISH).equals("endtest"))
761                 break;
762
763             val = tkn.nextToken();
764         }
765         failed = true;
766         // get ready for next test
767
reset();
768         // print out stack trace so we know where the failure occurred
769
Exception JavaDoc e = new Exception JavaDoc();
770         e.printStackTrace();
771     }
772     /**
773      * Check error sent back to application requester
774      *
775      * @exception IOException, DRDAProtocolException error reading file or protocol
776      */

777     private void checkError() throws IOException JavaDoc, DRDAProtocolException
778     {
779         int svrcod = 0;
780         int invalidCodePoint = 0;
781         int prccnvcd = 0;
782         int synerrcd = 0;
783         int codepoint;
784         int reqVal;
785         Vector JavaDoc manager = new Vector JavaDoc(), managerLevel = new Vector JavaDoc() ;
786         reader.readReplyDss();
787         int error = reader.readLengthAndCodePoint();
788         int reqCP = getCP();
789         if (error != reqCP)
790         {
791             cpError(error, reqCP);
792             return;
793         }
794         while (reader.moreDssData())
795         {
796             codepoint = reader.readLengthAndCodePoint();
797             switch (codepoint)
798             {
799                 case CodePoint.SVRCOD:
800                     svrcod = reader.readNetworkShort();
801                     break;
802                 case CodePoint.CODPNT:
803                     invalidCodePoint = reader.readNetworkShort();
804                     break;
805                 case CodePoint.PRCCNVCD:
806                     prccnvcd = reader.readByte();
807                     break;
808                 case CodePoint.SYNERRCD:
809                     synerrcd = reader.readByte();
810                     break;
811                 case CodePoint.MGRLVLLS:
812                     while (reader.moreDdmData())
813                     {
814                         manager.addElement(new Integer JavaDoc(reader.readNetworkShort()));
815                         managerLevel.addElement(new Integer JavaDoc(reader.readNetworkShort()));
816                     }
817                     break;
818                 default:
819                     //ignore codepoints we don't understand
820
reader.skipBytes();
821     
822             }
823         }
824         reqVal = getInt();
825         if (svrcod != reqVal)
826         {
827             fail("wrong svrcod val = " + Integer.toHexString(svrcod)
828                     + ", required val = " + Integer.toHexString(reqVal));
829             return;
830         }
831         if (error == CodePoint.PRCCNVRM)
832         {
833             reqVal = getInt();
834             if (prccnvcd != reqVal)
835             {
836                 fail("wrong prccnvd, val = " + Integer.toHexString(prccnvcd)
837                     + ", required val = " + Integer.toHexString(reqVal));
838                 return;
839             }
840         }
841         if (error == CodePoint.SYNTAXRM)
842         {
843             reqVal = getInt();
844             if (synerrcd != reqVal)
845             {
846                 fail("wrong synerrcd, val = " + Integer.toHexString(synerrcd)
847                     + ", required val = " + Integer.toHexString(reqVal));
848                 return;
849             }
850             reqVal = getIntOrCP();
851             if (invalidCodePoint != reqVal)
852             {
853                 cpError(invalidCodePoint, reqVal);
854                 return;
855             }
856         }
857         if (error == CodePoint.MGRLVLRM)
858         {
859             int mgr, mgrLevel;
860             for (int i = 0; i < manager.size(); i++)
861             {
862                 reqVal = getCP();
863                 mgr = ((Integer JavaDoc)(manager.elementAt(i))).intValue();
864                 if (mgr != reqVal)
865                 {
866                     cpError(mgr, reqVal);
867                     return;
868                 }
869                 mgrLevel = ((Integer JavaDoc)(managerLevel.elementAt(i))).intValue();
870                 reqVal = getInt();
871                 if (mgrLevel != reqVal)
872                 {
873                     fail("wrong manager level, level = " + Integer.toHexString(mgrLevel)
874                     + ", required val = " + Integer.toHexString(reqVal));
875                     return;
876                 }
877             }
878         }
879     }
880     /**
881      * Read length and codepoint and check against required values
882      *
883      * @exception IOException, DRDAProtocolException error reading file or protocol
884      */

885     private void readLengthAndCodePoint() throws IOException JavaDoc, DRDAProtocolException
886     {
887         int codepoint = reader.readLengthAndCodePoint();
888         int reqCP = getCP();
889         if (codepoint != reqCP)
890             cpError(codepoint, reqCP);
891     }
892     
893     /**
894      * Handle the case of testing the reading of SECMEC and SECCHKCD,
895      * where on an invalid SECMEC value for ACCSEC, the server can send
896      * valid supported SECMEC values. One of the valid supported value can be
897      * EUSRIDPWD (secmec value of 9) depending on if the server JVM
898      * can actually support it or not.
899      * @exception IOException, DRDAProtocolException error reading file or protocol
900      */

901     private void readSecMecAndSECCHKCD() throws IOException JavaDoc, DRDAProtocolException
902     {
903         int codepoint;
904         boolean notDone = true;
905         int val = -1;
906         do
907         {
908             codepoint = reader.readLengthAndCodePoint();
909             switch(codepoint)
910             {
911               case CodePoint.SECMEC:
912               {
913                   System.out.print("SECMEC=");
914                   val = reader.readNetworkShort();
915                   System.out.print(val+" ");
916               }
917               break;
918               case CodePoint.SECCHKCD:
919               {
920                   System.out.print("SECCHKCD=");
921                   val = reader.readByte();
922                   System.out.println(val);
923                   notDone = false;
924               }
925               break;
926               default:
927                   notDone=false;
928             }
929         }while(notDone);
930     }
931
932     
933     /**
934      * Codepoint error
935      *
936      * @exception IOException error reading command file
937      */

938     private void cpError(int cp, int reqCP) throws IOException JavaDoc
939     {
940         String JavaDoc cpName = codePointNameTable.lookup(cp);
941         String JavaDoc reqCPName = codePointNameTable.lookup(reqCP);
942         fail("wrong codepoint val = " + Integer.toHexString(cp) +
943              "("+cpName+")" +
944              ", required codepoint = " + Integer.toHexString(reqCP) +
945              "("+reqCPName+")");
946     }
947     /**
948      * Translate a string to EBCDIC for use in the protocol
949      *
950      * @param str string to transform
951      * @return EBCDIC string
952      */

953     private byte[] getEBCDIC(String JavaDoc str)
954     {
955         byte [] buf = new byte[str.length()];
956         ccsidManager.convertFromUCS2(str, buf, 0);
957         return buf;
958     }
959     /**
960      * Write an encoded string
961      *
962      * @param str string to write
963      * @param encoding Java encoding to use
964      * @exception IOException
965      */

966     private void writeEncodedString(String JavaDoc str, String JavaDoc encoding)
967         throws IOException JavaDoc
968     {
969         try {
970             byte [] buf = str.getBytes(encoding);
971             writer.writeBytes(buf);
972         } catch (UnsupportedEncodingException JavaDoc e) {
973             fail("Unsupported encoding " + encoding);
974         }
975     }
976     /**
977      * Write length and encoded string
978      *
979      * @param str string to write
980      * @param encoding Java encoding to use
981      * @param len Size of length value (2 or 4 bytes)
982      * @exception IOException
983      */

984     private void writeEncodedLDString(String JavaDoc str, String JavaDoc encoding, int len)
985         throws IOException JavaDoc
986     {
987         try {
988             byte [] buf = str.getBytes(encoding);
989             if (len == 2)
990                 writer.writeShort(buf.length);
991             else
992                 writer.writeInt(buf.length);
993             writer.writeBytes(buf);
994         } catch (UnsupportedEncodingException JavaDoc e) {
995             fail("Unsupported encoding " + encoding);
996         }
997     }
998     /**
999      * Check the value of SQLCARD
1000     *
1001     * @param sqlCode SQLCODE value
1002     * @param sqlState SQLSTATE value
1003     * @exception IOException, DRDAProtocolException
1004     */

1005    private void checkSQLCARD(int sqlCode, String JavaDoc sqlState)
1006        throws IOException JavaDoc, DRDAProtocolException
1007    {
1008        reader.readReplyDss();
1009        int codepoint = reader.readLengthAndCodePoint();
1010        if (codepoint != CodePoint.SQLCARD)
1011        {
1012            fail("Expecting SQLCARD got "+ Integer.toHexString(codepoint));
1013            return;
1014        }
1015        int nullind = reader.readByte();
1016        //cheating here and using readNetworkInt since the byteorder is the same
1017
int code = reader.readNetworkInt();
1018        if (code != sqlCode)
1019        {
1020            fail("Expecting sqlCode " + sqlCode + " got "+ Integer.toHexString(code));
1021            return;
1022        }
1023        String JavaDoc state = reader.readString(5, "UTF-8");
1024        if (!state.equals(sqlState))
1025        {
1026            fail("Expecting sqlState " + sqlState + " got "+ state);
1027            return;
1028        }
1029        // skip the rest of the SQLCARD
1030
reader.skipBytes();
1031    }
1032
1033    private static String JavaDoc getHostName()
1034    {
1035        String JavaDoc hostName = (System.getProperty("hostName"));
1036        if (hostName == null)
1037            hostName="localhost";
1038        return hostName;
1039    }
1040}
1041
Popular Tags