2424import org .apache .iotdb .itbase .category .LocalStandaloneIT ;
2525import org .apache .iotdb .itbase .category .RemoteIT ;
2626
27- import org .junit .After ;
27+ import org .junit .AfterClass ;
2828import org .junit .Assert ;
29- import org .junit .Before ;
29+ import org .junit .BeforeClass ;
3030import org .junit .Test ;
3131import org .junit .experimental .categories .Category ;
3232import org .junit .runner .RunWith ;
4242@ Category ({LocalStandaloneIT .class , ClusterIT .class , RemoteIT .class })
4343public class IoTDBDatetimeFormatIT {
4444
45- @ Before
46- public void setUp () throws Exception {
45+ @ BeforeClass
46+ public static void setUp () throws Exception {
4747 EnvFactory .getEnv ().getConfig ().getCommonConfig ().setTimestampPrecisionCheckEnabled (false );
4848 EnvFactory .getEnv ().initClusterEnvironment ();
4949 }
5050
51- @ After
52- public void tearDown () throws Exception {
51+ @ AfterClass
52+ public static void tearDown () throws Exception {
5353 EnvFactory .getEnv ().cleanClusterEnvironment ();
5454 }
5555
56+ private static void executeQuietly (Statement statement , String sql ) {
57+ try {
58+ statement .execute (sql );
59+ } catch (SQLException ignored ) {
60+ // ignore: cleanup may fail if target does not exist
61+ }
62+ }
63+
5664 @ Test
5765 public void testDatetimeInputFormat () {
5866 String [] datetimeStrings = {
@@ -87,25 +95,28 @@ public void testDatetimeInputFormat() {
8795 };
8896 try (Connection connection = EnvFactory .getEnv ().getConnection ();
8997 Statement statement = connection .createStatement ()) {
98+ try {
99+ connection .setClientInfo ("time_zone" , "+08:00" );
90100
91- connection .setClientInfo ("time_zone" , "+08:00" );
101+ for (int i = 0 ; i < datetimeStrings .length ; i ++) {
102+ String insertSql =
103+ String .format (
104+ "INSERT INTO root.sg1.d1(time, s1) values (%s, %d)" , datetimeStrings [i ], i );
105+ statement .execute (insertSql );
106+ }
92107
93- for (int i = 0 ; i < datetimeStrings .length ; i ++) {
94- String insertSql =
95- String .format (
96- "INSERT INTO root.sg1.d1(time, s1) values (%s, %d)" , datetimeStrings [i ], i );
97- statement .execute (insertSql );
98- }
99-
100- ResultSet resultSet = statement .executeQuery ("SELECT s1 FROM root.sg1.d1" );
101- Assert .assertNotNull (resultSet );
108+ ResultSet resultSet = statement .executeQuery ("SELECT s1 FROM root.sg1.d1" );
109+ Assert .assertNotNull (resultSet );
102110
103- int cnt = 0 ;
104- while (resultSet .next ()) {
105- Assert .assertEquals (timestamps [cnt ], resultSet .getLong (1 ));
106- cnt ++;
111+ int cnt = 0 ;
112+ while (resultSet .next ()) {
113+ Assert .assertEquals (timestamps [cnt ], resultSet .getLong (1 ));
114+ cnt ++;
115+ }
116+ Assert .assertEquals (timestamps .length , cnt );
117+ } finally {
118+ executeQuietly (statement , "DELETE DATABASE root.sg1" );
107119 }
108- Assert .assertEquals (timestamps .length , cnt );
109120 } catch (SQLException e ) {
110121 e .printStackTrace ();
111122 fail ();
@@ -116,26 +127,33 @@ public void testDatetimeInputFormat() {
116127 public void testBigDateTime () {
117128 try (Connection connection = EnvFactory .getEnv ().getConnection ();
118129 Statement statement = connection .createStatement ()) {
119- statement .setFetchSize (5 );
120- statement .execute ("CREATE DATABASE root.sg" );
130+ try {
131+ statement .setFetchSize (5 );
132+ statement .execute ("CREATE DATABASE root.sg" );
121133
122- statement .execute ("CREATE TIMESERIES root.sg.d1.s2 WITH DATATYPE=DOUBLE, ENCODING=PLAIN;" );
134+ statement .execute ("CREATE TIMESERIES root.sg.d1.s2 WITH DATATYPE=DOUBLE, ENCODING=PLAIN;" );
123135
124- statement .execute ("insert into root.sg.d1(time,s2) values (1618283005586000, 8.76);" );
125- statement .execute ("select * from root.sg.d1;" );
126- statement .execute ("select * from root.sg.d1 where time=53251-05-07T17:06:26.000+08:00" );
136+ statement .execute ("insert into root.sg.d1(time,s2) values (1618283005586000, 8.76);" );
137+ statement .execute ("select * from root.sg.d1;" );
138+ statement .execute ("select * from root.sg.d1 where time=53251-05-07T17:06:26.000+08:00" );
139+ } catch (SQLException e ) {
140+ e .printStackTrace ();
141+ fail ();
142+ }
143+ try (Connection connection2 = EnvFactory .getEnv ().getConnection ();
144+ Statement statement2 = connection2 .createStatement ()) {
145+ statement2 .execute ("insert into root.sg.d1(time,s2) values (16182830055860000000, 8.76);" );
146+ fail ();
147+ } catch (SQLException e ) {
148+ Assert .assertTrue (
149+ e .getMessage ()
150+ .contains ("please check whether the timestamp 16182830055860000000 is correct." ));
151+ } finally {
152+ executeQuietly (statement , "DELETE DATABASE root.sg" );
153+ }
127154 } catch (SQLException e ) {
128155 e .printStackTrace ();
129156 fail ();
130157 }
131- try (Connection connection = EnvFactory .getEnv ().getConnection ();
132- Statement statement = connection .createStatement ()) {
133- statement .execute ("insert into root.sg.d1(time,s2) values (16182830055860000000, 8.76);" );
134- fail ();
135- } catch (SQLException e ) {
136- Assert .assertTrue (
137- e .getMessage ()
138- .contains ("please check whether the timestamp 16182830055860000000 is correct." ));
139- }
140158 }
141159}
0 commit comments