KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > derbynet > testij


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.testij
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 package org.apache.derbyTesting.functionTests.tests.derbynet;
22
23 import java.sql.*;
24 import java.util.Vector JavaDoc;
25 import java.util.Properties JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileInputStream JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.BufferedOutputStream JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.OutputStream JavaDoc;
32 import java.io.ByteArrayInputStream JavaDoc;
33 import java.io.FileNotFoundException JavaDoc;
34
35 import org.apache.derbyTesting.functionTests.harness.jvm;
36 import org.apache.derbyTesting.functionTests.harness.Sed;
37 import org.apache.derbyTesting.functionTests.util.TestUtil;
38 import org.apache.derbyTesting.functionTests.util.ExecProcUtil;
39
40 import org.apache.derby.drda.NetworkServerControl;
41
42
43 public class testij
44 {
45
46
47     private static Properties JavaDoc properties = new java.util.Properties JavaDoc();
48     private static jvm jvm;
49     private static Vector JavaDoc vCmd;
50
51     private static String JavaDoc IjCmd="org.apache.derby.tools.ij";
52     private static String JavaDoc SqlDir="extin";
53     private static String JavaDoc jccSqlFile="testij.sql";
54     private static String JavaDoc sep;
55     private static String JavaDoc clientSqlFile="testclientij.sql";
56     private static String JavaDoc altExtinDir;
57     private static boolean useAltExtinDir=false;
58     
59     private static void execCmd (String JavaDoc[] args) throws Exception JavaDoc
60     {
61         int totalSize = vCmd.size() + args.length;
62         String JavaDoc serverCmd[] = new String JavaDoc[totalSize];
63         int i;
64         for (i = 0; i < vCmd.size(); i++)
65         {
66             serverCmd[i] = (String JavaDoc)vCmd.elementAt(i);
67         // System.out.println("serverCmd["+i+"]: "+serverCmd[i]);
68
}
69         int j = 0;
70         for (; i < totalSize; i++)
71         {
72             serverCmd[i] = args[j++];
73         // System.out.println("serverCmd["+i+"]: "+serverCmd[i]);
74
}
75  
76         // Start a process to run the command
77
Process JavaDoc pr = Runtime.getRuntime().exec(serverCmd);
78         pr.waitFor(); // make sure this is executed first
79
}
80     
81     public static void massageSqlFile (String JavaDoc hostName, String JavaDoc fileName) throws Exception JavaDoc {
82         // only called if hostName is *not* localhost.
83
// Need to replace each occurrence of the string 'localhost' with
84
// whatever is the hostName
85
File JavaDoc tmpFile = new File JavaDoc("extin", "tmpFile.sql");
86         File JavaDoc orgFile = new File JavaDoc("extin", fileName);
87         // wrap this in a try to possibly try using user.dir to find the file
88
InputStream JavaDoc original;
89         OutputStream JavaDoc copy;
90         try {
91             fileName = SqlDir + sep + fileName;
92             original = new FileInputStream JavaDoc(fileName);
93             copy = new FileOutputStream JavaDoc(tmpFile);
94         }
95         catch (FileNotFoundException JavaDoc fnfe) {
96             // we must be running from within a suite...
97
useAltExtinDir = true;
98             String JavaDoc userdir = System.getProperty("user.dir");
99             altExtinDir = userdir + sep + "..";
100             tmpFile = new File JavaDoc(altExtinDir, "tmpFile.sql");
101             orgFile = new File JavaDoc (altExtinDir, fileName);
102             fileName = altExtinDir + sep + fileName;
103             original = new FileInputStream JavaDoc(fileName);
104             copy = new FileOutputStream JavaDoc(tmpFile);
105         }
106         int content;
107         while ((content = original.read())> 0 ) {
108             copy.write(content);
109         }
110         copy.close();
111         original.close();
112         Sed hostSed = new Sed();
113         InputStream JavaDoc sedIs = new ByteArrayInputStream JavaDoc(("substitute=localhost;" + hostName).getBytes("UTF-8"));
114         hostSed.exec(tmpFile, orgFile, sedIs, false, false, false);
115     }
116
117     public static void main (String JavaDoc args[]) throws Exception JavaDoc
118     {
119         if ((System.getProperty("java.vm.name") != null) && System.getProperty("java.vm.name").equals("J9"))
120             jvm = jvm.getJvm("j9_13");
121         else
122             jvm = jvm.getJvm("currentjvm"); // ensure compatibility
123
vCmd = jvm.getCommandLine();
124         sep = System.getProperty("file.separator");
125         try
126         {
127             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(System.out, 1024);
128             /************************************************************
129              * Test comments in front of select's doesn't cause problems
130              ************************************************************/

131             //create wombat database
132
NetworkServerControl server = new NetworkServerControl();
133             System.out.println("Testing various ij connections and comments in front of selects");
134             
135             // first, we have to massage the .sql file to replace localhost, if
136
// there is a system property set.
137

138             String JavaDoc hostName=TestUtil.getHostName();
139             if (TestUtil.isJCCFramework()){
140                 // use jccSqlfile
141
if (!hostName.equals("localhost"))
142                     massageSqlFile(hostName,jccSqlFile);
143                 if (useAltExtinDir)
144                     ExecProcUtil.execCmdDumpResults(new String JavaDoc[]{IjCmd,(altExtinDir + sep + SqlDir + sep + jccSqlFile)},vCmd,bos);
145                 ExecProcUtil.execCmdDumpResults(new String JavaDoc[]{IjCmd,(SqlDir + sep + jccSqlFile)},vCmd,bos);
146             } else { // Derby Client
147
// use clientSqlFile
148
if(!hostName.equals("localhost")) {
149                     massageSqlFile(hostName,clientSqlFile);
150                 if (useAltExtinDir)
151                     ExecProcUtil.execCmdDumpResults(new String JavaDoc[]{IjCmd,(altExtinDir + sep + SqlDir + sep + clientSqlFile)},vCmd,bos);
152                 }
153                 ExecProcUtil.execCmdDumpResults(new String JavaDoc[]{IjCmd,(SqlDir + sep + clientSqlFile)},vCmd,bos);
154             }
155             System.out.println("End test");
156         }
157         catch (Exception JavaDoc e)
158         {
159             e.printStackTrace();
160         }
161     }
162
163 }
164
165
166
Popular Tags