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

GET_FIELD_TESTS #1

Merged
merged 2 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@
<version>24.0-jre</version>
<scope>test</scope>
</dependency>
<dependency>
rabelenda marked this conversation as resolved.
Show resolved Hide resolved
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/net/infordata/em/crt5250/XI5250Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,21 @@ protected void processFieldPaintEvent(XI5250FieldPaintEvent e) {
break;
}
}


public int getAttr() {
return ivAttr;
}
public final XI5250Crt getCrt() {
return ivCrt;
}


public byte[] getFFW() {
return ivFFW;
}

public byte[] getFCW() {
return ivFCW;
}
/**
* Initializes the field. It is called after that the 5250 panel construction has ended to let the
* field refresh its color attribute.
Expand Down Expand Up @@ -316,10 +326,11 @@ public void setString(String aStr) {
*
* @return value of the field as String
*/

public String getString() {
return ivCrt.getString(ivCol, ivRow, ivLength);
}

/**
* Returns value of the field as String, all trailing nulls and blanks are cut off. NOTE: signed
* numeric are followed by sign char
Expand Down
250 changes: 210 additions & 40 deletions src/test/java/net/infordata/em/TerminalClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

import com.google.common.base.Charsets;
import com.google.common.io.Resources;
import net.infordata.em.crt5250.XI5250Crt;
import net.infordata.em.crt5250.XI5250Field;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.abstracta.wiresham.Flow;
Expand All @@ -24,9 +27,19 @@
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.*;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

public class TerminalClientTest {
Expand All @@ -35,12 +48,11 @@ public class TerminalClientTest {
private static final long TIMEOUT_MILLIS = 10000;
private static final String SERVICE_HOST = "localhost";
private static final String TERMINAL_TYPE = "IBM-3477-FC";

private VirtualTcpService service = new VirtualTcpService();
private TerminalClient client;
private ExceptionWaiter exceptionWaiter;
private ScheduledExecutorService stableTimeoutExecutor = Executors
.newSingleThreadScheduledExecutor();
private static final String FIELD_STRING_OUTPUT = "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000";
private static final byte[] BYTES = {64, 0};
@Mock
public XI5250Crt screenMock;
Baraujo25 marked this conversation as resolved.
Show resolved Hide resolved


@Rule
public TestRule watchman = new TestWatcher() {
Expand All @@ -54,6 +66,11 @@ public void finished(Description description) {
LOG.debug("Finished {}", description.getMethodName());
}
};
private VirtualTcpService service = new VirtualTcpService();
private TerminalClient client;
private ExceptionWaiter exceptionWaiter;
private ScheduledExecutorService stableTimeoutExecutor = Executors
.newSingleThreadScheduledExecutor();

@Before
public void setup() throws IOException {
Expand All @@ -72,31 +89,6 @@ private String getResourceFilePath(String resourcePath) {
return getClass().getResource(resourcePath).getFile();
}

private static class ExceptionWaiter implements ExceptionHandler {

private CountDownLatch exceptionLatch = new CountDownLatch(1);
private CountDownLatch closeLatch = new CountDownLatch(1);

@Override
public void onException(Throwable ex) {
exceptionLatch.countDown();
}

@Override
public void onConnectionClosed() {
closeLatch.countDown();
}

private void awaitException() throws InterruptedException {
assertThat(exceptionLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue();
}

private void awaitClose() throws InterruptedException {
assertThat(closeLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue();
}

}

@After
public void teardown() throws Exception {
client.disconnect();
Expand Down Expand Up @@ -153,19 +145,19 @@ private SSLContext buildSslContext() throws GeneralSecurityException {
SSLContext sslContext = SSLContext.getInstance("TLS");
TrustManager trustManager = new X509TrustManager() {

public java.security.cert.X509Certificate[] getAcceptedIssuers() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}

public void checkClientTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
X509Certificate[] certs, String authType) {
}

public void checkServerTrusted(
java.security.cert.X509Certificate[] certs, String authType) {
X509Certificate[] certs, String authType) {
}
};
sslContext.init(null, new TrustManager[]{trustManager},
sslContext.init(null, new TrustManager[] {trustManager},
new SecureRandom());
return sslContext;
}
Expand Down Expand Up @@ -195,8 +187,8 @@ private void loginByCoord() {
}

private void loginByLabel() {
client.setFieldTextByLabel("User","TESTUSR");
client.setFieldTextByLabel("Password","TESTPSW");
client.setFieldTextByLabel("User", "TESTUSR");
client.setFieldTextByLabel("Password", "TESTPSW");
client.sendKeyEvent(KeyEvent.VK_ENTER, 0);
}

Expand Down Expand Up @@ -264,7 +256,7 @@ public void shouldThrowIllegalArgumentExceptionWhenSendIncorrectFieldPosition()
public void shouldThrowIllegalArgumentExceptionWhenSendIncorrectFieldLabel()
throws Exception {
awaitKeyboardUnlock();
client.setFieldTextByLabel("test","test");
client.setFieldTextByLabel("test", "test");
}

@Test
Expand All @@ -290,4 +282,182 @@ public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByLabel()
exceptionWaiter.awaitException();
}

@Test
public void shouldGetCorrectFieldsWhenGetFields() throws Exception {
awaitKeyboardUnlock();
List<TestField> actualFields = client.getFields().stream().map(TestField::new).collect(Collectors.toList());
Baraujo25 marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(buildExpectedFields(), actualFields);
}

private List<TestField> buildExpectedFields() {
List<TestField> testFieldList = new ArrayList<>();
testFieldList.add((new TestField(new TestField.FieldBuilder(FIELD_STRING_OUTPUT).withAttr(36).withDefaultColumn().withRow(5))));
Baraujo25 marked this conversation as resolved.
Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general the builder pattern should look more like new TestField.FieldBuilder()......build() and not as a new inside another. Please implement build method on builder and use that instead to make code a little shorter, better encapsulated, and mor obvious. Additionally, I think you have an unnecessary pair of parenthesis in this expression.

testFieldList.add((new TestField(new TestField.FieldBuilder(FIELD_STRING_OUTPUT).withAttr(39).withDefaultColumn().withRow(6))));
testFieldList.add((new TestField(new TestField.FieldBuilder(FIELD_STRING_OUTPUT).withAttr(36).withDefaultColumn().withRow(7).withFfwModified())));
testFieldList.add((new TestField(new TestField.FieldBuilder(FIELD_STRING_OUTPUT).withAttr(36).withDefaultColumn().withRow(8).withFfwModified())));
testFieldList.add((new TestField(new TestField.FieldBuilder(FIELD_STRING_OUTPUT).withAttr(36).withDefaultColumn().withRow(9).withFfwModified())));
return testFieldList;

}

private static class TestField {

private int row;
private int column;
private byte[] ffw;
private byte[] fcw;
private int attr;
private String text;

private TestField(FieldBuilder builder) {
row = builder.row;
column = builder.column;
attr = builder.attr;
text = builder.text;
ffw = builder.ffw;
fcw = builder.fcw;

}

private TestField(XI5250Field field){
column = field.getCol();
row = field.getRow();
attr = field.getAttr();
text = field.getString();
ffw = field.getFFW();
fcw = field.getFCW();
}
static final class FieldBuilder {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ad some new line here to separate :-), Also use private modifier on builder class to use the least possible visibility.


private int row;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary new line :-)

private int column;
private int attr;
private String text;
private byte[] ffw = {64, 32};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this working? shouldn't it be 0x64 and 0x32?

private byte[] fcw = {0, 0};
FieldBuilder(String text) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some missing spacing between methods and use minimum visibility

this.text = text;
}
FieldBuilder withFfwModified() {
ffw[0] = 64;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this working? or is it expecting 0x64 instead? 64 is the integer 64, 0x64 is the byte (the associated int would be 100)

ffw[1] = 0;
return this;
}

FieldBuilder withRow(int val) {
row = val;
return this;
}

FieldBuilder withDefaultColumn() {
column = 52;
return this;
}

FieldBuilder withAttr(int val) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation doesn't look consisten, please re format the entire file to avoid such issues.

attr = val;
return this;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary new lines



}
@Override
public String toString() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing spacing between methods

return "TestField{" +
"row=" + row +
", column=" + column +
", ffw=" + Arrays.toString(ffw) +
", fcw=" + Arrays.toString(fcw) +
", attr=" + attr +
", text='" + text + '\'' +
'}';
}
@Override

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing spacing between methods

public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestField testField = (TestField) o;
return row == testField.row &&
column == testField.column &&
attr == testField.attr &&
Arrays.equals(ffw, testField.ffw) &&
Arrays.equals(fcw, testField.fcw) &&
Objects.equals(text, testField.text);
}

@Override
public int hashCode() {
int result = Objects.hash(row, column, attr, text);
result = 31 * result + Arrays.hashCode(ffw);
result = 31 * result + Arrays.hashCode(fcw);
return result;
}


}
private static class ExceptionWaiter implements ExceptionHandler {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing spacing between classes


private CountDownLatch exceptionLatch = new CountDownLatch(1);
private CountDownLatch closeLatch = new CountDownLatch(1);

@Override
public void onException(Throwable ex) {
exceptionLatch.countDown();
}

@Override
public void onConnectionClosed() {
closeLatch.countDown();
}

private void awaitException() throws InterruptedException {
assertThat(exceptionLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue();
}

private void awaitClose() throws InterruptedException {
assertThat(closeLatch.await(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)).isTrue();
}

}

}


































Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

too many white lines