|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.annotations.lob; |
6 | 6 |
|
7 | | -import java.util.Arrays; |
8 | | - |
9 | | -import org.hibernate.Session; |
10 | 7 | import org.hibernate.dialect.SQLServerDialect; |
11 | 8 | import org.hibernate.dialect.SybaseDialect; |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 12 | +import org.junit.jupiter.api.Test; |
12 | 13 |
|
13 | | -import org.hibernate.testing.RequiresDialect; |
14 | | -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; |
15 | | -import org.junit.Assert; |
16 | | -import org.junit.Test; |
17 | | -import junit.framework.AssertionFailedError; |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
18 | 15 |
|
19 | | -import static org.junit.Assert.assertNull; |
20 | 16 |
|
21 | 17 | /** |
22 | 18 | * Tests eager materialization and mutation of long strings. |
|
25 | 21 | */ |
26 | 22 | @RequiresDialect(SQLServerDialect.class) |
27 | 23 | @RequiresDialect(SybaseDialect.class) |
28 | | -public class TextTest extends BaseCoreFunctionalTestCase { |
29 | | - |
30 | | - @Override |
31 | | - protected Class<?>[] getAnnotatedClasses() { |
32 | | - return new Class[] { LongStringHolder.class }; |
33 | | - } |
| 24 | +@DomainModel( |
| 25 | + annotatedPackageNames = "org.hibernate.orm.test.annotations.lob", |
| 26 | + annotatedClasses = { |
| 27 | + LongStringHolder.class |
| 28 | + } |
| 29 | +) |
| 30 | +public class TextTest { |
34 | 31 |
|
35 | 32 | private static final int LONG_STRING_SIZE = 10000; |
36 | 33 |
|
37 | 34 | @Test |
38 | | - public void testBoundedLongStringAccess() { |
39 | | - String original = buildRecursively(LONG_STRING_SIZE, 'x'); |
40 | | - String changed = buildRecursively(LONG_STRING_SIZE, 'y'); |
41 | | - |
42 | | - Session s = openSession(); |
43 | | - s.beginTransaction(); |
44 | | - LongStringHolder entity = new LongStringHolder(); |
45 | | - s.persist(entity); |
46 | | - s.getTransaction().commit(); |
47 | | - s.close(); |
48 | | - |
49 | | - s = openSession(); |
50 | | - s.beginTransaction(); |
51 | | - entity = (LongStringHolder) s.get(LongStringHolder.class, entity |
52 | | - .getId()); |
53 | | - assertNull(entity.getLongString()); |
54 | | - assertNull(entity.getName()); |
55 | | - assertNull(entity.getWhatEver()); |
56 | | - entity.setLongString(original); |
57 | | - entity.setName(original.toCharArray()); |
58 | | - entity.setWhatEver(wrapPrimitive(original.toCharArray())); |
59 | | - s.getTransaction().commit(); |
60 | | - s.close(); |
61 | | - |
62 | | - s = openSession(); |
63 | | - s.beginTransaction(); |
64 | | - entity = (LongStringHolder) s.get(LongStringHolder.class, entity |
65 | | - .getId()); |
66 | | - Assert.assertEquals( LONG_STRING_SIZE, entity.getLongString().length() ); |
67 | | - Assert.assertEquals( original, entity.getLongString() ); |
68 | | - Assert.assertNotNull( entity.getName() ); |
69 | | - Assert.assertEquals( LONG_STRING_SIZE, entity.getName().length ); |
70 | | - assertEquals( original.toCharArray(), entity.getName() ); |
71 | | - Assert.assertNotNull( entity.getWhatEver() ); |
72 | | - Assert.assertEquals( LONG_STRING_SIZE, entity.getWhatEver().length ); |
73 | | - assertEquals( original.toCharArray(), unwrapNonPrimitive( entity.getWhatEver() ) ); |
74 | | - entity.setLongString(changed); |
75 | | - entity.setName(changed.toCharArray()); |
76 | | - entity.setWhatEver(wrapPrimitive(changed.toCharArray())); |
77 | | - s.getTransaction().commit(); |
78 | | - s.close(); |
79 | | - |
80 | | - s = openSession(); |
81 | | - s.beginTransaction(); |
82 | | - entity = (LongStringHolder) s.get(LongStringHolder.class, entity |
83 | | - .getId()); |
84 | | - Assert.assertEquals( LONG_STRING_SIZE, entity.getLongString().length() ); |
85 | | - Assert.assertEquals( changed, entity.getLongString() ); |
86 | | - Assert.assertNotNull( entity.getName() ); |
87 | | - Assert.assertEquals( LONG_STRING_SIZE, entity.getName().length ); |
88 | | - assertEquals( changed.toCharArray(), entity.getName() ); |
89 | | - Assert.assertNotNull( entity.getWhatEver() ); |
90 | | - Assert.assertEquals( LONG_STRING_SIZE, entity.getWhatEver().length ); |
91 | | - assertEquals( changed.toCharArray(), unwrapNonPrimitive( entity.getWhatEver() ) ); |
92 | | - entity.setLongString(null); |
93 | | - entity.setName(null); |
94 | | - entity.setWhatEver(null); |
95 | | - s.getTransaction().commit(); |
96 | | - s.close(); |
97 | | - |
98 | | - s = openSession(); |
99 | | - s.beginTransaction(); |
100 | | - entity = (LongStringHolder) s.get(LongStringHolder.class, entity |
101 | | - .getId()); |
102 | | - assertNull(entity.getLongString()); |
103 | | - assertNull(entity.getName()); |
104 | | - assertNull(entity.getWhatEver()); |
105 | | - s.remove(entity); |
106 | | - s.getTransaction().commit(); |
107 | | - s.close(); |
| 35 | + public void testBoundedLongStringAccess(SessionFactoryScope scope) { |
| 36 | + String original = buildRecursively( LONG_STRING_SIZE, 'x' ); |
| 37 | + String changed = buildRecursively( LONG_STRING_SIZE, 'y' ); |
| 38 | + |
| 39 | + LongStringHolder e = new LongStringHolder(); |
| 40 | + scope.inTransaction( |
| 41 | + session -> |
| 42 | + session.persist( e ) |
| 43 | + ); |
| 44 | + |
| 45 | + scope.inTransaction( |
| 46 | + session -> { |
| 47 | + LongStringHolder entity = session.find( LongStringHolder.class, e.getId() ); |
| 48 | + assertThat( entity.getLongString() ).isNull(); |
| 49 | + assertThat( entity.getName() ).isNull(); |
| 50 | + assertThat( entity.getWhatEver() ).isNull(); |
| 51 | + entity.setLongString( original ); |
| 52 | + entity.setName( original.toCharArray() ); |
| 53 | + entity.setWhatEver( wrapPrimitive( original.toCharArray() ) ); |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + |
| 58 | + scope.inTransaction( |
| 59 | + session -> { |
| 60 | + LongStringHolder entity = session.find( LongStringHolder.class, e.getId() ); |
| 61 | + assertThat( entity.getLongString().length() ).isEqualTo( LONG_STRING_SIZE ); |
| 62 | + assertThat( entity.getLongString() ).isEqualTo( original ); |
| 63 | + assertThat( entity.getName() ).isNotNull(); |
| 64 | + assertThat( entity.getName().length ).isEqualTo( LONG_STRING_SIZE ); |
| 65 | + assertThat( entity.getName() ).isEqualTo( original.toCharArray() ); |
| 66 | + assertThat( entity.getWhatEver() ).isNotNull(); |
| 67 | + assertThat( entity.getWhatEver().length ).isEqualTo( LONG_STRING_SIZE ); |
| 68 | + assertThat( unwrapNonPrimitive( entity.getWhatEver() ) ).isEqualTo( original.toCharArray() ); |
| 69 | + entity.setLongString( changed ); |
| 70 | + entity.setName( changed.toCharArray() ); |
| 71 | + entity.setWhatEver( wrapPrimitive( changed.toCharArray() ) ); |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + scope.inTransaction( |
| 76 | + session -> { |
| 77 | + LongStringHolder entity = session.find( LongStringHolder.class, e.getId() ); |
| 78 | + assertThat( entity.getLongString().length() ).isEqualTo( LONG_STRING_SIZE ); |
| 79 | + assertThat( entity.getLongString() ).isEqualTo( changed ); |
| 80 | + assertThat( entity.getName() ).isNotNull(); |
| 81 | + assertThat( entity.getName().length ).isEqualTo( LONG_STRING_SIZE ); |
| 82 | + assertThat( entity.getName() ).isEqualTo( changed.toCharArray() ); |
| 83 | + assertThat( entity.getWhatEver() ).isNotNull(); |
| 84 | + assertThat( entity.getWhatEver().length ).isEqualTo( LONG_STRING_SIZE ); |
| 85 | + assertThat( unwrapNonPrimitive( entity.getWhatEver() ) ).isEqualTo( changed.toCharArray() ); |
| 86 | + entity.setLongString( null ); |
| 87 | + entity.setName( null ); |
| 88 | + entity.setWhatEver( null ); |
| 89 | + } |
| 90 | + ); |
| 91 | + |
| 92 | + scope.inTransaction( |
| 93 | + session -> { |
| 94 | + LongStringHolder entity = session.find( LongStringHolder.class, e.getId() ); |
| 95 | + assertThat( entity.getLongString() ).isNull(); |
| 96 | + assertThat( entity.getName() ).isNull(); |
| 97 | + assertThat( entity.getWhatEver() ).isNull(); |
| 98 | + session.remove( entity ); |
| 99 | + } |
| 100 | + ); |
108 | 101 | } |
109 | 102 |
|
110 | | - public static void assertEquals(char[] val1, char[] val2) { |
111 | | - if ( !Arrays.equals( val1, val2 ) ) { |
112 | | - throw new AssertionFailedError("byte arrays did not match"); |
113 | | - } |
114 | | - } |
115 | 103 |
|
116 | 104 | private String buildRecursively(int size, char baseChar) { |
117 | 105 | StringBuilder buff = new StringBuilder(); |
118 | | - for (int i = 0; i < size; i++) { |
119 | | - buff.append(baseChar); |
| 106 | + for ( int i = 0; i < size; i++ ) { |
| 107 | + buff.append( baseChar ); |
120 | 108 | } |
121 | 109 | return buff.toString(); |
122 | 110 | } |
123 | 111 |
|
124 | 112 | private Character[] wrapPrimitive(char[] bytes) { |
125 | 113 | int length = bytes.length; |
126 | 114 | Character[] result = new Character[length]; |
127 | | - for (int index = 0; index < length; index++) { |
128 | | - result[index] = Character.valueOf(bytes[index]); |
| 115 | + for ( int index = 0; index < length; index++ ) { |
| 116 | + result[index] = Character.valueOf( bytes[index] ); |
129 | 117 | } |
130 | 118 | return result; |
131 | 119 | } |
132 | 120 |
|
133 | 121 | private char[] unwrapNonPrimitive(Character[] bytes) { |
134 | 122 | int length = bytes.length; |
135 | 123 | char[] result = new char[length]; |
136 | | - for (int i = 0; i < length; i++) { |
| 124 | + for ( int i = 0; i < length; i++ ) { |
137 | 125 | result[i] = bytes[i].charValue(); |
138 | 126 | } |
139 | 127 | return result; |
140 | 128 | } |
141 | | - |
142 | | - @Override |
143 | | - protected String[] getAnnotatedPackages() { |
144 | | - return new String[] { "org.hibernate.orm.test.annotations.lob" }; |
145 | | - } |
146 | | - |
147 | 129 | } |
0 commit comments