Expect for Java library

This library is an implementation of DSL Expect for Java language

License

License

Categories

Categories

Net ORM Data
GroupId

GroupId

net.itransformers
ArtifactId

ArtifactId

expect4java
Last Version

Last Version

1.0.6
Release Date

Release Date

Type

Type

jar
Description

Description

Expect for Java library
This library is an implementation of DSL Expect for Java language
Project URL

Project URL

https://github.com/iTransformers/expect4java
Project Organization

Project Organization

iTransformers Labs
Source Code Management

Source Code Management

https://github.com/iTransformers/expect4java

Download expect4java

How to add to project

<!-- https://jarcasting.com/artifacts/net.itransformers/expect4java/ -->
<dependency>
    <groupId>net.itransformers</groupId>
    <artifactId>expect4java</artifactId>
    <version>1.0.6</version>
</dependency>
// https://jarcasting.com/artifacts/net.itransformers/expect4java/
implementation 'net.itransformers:expect4java:1.0.6'
// https://jarcasting.com/artifacts/net.itransformers/expect4java/
implementation ("net.itransformers:expect4java:1.0.6")
'net.itransformers:expect4java:jar:1.0.6'
<dependency org="net.itransformers" name="expect4java" rev="1.0.6">
  <artifact name="expect4java" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.itransformers', module='expect4java', version='1.0.6')
)
libraryDependencies += "net.itransformers" % "expect4java" % "1.0.6"
[net.itransformers/expect4java "1.0.6"]

Dependencies

compile (7)

Group / Artifact Type Version
oro : oro jar 2.0.8
org.apache.commons : commons-lang3 jar 3.3.1
com.jcraft : jsch jar 0.1.52
commons-net : commons-net jar 3.0.1
org.slf4j : slf4j-api jar 1.7.12
org.slf4j : slf4j-log4j12 jar 1.7.12
log4j : log4j jar 1.2.16

test (2)

Group / Artifact Type Version
junit : junit jar 4.11
commons-lang : commons-lang jar 2.6

Project Modules

There are no modules declared in this project.

expect4java

Implementation of expect language for java. Using java 8 closures

Build Status for iTransformers/expect4java

How to use this library

The library is registered in the central Maven repository. All you need to do is to add the following dependecy into your Maven project

<dependencies>
    <dependency>
        <groupId>net.itransformers</groupId>
        <artifactId>expect4java</artifactId>
        <version>1.0.5</version>
    </dependency>
</dependencies>

How to use expect4java

  1. Create CLIConnection instance
  2. Open the connection
  3. Create Expext4j object using the created CLIConnection
  4. Close the Expect4j object
  5. Close the CLIConnection

Create CLIConnection object

Expect4java has the following implemented CLIConnections:

net.itransformers.expect4java.cliconnection.impl.CrossPipedCLIConnection
net.itransformers.expect4java.cliconnection.impl.EchoCLIConnection
net.itransformers.expect4java.cliconnection.impl.LoggableCLIConnection
net.itransformers.expect4java.cliconnection.impl.RawSocketCLIConnection
net.itransformers.expect4java.cliconnection.impl.SshCLIConnection
net.itransformers.expect4java.cliconnection.impl.TelnetCLIConnection

It is possible to implement your own CLIConnection by implementing the following interface:

net.itransformers.expect4java.cliconnection.CLIConnection

The SshCLIConnection and the TelnetCLIConnection are most commonly used. The LoggableCLIConnection follows the decorator pattern and add logging functionality to any CLIConnection. Here is an example:

CLIConnection sshConn = new LoggableCLIConnection(
        new SshCLIConnection(),
        msg -> System.out.println(">>> "+msg),
        msg -> System.out.println("<<< "+msg)
);

The first parameter of LoggaleCLIConnection is the CLIConnection to be wrapped. The second and the third parameter are objects of type CLIStreamLogger.

Open CLIConnection

The SSHClIConnection can be opened in the following way:

Map<String, Object> connParams = new HashMap<>();
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "keyboard-interactive,password");
connParams.put("username", "guest");
connParams.put("password", "pass123");
connParams.put("address", "vyordanov.tk");
connParams.put("port", 22);
connParams.put("timeout", 1000);
connParams.put("config", config);

sshConn.connect(connParams);

It is also possible to set UserInfo class to the connection (See the JSH documentation for more details)

UserInfo ui=new MySimpleUserInfo("pass123");
connParams.put("userInfo", ui);

Create Expect4j object

This is quite simple:

Expect4j e4j = new Expect4jImpl(sshConn);

Close the Expect4j object

Awlays close the expect4j object. This will stop the internal thread by this object.

Close the CLIConnection

Awlays close the CLICOnnection, to free os resources.

Using the Expect4j object

Sending characters to process input stream

This is done by using Expect4j.send method

Example:

e4j.send("say hello\r")

Expecting characters from process output stream

This is done withe Expect4j.expect methods. The expect method has two overloads.

The first 'expect' overload is:

void expect(Match mathes)

Example of invoking this overload is:

e4j.expect(new RegExpMatch("hello ([^\n]*)\n", (ExpectContext context) -> {
    System.out.println("Hello " + context.getMatch(1));
    status.setValue(true);
}));

The second 'expect' overload is:

void expect(Match[] mathes)

Example of invoking this overload is:

e4j.expect(new Match[]{
    new RegExpMatch("hello ([^\n]*)\n", (ExpectContext it) -> {
        System.out.println("Hello " + it.getMatch(1));
        firsMatch.setValue(true);
        it.exp_continue();
    }),
    new RegExpMatch("hello2 ([^\n]*)\n", (ExpectContext context2) -> {
        System.out.println("Hello2 " + context2.getMatch(1));
        if (firsMatch.booleanValue()) status.setValue(true);
    })
});

Match class

net.itransformers.expect4java.matches.Match is an abstract class used to match character from the process output. It has several child classes like:

net.itransformers.expect4java.matches.RegExpMatch
net.itransformers.expect4java.matches.GlobMatch
net.itransformers.expect4java.matches.EofMatch
net.itransformers.expect4java.matches.TimeoutMatch

RegExpMatch class

Match object used for matching characters received into input stream using regular expression pattern. This Match object can be used as an array element of parameter of expect method.

The RegExpMatch constructor has two overloads:

RegExpMatch(String patternStr)
RegExpMatch(String patternStr, Closure closure)

The second one has a closure parameter which will be invoked if the regexp matches.

GlobMatch class

Match object used for matching characters received into input stream using glob pattern.

This Match object can be used as an array element of parameter of expect method.

The Glob constructor has two overloads:

GlobMatch(String pattern
GlobMatch(String pattern, Closure closure)

The second one has a closure parameter which will be invoked if the regexp matches.

TimeoutMatch class

Match object used to handle expect timeouts.

This Match object can be used as an array element of parameter of expect method.

For example:

e4j.expect(new TimeoutMatch(1000L, it -> {
    // some code is executed here if there is timeout
}));

or:

e4j.expect(new TimeoutMatch(1000L));

EofMatch class

Inside each match closure the following object is available: net.itransformers.expect4java.ExpectContext.

This object has the following most important methods:

void exp_continue();
void exp_continue_reset_timer();
String getBuffer();
String getMatch(int groupnum);
String getMatch();
Registering groovy closures
net.itransformers

iTransformers Labs

networking research

Versions

Version
1.0.6
1.0.5
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0