-
Notifications
You must be signed in to change notification settings - Fork 8
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import com.google.common.base.Charsets; | ||
import com.google.common.io.Resources; | ||
import net.infordata.em.crt5250.XI5250Field; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
|
@@ -24,10 +25,13 @@ | |
import java.security.GeneralSecurityException; | ||
import java.security.SecureRandom; | ||
import java.security.cert.X509Certificate; | ||
import java.util.Optional; | ||
import java.util.List; | ||
import java.util.*; | ||
import java.util.concurrent.*; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class TerminalClientTest { | ||
|
||
|
@@ -36,12 +40,6 @@ public class TerminalClientTest { | |
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(); | ||
|
||
@Rule | ||
public TestRule watchman = new TestWatcher() { | ||
@Override | ||
|
@@ -54,6 +52,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 +75,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,16 +131,16 @@ 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}, | ||
|
@@ -195,8 +173,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 +242,7 @@ public void shouldThrowIllegalArgumentExceptionWhenSendIncorrectFieldPosition() | |
public void shouldThrowIllegalArgumentExceptionWhenSendIncorrectFieldLabel() | ||
throws Exception { | ||
awaitKeyboardUnlock(); | ||
client.setFieldTextByLabel("test","test"); | ||
client.setFieldTextByLabel("test", "test"); | ||
} | ||
|
||
@Test | ||
|
@@ -275,19 +253,179 @@ public void shouldSendCloseToExceptionHandlerWhenServerDown() throws Exception { | |
} | ||
|
||
@Test | ||
public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByCoord() throws Exception { | ||
public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByCoord() | ||
throws Exception { | ||
awaitKeyboardUnlock(); | ||
service.stop(TIMEOUT_MILLIS); | ||
loginByCoord(); | ||
exceptionWaiter.awaitException(); | ||
} | ||
|
||
@Test | ||
public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByLabel() throws Exception { | ||
public void shouldSendExceptionToExceptionHandlerWhenLoginAndServerDownByLabel() | ||
throws Exception { | ||
awaitKeyboardUnlock(); | ||
service.stop(TIMEOUT_MILLIS); | ||
loginByLabel(); | ||
exceptionWaiter.awaitException(); | ||
} | ||
|
||
@Test | ||
public void shouldGetCorrectFieldsWhenGetFields() throws Exception { | ||
awaitKeyboardUnlock(); | ||
List<TestField> actualFields = client.getFields().stream() | ||
.map(TestField::new) | ||
.collect(Collectors.toList()); | ||
assertEquals(buildExpectedFields(), actualFields); | ||
} | ||
|
||
private List<TestField> buildExpectedFields() { | ||
List<TestField> testFieldList = new ArrayList<>(); | ||
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 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 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 Builder(int row, int column, String text) { | ||
this.row = row; | ||
this.column = column; | ||
this.text = text; | ||
} | ||
|
||
private Builder withMonocase() { | ||
ffw[1] |= MONOCASE_MASK; | ||
return this; | ||
} | ||
|
||
private Builder withHighIntensity() { | ||
attr |= HIGH_INTENSITY_MASK; | ||
return this; | ||
} | ||
|
||
private Builder withReverseImage() { | ||
attr |= REVERSE_IMAGE_MASK; | ||
return this; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unnecessary new lines |
||
private TestField build() { | ||
return new TestField(this); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public String toString() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
|
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary new line :-)