001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.derbynet.DerbyNetNewServer
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.derbynet;
023:
024: import org.apache.derby.drda.NetworkServerControl;
025:
026: import org.apache.derbyTesting.functionTests.harness.jvm;
027: import org.apache.derby.tools.ij;
028:
029: import java.util.Properties;
030:
031: import java.io.ByteArrayOutputStream;
032: import java.io.OutputStream;
033: import java.io.PrintWriter;
034:
035: import java.sql.DriverManager;
036: import java.sql.Connection;
037: import java.sql.DatabaseMetaData;
038: import java.sql.ResultSet;
039: import java.sql.SQLException;
040: import java.sql.Statement;
041: import org.apache.derbyTesting.functionTests.util.TestUtil;
042:
043: /**
044: * Test NetworkServerControl.start(PrintWriter) writes to the print Writer
045: *
046: * test:
047: *<ul>
048: *<li> start( printWriter)
049: *<li> start( (PrintWriter) null)
050: *</ul>
051: */
052:
053: public class DerbyNetNewServer extends Thread {
054:
055: private static final String DATABASE_NAME = "wombat";
056: private static boolean passed = true;
057: private static final Properties authenticationProperties;
058: static {
059: authenticationProperties = new Properties();
060: authenticationProperties.put("user", "admin");
061: authenticationProperties.put("password", "admin");
062: }
063:
064: private NetworkServerControl server;
065:
066: public static void main(String[] args) {
067: try {
068: TestUtil.loadDriver();
069: Class.forName("org.apache.derby.jdbc.EmbeddedDriver")
070: .newInstance();
071:
072: ij.getPropertyArg(args);
073:
074: ByteArrayOutputStream bos = new ByteArrayOutputStream();
075:
076: testServer(new NetworkServerControl(), bos,
077: "non-null PrintWriter");
078: testServer(new NetworkServerControl(), null,
079: "null PrintWriter");
080: } catch (Exception e) {
081: e.printStackTrace();
082: passed = false;
083: }
084: if (passed)
085: System.out.println("PASSED.");
086: else
087: System.out.println("FAILED.");
088: }
089:
090: private static void testServer(NetworkServerControl server,
091: ByteArrayOutputStream bos, String label) throws Exception {
092: PrintWriter writer = null;
093:
094: System.out.println("Testing " + label);
095: if (bos != null) {
096: bos.reset();
097: // DERBY-1466, Test that messages are flushed to the
098: // writer irrespective of whether the user's writer is
099: // set to autoflush true.
100: writer = new PrintWriter(bos);
101: }
102: server.start(writer);
103: Connection conn = null;
104:
105: // Wait for it to start
106: for (int ntries = 1;; ntries++) {
107: try {
108: Thread.sleep(500);
109: } catch (InterruptedException ie) {
110: }
111: ;
112:
113: try {
114: conn = DriverManager.getConnection(TestUtil
115: .getJdbcUrlPrefix()
116: + DATABASE_NAME + ";create=true",
117: authenticationProperties);
118:
119: break;
120: } catch (SQLException sqle) {
121: if (ntries > 10) {
122: System.out.println("Server start failed: "
123: + sqle.getMessage());
124: if (bos != null) {
125: System.out.println("Server log:");
126: System.out.println(bos.toString());
127: }
128: passed = false;
129: break;
130: }
131: }
132: }
133: if (conn != null) {
134: try {
135: conn.close();
136: } catch (SQLException sqle) {
137: passed = false;
138: System.out.println("SQLException thrown in close: "
139: + sqle.getMessage());
140: }
141: }
142: try {
143: server.shutdown();
144: } catch (Exception e) {
145: passed = false;
146: System.out.println("Server shutdown failed: "
147: + e.getMessage());
148: }
149:
150: if (bos != null) {
151: if (bos.size() == 0) {
152: passed = false;
153: System.out
154: .println("Nothing written to the server log.");
155: }
156: }
157: } // end of testServer
158:
159: private DerbyNetNewServer(NetworkServerControl server) {
160: this.server = server;
161: }
162:
163: }
|