Skip to content

Commit

Permalink
Modernize (#80)
Browse files Browse the repository at this point in the history
* Refactor tests to use JUnit 5

Migrated test annotations from JUnit 4 to JUnit 5 for better compatibility and future-proofing. Replaced JUnit 4 import statements, annotations, and assertion methods with their JUnit 5 equivalents.

* Switch to AssertJ for assertions in tests

Refactored the test classes to use AssertJ for assertions instead of JUnit's Assertions. This enhances the readability of the tests and provides more expressive error messages. All relevant assertion statements have been updated accordingly.

* Update tests to use correct offset for float assertions

Refactor test assertions in FieldFactoryTest to use Offset.offset for float values. This ensures accurate float comparison by specifying a float-specific offset, improving test reliability.

* tests

* Update FloatFieldTest to use Percentage for comparisons

Replaced 'within' with 'Percentage.withPercentage' for better accuracy in float comparisons. Also streamlined string assertions using 'hasToString' for improved readability.

* Rename test method and enhance decoder error handling

Renamed the test method from `decode` to `shouldDecode` to follow naming conventions. Added a new private `decode` method to improve error handling, ensuring the test correctly captures `DecoderException` when unexpected tags are encountered.

* Refactor test case setup in FixedPointNumberFromStringTest

Simplify the initialization of FixedPointNumber instances in test methods by removing the initFixedPointNumberFromStringTest method. This reduces redundant code and ensures each test directly initializes FixedPointNumber, enhancing readability and maintainability.

* Refactor BooleanFieldTest assertions for clarity

Update assertions in BooleanFieldTest to use `hasSize` for byte array length checks and explicitly cast characters to byte type for better readability and consistency. This refactoring improves code clarity and maintainability.

* Refactor ClientSessionHandlerTest for improved assertions

Updated ClientSessionHandlerTest to use AssertJ assertions to enhance readability and consistency. Introduced lenient mocking for the sequenceProvider and improved method naming for clarity. Additionally, restored password authentication for channelActive tests and fixed mock setups to align with the updated assertions.

* Refactor test class to remove redundant initialization.

Removed unused fields and methods in `FixedPointNumberFromDoubleTest` to streamline the test setup. Parameterized tests now directly initialize `FixedPointNumber` instances, resulting in cleaner and more efficient code.

* Refactor FixedPointNumberFromLongTest setup logic

Removed the initialization method and the setup method in FixedPointNumberFromLongTest, consolidating the setup logic directly within the test methods. This simplification reduces redundancy and improves readability. All test methods now instantiate FixedPointNumber directly.

* Refactor tests and update mock strictness in FixMessageEncoderTest

Update test names for clarity and remove redundant exceptions in encode tests. Also, change mock strictness to LENIENT to avoid unnecessary strict mock errors during testing.

* Refactor IntFieldFromBytesTest initialization

Removed unnecessary initialization method and directly set up IntField in tests. Also handled ParseException where required. These changes help streamline test code and improve readability.

* Relax Mock strictness to LENIENT for unit tests

Updated the strictness of @mock annotations to LENIENT in ServerChannelPipelineIntegrationTest and AbstractSessionHandlerTest classes. This change enhances test flexibility by allowing certain interactions without strict verification, ensuring smoother test execution.

* Update dependencies and add maven-dependency-plugin

Replaced netty-all with netty-handler and adjusted JUnit dependencies. Removed unnecessary JUnitParams and shifted slf4j-simple to test scope. Added maven-dependency-plugin for better dependency analysis and management.

* Refactor imports for multiple classes

Organized and expanded static imports for `UTCTimeOnlyField` and `UTCTimestampField` classes for improved readability. Aligned indentation in `FieldType.xsl` and removed unnecessary whitespace in other modified files.

---------

Co-authored-by: kpavlov <[email protected]>
  • Loading branch information
kpavlov and kpavlov authored Oct 28, 2024
1 parent 0d5d887 commit 247acc7
Show file tree
Hide file tree
Showing 52 changed files with 943 additions and 872 deletions.
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ ij_java_case_statement_on_separate_line = true
ij_java_catch_on_new_line = false
ij_java_class_annotation_wrap = split_into_lines
ij_java_class_brace_style = end_of_line
ij_java_class_count_to_use_import_on_demand = 5
ij_java_class_count_to_use_import_on_demand = 999
ij_java_class_names_in_javadoc = 1
ij_java_deconstruction_list_wrap = normal
ij_java_do_not_indent_top_level_class_members = false
Expand Down Expand Up @@ -180,7 +180,7 @@ ij_java_method_parameters_right_paren_on_new_line = false
ij_java_method_parameters_wrap = off
ij_java_modifier_list_wrap = false
ij_java_multi_catch_types_wrap = normal
ij_java_names_count_to_use_import_on_demand = 3
ij_java_names_count_to_use_import_on_demand = 999
ij_java_new_line_after_lparen_in_annotation = false
ij_java_new_line_after_lparen_in_deconstruction_pattern = true
ij_java_new_line_after_lparen_in_record_header = false
Expand Down
12 changes: 12 additions & 0 deletions benchmark/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ THE POSSIBILITY OF SUCH DAMAGE.
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-junit</artifactId>
<version>2.0.0.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
33 changes: 17 additions & 16 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
~ License for the specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.github.kpavlov.fixio</groupId>
<artifactId>fixio-parent</artifactId>
Expand All @@ -31,7 +32,7 @@
<!-- production dependencies -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<artifactId>netty-handler</artifactId>
<version>${netty.version}</version>
<scope>compile</scope>
</dependency>
Expand All @@ -46,32 +47,32 @@
<artifactId>fastutil</artifactId>
<version>${fastutil.version}</version>
</dependency>

<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.17.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.1.1</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.17.0</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
<optional>true</optional>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
Expand Down Expand Up @@ -146,4 +147,4 @@
<url>http://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
</project>
</project>
6 changes: 5 additions & 1 deletion core/src/main/java/fixio/fixprotocol/FixMessageImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package fixio.fixprotocol;

import fixio.fixprotocol.fields.*;
import fixio.fixprotocol.fields.AbstractField;
import fixio.fixprotocol.fields.CharField;
import fixio.fixprotocol.fields.FieldFactory;
import fixio.fixprotocol.fields.IntField;
import fixio.fixprotocol.fields.StringField;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class BooleanField extends AbstractField<Boolean> {

private static final byte[] TRUE_BYTES = new byte[]{(byte) 'Y'};
private static final byte[] FALSE_BYTES = new byte[]{(byte) 'N'};

private final boolean value;

protected BooleanField(int tagNum, boolean value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@
import java.time.LocalTime;
import java.util.Objects;

import static fixio.fixprotocol.FixConst.*;
import static fixio.fixprotocol.FixConst.TIME_FORMATTER_MICROS;
import static fixio.fixprotocol.FixConst.TIME_FORMATTER_MILLIS;
import static fixio.fixprotocol.FixConst.TIME_FORMATTER_NANOS;
import static fixio.fixprotocol.FixConst.TIME_FORMATTER_PICOS;
import static fixio.fixprotocol.FixConst.TIME_FORMATTER_SECONDS;
import static fixio.fixprotocol.FixConst.TIME_PATTERN_MICROS_LENGTH;
import static fixio.fixprotocol.FixConst.TIME_PATTERN_MILLIS_LENGTH;
import static fixio.fixprotocol.FixConst.TIME_PATTERN_NANOS_LENGTH;
import static fixio.fixprotocol.FixConst.TIME_PATTERN_PICOS_LENGTH;
import static fixio.fixprotocol.FixConst.TIME_PATTERN_SECONDS_LENGTH;
import static java.nio.charset.StandardCharsets.US_ASCII;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@
import java.time.ZonedDateTime;
import java.util.Objects;

import static fixio.fixprotocol.FixConst.*;
import static fixio.fixprotocol.FixConst.DATE_TIME_FORMATTER_MICROS;
import static fixio.fixprotocol.FixConst.DATE_TIME_FORMATTER_MILLIS;
import static fixio.fixprotocol.FixConst.DATE_TIME_FORMATTER_NANOS;
import static fixio.fixprotocol.FixConst.DATE_TIME_FORMATTER_PICOS;
import static fixio.fixprotocol.FixConst.DATE_TIME_FORMATTER_SECONDS;
import static fixio.fixprotocol.FixConst.DATE_TIME_PATTERN_MICROS_LENGTH;
import static fixio.fixprotocol.FixConst.DATE_TIME_PATTERN_MILLIS;
import static fixio.fixprotocol.FixConst.DATE_TIME_PATTERN_MILLIS_LENGTH;
import static fixio.fixprotocol.FixConst.DATE_TIME_PATTERN_NANOS_LENGTH;
import static fixio.fixprotocol.FixConst.DATE_TIME_PATTERN_PICOS_LENGTH;
import static fixio.fixprotocol.FixConst.DATE_TIME_PATTERN_SECONDS_LENGTH;
import static java.nio.charset.StandardCharsets.US_ASCII;


Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/fixio/netty/codec/FixMessageEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

package fixio.netty.codec;

import fixio.fixprotocol.*;
import fixio.fixprotocol.FixConst;
import fixio.fixprotocol.FixMessageBuilder;
import fixio.fixprotocol.FixMessageFragment;
import fixio.fixprotocol.FixMessageHeader;
import fixio.fixprotocol.Group;
import fixio.fixprotocol.GroupField;
import fixio.fixprotocol.fields.AbstractField;
import fixio.fixprotocol.fields.DateTimeFormatterWrapper;
import io.netty.buffer.ByteBuf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
package fixio.netty.pipeline;

import fixio.events.LogoutEvent;
import fixio.fixprotocol.*;
import fixio.fixprotocol.FieldType;
import fixio.fixprotocol.FixMessage;
import fixio.fixprotocol.FixMessageBuilder;
import fixio.fixprotocol.FixMessageBuilderImpl;
import fixio.fixprotocol.FixMessageHeader;
import fixio.fixprotocol.MessageTypes;
import fixio.fixprotocol.session.FixSession;
import fixio.handlers.FixApplication;
import fixio.validator.BusinessRejectException;
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/xml/FieldType.xsl
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@
private static final Int2ObjectArrayMap&lt;FieldType&gt; TYPES = new Int2ObjectArrayMap&lt;&gt;(FieldType.values().length);

static {
for (FieldType fieldType : FieldType.values()) {
if (fieldType.tag &gt; 0) {
TYPES.put(fieldType.tag, fieldType);
}
}
for (FieldType fieldType : FieldType.values()) {
if (fieldType.tag &gt; 0) {
TYPES.put(fieldType.tag, fieldType);
}
}
}

public static FieldType forTag(int tag) {
FieldType fieldType = TYPES.get(tag);
return (fieldType != null) ? fieldType : UNKNOWN;
FieldType fieldType = TYPES.get(tag);
return (fieldType != null) ? fieldType : UNKNOWN;
}

private FieldType(int tag, DataType type) {
this.tag = tag;
this.type = type;
this.enumValues = null;
this.tag = tag;
this.type = type;
this.enumValues = null;
}

private FieldType(int tag, DataType type, String... enumValues) {
this.tag = tag;
this.type = type;
this.enumValues = enumValues;
this.tag = tag;
this.type = type;
this.enumValues = enumValues;
}

public int tag() {
Expand Down Expand Up @@ -106,4 +106,4 @@
,"<value-of select="@enum"/>"//<value-of select="@description"/>
</template>

</stylesheet>
</stylesheet>
43 changes: 23 additions & 20 deletions core/src/test/java/fixio/FixConversationIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,25 @@
import fixio.netty.pipeline.server.AcceptAllAuthenticator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static fixio.fixprotocol.FieldType.UserRequestID;
import static fixio.fixprotocol.FieldType.UserRequestType;
import static fixio.fixprotocol.FieldType.UserStatus;
import static fixio.fixprotocol.FieldType.UserStatusText;
import static fixio.fixprotocol.FieldType.Username;
import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

public class FixConversationIT {
class FixConversationIT {

private static final int TEST_TIMEOUT = 5000;
private static final int PORT = 10453;
Expand All @@ -53,17 +55,17 @@ public class FixConversationIT {
private FixClient client;
private ChannelFuture clientCloseFuture;

@BeforeClass
public static void beforeClass() throws InterruptedException {
@BeforeAll
static void beforeClass() throws InterruptedException {
server = new FixServer(PORT,
new ServerLogicHandler(),
new AcceptAllAuthenticator(),
new InMemorySessionRepository());
server.start();
}

@AfterClass
public static void afterClass() {
@AfterAll
static void afterClass() {
server.stop();
}

Expand All @@ -84,8 +86,8 @@ private static FixMessageBuilder createUserStatusReport() {
return userRequest;
}

@Before
public void beforeMethod() throws InterruptedException {
@BeforeEach
void beforeMethod() throws InterruptedException {
client = new FixClient(new ClientApp(), new InMemorySessionRepository());
final PropertyFixSessionSettingsProviderImpl settingsProvider = new PropertyFixSessionSettingsProviderImpl("/fixClient.properties");
final PropertyAuthenticationProvider authenticationProvider = new PropertyAuthenticationProvider(settingsProvider.getProperties());
Expand All @@ -95,19 +97,20 @@ public void beforeMethod() throws InterruptedException {
conversation.clear();
}

@After
public void afterMethod() throws InterruptedException {
@AfterEach
void afterMethod() throws InterruptedException {
client.disconnect();
}

@Test(timeout = TEST_TIMEOUT)
public void testBusinessMessage() throws InterruptedException {
@Test
@Timeout(value = TEST_TIMEOUT, unit = TimeUnit.MILLISECONDS)
void businessMessage() throws InterruptedException {

clientCloseFuture.sync();

assertEquals(2, conversation.size());
assertEquals(MessageTypes.USER_REQUEST, conversation.get(0).getMessageType());
assertEquals(MessageTypes.USER_RESPONSE, conversation.get(1).getMessageType());
assertThat(conversation.size()).isEqualTo(2);
assertThat(conversation.get(0).getMessageType()).isEqualTo(MessageTypes.USER_REQUEST);
assertThat(conversation.get(1).getMessageType()).isEqualTo(MessageTypes.USER_RESPONSE);
}

private static class ServerLogicHandler extends FixApplicationAdapter {
Expand Down
26 changes: 15 additions & 11 deletions core/src/test/java/fixio/FixServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
import fixio.handlers.FixApplication;
import fixio.netty.pipeline.SessionRepository;
import fixio.netty.pipeline.server.FixAuthenticator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@RunWith(org.mockito.junit.MockitoJUnitRunner.class)
public class FixServerTest {
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

@ExtendWith(MockitoExtension.class)
class FixServerTest {

private FixServer server;
@Mock
Expand All @@ -34,19 +37,20 @@ public class FixServerTest {
@Mock
private SessionRepository sessionRepository;

@Before
public void setUp() {
@BeforeEach
void setUp() {
server = new FixServer(10100, fixApplication, fixAuthenticator, sessionRepository);
}

@Test
public void testStartStop() throws Exception {
void startStop() throws Exception {
server.start();
server.stop();
}

@Test(expected = IllegalStateException.class)
public void testStopBeforeStart() {
server.stop();
@Test
void stopBeforeStart() {
assertThatExceptionOfType(IllegalStateException.class).isThrownBy(() ->
server.stop());
}
}
Loading

0 comments on commit 247acc7

Please sign in to comment.