From 7c9800b2a669bbf89e49f6f314c3cefaa31a4e84 Mon Sep 17 00:00:00 2001 From: Joaquin Araujo Blanco Date: Mon, 22 Apr 2019 14:53:13 -0300 Subject: [PATCH 1/2] Test for getFields method added. Additionally, implementation of new methods in XI5250Field.class were needed. --- pom.xml | 6 + .../net/infordata/em/crt5250/XI5250Field.java | 17 +- .../net/infordata/em/TerminalClientTest.java | 250 +++++++++++++++--- 3 files changed, 230 insertions(+), 43 deletions(-) diff --git a/pom.xml b/pom.xml index 46c8abc..d944dd1 100644 --- a/pom.xml +++ b/pom.xml @@ -80,6 +80,12 @@ 24.0-jre test + + org.mockito + mockito-all + 1.10.19 + test + diff --git a/src/main/java/net/infordata/em/crt5250/XI5250Field.java b/src/main/java/net/infordata/em/crt5250/XI5250Field.java index b6c2966..0a11b65 100644 --- a/src/main/java/net/infordata/em/crt5250/XI5250Field.java +++ b/src/main/java/net/infordata/em/crt5250/XI5250Field.java @@ -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. @@ -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 diff --git a/src/test/java/net/infordata/em/TerminalClientTest.java b/src/test/java/net/infordata/em/TerminalClientTest.java index 2555efe..d1500c1 100644 --- a/src/test/java/net/infordata/em/TerminalClientTest.java +++ b/src/test/java/net/infordata/em/TerminalClientTest.java @@ -2,6 +2,8 @@ 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; @@ -9,6 +11,7 @@ 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; @@ -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 { @@ -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; + @Rule public TestRule watchman = new TestWatcher() { @@ -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 { @@ -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(); @@ -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; } @@ -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); } @@ -264,7 +256,7 @@ public void shouldThrowIllegalArgumentExceptionWhenSendIncorrectFieldPosition() public void shouldThrowIllegalArgumentExceptionWhenSendIncorrectFieldLabel() throws Exception { awaitKeyboardUnlock(); - client.setFieldTextByLabel("test","test"); + client.setFieldTextByLabel("test", "test"); } @Test @@ -290,4 +282,182 @@ public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByLabel() exceptionWaiter.awaitException(); } + @Test + public void shouldGetCorrectFieldsWhenGetFields() throws Exception { + awaitKeyboardUnlock(); + List actualFields = client.getFields().stream().map(TestField::new).collect(Collectors.toList()); + assertEquals(buildExpectedFields(), actualFields); + } + + private List buildExpectedFields() { + List testFieldList = new ArrayList<>(); + testFieldList.add((new TestField(new TestField.FieldBuilder(FIELD_STRING_OUTPUT).withAttr(36).withDefaultColumn().withRow(5)))); + 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 { + + private int row; + + private int column; + private int attr; + private String text; + private byte[] ffw = {64, 32}; + private byte[] fcw = {0, 0}; + FieldBuilder(String text) { + this.text = text; + } + FieldBuilder withFfwModified() { + ffw[0] = 64; + ffw[1] = 0; + return this; + } + + FieldBuilder withRow(int val) { + row = val; + return this; + } + + FieldBuilder withDefaultColumn() { + column = 52; + return this; + } + + FieldBuilder withAttr(int val) { + attr = val; + return this; + } + + + + } + @Override + public String toString() { + return "TestField{" + + "row=" + row + + ", column=" + column + + ", ffw=" + Arrays.toString(ffw) + + ", fcw=" + Arrays.toString(fcw) + + ", attr=" + attr + + ", text='" + text + '\'' + + '}'; + } + @Override + 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 { + + 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(); + } + + } + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From edfeb7c9ccf1d46e63144553d91fd64a5c7c1e5c Mon Sep 17 00:00:00 2001 From: Roger Abelenda Date: Wed, 24 Apr 2019 13:35:27 -0300 Subject: [PATCH 2/2] Refactor get fields test to clean up code Additionally, add missing java doc to TerminalClient.getFields method. --- pom.xml | 6 - .../java/net/infordata/em/TerminalClient.java | 10 +- .../net/infordata/em/TerminalClientTest.java | 166 +++++++----------- 3 files changed, 76 insertions(+), 106 deletions(-) diff --git a/pom.xml b/pom.xml index d944dd1..46c8abc 100644 --- a/pom.xml +++ b/pom.xml @@ -80,12 +80,6 @@ 24.0-jre test - - org.mockito - mockito-all - 1.10.19 - test - diff --git a/src/main/java/net/infordata/em/TerminalClient.java b/src/main/java/net/infordata/em/TerminalClient.java index a208f24..1552036 100644 --- a/src/main/java/net/infordata/em/TerminalClient.java +++ b/src/main/java/net/infordata/em/TerminalClient.java @@ -162,7 +162,8 @@ public void setFieldTextByLabel(String label, String text) { */ public void sendKeyEvent(int keyCode, int modifiers) { emulator.processRawKeyEvent( - new KeyEvent(emulator, KeyEvent.KEY_PRESSED, 0, modifiers, keyCode, KeyEvent.CHAR_UNDEFINED)); + new KeyEvent(emulator, KeyEvent.KEY_PRESSED, 0, modifiers, keyCode, + KeyEvent.CHAR_UNDEFINED)); } /** @@ -181,6 +182,13 @@ public String getScreenText() { return screen.toString(); } + /** + * Gets the fields present on the current screen. + * + * @return fields of the screen. Take into consideration that this only returns fields that hold + * some information to be sent to the server. The screen is composed by its textual representation + * plus these fields. + */ public List getFields() { return emulator.getFields(); } diff --git a/src/test/java/net/infordata/em/TerminalClientTest.java b/src/test/java/net/infordata/em/TerminalClientTest.java index d1500c1..3ed6b3b 100644 --- a/src/test/java/net/infordata/em/TerminalClientTest.java +++ b/src/test/java/net/infordata/em/TerminalClientTest.java @@ -2,7 +2,6 @@ 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; @@ -11,7 +10,6 @@ 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; @@ -27,20 +25,13 @@ 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.CountDownLatch; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; +import java.util.*; +import java.util.concurrent.*; import java.util.stream.Collectors; -import static org.junit.Assert.assertEquals; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.assertEquals; public class TerminalClientTest { @@ -48,11 +39,6 @@ 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 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; - @Rule public TestRule watchman = new TestWatcher() { @@ -157,7 +143,7 @@ public void checkServerTrusted( X509Certificate[] certs, String authType) { } }; - sslContext.init(null, new TrustManager[] {trustManager}, + sslContext.init(null, new TrustManager[]{trustManager}, new SecureRandom()); return sslContext; } @@ -267,7 +253,8 @@ public void shouldSendCloseToExceptionHandlerWhenServerDown() throws Exception { } @Test - public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByCoord() throws Exception { + public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByCoord() + throws Exception { awaitKeyboardUnlock(); service.stop(TIMEOUT_MILLIS); loginByCoord(); @@ -275,7 +262,8 @@ public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByCoord() } @Test - public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByLabel() throws Exception { + public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByLabel() + throws Exception { awaitKeyboardUnlock(); service.stop(TIMEOUT_MILLIS); loginByLabel(); @@ -285,84 +273,97 @@ public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByLabel() @Test public void shouldGetCorrectFieldsWhenGetFields() throws Exception { awaitKeyboardUnlock(); - List actualFields = client.getFields().stream().map(TestField::new).collect(Collectors.toList()); + List actualFields = client.getFields().stream() + .map(TestField::new) + .collect(Collectors.toList()); assertEquals(buildExpectedFields(), actualFields); } private List buildExpectedFields() { List testFieldList = new ArrayList<>(); - testFieldList.add((new TestField(new TestField.FieldBuilder(FIELD_STRING_OUTPUT).withAttr(36).withDefaultColumn().withRow(5)))); - 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; - + int fieldsColumn = 52; + String fieldsText = "\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000"; + testFieldList.add(new TestField.Builder(5, fieldsColumn, fieldsText).withMonocase().build()); + testFieldList.add( + new TestField.Builder(6, fieldsColumn, fieldsText).withMonocase().withHighIntensity() + .withReverseImage() + .build()); + testFieldList.add(new TestField.Builder(7, fieldsColumn, fieldsText).build()); + testFieldList.add(new TestField.Builder(8, fieldsColumn, fieldsText).build()); + testFieldList.add(new TestField.Builder(9, fieldsColumn, fieldsText).build()); + 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) { + private static final byte FIELD_FORMAT_MASK = 0x40; + private static final byte MONOCASE_MASK = 0x20; + private static final byte SCREEN_ATTRIBUTE_MASK = 0x20; + private static final byte UNDERSCORE_MASK = 0x04; + private static final byte HIGH_INTENSITY_MASK = 0x02; + private static final byte REVERSE_IMAGE_MASK = 0x01; + + private final int row; + private final int column; + private final String text; + private final byte[] ffw; + private final byte[] fcw; + private final int attr; + + private TestField(Builder 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(); + 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 { - private int row; + private static final class Builder { + + private final int row; + private final int column; + private final String text; + private final byte[] fcw = {0, 0}; + private byte[] ffw = {FIELD_FORMAT_MASK, 0}; + private byte attr = SCREEN_ATTRIBUTE_MASK | UNDERSCORE_MASK; - private int column; - private int attr; - private String text; - private byte[] ffw = {64, 32}; - private byte[] fcw = {0, 0}; - FieldBuilder(String text) { + private Builder(int row, int column, String text) { + this.row = row; + this.column = column; this.text = text; } - FieldBuilder withFfwModified() { - ffw[0] = 64; - ffw[1] = 0; - return this; - } - FieldBuilder withRow(int val) { - row = val; + private Builder withMonocase() { + ffw[1] |= MONOCASE_MASK; return this; } - FieldBuilder withDefaultColumn() { - column = 52; + private Builder withHighIntensity() { + attr |= HIGH_INTENSITY_MASK; return this; } - FieldBuilder withAttr(int val) { - attr = val; + private Builder withReverseImage() { + attr |= REVERSE_IMAGE_MASK; return this; } - + private TestField build() { + return new TestField(this); + } } + @Override public String toString() { return "TestField{" + @@ -374,6 +375,7 @@ public String toString() { ", text='" + text + '\'' + '}'; } + @Override public boolean equals(Object o) { if (this == o) { @@ -399,8 +401,8 @@ public int hashCode() { return result; } - } + private static class ExceptionWaiter implements ExceptionHandler { private CountDownLatch exceptionLatch = new CountDownLatch(1); @@ -427,37 +429,3 @@ private void awaitClose() throws InterruptedException { } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -