Repository: sqoop Updated Branches: refs/heads/sqoop2 39e99cc5f -> 6822e8ba3
SQOOP-1816: Sqoop2: Add logging to the test DerbyProvider implementation (Jarek Jarcec Cecho via Abraham Elmahrek) Project: http://git-wip-us.apache.org/repos/asf/sqoop/repo Commit: http://git-wip-us.apache.org/repos/asf/sqoop/commit/6822e8ba Tree: http://git-wip-us.apache.org/repos/asf/sqoop/tree/6822e8ba Diff: http://git-wip-us.apache.org/repos/asf/sqoop/diff/6822e8ba Branch: refs/heads/sqoop2 Commit: 6822e8ba3bf9bf5bdf92855298e1fa509c79b6af Parents: 39e99cc Author: Abraham Elmahrek <[email protected]> Authored: Wed Nov 26 12:45:23 2014 -0800 Committer: Abraham Elmahrek <[email protected]> Committed: Wed Nov 26 12:45:23 2014 -0800 ---------------------------------------------------------------------- .../sqoop/common/test/db/DerbyProvider.java | 9 ++- .../sqoop/common/test/utils/LoggerWriter.java | 65 ++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/sqoop/blob/6822e8ba/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java ---------------------------------------------------------------------- diff --git a/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java b/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java index f7b91bb..8b4643c 100644 --- a/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java +++ b/common-test/src/main/java/org/apache/sqoop/common/test/db/DerbyProvider.java @@ -17,8 +17,10 @@ */ package org.apache.sqoop.common.test.db; +import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.derby.drda.NetworkServerControl; +import org.apache.sqoop.common.test.utils.LoggerWriter; import java.net.InetAddress; @@ -40,7 +42,12 @@ public class DerbyProvider extends DatabaseProvider { // Start embedded server try { server = new NetworkServerControl(InetAddress.getByName("localhost"), 1527); - server.start(null); + server.start(new LoggerWriter(LOG, Level.INFO)); + + // Start won't thrown an exception in case that it fails to start, one + // have to explicitly call ping() in order to verify if the server is + // up. Check DERBY-1465 for more details. + server.ping(); } catch (Exception e) { LOG.error("Can't start Derby network server", e); throw new RuntimeException("Can't derby server", e); http://git-wip-us.apache.org/repos/asf/sqoop/blob/6822e8ba/common-test/src/main/java/org/apache/sqoop/common/test/utils/LoggerWriter.java ---------------------------------------------------------------------- diff --git a/common-test/src/main/java/org/apache/sqoop/common/test/utils/LoggerWriter.java b/common-test/src/main/java/org/apache/sqoop/common/test/utils/LoggerWriter.java new file mode 100644 index 0000000..6039363 --- /dev/null +++ b/common-test/src/main/java/org/apache/sqoop/common/test/utils/LoggerWriter.java @@ -0,0 +1,65 @@ +/** + * 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.sqoop.common.test.utils; + +import org.apache.log4j.Level; +import org.apache.log4j.Logger; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Writer; + +/** + * PrintWriter implementation that will forward all the messages into given logger. + */ +public class LoggerWriter extends PrintWriter { + + public LoggerWriter(final Logger logger, final Level level) { + super(new InternalWriter(logger, level)); + } + + private static class InternalWriter extends Writer { + + private final Logger logger; + private final Level level; + + public InternalWriter(final Logger logger, final Level level) { + this.logger = logger; + this.level = level; + } + + @Override + public void write(char[] chars, int offset, int len) throws IOException { + while(len > 0 && (chars[len - 1] == '\n' || chars[len - 1] == '\r')) { + len--; + } + + if(len > 0) { + logger.log(level, String.copyValueOf(chars, offset, len)); + } + } + + @Override + public void flush() throws IOException { + } + + @Override + public void close() throws IOException { + } + } +}
