Skip to content

Commit 257b70c

Browse files
committed
https://jira.baeldung.com/browse/BAEL-7958
1 parent 8ff9cf1 commit 257b70c

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed
139 KB
Loading

persistence-modules/core-java-persistence-3/src/main/java/storefileblob/JdbcConnection.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class JdbcConnection {
1818
public static Connection connect() throws SQLException {
1919
Connection connection = DriverManager.getConnection("jdbc:h2:./test", "sa", "");
2020
return connection;
21-
2221
}
2322

2423
public static byte[] convertFileToByteArray(String filePath) throws IOException {
Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,54 @@
11
package storefileblob;
22

33
import org.junit.Test;
4-
import org.junit.jupiter.api.MethodOrderer;
5-
import org.junit.jupiter.api.Order;
6-
import org.junit.jupiter.api.TestMethodOrder;
7-
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
87
import java.io.IOException;
98
import java.sql.SQLException;
10-
9+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1110
import static org.junit.jupiter.api.Assertions.assertTrue;
1211

13-
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1412
public class JdbcConnectionUnitTest {
1513

14+
private static final String TEST_FILE_PATH = "warehouse.png";
15+
private static final String RETRIEVED_FILE_PATH = "retrieve-warehouse.png";
16+
private static JdbcConnection jdbcConnection;
1617
String selectBlob = """
1718
SELECT picture FROM warehouses WHERE id=?
1819
""";
1920

21+
@BeforeAll
22+
public static void setUp() throws SQLException {
23+
jdbcConnection = new JdbcConnection();
24+
jdbcConnection.createSchema();
25+
}
26+
2027
@Test
21-
@Order(1)
2228
public void givenDatabaseSchema_whenJdbcConnectionIsEstablished_thenCreateSchema() throws SQLException {
23-
JdbcConnection jdbcConnection = new JdbcConnection();
2429
assertTrue(jdbcConnection.createSchema());
2530
}
2631

27-
@Test
28-
@Order(3)
29-
public void givenBlobFile_whenInsertingTheBlobFileIntoTheWarehouseTable_thenSuccessful() throws SQLException, IOException {
30-
JdbcConnection jdbcConnection = new JdbcConnection();
31-
jdbcConnection.createSchema();
32-
assertTrue(jdbcConnection.insertFile(4, "Liu", 3000, "warehouse.png"));
32+
@ParameterizedTest
33+
@CsvSource({ "1, 'Liu', 3000", "2, 'Walmart', 5000" })
34+
public void givenBlobFile_whenInsertingTheBlobFileAsByteArray_thenSuccessful(int id, String name, int capacity) throws SQLException, IOException {
35+
boolean result = jdbcConnection.insertFile(id, name, capacity, TEST_FILE_PATH);
36+
assertTrue(result);
3337
}
3438

35-
@Test
36-
@Order(2)
37-
public void givenBlobFile_whenInsertingTheFileIntoTheDatabaseAsStream_thenSuccessful() throws SQLException, IOException {
38-
JdbcConnection jdbcConnection = new JdbcConnection();
39-
jdbcConnection.createSchema();
40-
assertTrue(jdbcConnection.insertFileAsStream(6, "Liu", 3000, "warehouse.png"));
39+
@ParameterizedTest
40+
@CsvSource({ "7, 'Dylan', 8000", "8, 'Jumia', 1000" })
41+
public void givenBlobFile_whenInsertingTheFileIntoTheDatabaseInChunks_thenSuccessful(int id, String name, int capacity) throws SQLException, IOException {
42+
boolean result = jdbcConnection.insertFileAsStream(id, name, capacity, TEST_FILE_PATH);
43+
assertTrue(result);
4144
}
4245

4346
@Test
44-
@Order(4)
4547
public void givenAnEntityWithBlob_whenReadingBlobAndConvertingItToFile_thenSuccessful() throws SQLException, IOException {
46-
JdbcConnection jdbcConnection = new JdbcConnection();
47-
assertTrue(JdbcConnection.writeBlobToFile(selectBlob, 1, 6, "retrieve-warehouse.png"));
48+
boolean result = JdbcConnection.writeBlobToFile(selectBlob, 1, 2, RETRIEVED_FILE_PATH);
49+
byte[] expectedBytes = JdbcConnection.convertFileToByteArray(TEST_FILE_PATH);
50+
byte[] retrievedBytes = JdbcConnection.convertFileToByteArray(RETRIEVED_FILE_PATH);
51+
assertArrayEquals(expectedBytes, retrievedBytes);
4852
}
4953

5054
}

0 commit comments

Comments
 (0)