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

Checkstyle configuration (WIP) #284

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
313 changes: 313 additions & 0 deletions checkstyle.xml

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,37 @@
<maven-bundle-plugin-version>3.5.1</maven-bundle-plugin-version>
<maven-gpg-plugin-version>1.6</maven-gpg-plugin-version>
<maven-deploy-plugin-version>2.8.2</maven-deploy-plugin-version>
<maven-checkstyle-plugin-version>3.1.1</maven-checkstyle-plugin-version>
<nexus-staging-maven-plugin-version>1.6.8</nexus-staging-maven-plugin-version>
<build-helper-maven-plugin-version>3.0.0</build-helper-maven-plugin-version>
<checkstyle-version>8.32</checkstyle-version>
</properties>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin-version}</version>
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle-version}</version>
</dependency>
</dependencies>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
<executions>
<execution>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
Expand Down Expand Up @@ -293,6 +317,28 @@
</profile>
</profiles>

<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin-version}</version>
<!--
<dependencies>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle-version}</version>
</dependency>
</dependencies>
<configuration>
<configLocation>checkstyle.xml</configLocation>
</configuration>
-->
</plugin>
</plugins>
</reporting>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand Down
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/CachedFileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ public Date getCreationTime() throws IOException {
return cache.getCreationTime();
}

/*
* (non-Javadoc)
* @see quickfix.MessageStore#getCreationTimeCalendar()
*/
public Calendar getCreationTimeCalendar() throws IOException {
return cache.getCreationTimeCalendar();
}

