Skip to content

Commit

Permalink
HHH-17187 - Disallow UUIDs with a trailing 0 byte in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Schatteman <jschatte@redhat.com>
  • Loading branch information
jrenaat authored and beikov committed Sep 19, 2023
1 parent b38bc76 commit 7d19732
Show file tree
Hide file tree
Showing 50 changed files with 233 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
Expand All @@ -32,7 +34,7 @@ public abstract class AbstractEntity implements Serializable {

public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
Expand All @@ -32,7 +34,7 @@ public abstract class AbstractEntity implements Serializable {

public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
Expand All @@ -30,7 +32,7 @@ public abstract class AbstractEntity implements Serializable {

public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
Expand All @@ -32,7 +34,7 @@ public abstract class AbstractEntity implements Serializable {

public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
Expand All @@ -30,7 +32,7 @@ public abstract class AbstractEntity implements Serializable {

public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

import java.io.Serializable;
import java.util.Date;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import jakarta.persistence.Basic;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
Expand All @@ -32,7 +34,7 @@ public abstract class AbstractEntity implements Serializable {

public AbstractEntity() {
super();
uuid = UUID.randomUUID().toString();
uuid = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
created = new Date();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected Class<?>[] getAnnotatedClasses() {
public void testMapKeyEnumerated() {
Session s = openSession();
s.beginTransaction();
User user = new User(SocialNetwork.STUB_NETWORK_NAME, "facebookId");
User user = new User("User1", SocialNetwork.STUB_NETWORK_NAME, "facebookId");
s.save( user );
s.getTransaction().commit();
s.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
@Table(name = "social_network_profile", uniqueConstraints = {@UniqueConstraint(columnNames = {"social_network", "network_id"})})
public class SocialNetworkProfile {
@jakarta.persistence.Id
@jakarta.persistence.GeneratedValue(generator = "system-uuid")
@org.hibernate.annotations.GenericGenerator(name = "system-uuid", strategy = "uuid2")
@jakarta.persistence.Column(name = "id", unique = true)
private String id;

Expand All @@ -44,6 +42,7 @@ protected SocialNetworkProfile() {
}

protected SocialNetworkProfile(User user, SocialNetwork socialNetworkType, String networkId) {
this.id = "snp_" + networkId + "_" + user.getId();
this.user = user;
this.socialNetworkType = socialNetworkType;
this.networkId = networkId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
@Table( name = "USER_TABLE" )
public class User {
@jakarta.persistence.Id
@jakarta.persistence.GeneratedValue(generator = "system-uuid")
@org.hibernate.annotations.GenericGenerator(name = "system-uuid", strategy = "uuid2")
@jakarta.persistence.Column(name = "id", unique = true)
private String id;

Expand All @@ -39,7 +37,8 @@ public class User {
protected User() {
}

public User(SocialNetwork sn, String socialNetworkId) {
public User(String id, SocialNetwork sn, String socialNetworkId) {
this.id = id;
SocialNetworkProfile profile = new SocialNetworkProfile(this, sn, socialNetworkId);
socialNetworkProfiles.put(sn, profile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

/**
* Unlike Hibernate's UUID generator. This avoids
Expand All @@ -24,7 +25,7 @@
public class UUIDGenerator implements IdentifierGenerator {
@Override
public Object generate(SharedSessionContractImplementor session, Object entity) throws HibernateException {
UUID uuid = UUID.randomUUID();
UUID uuid = SafeRandomUUIDGenerator.safeRandomUUID();
String sud = uuid.toString();
System.out.println("uuid="+uuid);
sud = sud.replaceAll("-", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

/**
* Unlike Hibernate's UUID generator. This avoids
Expand All @@ -24,7 +25,7 @@
public class UUIDGenerator implements IdentifierGenerator {
@Override
public Object generate(SharedSessionContractImplementor session, Object entity) throws HibernateException {
UUID uuid = UUID.randomUUID();
UUID uuid = SafeRandomUUIDGenerator.safeRandomUUID();
String sud = uuid.toString();
System.out.println("uuid="+uuid);
sud = sud.replaceAll("-", "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.hibernate.Hibernate;
import org.hibernate.Transaction;
import org.hibernate.boot.spi.MetadataImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.orm.test.bootstrap.binding.annotations.embedded.FloatLeg.RateIndex;
import org.hibernate.orm.test.bootstrap.binding.annotations.embedded.Leg.Frequency;
import org.hibernate.query.Query;
Expand Down Expand Up @@ -772,7 +772,7 @@ public void testTransientMergeComponentParent(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
Book b = new Book();
b.setIsbn( UUID.randomUUID().toString() );
b.setIsbn( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
b.setSummary( new Summary() );
b = (Book) session.merge( b );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

/**
* @author Luis Barreiro
Expand All @@ -26,7 +27,7 @@ public class OneToOneAssociationTest {
@Test
public void test() {
User user = new User();
user.setLogin( UUID.randomUUID().toString() );
user.setLogin( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );

Customer customer = new Customer();
customer.setUser( user );
Expand All @@ -37,7 +38,7 @@ public void test() {
EnhancerTestUtils.checkDirtyTracking( user, "login", "customer" );

User anotherUser = new User();
anotherUser.setLogin( UUID.randomUUID().toString() );
anotherUser.setLogin( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );

customer.setUser( anotherUser );

Expand All @@ -52,7 +53,7 @@ public void test() {
@Test
public void testSetNull() {
User user = new User();
user.setLogin( UUID.randomUUID().toString() );
user.setLogin( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );

Customer customer = new Customer();
customer.setUser( user );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import static org.hibernate.testing.bytecode.enhancement.EnhancerTestUtils.getFieldByReflection;
import static org.junit.Assert.assertEquals;
Expand All @@ -30,7 +31,7 @@ public class ExtendedAssociationManagementTest {
@Test
public void test() {
User user = new User();
user.login = UUID.randomUUID().toString();
user.login = SafeRandomUUIDGenerator.safeRandomUUIDAsString();

Customer customer = new Customer();
customer.user = user;
Expand All @@ -41,7 +42,7 @@ public void test() {
EnhancerTestUtils.checkDirtyTracking( user, "login", "customer" );

User anotherUser = new User();
anotherUser.login = UUID.randomUUID().toString();
anotherUser.login = SafeRandomUUIDGenerator.safeRandomUUIDAsString();

customer.user = anotherUser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
import jakarta.persistence.EmbeddedId;
Expand All @@ -19,6 +18,7 @@
import jakarta.persistence.Id;

import org.hibernate.annotations.Immutable;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
Expand Down Expand Up @@ -172,7 +172,7 @@ public static class EntityWithEmbeddedIdWithFinalField {
private String name;

public EntityWithEmbeddedIdWithFinalField() {
this.id = EmbeddableId.of( UUID.randomUUID().toString() );
this.id = EmbeddableId.of( SafeRandomUUIDGenerator.safeRandomUUIDAsString() );
}

public EmbeddableId getId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import jakarta.persistence.Table;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;

import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;

Expand Down Expand Up @@ -95,7 +96,7 @@ private static class Garage {
Set<Car> cars = new HashSet<>();

Garage() {
id = UUID.randomUUID().toString();
id = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
}

void insert(Car aCar) {
Expand All @@ -111,7 +112,7 @@ public static class Car {
String id;

Car() {
id = UUID.randomUUID().toString();
id = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.hibernate.orm.test.bytecode.enhancement.lazy.proxy.batch;

import java.util.UUID;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -19,6 +18,7 @@
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.testing.util.uuid.SafeRandomUUIDGenerator;
import org.hibernate.stat.spi.StatisticsImplementor;

import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
Expand All @@ -40,7 +40,7 @@
@RunWith(BytecodeEnhancerRunner.class)
@CustomEnhancementContext({ NoDirtyCheckEnhancementContext.class })
public abstract class AbstractBatchingTest extends BaseNonConfigCoreFunctionalTestCase {
protected String childName = UUID.randomUUID().toString();
protected String childName = SafeRandomUUIDGenerator.safeRandomUUIDAsString();
protected Long parentId;

@Override
Expand Down
Loading

0 comments on commit 7d19732

Please sign in to comment.