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

Updated testcase to use zoneddatetime instead of instant #30270

Open
wants to merge 4 commits into
base: integration
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*******************************************************************************
* Copyright (c) 2024 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package io.openliberty.jpa.data.tests.models;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;

/**
* Recreate from io.openliberty.data.internal_fat_jpa
* Same as DemographicInfo but uses OffsetDateTime instead of Instance
*/
@Entity
public class DemographicInfoWithOffset {

@Column
public OffsetDateTime collectedOn;

@GeneratedValue
@Id
public BigInteger id;

@Column
public BigDecimal publicDebt;

@Column
public BigDecimal intragovernmentalDebt;

@Column
public BigInteger numFullTimeWorkers;

public static DemographicInfoWithOffset of(int year, int month, int day,
long numFullTimeWorkers,
double intragovernmentalDebt, double publicDebt) {
DemographicInfoWithOffset inst = new DemographicInfoWithOffset();
inst.collectedOn = ZonedDateTime.of(year, month, day, 12, 0, 0, 0, ZoneId.of("America/New_York")).toOffsetDateTime();
inst.numFullTimeWorkers = BigInteger.valueOf(numFullTimeWorkers);
inst.intragovernmentalDebt = BigDecimal.valueOf(intragovernmentalDebt);
inst.publicDebt = BigDecimal.valueOf(publicDebt);
return inst;
}

@Override
public String toString() {
return "DemographicInfo from " + collectedOn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
Expand All @@ -53,6 +54,7 @@
import io.openliberty.jpa.data.tests.models.CityId;
import io.openliberty.jpa.data.tests.models.Coordinate;
import io.openliberty.jpa.data.tests.models.DemographicInfo;
import io.openliberty.jpa.data.tests.models.DemographicInfoWithOffset;
import io.openliberty.jpa.data.tests.models.DemographicInformation;
import io.openliberty.jpa.data.tests.models.Item;
import io.openliberty.jpa.data.tests.models.Line;
Expand Down Expand Up @@ -1354,10 +1356,10 @@ public void testOLGH28898() throws Exception {
public void testOLGH29781() throws Exception {
ZoneId ET = ZoneId.of("America/New_York");
Instant when = ZonedDateTime.of(2022, 4, 29, 12, 0, 0, 0, ET)
.toInstant();
.toInstant();
Store s1 = Store.of(2022, 4, 29, "Billy", 12L);
Store s2 = Store.of(2024, 5, 12, "Bobby", 9L);

int count;

tx.begin();
Expand All @@ -1368,8 +1370,8 @@ public void testOLGH29781() throws Exception {
tx.begin();
try {
count = em.createQuery("DELETE FROM Store WHERE this.time>:when")
.setParameter("when", when)
.executeUpdate();
.setParameter("when", when)
.executeUpdate();
tx.commit();
} catch (Exception e) {
tx.rollback();
Expand Down Expand Up @@ -1568,6 +1570,55 @@ public void testOLGH29443ZonedDateTime() throws Exception {
}
}

@Test //Original issue: https://github.com/OpenLiberty/open-liberty/issues/29443
public void testOLGH29443OffsetDateTime() throws Exception {
deleteAllEntities(DemographicInfoWithOffset.class);

ZoneId ET = ZoneId.of("America/New_York");
OffsetDateTime when = ZonedDateTime.of(2022, 4, 29, 12, 0, 0, 0, ET).toOffsetDateTime();

DemographicInfoWithOffset US2022 = DemographicInfoWithOffset.of(2022, 4, 29, 132250000, 6526909395140.41, 23847245116757.60);
DemographicInfoWithOffset US2007 = DemographicInfoWithOffset.of(2007, 4, 30, 121090000, 3833110332444.19, 5007058051986.64);

List<BigInteger> results;

tx.begin();
em.persist(US2022);
em.persist(US2007);
tx.commit();

List<Error> errors = new ArrayList<>();

Thread.sleep(Duration.ofSeconds(1).toMillis());

for (int i = 0; i < 10; i++) {
System.out.println("Executing SELECT query, iteration: " + i);

tx.begin();
results = em
.createQuery("SELECT this.numFullTimeWorkers FROM DemographicInfoWithOffset WHERE this.collectedOn=:when",
BigInteger.class)
.setParameter("when", when)
.getResultList();
tx.commit();

try {
assertNotNull("Query should not have returned null after iteration " + i, results);
// Recreate - an empty list is returned
assertFalse("Query should not have returned an empty list after iteration " + i, results.isEmpty());
assertEquals("Query should not have returned more than one result after iteration " + i, 1,
results.size());
assertEquals(US2022.numFullTimeWorkers, results.get(0));
} catch (AssertionError e) {
errors.add(e);
}
}

if (!errors.isEmpty()) {
throw new AssertionError("Executing the same query returned incorrect results " + errors.size() + " out of 10 executions", errors.get(0));
}
}

@Test
@Ignore("Reference issue: https://github.com/OpenLiberty/open-liberty/issues/29475")
public void testOLGH29475() throws Exception {
Expand Down