Switch to ZonedDateTime implementation get tests passing

This commit is contained in:
dotasek 2023-07-26 17:08:08 -04:00
parent f88dae6ab8
commit 50047af493
3 changed files with 82 additions and 73 deletions

View File

@ -43,10 +43,13 @@ import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.sql.Timestamp;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.ZonedDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
@ -333,47 +336,30 @@ public final class IniFile
/** /**
* Returns the specified date property from the specified section. * Returns the specified date property from the specified section.
* @param pstrSection the INI section name. * @param iniSectionName the INI section name.
* @param pstrProp the property to be retrieved. * @param propertyKey the property to be retrieved.
* @return the date property value. * @return the date property value.
*/ */
public Date getTimestampProperty(String pstrSection, String pstrProp) public Date getTimestampProperty(String iniSectionName, String propertyKey) {
{
Timestamp tsRet = null;
Date dtTmp = null;
String strVal = null;
DateFormat dtFmt = null;
INIProperty objProp = null;
INISection objSec = null;
objSec = this.iniSections.get(pstrSection); Date dateToReturn = null;
if (objSec != null)
{ INISection iniSection = this.iniSections.get(iniSectionName);
objProp = objSec.getProperty(pstrProp); if (iniSection != null) {
try INIProperty objProp = iniSection.getProperty(propertyKey);
{ try {
if (objProp != null) strVal = objProp.getPropValue(); String propertyStringValue = null;
if (strVal != null) if (objProp != null) propertyStringValue = objProp.getPropValue();
{ if (propertyStringValue != null) {
dtFmt = new SimpleDateFormat(this.mstrTimeStampFmt); DateTimeFormatter dtFmt = DateTimeFormatter.ofPattern(this.mstrTimeStampFmt).withZone(ZoneId.systemDefault());
dtFmt.setTimeZone(TimeZone.getTimeZone("UTC"));
dtTmp = dtFmt.parse(strVal); ZonedDateTime zonedDateTime = ZonedDateTime.parse(propertyStringValue, dtFmt);
tsRet = new Timestamp(dtTmp.getTime()); dateToReturn = Date.from(zonedDateTime.toInstant());
} }
} } catch (IllegalArgumentException IAEx) {
catch (ParseException PExIgnore)
{
}
catch (IllegalArgumentException IAEx)
{
}
finally
{
if (objProp != null) objProp = null;
}
objSec = null;
} }
return tsRet; }
return dateToReturn;
} }
/*------------------------------------------------------------------------------ /*------------------------------------------------------------------------------
@ -529,7 +515,7 @@ public final class IniFile
* @param propertyValue the timestamp value to be persisted. * @param propertyValue the timestamp value to be persisted.
*/ */
public void setTimestampProperty(String iniSection, String propertyKey, public void setTimestampProperty(String iniSection, String propertyKey,
Timestamp propertyValue, String propertyComments) ZonedDateTime propertyValue, String propertyComments)
{ {
INISection objSec = null; INISection objSec = null;
@ -539,12 +525,12 @@ public final class IniFile
objSec = new INISection(iniSection); objSec = new INISection(iniSection);
this.iniSections.put(iniSection, objSec); this.iniSections.put(iniSection, objSec);
} }
objSec.setProperty(propertyKey, timeToStr(propertyValue, this.mstrTimeStampFmt), objSec.setProperty(propertyKey, timeToString(propertyValue, this.mstrTimeStampFmt),
propertyComments); propertyComments);
} }
/** /**
* Sets the format to be used to interpreat date values. * Sets the format to be used to interpret date values.
* @param pstrDtFmt the format string * @param pstrDtFmt the format string
* @throws IllegalArgumentException if the if the given pattern is invalid * @throws IllegalArgumentException if the if the given pattern is invalid
*/ */
@ -1074,7 +1060,7 @@ public final class IniFile
/** /**
* Helper method to check the existance of a file. * Helper method to check the existance of a file.
* @param the full path and name of the file to be checked. * @param pathName the full path and name of the file to be checked.
* @return true if file exists, false otherwise. * @return true if file exists, false otherwise.
*/ */
private boolean checkFile(String pathName) private boolean checkFile(String pathName)
@ -1100,19 +1086,19 @@ public final class IniFile
/** /**
* Converts a java.util.date into String * Converts a java.util.date into String
* @param pd Date that need to be converted to String * @param date Date to be converted to String
* @param pstrFmt The date format pattern. * @param dateFormat The date format pattern.
* @return String * @return String
*/ */
private String utilDateToStr(Date pdt, String pstrFmt) private String utilDateToStr(Date date, String dateFormat)
{ {
String strRet = null; String strRet = null;
SimpleDateFormat dtFmt = null; SimpleDateFormat dtFmt = null;
try try
{ {
dtFmt = new SimpleDateFormat(pstrFmt); dtFmt = new SimpleDateFormat(dateFormat);
strRet = dtFmt.format(pdt); strRet = dtFmt.format(date);
} }
catch (Exception e) catch (Exception e)
{ {
@ -1129,34 +1115,33 @@ public final class IniFile
* Converts the given sql timestamp object to a string representation. The format * Converts the given sql timestamp object to a string representation. The format
* to be used is to be obtained from the configuration file. * to be used is to be obtained from the configuration file.
* *
* @param pobjTS the sql timestamp object to be converted. * @param zonedDateTime the sql timestamp object to be converted.
* @param pblnGMT If true formats the string using GMT timezone * @param timeFormat the time format used to store the zonedDateTime value.
* otherwise using local timezone.
* @return the formatted string representation of the timestamp. * @return the formatted string representation of the timestamp.
*/ */
private String timeToStr(Timestamp pobjTS, String pstrFmt) private String timeToString(ZonedDateTime zonedDateTime, String timeFormat)
{ {
String strRet = null; String stringValue = null;
SimpleDateFormat dtFmt = null; DateTimeFormatter dateTimeFormatter = null;
try try
{ {
dtFmt = new SimpleDateFormat(pstrFmt); dateTimeFormatter = DateTimeFormatter.ofPattern(timeFormat);
strRet = dtFmt.format(pobjTS); stringValue = dateTimeFormatter.format(zonedDateTime);
} }
catch (IllegalArgumentException iae) catch (IllegalArgumentException iae)
{ {
strRet = ""; stringValue = "";
} }
catch (NullPointerException npe) catch (NullPointerException npe)
{ {
strRet = ""; stringValue = "";
} }
finally finally
{ {
if (dtFmt != null) dtFmt = null; if (dateTimeFormatter != null) dateTimeFormatter = null;
} }
return strRet; return stringValue;
} }
/** /**

View File

@ -9,10 +9,11 @@ import java.io.InputStream;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.sql.Timestamp;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
@ -88,7 +89,7 @@ import javax.annotation.Nonnull;
public class FilesystemPackageCacheManager extends BasePackageCacheManager implements IPackageCacheManager { public class FilesystemPackageCacheManager extends BasePackageCacheManager implements IPackageCacheManager {
public static final String INI_TIMESTAMP_FORMAT = "yyyyMMddhhmmss"; public static final String INI_TIMESTAMP_FORMAT = "yyyyMMddHHmmss";
public enum FilesystemPackageCacheMode { public enum FilesystemPackageCacheMode {
USER, SYSTEM, TESTING, CUSTOM USER, SYSTEM, TESTING, CUSTOM
@ -489,7 +490,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini")); IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.setTimeStampFormat(INI_TIMESTAMP_FORMAT); ini.setTimeStampFormat(INI_TIMESTAMP_FORMAT);
ini.setTimestampProperty("packages", id + "#" + v, Timestamp.from(Instant.now()), null); ini.setTimestampProperty("packages", id + "#" + v, ZonedDateTime.now(), null);
ini.setIntegerProperty("package-sizes", id + "#" + v, npm.getSize(), null); ini.setIntegerProperty("package-sizes", id + "#" + v, npm.getSize(), null);
ini.save(); ini.save();
if (progress) if (progress)

View File

@ -5,8 +5,11 @@ import org.junit.Test;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.sql.Timestamp; import java.text.ParseException;
import java.time.Instant; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -17,31 +20,51 @@ public class IniFileTest {
public static final String DUMMY_SECTION = "dummy-section"; public static final String DUMMY_SECTION = "dummy-section";
public static final String DUMMY_TIMESTAMP_KEY = "dummyTimestamp"; public static final String DUMMY_TIMESTAMP_KEY = "dummyTimestamp";
public static final String DUMMY_TIMESTAMP_COMMENT = "dummy comment"; public static final String DUMMY_TIMESTAMP_COMMENT = "dummy comment";
public static final String INTEGER_ONLY_DATETIME_STRING = "19791231191640";
public static final String INTEGER_ONLY_DATE_TIME_FORMAT = "yyyyMMddHHmmss";
public ZonedDateTime getDummyInstant() throws ParseException {
DateTimeFormatter dtFmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault());
return ZonedDateTime.parse(EXPECTED_TIMESTAMP_STRING,dtFmt);
}
@Test @Test
public void testSetTimestampPropertyDefaultFormat() throws IOException { public void testZonedDateTime() {
DateTimeFormatter dtFmt = DateTimeFormatter.ofPattern(INTEGER_ONLY_DATE_TIME_FORMAT);
LocalDateTime dateTime = LocalDateTime.parse(INTEGER_ONLY_DATETIME_STRING,dtFmt);
}
@Test
public void testSetTimestampPropertyDefaultFormat() throws IOException, ParseException {
Path testIni = Files.createTempFile("testIni", ".ini"); Path testIni = Files.createTempFile("testIni", ".ini");
IniFile iniFile = new IniFile(testIni.toAbsolutePath().toString()); IniFile iniFile = new IniFile(testIni.toAbsolutePath().toString());
java.sql.Timestamp timestamp = Timestamp.valueOf(EXPECTED_TIMESTAMP_STRING); ZonedDateTime timestamp = getDummyInstant();
iniFile.setTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY, timestamp, DUMMY_TIMESTAMP_COMMENT); iniFile.setTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY, timestamp, DUMMY_TIMESTAMP_COMMENT);
Date date = iniFile.getTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY); Date date = iniFile.getTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY);
assertEquals(EXPECTED_TIMESTAMP_STRING, iniFile.getStringProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY)); assertEquals(EXPECTED_TIMESTAMP_STRING, iniFile.getStringProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY));
assertEquals(timestamp.getTime(), date.getTime()); assertEquals(Date.from(timestamp.toInstant()).getTime(), date.getTime());
} }
@Test @Test
public void voidTestSetTimeStampPropertyExplicitFormat() throws IOException { public void voidTestSetTimeStampPropertyExplicitFormat() throws IOException, ParseException {
Path testIni = Files.createTempFile("testIni", ".ini"); Path testIni = Files.createTempFile("testIni", ".ini");
IniFile iniFile = new IniFile(testIni.toAbsolutePath().toString()); IniFile iniFile = new IniFile(testIni.toAbsolutePath().toString());
java.sql.Timestamp timestamp = Timestamp.valueOf(EXPECTED_TIMESTAMP_STRING); ZonedDateTime timestamp = getDummyInstant();
iniFile.setTimeStampFormat("yyyyMMddhhmmss"); iniFile.setTimeStampFormat(INTEGER_ONLY_DATE_TIME_FORMAT);
iniFile.setTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY, timestamp, DUMMY_TIMESTAMP_COMMENT); iniFile.setTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY, timestamp, DUMMY_TIMESTAMP_COMMENT);
Date date = iniFile.getTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY); Date date = iniFile.getTimestampProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY);
assertEquals("19791231071640", iniFile.getStringProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY)); assertEquals(INTEGER_ONLY_DATETIME_STRING, iniFile.getStringProperty(DUMMY_SECTION, DUMMY_TIMESTAMP_KEY));
assertEquals(timestamp.getTime(), date.getTime()); assertEquals(Date.from(timestamp.toInstant()).getTime(), date.getTime());
} }
} }