private void initializeSequenceNumbers() throws IOException {
sequenceNumberFile.seek(0);
if (sequenceNumberFile.length() > 0) {
Expand Down
24 changes: 19 additions & 5 deletions quickfixj-core/src/main/java/quickfix/DefaultSessionSchedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ public class DefaultSessionSchedule implements SessionSchedule {
private final int[] weekdayOffsets;
protected static final Logger LOG = LoggerFactory.getLogger(DefaultSessionSchedule.class);

//Cache recent time data to reduce creation of calendar objects
private ThreadLocal<Calendar> threadLocalCalendar;
private ThreadLocal<TimeInterval> threadLocalRecentTimeInterval;

public DefaultSessionSchedule(SessionSettings settings, SessionID sessionID) throws ConfigError,
FieldConvertError {
threadLocalCalendar = ThreadLocal.withInitial(SystemTime::getUtcCalendar);
threadLocalRecentTimeInterval = new ThreadLocal<>();
isNonStopSession = settings.isSetting(sessionID, Session.SETTING_NON_STOP_SESSION)
&& settings.getBool(sessionID, Session.SETTING_NON_STOP_SESSION);

isNonStopSession = settings.isSetting(sessionID, Session.SETTING_NON_STOP_SESSION) && settings.getBool(sessionID, Session.SETTING_NON_STOP_SESSION);
TimeZone defaultTimeZone = getDefaultTimeZone(settings, sessionID);
if (isNonStopSession) {
isWeekdaySession = false;
Expand Down Expand Up @@ -104,7 +111,7 @@ private TimeEndPoint getTimeEndPoint(SessionSettings settings, SessionID session
}

private TimeZone getDefaultTimeZone(SessionSettings settings, SessionID sessionID)
throws ConfigError, FieldConvertError {
throws ConfigError {
TimeZone sessionTimeZone;
if (settings.isSetting(sessionID, Session.SETTING_TIMEZONE)) {
String sessionTimeZoneID = settings.getString(sessionID, Session.SETTING_TIMEZONE);
Expand Down Expand Up @@ -300,9 +307,16 @@ public boolean isSessionTime() {
if(isNonStopSession()) {
return true;
}
Calendar now = SystemTime.getUtcCalendar();
TimeInterval interval = theMostRecentIntervalBefore(now);
return interval.isContainingTime(now);
Calendar now = threadLocalCalendar.get();
now.setTimeInMillis(SystemTime.currentTimeMillis());
TimeInterval mostRecentInterval = threadLocalRecentTimeInterval.get();
if (mostRecentInterval != null && mostRecentInterval.isContainingTime(now)) {
return true;
}
mostRecentInterval = theMostRecentIntervalBefore(now);
boolean result = mostRecentInterval.isContainingTime(now);
threadLocalRecentTimeInterval.set(mostRecentInterval);
return result;
}

public String toString() {
Expand Down
8 changes: 8 additions & 0 deletions quickfixj-core/src/main/java/quickfix/FileStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ public Date getCreationTime() throws IOException {
return cache.getCreationTime();
}

/* (non-Javadoc)
* @see quickfix.MessageStore#getCreationTimeCalendar()
*/
@Override
public Calendar getCreationTimeCalendar() throws IOException {
return cache.getCreationTimeCalendar();
}

private void initializeSequenceNumbers() throws IOException {
senderSequenceNumberFile.seek(0);
if (senderSequenceNumberFile.length() > 0) {
Expand Down
4 changes: 4 additions & 0 deletions quickfixj-core/src/main/java/quickfix/JdbcStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public Date getCreationTime() throws IOException {
return cache.getCreationTime();
}

public Calendar getCreationTimeCalendar() throws IOException {
return cache.getCreationTimeCalendar();
}

public int getNextSenderMsgSeqNum() throws IOException {
return cache.getNextSenderMsgSeqNum();
}
Expand Down
4 changes: 4 additions & 0 deletions quickfixj-core/src/main/java/quickfix/MemoryStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public Date getCreationTime() throws IOException {
return creationTime.getTime();
}

public Calendar getCreationTimeCalendar() throws IOException {
return creationTime;
}

/* package */void setCreationTime(Calendar creationTime) {
this.creationTime = creationTime;
}
Expand Down
9 changes: 9 additions & 0 deletions quickfixj-core/src/main/java/quickfix/MessageStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package quickfix;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.io.IOException;
Expand Down Expand Up @@ -73,6 +74,14 @@ public interface MessageStore {
*/
Date getCreationTime() throws IOException;

/**
* Get the session creation time as a calendar object.
*
* @return the session creation time.
* @throws IOException IO error
*/
Calendar getCreationTimeCalendar() throws IOException;

/**
* Reset the message store. Sequence numbers are set back to 1 and stored
* messages are erased. The session creation time is also set to the time of
Expand Down
6 changes: 6 additions & 0 deletions quickfixj-core/src/main/java/quickfix/NoopStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package quickfix;

import java.util.Calendar;
import java.util.Collection;
import java.util.Date;

Expand All @@ -31,6 +32,7 @@
public class NoopStore implements MessageStore {

private Date creationTime = new Date();
private Calendar creationTimeCalendar = SystemTime.getUtcCalendar(creationTime);
private int nextSenderMsgSeqNum = 1;
private int nextTargetMsgSeqNum = 1;

Expand All @@ -41,6 +43,10 @@ public Date getCreationTime() {
return creationTime;
}

public Calendar getCreationTimeCalendar() {
return creationTimeCalendar;
}

public int getNextSenderMsgSeqNum() {
return nextSenderMsgSeqNum;
}
Expand Down
2 changes: 1 addition & 1 deletion quickfixj-core/src/main/java/quickfix/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ public String getRemoteAddress() {
private boolean isCurrentSession(final long time)
throws IOException {
return sessionSchedule == null || sessionSchedule.isSameSession(
SystemTime.getUtcCalendar(time), SystemTime.getUtcCalendar(state.getCreationTime()));
SystemTime.getUtcCalendar(time), state.getCreationTimeCalendar());
}

/**
Expand Down
5 changes: 5 additions & 0 deletions quickfixj-core/src/main/java/quickfix/SessionState.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package quickfix;

import java.io.IOException;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -502,6 +503,10 @@ public Object getLock() {
return lock;
}

public Calendar getCreationTimeCalendar() throws IOException {
return messageStore.getCreationTimeCalendar();
}

private final static class NullLog implements Log {
public void onOutgoing(String message) {
}
Expand Down
13 changes: 13 additions & 0 deletions quickfixj-core/src/main/java/quickfix/SleepycatStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,23 @@ private void convertToIOExceptionAndRethrow(Exception e) throws IOException {
throw ioe;
}


/*
* (non-Javadoc)
* @see quickfix.MessageStore#getCreationTime()
*/
public Date getCreationTime() throws IOException {
return info.getCreationTime().getTime();
}

/*
* (non-Javadoc)
* @see quickfix.MessageStore#getCreationTimeCalendar()
*/
public Calendar getCreationTimeCalendar() throws IOException {
return info.getCreationTime();
}

public int getNextSenderMsgSeqNum() throws IOException {
return info.getNextSenderMsgSeqNum();
}
Expand Down
120 changes: 120 additions & 0 deletions quickfixj-core/src/test/java/quickfix/DefaultSessionScheduleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package quickfix;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.ByteArrayInputStream;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

public class DefaultSessionScheduleTest {

private SessionID sessionID;
@Mock
private SystemTimeSource mockTimeSource;

@Before
public void before() {
MockitoAnnotations.initMocks(this);

sessionID = new SessionID("FIX.4.2:A->B");
SystemTime.setTimeSource(mockTimeSource);
}

@After
public void after() {
SystemTime.setTimeSource(null);
}

@Test
public void isNonStopSession_returns_true_when_SETTING_NON_STOP_SESSION_Y() throws FieldConvertError, ConfigError {
String sessionSettingsString = ""
+ "[DEFAULT]\n"
+ "NonStopSession=Y\n"
+ "\n"
+ "[SESSION]\n"
+ "BeginString=FIX.4.2\n"
+ "SenderCompID=A\n"
+ "TargetCompID=B\n";
SessionSettings sessionSettings = new SessionSettings(new ByteArrayInputStream(sessionSettingsString.getBytes()));
DefaultSessionSchedule schedule = new DefaultSessionSchedule(sessionSettings, sessionID);

assertTrue(schedule.isNonStopSession());
}

@Test
public void isNonStopSession_returns_false_when_SETTING_NON_STOP_SESSION_N() throws FieldConvertError, ConfigError {
String sessionSettingsString = ""
+ "[DEFAULT]\n"
+ "NonStopSession=N\n"
+ "\n"
+ "[SESSION]\n"
+ "BeginString=FIX.4.2\n"
+ "SenderCompID=A\n"
+ "TargetCompID=B\n"
+ "StartTime=00:00:00\n"
+ "EndTime=00:00:01\n";
SessionSettings sessionSettings = new SessionSettings(new ByteArrayInputStream(sessionSettingsString.getBytes()));
DefaultSessionSchedule schedule = new DefaultSessionSchedule(sessionSettings, sessionID);

assertFalse(schedule.isNonStopSession());
}

@Test
public void isNonStopSession_returns_false_when_SETTING_NON_STOP_SESSION_not_present() throws FieldConvertError, ConfigError {
String sessionSettingsString = ""
+ "[DEFAULT]\n"
+ "\n"
+ "[SESSION]\n"
+ "BeginString=FIX.4.2\n"
+ "SenderCompID=A\n"
+ "TargetCompID=B\n"
+ "StartTime=00:00:00\n"
+ "EndTime=00:00:01\n";
SessionSettings sessionSettings = new SessionSettings(new ByteArrayInputStream(sessionSettingsString.getBytes()));
DefaultSessionSchedule schedule = new DefaultSessionSchedule(sessionSettings, sessionID);

assertFalse(schedule.isNonStopSession());
}

@Test
public void isSessionTime_returns_true_for_time_within_window() throws FieldConvertError, ConfigError {
when(mockTimeSource.getTime()).thenReturn(1L);
String sessionSettingsString = ""
+ "[DEFAULT]\n"
+ "\n"
+ "[SESSION]\n"
+ "BeginString=FIX.4.2\n"
+ "SenderCompID=A\n"
+ "TargetCompID=B\n"
+ "StartTime=00:00:00\n"
+ "EndTime=00:00:01\n";
SessionSettings sessionSettings = new SessionSettings(new ByteArrayInputStream(sessionSettingsString.getBytes()));
DefaultSessionSchedule schedule = new DefaultSessionSchedule(sessionSettings, sessionID);

assertTrue(schedule.isSessionTime());
}

@Test
public void isSessionTime_returns_false_for_time_outside_window() throws FieldConvertError, ConfigError {
when(mockTimeSource.getTime()).thenReturn(2000L);
String sessionSettingsString = ""
+ "[DEFAULT]\n"
+ "\n"
+ "[SESSION]\n"
+ "BeginString=FIX.4.2\n"
+ "SenderCompID=A\n"
+ "TargetCompID=B\n"
+ "StartTime=00:00:00\n"
+ "EndTime=00:00:01\n";
SessionSettings sessionSettings = new SessionSettings(new ByteArrayInputStream(sessionSettingsString.getBytes()));
DefaultSessionSchedule schedule = new DefaultSessionSchedule(sessionSettings, sessionID);

assertFalse(schedule.isSessionTime());
}
}
Loading