44 */
55package org .hibernate .orm .test .serialization ;
66
7- import java .io .ByteArrayInputStream ;
8- import java .io .ByteArrayOutputStream ;
9- import java .io .ObjectInputStream ;
10- import java .io .ObjectOutputStream ;
11-
12- import org .junit .Test ;
13-
147import org .hibernate .internal .util .ReflectHelper ;
158import org .hibernate .orm .test .serialization .entity .AnEntity ;
169import org .hibernate .orm .test .serialization .entity .PK ;
2013import org .hibernate .property .access .spi .Setter ;
2114import org .hibernate .property .access .spi .SetterFieldImpl ;
2215import org .hibernate .property .access .spi .SetterMethodImpl ;
23-
2416import org .hibernate .testing .orm .junit .JiraKey ;
17+ import org .junit .jupiter .api .Test ;
2518
26- import static org .junit .Assert .assertSame ;
19+ import java .io .ByteArrayInputStream ;
20+ import java .io .ByteArrayOutputStream ;
21+ import java .io .ObjectInputStream ;
22+ import java .io .ObjectOutputStream ;
23+
24+ import static org .assertj .core .api .Assertions .assertThat ;
2725
2826/**
2927 * Tests that the can access inaccessible private fields and
3432public class GetterSetterSerializationTest {
3533
3634 @ Test
37- @ JiraKey ( value = "HHH-11202" )
35+ @ JiraKey (value = "HHH-11202" )
3836 public void testPrivateFieldGetter () throws Exception {
3937 final AnEntity entity = new AnEntity ( new PK ( 1L ) );
4038
4139 final String propertyName = "pk" ;
4240 final Getter getter = new GetterFieldImpl (
4341 AnEntity .class ,
4442 propertyName ,
45- ReflectHelper .findField ( AnEntity .class , propertyName )
43+ ReflectHelper .findField ( AnEntity .class , propertyName )
4644 );
47- final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
48- final ObjectOutputStream oos = new ObjectOutputStream (baos );
49- oos .writeObject ( getter );
5045
51- final ObjectInputStream ois = new ObjectInputStream ( new ByteArrayInputStream ( baos .toByteArray () ) );
46+ try (final ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
47+ try (final ObjectOutputStream oos = new ObjectOutputStream ( baos )) {
48+ oos .writeObject ( getter );
5249
53- final Getter getterClone = (Getter ) ois .readObject ();
50+ try (final ObjectInputStream ois = new ObjectInputStream (
51+ new ByteArrayInputStream ( baos .toByteArray () ) )) {
5452
55- assertSame ( getter .get ( entity ), getterClone .get ( entity ) );
53+ final Getter getterClone = (Getter ) ois .readObject ();
54+
55+ assertThat ( getterClone .get ( entity ) ).isSameAs ( getter .get ( entity ) );
56+ }
57+ }
58+ }
5659 }
5760
5861 @ Test
59- @ JiraKey ( value = "HHH-11202" )
62+ @ JiraKey (value = "HHH-11202" )
6063 public void testPrivateFieldSetter () throws Exception {
6164 AnEntity entity = new AnEntity ( new PK ( 1L ) );
6265
6366 final String propertyName = "pk" ;
6467 final Getter getter = new GetterFieldImpl (
6568 AnEntity .class ,
6669 propertyName ,
67- ReflectHelper .findField ( AnEntity .class , propertyName )
70+ ReflectHelper .findField ( AnEntity .class , propertyName )
6871 );
6972 final Setter setter = new SetterFieldImpl (
7073 AnEntity .class ,
7174 propertyName ,
72- ReflectHelper .findField ( AnEntity .class , propertyName )
75+ ReflectHelper .findField ( AnEntity .class , propertyName )
7376 );
7477
75- final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
76- final ObjectOutputStream oos = new ObjectOutputStream (baos );
77- oos .writeObject ( setter );
78+ try ( final ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
79+ try ( final ObjectOutputStream oos = new ObjectOutputStream ( baos )) {
80+ oos .writeObject ( setter );
7881
79- final ObjectInputStream ois = new ObjectInputStream ( new ByteArrayInputStream ( baos .toByteArray () ) );
82+ try (final ObjectInputStream ois = new ObjectInputStream (
83+ new ByteArrayInputStream ( baos .toByteArray () ) )) {
8084
81- final Setter setterClone = (Setter ) ois .readObject ();
82- final PK pkNew = new PK ( 2L );
83- setterClone .set ( entity , pkNew );
85+ final Setter setterClone = (Setter ) ois .readObject ();
86+ final PK pkNew = new PK ( 2L );
87+ setterClone .set ( entity , pkNew );
8488
85- assertSame ( pkNew , getter .get ( entity ) );
89+ assertThat ( pkNew ).isSameAs ( getter .get ( entity ) );
90+ }
91+ }
92+ }
8693 }
8794
8895 @ Test
89- @ JiraKey ( value = "HHH-11202" )
96+ @ JiraKey (value = "HHH-11202" )
9097 public void testProtectedMethodGetter () throws Exception {
9198 final AnEntity entity = new AnEntity ( new PK ( 1L ) );
9299
@@ -95,19 +102,24 @@ public void testProtectedMethodGetter() throws Exception {
95102 "pk" ,
96103 ReflectHelper .findGetterMethod ( AnEntity .class , "pk" )
97104 );
98- final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
99- final ObjectOutputStream oos = new ObjectOutputStream (baos );
100- oos .writeObject ( getter );
101105
102- final ObjectInputStream ois = new ObjectInputStream ( new ByteArrayInputStream ( baos .toByteArray () ) );
106+ try (final ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
107+ try (final ObjectOutputStream oos = new ObjectOutputStream ( baos )) {
108+ oos .writeObject ( getter );
109+
110+ try (final ObjectInputStream ois = new ObjectInputStream (
111+ new ByteArrayInputStream ( baos .toByteArray () ) )) {
103112
104- final Getter getterClone = (Getter ) ois .readObject ();
113+ final Getter getterClone = (Getter ) ois .readObject ();
105114
106- assertSame ( getter .get ( entity ), getterClone .get ( entity ) );
115+ assertThat ( getterClone .get ( entity ) ).isSameAs ( getter .get ( entity ) );
116+ }
117+ }
118+ }
107119 }
108120
109121 @ Test
110- @ JiraKey ( value = "HHH-11202" )
122+ @ JiraKey (value = "HHH-11202" )
111123 public void testProtectedMethodSetter () throws Exception {
112124 final AnEntity entity = new AnEntity ( new PK ( 1L ) );
113125
@@ -122,16 +134,19 @@ public void testProtectedMethodSetter() throws Exception {
122134 ReflectHelper .findSetterMethod ( AnEntity .class , "pk" , PK .class )
123135 );
124136
125- final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
126- final ObjectOutputStream oos = new ObjectOutputStream (baos );
127- oos .writeObject ( setter );
128-
129- final ObjectInputStream ois = new ObjectInputStream ( new ByteArrayInputStream ( baos .toByteArray () ) );
137+ try (final ByteArrayOutputStream baos = new ByteArrayOutputStream ()) {
138+ try (final ObjectOutputStream oos = new ObjectOutputStream ( baos )) {
139+ oos .writeObject ( setter );
130140
131- final Setter setterClone = (Setter ) ois .readObject ();
132- final PK pkNew = new PK ( 2L );
133- setterClone .set ( entity , pkNew );
141+ try (final ObjectInputStream ois = new ObjectInputStream (
142+ new ByteArrayInputStream ( baos .toByteArray () ) )) {
143+ final Setter setterClone = (Setter ) ois .readObject ();
144+ final PK pkNew = new PK ( 2L );
145+ setterClone .set ( entity , pkNew );
134146
135- assertSame ( pkNew , getter .get ( entity ) );
147+ assertThat ( pkNew ).isSameAs ( getter .get ( entity ) );
148+ }
149+ }
150+ }
136151 }
137152}
0 commit comments