Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced log4j dependency with SLF4J #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 27 additions & 53 deletions Expect.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.successehs.direct.mailserver.admin;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -12,12 +14,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
Expand Down Expand Up @@ -45,11 +43,7 @@
* @version 1.1
*/
public class Expect {
static final Logger log = Logger.getLogger(Expect.class);
/**Logging is turned off by default.*/
static {
log.setLevel(Level.OFF);
}
private static final Logger log = LoggerFactory.getLogger(Expect.class);

private OutputStream output;
private Pipe.SourceChannel inputChannel;
Expand All @@ -62,7 +56,7 @@ public Expect(InputStream input, OutputStream output) {
selector = Selector.open();
inputChannel.register(selector, SelectionKey.OP_READ);
} catch (IOException e) {
log.fatal("Fatal error when initializing pipe or selector", e);
log.error("Fatal error when initializing pipe or selector", e);
//e.printStackTrace();
}
this.output = output;
Expand Down Expand Up @@ -181,10 +175,10 @@ public void send(byte[] toWrite) {
}
}

private int default_timeout = 60;
private boolean restart_timeout_upon_receive = false;
private int defaultTimeout = 60;
private boolean restartTimeoutUponReceive = false;
private StringBuffer buffer = new StringBuffer();
private boolean notransfer = false;
private boolean noTransfer = false;

/**String before the last match(if there was a match),
* updated after each expect() call*/
Expand All @@ -207,7 +201,7 @@ public void send(byte[] toWrite) {
* @return
*/
public int expect(Object... patterns) {
return expect(default_timeout, patterns);
return expect(defaultTimeout, patterns);
}

/**
Expand Down Expand Up @@ -258,7 +252,7 @@ public int expect(int timeout, List<Pattern> list) {
log.debug("Expecting " + list);

clearGlobalVariables();
long endTime = System.currentTimeMillis() + timeout * 1000;
long endTime = System.currentTimeMillis() + (long)timeout * 1000;

try {
ByteBuffer bytes = ByteBuffer.allocate(1024);
Expand All @@ -274,13 +268,13 @@ public int expect(int timeout, List<Pattern> list) {
this.before = buffer.substring(0, matchStart);
this.match = m.group();
this.isSuccess = true;
if(!notransfer)buffer.delete(0, matchEnd);
if(!noTransfer)buffer.delete(0, matchEnd);
return i;
}
}

long waitTime = endTime - System.currentTimeMillis();
if (restart_timeout_upon_receive)
if (restartTimeoutUponReceive)
waitTime = timeout * 1000;
if (waitTime <= 0) {
log.debug("Timeout when expecting " + list);
Expand Down Expand Up @@ -345,7 +339,7 @@ public int expectEOF(int timeout) {
/**Convenience method, same as calling {@link #expectEOF(int)
* expectEOF(default_timeout)}*/
public int expectEOF() {
return expectEOF(default_timeout);
return expectEOF(defaultTimeout);
}

/**
Expand All @@ -363,7 +357,7 @@ public int expectEOFOrThrow(int timeout) throws TimeoutException,
/**Convenience method, same as calling {@link #expectEOF(int)
* expectEOF(default_timeout)}*/
public int expectEOFOrThrow() throws TimeoutException, IOException {
return expectEOFOrThrow(default_timeout);
return expectEOFOrThrow(defaultTimeout);
}

/**useful when calling {@link #expectOrThrow(int, Object...)}*/
Expand Down Expand Up @@ -406,7 +400,7 @@ public int expectOrThrow(int timeout, Object... patterns)
* expectOrThrow(default_timeout, patterns)}*/
public int expectOrThrow(Object... patterns) throws TimeoutException,
EOFException, IOException {
return expectOrThrow(default_timeout, patterns);
return expectOrThrow(defaultTimeout, patterns);
}

private void clearGlobalVariables() {
Expand Down Expand Up @@ -441,23 +435,23 @@ public void close() {
}
}

public int getDefault_timeout() {
return default_timeout;
public int getDefaultTimeout() {
return defaultTimeout;
}
public void setDefault_timeout(int default_timeout) {
this.default_timeout = default_timeout;
public void setDefaultTimeout(int defaultTimeout) {
this.defaultTimeout = defaultTimeout;
}
public boolean isRestart_timeout_upon_receive() {
return restart_timeout_upon_receive;
public boolean isRestartTimeoutUponReceive() {
return restartTimeoutUponReceive;
}
public void setRestart_timeout_upon_receive(boolean restart_timeout_upon_receive) {
this.restart_timeout_upon_receive = restart_timeout_upon_receive;
public void setRestartTimeoutUponReceive(boolean restartTimeoutUponReceive) {
this.restartTimeoutUponReceive = restartTimeoutUponReceive;
}
public void setNotransfer(boolean notransfer) {
this.notransfer = notransfer;
public void setNoTransfer(boolean noTransfer) {
this.noTransfer = noTransfer;
}
public boolean isNotransfer() {
return notransfer;
return noTransfer;
}

/**
Expand Down Expand Up @@ -496,26 +490,6 @@ public static class TimeoutException extends Exception{
public static class EOFException extends Exception{
}

private static Layout layout = new PatternLayout(
PatternLayout.TTCC_CONVERSION_PATTERN);

public static void addLogToConsole(Level level) {
log.setLevel(Level.ALL);
ConsoleAppender console = new ConsoleAppender(layout);
console.setThreshold(level);
log.addAppender(console);
}
public static void addLogToFile(String filename, Level level) throws IOException {
log.setLevel(Level.ALL);
FileAppender file = new FileAppender(layout, filename);
file.setThreshold(level);
log.addAppender(file);
}
public static void turnOffLogging(){
log.setLevel(Level.OFF);
log.removeAllAppenders();
}

private static PrintStream duplicatedTo = null;
/**
* While performing expect operations on the InputStream provided, duplicate
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ In order to use this tool, you need to know Java regular expressions and have so
Dependencies
------------
Expect.java:
[log4j-1.2.X.jar](http://archive.apache.org/dist/logging/log4j/1.2.17/log4j-1.2.17.jar)
[slf4j-api.jar](http://www.slf4j.org/download.html)

TestExpect.java:
[junit-4.X.jar](http://cloud.github.com/downloads/KentBeck/junit/junit-4.10.jar)
Expand Down