Skip to content

Commit

Permalink
[master] org.eclipse.persistence.internal.helper.Helper.dateFromYearM…
Browse files Browse the repository at this point in the history
…onthDate fix (#2033)

Signed-off-by: Radek Felcman <radek.felcman@oracle.com>
  • Loading branch information
rfelcman committed Jan 11, 2024
1 parent 7ffa026 commit e70ba5c
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -79,7 +79,7 @@ public static PolicyHolder example1() {
holder.addChildName("Bessy-Ray");
holder.setMale();
holder.setSsn(1111);
holder.setBirthDate(Helper.dateFromString("1950/02/30"));
holder.setBirthDate(Helper.dateFromString("1950/02/25"));
holder.setOccupation("Engineer");

holder.setAddress(Address.example1());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ public void populate(DatabaseSession session) {
emp.setFirstName("ALCTEmp");
emp.setLastName("NotRequired");
ALCTEmploymentPeriod empPeriod = new ALCTEmploymentPeriod();
empPeriod.setEndDate(Helper.dateFromYearMonthDate(2006, 12, 2));
empPeriod.setStartDate(Helper.dateFromYearMonthDate(2004, 12, 2));
//Valid month is 0-11
empPeriod.setEndDate(Helper.dateFromYearMonthDate(2006, 11, 2));
empPeriod.setStartDate(Helper.dateFromYearMonthDate(2004, 11, 2));
emp.setPeriod(empPeriod);
unitOfWork.registerObject(emp);
unitOfWork.commit();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -22,6 +22,7 @@
import java.util.HashSet;
import java.util.Vector;

import org.eclipse.persistence.exceptions.ConversionException;
import org.eclipse.persistence.internal.helper.Helper;
import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -315,4 +316,23 @@ public void timestampFromStringTest() {
Helper.setShouldOptimizeDates(optimizedDatesState);
}
}

@Test
public void dateFromYearMonthDateTest() {
//1950-02-20 //valid month is 0-11 0-January
java.sql.Date date = Helper.dateFromYearMonthDate(1950, 1, 20);
Assert.assertEquals("1950-02-20", date.toString());
}

@Test
public void dateFromYearMonthDateInvalidValueTest() {
//1950-02-30 //valid month is 0-11 0-January
//Invalid day value 30
try {
java.sql.Date date = Helper.dateFromYearMonthDate(1950, 1, 30);
Assert.assertEquals("1950-02-30", date.toString());
} catch (ConversionException e) {
Assert.assertTrue("The incorrect exception was thrown", e.getErrorCode() == ConversionException.INCORRECT_DATE_VALUE);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -30,6 +30,7 @@ public class ConversionException extends EclipseLinkException {
public final static int COULD_NOT_BE_CONVERTED = 3001;
public final static int COULD_NOT_BE_CONVERTED_EXTENDED = 3002;
public final static int INCORRECT_DATE_FORMAT = 3003;
public final static int INCORRECT_DATE_VALUE = 3010;
public final static int INCORRECT_TIME_FORMAT = 3004;
public final static int INCORRECT_TIMESTAMP_FORMAT = 3005;
public final static int COULD_NOT_CONVERT_TO_BYTE_ARRAY = 3006;
Expand Down Expand Up @@ -129,6 +130,14 @@ public static ConversionException incorrectDateFormat(String dateString) {
return conversionException;
}

public static ConversionException incorrectDateValue(String dateString) {
Object[] args = { dateString };
String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_DATE_VALUE, args);
ConversionException conversionException = new ConversionException(message, dateString, java.sql.Date.class, null);
conversionException.setErrorCode(INCORRECT_DATE_VALUE);
return conversionException;
}

public static ConversionException incorrectTimeFormat(String timeString) {
Object[] args = { timeString };
String message = ExceptionMessageGenerator.buildMessage(ConversionException.class, INCORRECT_TIME_FORMAT, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2018 IBM Corporation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -36,7 +36,8 @@ public final class ConversionExceptionResource extends ListResourceBundle {
{ "3006", "[{0}] must be of even length to be converted to a byte array." },
{ "3007", "The object [{0}], of class [{1}], could not be converted to [{2}]. Ensure that the class [{2}] is on the CLASSPATH. You may need to use alternate API passing in the appropriate class loader as required, or setting it on the default ConversionManager" },
{ "3008", "Incorrect date-time format: [{0}] (expected [YYYY-MM-DD''T''HH:MM:SS])" },
{ "3009", "Unable to set {0} properties [{1}] into [{2}]." }
{ "3009", "Unable to set {0} properties [{1}] into [{2}]." },
{ "3010", "Incorrect date value [YYYY-MM-DD]: [{0}]." }
};

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -48,6 +48,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Timestamp;
import java.time.DateTimeException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -882,13 +884,12 @@ public static java.sql.Date dateFromLong(Long longObject) {
* i.e. year is from 0, month is 0-11, date is 1-31.
*/
public static java.sql.Date dateFromYearMonthDate(int year, int month, int day) {
// Use a calendar to compute the correct millis for the date.
Calendar localCalendar = allocateCalendar();
localCalendar.clear();
localCalendar.set(year, month, day, 0, 0, 0);
long millis = localCalendar.getTimeInMillis();
java.sql.Date date = new java.sql.Date(millis);
releaseCalendar(localCalendar);
java.sql.Date date = null;
try {
date = java.sql.Date.valueOf(LocalDate.of(year, month + 1, day));
} catch (DateTimeException exception) {
throw ConversionException.incorrectDateValue(year + "-" + (month + 1) + "-" + day);
}
return date;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -546,7 +546,7 @@ public void testCreateExpertBeerConsumer() {

Record record3 = new Record();
record3.setDescription("Most beers consumed in a second - 5");
record3.setDate(Helper.dateFromYearMonthDate(2005, 12, 12));
record3.setDate(Helper.dateFromYearMonthDate(2005, 11, 12));
record3.setLocation(new Location("Miami", "USA"));
Venue venue3 = new Venue();
venue3.setAttendance(63000);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -347,7 +347,7 @@ public void testCreateExpertBeerConsumer() {

Record record2 = new Record();
record2.setDescription("Most beers consumed in a second - 5");
record2.setDate(Helper.dateFromYearMonthDate(2005, 12, 12));
record2.setDate(Helper.dateFromYearMonthDate(2005, 11, 12));
record2.setLocation(new Location("Miami", "USA"));
beerConsumer.getRecords().add(record2);

Expand Down
2 changes: 0 additions & 2 deletions moxy/org.eclipse.persistence.moxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@
<configuration>
<skipTests>${test-skip-moxy-jaxb}</skipTests>
<reportNameSuffix>test-moxy-jaxb</reportNameSuffix>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
<includes>
<include>org.eclipse.persistence.testing.jaxb.JAXBTestSuite</include>
<include>org.eclipse.persistence.testing.jaxb.JAXBTestSuite2</include>
Expand Down

0 comments on commit e70ba5c

Please sign in to comment.