Skip to content

Commit

Permalink
[misc] checktyle code correction
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Sep 13, 2019
1 parent 53aeace commit 01680d1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private void createConnection(String host, int port, String username) throws SQL

compressionHandler(options);
} catch (IOException ioException) {
closeSocket();
destroySocket();
if (host == null) {
throw ExceptionMapper.connException(
"Could not connect to socket : " + ioException.getMessage(),
Expand All @@ -407,7 +407,7 @@ private void createConnection(String host, int port, String username) throws SQL
.getMessage(),
ioException);
} catch (SQLException sqlException) {
closeSocket();
destroySocket();
throw sqlException;
}

Expand Down Expand Up @@ -471,7 +471,10 @@ private static Socket createSocket(final String host, final int port, final Opti
}
}

public void closeSocket() {
/**
* Closing socket in case of Connection error after socket creation.
*/
public void destroySocket() {
if (this.reader != null) {
try {
this.reader.close();
Expand Down Expand Up @@ -671,7 +674,7 @@ private void assignStream(Socket socket, Options options) throws SQLException {
}

} catch (IOException ioe) {
closeSocket();
destroySocket();
throw ExceptionMapper.connException("Socket error: " + ioe.getMessage(), ioe);
}
}
Expand Down Expand Up @@ -721,12 +724,12 @@ private void postConnectionQueries() throws SQLException {
activeStreamingResult = null;
hostFailed = false;
} catch (IOException ioException) {
closeSocket();
destroySocket();
throw ExceptionMapper.connException(
"Socket error during post connection queries: " + ioException.getMessage(),
ioException);
} catch (SQLException sqlException) {
closeSocket();
destroySocket();
throw sqlException;
}
}
Expand Down
143 changes: 74 additions & 69 deletions src/test/java/org/mariadb/jdbc/TimezoneExplicitCalendarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,82 +52,87 @@

package org.mariadb.jdbc;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.sql.*;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TimezoneExplicitCalendarTest extends BaseTest {

private static final TimeZone EUROPE_PARIS = TimeZone.getTimeZone("Europe/Paris");

private TimeZone previousTimeZone;

// Just to avoid the tests passing by chance when the JVM timezone is the same as the explicit timezone passed in tests
@Before
public void setDefaultTimeZoneToGMT() {
previousTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
}

@After
public void restorePreviousDefaultTimeZone() {
TimeZone.setDefault(previousTimeZone);
}

@Test
public void testDateWithExplicitTimeZone() throws SQLException {
try (Connection connection = setConnectionWithEuropeParisTimeZone()) {
Date epochInGMT = new Date(0);
PreparedStatement st = connection.prepareStatement("SELECT ?");
Calendar writeCalendar = Calendar.getInstance(EUROPE_PARIS);
st.setDate(1, epochInGMT, writeCalendar);

ResultSet rs = st.executeQuery();
assertTrue(rs.next());
Calendar readCalendar = Calendar.getInstance(EUROPE_PARIS);
assertEquals(rs.getDate(1, readCalendar), epochInGMT);
}
private static final TimeZone EUROPE_PARIS = TimeZone.getTimeZone("Europe/Paris");

private TimeZone previousTimeZone;

// Just to avoid the tests passing by chance when the JVM timezone is the same as the explicit
// timezone passed in tests
@Before
public void setDefaultTimeZoneToGmt() {
previousTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
}

@After
public void restorePreviousDefaultTimeZone() {
TimeZone.setDefault(previousTimeZone);
}

@Test
public void testDateWithExplicitTimeZone() throws SQLException {
try (Connection connection = setConnectionWithEuropeParisTimeZone()) {
Date epochInGmt = new Date(0);
PreparedStatement st = connection.prepareStatement("SELECT ?");
Calendar writeCalendar = Calendar.getInstance(EUROPE_PARIS);
st.setDate(1, epochInGmt, writeCalendar);

ResultSet rs = st.executeQuery();
assertTrue(rs.next());
Calendar readCalendar = Calendar.getInstance(EUROPE_PARIS);
assertEquals(rs.getDate(1, readCalendar), epochInGmt);
}

@Test
public void testTimestampWithExplicitTimeZone() throws SQLException {
try (Connection connection = setConnectionWithEuropeParisTimeZone()) {
Timestamp epochInGMT = new Timestamp(0);
PreparedStatement st = connection.prepareStatement("SELECT ?");
Calendar writeCalendar = Calendar.getInstance(EUROPE_PARIS);
st.setTimestamp(1, epochInGMT, writeCalendar);

ResultSet rs = st.executeQuery();
assertTrue(rs.next());
Calendar readCalendar = Calendar.getInstance(EUROPE_PARIS);
assertEquals(rs.getTimestamp(1, readCalendar), epochInGMT);
}
}

@Test
public void testTimeWithExplicitTimeZone() throws SQLException {
try (Connection connection = setConnectionWithEuropeParisTimeZone()) {
Time midnightInGMT = new Time(0);
PreparedStatement st = connection.prepareStatement("SELECT ?");
Calendar writeCalendar = Calendar.getInstance(EUROPE_PARIS);
st.setTime(1, midnightInGMT, writeCalendar);

ResultSet rs = st.executeQuery();
assertTrue(rs.next());
Calendar readCalendar = Calendar.getInstance(EUROPE_PARIS);
assertEquals(rs.getTime(1, readCalendar), midnightInGMT);
}
}

@Test
public void testTimestampWithExplicitTimeZone() throws SQLException {
try (Connection connection = setConnectionWithEuropeParisTimeZone()) {
Timestamp epochInGmt = new Timestamp(0);
PreparedStatement st = connection.prepareStatement("SELECT ?");
Calendar writeCalendar = Calendar.getInstance(EUROPE_PARIS);
st.setTimestamp(1, epochInGmt, writeCalendar);

ResultSet rs = st.executeQuery();
assertTrue(rs.next());
Calendar readCalendar = Calendar.getInstance(EUROPE_PARIS);
assertEquals(rs.getTimestamp(1, readCalendar), epochInGmt);
}

private Connection setConnectionWithEuropeParisTimeZone() throws SQLException {
return setConnection("&serverTimezone=Europe/Paris");
}

@Test
public void testTimeWithExplicitTimeZone() throws SQLException {
try (Connection connection = setConnectionWithEuropeParisTimeZone()) {
Time midnightInGmt = new Time(0);
PreparedStatement st = connection.prepareStatement("SELECT ?");
Calendar writeCalendar = Calendar.getInstance(EUROPE_PARIS);
st.setTime(1, midnightInGmt, writeCalendar);

ResultSet rs = st.executeQuery();
assertTrue(rs.next());
Calendar readCalendar = Calendar.getInstance(EUROPE_PARIS);
assertEquals(rs.getTime(1, readCalendar), midnightInGmt);
}
}

}
private Connection setConnectionWithEuropeParisTimeZone() throws SQLException {
return setConnection("&serverTimezone=Europe/Paris");
}
}

0 comments on commit 01680d1

Please sign in to comment.