Skip to content

Commit e18b585

Browse files
committed
clean up an antiquated test
1 parent 8d81010 commit e18b585

File tree

1 file changed

+49
-51
lines changed
  • hibernate-core/src/test/java/org/hibernate/orm/test/annotations

1 file changed

+49
-51
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/annotations/EntityTest.java

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import java.text.SimpleDateFormat;
99
import java.time.LocalDate;
1010
import java.time.ZoneId;
11+
import java.util.Calendar;
1112
import java.util.Date;
1213
import java.util.GregorianCalendar;
13-
import java.util.List;
1414
import java.util.TimeZone;
1515

1616
import jakarta.persistence.OptimisticLockException;
@@ -22,7 +22,6 @@
2222
import org.hibernate.cfg.AvailableSettings;
2323
import org.hibernate.dialect.MySQLDialect;
2424
import org.hibernate.dialect.OracleDialect;
25-
import org.hibernate.query.Query;
2625
import org.hibernate.type.StandardBasicTypes;
2726

2827
import org.hibernate.testing.orm.junit.DomainModel;
@@ -37,8 +36,9 @@
3736
import org.junit.jupiter.api.Test;
3837

3938
import static org.junit.jupiter.api.Assertions.assertEquals;
40-
import static org.junit.jupiter.api.Assertions.assertFalse;
39+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
4140
import static org.junit.jupiter.api.Assertions.assertNotNull;
41+
import static org.junit.jupiter.api.Assertions.assertNotSame;
4242
import static org.junit.jupiter.api.Assertions.fail;
4343

4444
/**
@@ -59,7 +59,7 @@
5959
)
6060
@SessionFactory
6161
public class EntityTest {
62-
private DateFormat df = SimpleDateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG );
62+
private final DateFormat dateFormat = SimpleDateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG );
6363

6464
@Test
6565
public void testLoad(DomainModelScope domainModelScope, SessionFactoryScope sessionFactoryScope) throws Exception {
@@ -69,9 +69,9 @@ public void testLoad(DomainModelScope domainModelScope, SessionFactoryScope sess
6969
sessionFactoryScope.inTransaction(
7070
session -> {
7171
Flight firstOne = new Flight();
72-
firstOne.setId( Long.valueOf( 1 ) );
72+
firstOne.setId( 1L );
7373
firstOne.setName( "AF3202" );
74-
firstOne.setDuration( new Long( 1000000 ) );
74+
firstOne.setDuration( 1000000L );
7575
firstOne.setDurationInSec( 2000 );
7676
session.persist( firstOne );
7777
session.flush();
@@ -81,12 +81,12 @@ public void testLoad(DomainModelScope domainModelScope, SessionFactoryScope sess
8181
//read it
8282
sessionFactoryScope.inTransaction(
8383
session -> {
84-
Flight firstOne = session.get( Flight.class, Long.valueOf( 1 ) );
84+
Flight firstOne = session.get( Flight.class, 1L );
8585
assertNotNull( firstOne );
86-
assertEquals( Long.valueOf( 1 ), firstOne.getId() );
86+
assertEquals( 1L, firstOne.getId() );
8787
assertEquals( "AF3202", firstOne.getName() );
88-
assertEquals( Long.valueOf( 1000000 ), firstOne.getDuration() );
89-
assertFalse( 2000l == firstOne.getDurationInSec(), "Transient is not working" );
88+
assertEquals( 1000000L, firstOne.getDuration() );
89+
assertNotEquals( 2000L, firstOne.getDurationInSec(), "Transient is not working" );
9090
}
9191
);
9292
}
@@ -97,9 +97,9 @@ public void testColumn(SessionFactoryScope scope) {
9797
scope.inTransaction(
9898
session -> {
9999
Flight firstOne = new Flight();
100-
firstOne.setId( Long.valueOf( 1 ) );
100+
firstOne.setId( 1L );
101101
firstOne.setName( "AF3202" );
102-
firstOne.setDuration( Long.valueOf( 1000000 ) );
102+
firstOne.setDuration( 1000000L );
103103
firstOne.setDurationInSec( 2000 );
104104
session.persist( firstOne );
105105
session.flush();
@@ -110,7 +110,7 @@ public void testColumn(SessionFactoryScope scope) {
110110
session -> {
111111
Transaction tx = session.beginTransaction();
112112
Flight firstOne = new Flight();
113-
firstOne.setId( Long.valueOf( 1 ) );
113+
firstOne.setId( 1L );
114114
firstOne.setName( null );
115115

116116
try {
@@ -127,32 +127,33 @@ public void testColumn(SessionFactoryScope scope) {
127127
//insert an object and check that name is not updatable
128128
scope.inTransaction(
129129
session -> {
130+
@SuppressWarnings("WriteOnlyObject")
130131
Flight firstOne = new Flight();
131-
firstOne.setId( Long.valueOf( 1 ) );
132+
firstOne.setId( 1L );
132133
firstOne.setName( "AF3202" );
133134
firstOne.setTriggeredData( "should not be insertable" );
134135
}
135136
);
136137

137138
scope.inTransaction(
138139
session -> {
139-
Flight firstOne = session.get( Flight.class, Long.valueOf( 1 ) );
140+
Flight firstOne = session.get( Flight.class, 1L );
140141
assertNotNull( firstOne );
141-
assertEquals( Long.valueOf( 1 ), firstOne.getId() );
142+
assertEquals( 1L, firstOne.getId() );
142143
assertEquals( "AF3202", firstOne.getName() );
143-
assertFalse( "should not be insertable".equals( firstOne.getTriggeredData() ) );
144+
assertNotEquals( "should not be insertable", firstOne.getTriggeredData() );
144145
firstOne.setName( "BA1234" );
145146
firstOne.setTriggeredData( "should not be updatable" );
146147
}
147148
);
148149

149150
scope.inTransaction(
150151
session -> {
151-
Flight firstOne = session.get( Flight.class, Long.valueOf( 1 ) );
152+
Flight firstOne = session.get( Flight.class, 1L );
152153
assertNotNull( firstOne );
153-
assertEquals( Long.valueOf( 1 ), firstOne.getId() );
154+
assertEquals( 1L, firstOne.getId() );
154155
assertEquals( "AF3202", firstOne.getName() );
155-
assertFalse( "should not be updatable".equals( firstOne.getTriggeredData() ) );
156+
assertNotEquals( "should not be updatable", firstOne.getTriggeredData() );
156157
}
157158
);
158159
}
@@ -163,13 +164,13 @@ public void testColumnUnique(SessionFactoryScope scope) {
163164
session -> {
164165
Transaction tx = session.beginTransaction();
165166
Sky sky = new Sky();
166-
sky.id = Long.valueOf( 2 );
167+
sky.id = 2L;
167168
sky.color = "blue";
168169
sky.day = "monday";
169170
sky.month = "January";
170171

171172
Sky sameSky = new Sky();
172-
sameSky.id = Long.valueOf( 3 );
173+
sameSky.id = 3L;
173174
sameSky.color = "blue";
174175
sky.day = "tuesday";
175176
sky.month = "January";
@@ -197,19 +198,19 @@ public void testColumnUnique(SessionFactoryScope scope) {
197198
public void testUniqueConstraint(SessionFactoryScope scope) {
198199
int id = 5;
199200
Sky sky = new Sky();
200-
sky.id = Long.valueOf( id++ );
201+
sky.id = (long) id++;
201202
sky.color = "green";
202203
sky.day = "monday";
203204
sky.month = "March";
204205

205206
Sky otherSky = new Sky();
206-
otherSky.id = Long.valueOf( id++ );
207+
otherSky.id = (long) id++;
207208
otherSky.color = "red";
208209
otherSky.day = "friday";
209210
otherSky.month = "March";
210211

211212
Sky sameSky = new Sky();
212-
sameSky.id = Long.valueOf( id++ );
213+
sameSky.id = (long) id++;
213214
sameSky.color = "green";
214215
sameSky.day = "monday";
215216
sameSky.month = "March";
@@ -248,30 +249,30 @@ public void testVersion(SessionFactoryScope scope) {
248249
scope.inTransaction(
249250
session -> {
250251
Flight firstOne = new Flight();
251-
firstOne.setId( Long.valueOf( 2 ) );
252+
firstOne.setId( 2L );
252253
firstOne.setName( "AF3202" );
253-
firstOne.setDuration( Long.valueOf( 500 ) );
254+
firstOne.setDuration( 500L );
254255
session.persist( firstOne );
255256
session.flush();
256257
}
257258
);
258259

259260
//read it
260261
Flight firstOne = scope.fromTransaction(
261-
session -> session.get( Flight.class, Long.valueOf( 2 ) )
262+
session -> session.get( Flight.class, 2L )
262263
);
263264

264265
//read it again
265266
Flight concurrentOne = scope.fromTransaction(
266267
session -> {
267-
Flight _concurrentOne = session.get( Flight.class, Long.valueOf( 2 ) );
268-
_concurrentOne.setDuration( Long.valueOf( 1000 ) );
268+
Flight _concurrentOne = session.get( Flight.class, 2L );
269+
_concurrentOne.setDuration( 1000L );
269270
return session.merge( _concurrentOne );
270271
}
271272
);
272273

273-
assertFalse( firstOne == concurrentOne );
274-
assertFalse( firstOne.getVersion().equals( concurrentOne.getVersion() ) );
274+
assertNotSame( firstOne, concurrentOne );
275+
assertNotEquals( firstOne.getVersion(), concurrentOne.getVersion() );
275276

276277
//reattach the first one
277278
scope.inSession(
@@ -284,10 +285,7 @@ public void testVersion(SessionFactoryScope scope) {
284285
fail( "Optimistic locking should work" );
285286
}
286287
catch (OptimisticLockException expected) {
287-
if ( expected.getCause() instanceof StaleStateException ) {
288-
//expected
289-
}
290-
else {
288+
if ( !(expected.getCause() instanceof StaleStateException) ) {
291289
fail( "StaleStateException expected but is " + expected.getCause() );
292290
}
293291
}
@@ -303,7 +301,7 @@ public void testVersion(SessionFactoryScope scope) {
303301
@Test
304302
public void testFieldAccess(SessionFactoryScope scope) {
305303
final Sky sky = new Sky();
306-
sky.id = Long.valueOf( 1 );
304+
sky.id = 1L;
307305
sky.color = "black";
308306
sky.area = "Paris";
309307
sky.day = "23";
@@ -320,7 +318,7 @@ public void testFieldAccess(SessionFactoryScope scope) {
320318
Sky _sky = session.get( Sky.class, sky.id );
321319
assertNotNull( _sky );
322320
assertEquals( "black", _sky.color );
323-
assertFalse( "Paris".equals( _sky.area ) );
321+
assertNotEquals( "Paris", _sky.area );
324322
}
325323
);
326324
}
@@ -339,7 +337,7 @@ public void testEntityName(DomainModelScope domainModelScope, SessionFactoryScop
339337

340338
sessionFactoryScope.inTransaction(
341339
session -> {
342-
List result = session.createQuery( "from Corporation" ).list();
340+
var result = session.createQuery( "from Corporation" ).list();
343341
assertNotNull( result );
344342
assertEquals( 1, result.size() );
345343
}
@@ -350,9 +348,9 @@ public void testEntityName(DomainModelScope domainModelScope, SessionFactoryScop
350348
@Test
351349
public void testNonGetter(SessionFactoryScope scope) {
352350
Flight airFrance = new Flight();
353-
airFrance.setId( Long.valueOf( 747 ) );
351+
airFrance.setId( 747L );
354352
airFrance.setName( "Paris-Amsterdam" );
355-
airFrance.setDuration( Long.valueOf( 10 ) );
353+
airFrance.setDuration( 10L );
356354
airFrance.setFactor( 25 );
357355

358356
scope.inTransaction(
@@ -363,8 +361,8 @@ public void testNonGetter(SessionFactoryScope scope) {
363361
session -> {
364362
Flight _airFrance = session.get( Flight.class, airFrance.getId() );
365363
assertNotNull( _airFrance );
366-
assertEquals( Long.valueOf( 10 ), _airFrance.getDuration() );
367-
assertFalse( 25 == _airFrance.getFactor( false ) );
364+
assertEquals( 10L, _airFrance.getDuration() );
365+
assertNotEquals( 25, _airFrance.getFactor( false ) );
368366
session.remove( _airFrance );
369367
}
370368
);
@@ -377,11 +375,11 @@ public void testTemporalType(SessionFactoryScope scope) {
377375
: ZoneId.systemDefault();
378376

379377
Flight airFrance = new Flight();
380-
airFrance.setId( Long.valueOf( 747 ) );
378+
airFrance.setId( 747L );
381379
airFrance.setName( "Paris-Amsterdam" );
382-
airFrance.setDuration( Long.valueOf( 10 ) );
383-
airFrance.setDepartureDate( Date.from(LocalDate.of( 2005, 06, 21 ).atStartOfDay(zoneId).toInstant()) );
384-
airFrance.setAlternativeDepartureDate( new GregorianCalendar( 2006, 02, 03, 10, 00 ) );
380+
airFrance.setDuration( 10L );
381+
airFrance.setDepartureDate( Date.from(LocalDate.of( 2005, 6, 21 ).atStartOfDay(zoneId).toInstant()) );
382+
airFrance.setAlternativeDepartureDate( new GregorianCalendar( 2006, Calendar.MARCH, 3, 10, 0 ) );
385383
airFrance.getAlternativeDepartureDate().setTimeZone( TimeZone.getTimeZone( "GMT" ) );
386384
airFrance.setBuyDate( new java.sql.Timestamp( 122367443 ) );
387385
airFrance.setFactor( 25 );
@@ -392,15 +390,15 @@ public void testTemporalType(SessionFactoryScope scope) {
392390

393391
scope.inTransaction(
394392
session -> {
395-
Query q = session.createQuery( "from Flight f where f.departureDate = :departureDate" );
393+
var q = session.createQuery( "from Flight f where f.departureDate = :departureDate" );
396394
q.setParameter( "departureDate", airFrance.getDepartureDate(), StandardBasicTypes.DATE );
397395
Flight copyAirFrance = (Flight) q.uniqueResult();
398396
assertNotNull( copyAirFrance );
399397
assertEquals(
400-
Date.from(LocalDate.of( 2005, 06, 21 ).atStartOfDay(zoneId).toInstant()),
398+
Date.from(LocalDate.of( 2005, 6, 21 ).atStartOfDay(zoneId).toInstant()),
401399
copyAirFrance.getDepartureDate()
402400
);
403-
assertEquals( df.format( airFrance.getBuyDate() ), df.format( copyAirFrance.getBuyDate() ) );
401+
assertEquals( dateFormat.format( airFrance.getBuyDate() ), dateFormat.format( copyAirFrance.getBuyDate() ) );
404402

405403
session.remove( copyAirFrance );
406404
}
@@ -413,7 +411,7 @@ public void testBasic(SessionFactoryScope scope) throws Exception {
413411
session -> {
414412
Transaction tx = session.beginTransaction();
415413
Flight airFrance = new Flight();
416-
airFrance.setId( Long.valueOf( 747 ) );
414+
airFrance.setId( 747L );
417415
airFrance.setName( "Paris-Amsterdam" );
418416
airFrance.setDuration( null );
419417
try {

0 commit comments

Comments
 (0)