Merge branch 'master' of github.com:eugenp/tutorials
This commit is contained in:
@@ -13,4 +13,5 @@ This module contains articles about core Groovy concepts
|
||||
- [Metaprogramming in Groovy](https://www.baeldung.com/groovy-metaprogramming)
|
||||
- [A Quick Guide to Working with Web Services in Groovy](https://www.baeldung.com/groovy-web-services)
|
||||
- [Categories in Groovy](https://www.baeldung.com/groovy-categories)
|
||||
- [How to Determine the Data Type in Groovy](https://www.baeldung.com/groovy-determine-data-type)
|
||||
- [[<-- Prev]](/core-groovy)
|
||||
|
||||
@@ -8,3 +8,4 @@ This module contains articles about Java 14.
|
||||
- [Java Text Blocks](https://www.baeldung.com/java-text-blocks)
|
||||
- [Pattern Matching for instanceof in Java 14](https://www.baeldung.com/java-pattern-matching-instanceof)
|
||||
- [Helpful NullPointerExceptions in Java 14](https://www.baeldung.com/java-14-nullpointerexception)
|
||||
- [Foreign Memory Access API in Java 14](https://www.baeldung.com/java-foreign-memory-access)
|
||||
|
||||
@@ -11,4 +11,5 @@ This module contains articles about advanced topics about multithreading with co
|
||||
- [Guide to Work Stealing in Java](https://www.baeldung.com/java-work-stealing)
|
||||
- [Asynchronous Programming in Java](https://www.baeldung.com/java-asynchronous-programming)
|
||||
- [Java Thread Deadlock and Livelock](https://www.baeldung.com/java-deadlock-livelock)
|
||||
- [Guide to AtomicStampedReference in Java](https://www.baeldung.com/java-atomicstampedreference)
|
||||
- [[<-- previous]](/core-java-modules/core-java-concurrency-advanced-2)
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.weeknumber;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class WeekNumberUsingCalendar {
|
||||
|
||||
public int getWeekNumberFrom(String day, String dateFormat, Locale locale) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
|
||||
|
||||
Calendar calendar = Calendar.getInstance(locale);
|
||||
Date date = sdf.parse(day);
|
||||
calendar.setTime(date);
|
||||
|
||||
return calendar.get(Calendar.WEEK_OF_YEAR);
|
||||
}
|
||||
|
||||
public int getWeekNumberFrom(int year, int month, int day, Locale locale) {
|
||||
Calendar calendar = Calendar.getInstance(locale);
|
||||
calendar.set(year, month, day);
|
||||
|
||||
return calendar.get(Calendar.WEEK_OF_YEAR);
|
||||
}
|
||||
|
||||
public int getWeekNumberFrom(int year, int month, int day, int firstDayOfWeek, int minimalDaysInFirstWeek, Locale locale) {
|
||||
Calendar calendar = Calendar.getInstance(locale);
|
||||
calendar.setFirstDayOfWeek(firstDayOfWeek);
|
||||
calendar.setMinimalDaysInFirstWeek(minimalDaysInFirstWeek);
|
||||
calendar.set(year, month, day);
|
||||
|
||||
return calendar.get(Calendar.WEEK_OF_YEAR);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
System.out.println(calendar.getWeekNumberFrom(2020, 2, 22, Locale.CANADA));
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.weeknumber;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.WeekFields;
|
||||
import java.util.Locale;
|
||||
|
||||
public class WeekNumberUsingLocalDate {
|
||||
|
||||
public Integer getWeekNumberUsingWeekFiedsFrom(String day, String dayFormat, Locale locale) {
|
||||
LocalDate date = LocalDate.parse(day, DateTimeFormatter.ofPattern(dayFormat));
|
||||
|
||||
return date.get(WeekFields.of(locale)
|
||||
.weekOfYear());
|
||||
}
|
||||
|
||||
public Integer getWeekNumberUsinWeekFieldsFrom(int year, int month, int day, Locale locale) {
|
||||
LocalDate date = LocalDate.of(year, month, day);
|
||||
|
||||
return date.get(WeekFields.of(locale)
|
||||
.weekOfYear());
|
||||
}
|
||||
|
||||
public Integer getWeekNumberUsingChronoFieldFrom(int year, int month, int day) {
|
||||
LocalDate date = LocalDate.of(year, month, day);
|
||||
|
||||
return date.get(ChronoField.ALIGNED_WEEK_OF_YEAR);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.weeknumber;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Calendar;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class WeekNumberUsingCalendarUnitTest {
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingLocaleItaly_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() throws ParseException {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(12, calendar.getWeekNumberFrom("20200322", "yyyyMMdd", Locale.ITALY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingLocaleCanada_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() throws ParseException {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(13, calendar.getWeekNumberFrom("20200322", "yyyyMMdd", Locale.CANADA));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersLocaleItaly_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(12, calendar.getWeekNumberFrom(2020, 2, 22, Locale.ITALY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersLocaleItalyChangingWeekCalculationSettings_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(13, calendar.getWeekNumberFrom(2020, 2, 22, Calendar.SUNDAY, 4, Locale.ITALY));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersLocaleCanada_thenGettingWeekNumberUsingCalendarIsCorrectlyReturned() {
|
||||
WeekNumberUsingCalendar calendar = new WeekNumberUsingCalendar();
|
||||
|
||||
assertEquals(13, calendar.getWeekNumberFrom(2020, 2, 22, Locale.CANADA));
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.baeldung.weeknumber;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class WeekNumberUsingLocalDateUnitTest {
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingWeekFieldsWithLocaleItaly_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(12, localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.ITALY)
|
||||
.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingWeekFieldsWithLocaleCanada_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(13, localDate.getWeekNumberUsingWeekFiedsFrom("20200322", "yyyyMMdd", Locale.CANADA)
|
||||
.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInStringAndDateFormatUsingChronoFieds_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(12, localDate.getWeekNumberUsingChronoFieldFrom(2020, 3, 22)
|
||||
.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersUsingWeekFieldsWithLocaleItaly_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(12, localDate.getWeekNumberUsinWeekFieldsFrom(2020, 3, 22, Locale.ITALY)
|
||||
.longValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDateInYearMonthDayNumbersUsingWeekFieldsWithLocaleCanada_thenGettingWeekNumberUsingLocalDateIsCorrectlyReturned() {
|
||||
WeekNumberUsingLocalDate localDate = new WeekNumberUsingLocalDate();
|
||||
|
||||
assertEquals(13, localDate.getWeekNumberUsinWeekFieldsFrom(2020, 3, 22, Locale.CANADA)
|
||||
.longValue());
|
||||
}
|
||||
}
|
||||
@@ -10,3 +10,4 @@
|
||||
- [Difference Between Java Matcher find() and matches()](https://www.baeldung.com/java-matcher-find-vs-matches)
|
||||
- [How to Use Regular Expressions to Replace Tokens in Strings](https://www.baeldung.com/java-regex-token-replacement)
|
||||
- [Regular Expressions \s and \s+ in Java](https://www.baeldung.com/java-regex-s-splus)
|
||||
- [Validate Phone Numbers With Java Regex](https://www.baeldung.com/java-regex-validate-phone-numbers)
|
||||
|
||||
+23
@@ -4,8 +4,10 @@ import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@@ -39,6 +41,27 @@ public class EncodeDecodeUnitTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJavaBase64_whenEncodedStream_thenDecodedStreamOK() throws IOException {
|
||||
|
||||
try (OutputStream os = java.util.Base64.getEncoder().wrap(new FileOutputStream(OUT_FILE));
|
||||
FileInputStream fis = new FileInputStream(IN_FILE)) {
|
||||
byte[] bytes = new byte[1024];
|
||||
int read;
|
||||
while ((read = fis.read(bytes)) > -1) {
|
||||
os.write(bytes, 0, read);
|
||||
}
|
||||
}
|
||||
|
||||
byte[] encoded = java.util.Base64.getEncoder().encode(inFileBytes);
|
||||
byte[] encodedOnDisk = Files.readAllBytes(Paths.get(OUT_FILE));
|
||||
assertArrayEquals(encoded, encodedOnDisk);
|
||||
|
||||
byte[] decoded = java.util.Base64.getDecoder().decode(encoded);
|
||||
byte[] decodedOnDisk = java.util.Base64.getDecoder().decode(encodedOnDisk);
|
||||
assertArrayEquals(decoded, decodedOnDisk);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenApacheCommons_givenJavaBase64_whenEncoded_thenDecodedOK() throws IOException {
|
||||
|
||||
|
||||
@@ -9,3 +9,4 @@ This module contains articles about core Kotlin collections.
|
||||
- [Converting a List to Map in Kotlin](https://www.baeldung.com/kotlin-list-to-map)
|
||||
- [Filtering Kotlin Collections](https://www.baeldung.com/kotlin-filter-collection)
|
||||
- [Collection Transformations in Kotlin](https://www.baeldung.com/kotlin-collection-transformations)
|
||||
- [Difference between fold and reduce in Kotlin](https://www.baeldung.com/kotlin/fold-vs-reduce)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Building a Java Application With Gradle](https://www.baeldung.com/gradle-building-a-java-app)
|
||||
@@ -4,4 +4,5 @@ This module contains articles about conversions among Collection types and array
|
||||
|
||||
### Relevant Articles:
|
||||
- [Array to String Conversions](https://www.baeldung.com/java-array-to-string)
|
||||
- More articles: [[<-- prev]](../java-collections-conversions)
|
||||
- [Mapping Lists with ModelMapper](https://www.baeldung.com/java-modelmapper-lists)
|
||||
- More articles: [[<-- prev]](../java-collections-conversions)
|
||||
|
||||
@@ -11,3 +11,4 @@ This module contains articles about JEE 7.
|
||||
- [Introduction to Testing with Arquillian](https://www.baeldung.com/arquillian)
|
||||
- [Java EE 7 Batch Processing](https://www.baeldung.com/java-ee-7-batch-processing)
|
||||
- [The Difference Between CDI and EJB Singleton](https://www.baeldung.com/jee-cdi-vs-ejb-singleton)
|
||||
- [Invoking a SOAP Web Service in Java](https://www.baeldung.com/java-soap-web-service)
|
||||
|
||||
@@ -4,6 +4,7 @@ This module contains articles about jsoup.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Parsing HTML in Java with Jsoup](https://www.baeldung.com/java-with-jsoup)
|
||||
- [How to add proxy support to Jsoup?](https://www.baeldung.com/java-jsoup-proxy)
|
||||
|
||||
### Build the Project
|
||||
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import java.net.Proxy;
|
||||
import org.jsoup.Jsoup;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JsoupProxyIntegrationTest {
|
||||
public class JsoupProxyLiveTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingHostAndPort_thenConnect() throws IOException {
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [An Introduction to Kaniko](https://www.baeldung.com/ops/kaniko)
|
||||
@@ -11,7 +11,8 @@ This module contains articles about libraries for data processing in Java.
|
||||
- [Guide to JMapper](https://www.baeldung.com/jmapper)
|
||||
- [An Introduction to SuanShu](https://www.baeldung.com/suanshu)
|
||||
- [Intro to Derive4J](https://www.baeldung.com/derive4j)
|
||||
More articles: [[<-- prev]](/../libraries-data)
|
||||
- [Java-R Integration](https://www.baeldung.com/java-r-integration)
|
||||
- More articles: [[<-- prev]](/../libraries-data)
|
||||
|
||||
##### Building the project
|
||||
You can build the project from the command line using: *mvn clean install*, or in an IDE. If you have issues with the derive4j imports in your IDE, you have to add the folder: *target/generated-sources/annotations* to the project build path in your IDE.
|
||||
|
||||
@@ -24,6 +24,8 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import static com.baeldung.dbunit.ConnectionSettings.JDBC_URL;
|
||||
import static com.baeldung.dbunit.ConnectionSettings.PASSWORD;
|
||||
import static com.baeldung.dbunit.ConnectionSettings.USER;
|
||||
import static java.util.stream.Collectors.joining;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.dbunit.Assertion.assertEqualsIgnoreCols;
|
||||
@@ -33,12 +35,14 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(DataSourceDBUnitTest.class);
|
||||
|
||||
private Connection connection;
|
||||
|
||||
@Override
|
||||
protected DataSource getDataSource() {
|
||||
JdbcDataSource dataSource = new JdbcDataSource();
|
||||
dataSource.setURL(JDBC_URL);
|
||||
dataSource.setUser("sa");
|
||||
dataSource.setPassword("");
|
||||
dataSource.setUser(USER);
|
||||
dataSource.setPassword(PASSWORD);
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@@ -62,6 +66,7 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
connection = getConnection().getConnection();
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -71,9 +76,7 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenSelect_thenFirstTitleIsGreyTShirt() throws SQLException {
|
||||
final Connection connection = getDataSource().getConnection();
|
||||
|
||||
final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
|
||||
ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
|
||||
|
||||
assertThat(rs.next()).isTrue();
|
||||
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
|
||||
@@ -81,58 +84,59 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
||||
|
||||
@Test
|
||||
public void givenDataSetEmptySchema_whenDataSetCreated_thenTablesAreEqual() throws Exception {
|
||||
final IDataSet expectedDataSet = getDataSet();
|
||||
final ITable expectedTable = expectedDataSet.getTable("CLIENTS");
|
||||
final IDataSet databaseDataSet = getConnection().createDataSet();
|
||||
final ITable actualTable = databaseDataSet.getTable("CLIENTS");
|
||||
IDataSet expectedDataSet = getDataSet();
|
||||
ITable expectedTable = expectedDataSet.getTable("CLIENTS");
|
||||
IDataSet databaseDataSet = getConnection().createDataSet();
|
||||
ITable actualTable = databaseDataSet.getTable("CLIENTS");
|
||||
Assertion.assertEquals(expectedTable, actualTable);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenInsert_thenTableHasNewClient() throws Exception {
|
||||
try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-user.xml")) {
|
||||
final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
|
||||
final ITable expectedTable = expectedDataSet.getTable("CLIENTS");
|
||||
final Connection conn = getDataSource().getConnection();
|
||||
try (InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-user.xml")) {
|
||||
// given
|
||||
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
|
||||
ITable expectedTable = expectedDataSet.getTable("CLIENTS");
|
||||
Connection conn = getDataSource().getConnection();
|
||||
|
||||
// when
|
||||
conn.createStatement()
|
||||
.executeUpdate(
|
||||
"INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')");
|
||||
final ITable actualData = getConnection()
|
||||
.createQueryTable(
|
||||
"result_name",
|
||||
"SELECT * FROM CLIENTS WHERE last_name='Jansen'");
|
||||
.executeUpdate("INSERT INTO CLIENTS (first_name, last_name) VALUES ('John', 'Jansen')");
|
||||
ITable actualData = getConnection()
|
||||
.createQueryTable("result_name", "SELECT * FROM CLIENTS WHERE last_name='Jansen'");
|
||||
|
||||
// then
|
||||
assertEqualsIgnoreCols(expectedTable, actualData, new String[] { "id" });
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenDelete_thenItemIsDeleted() throws Exception {
|
||||
final Connection connection = getConnection().getConnection();
|
||||
|
||||
try (final InputStream is = DataSourceDBUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) {
|
||||
try (InputStream is = DataSourceDBUnitTest.class.getClassLoader()
|
||||
.getResourceAsStream("dbunit/items_exp_delete.xml")) {
|
||||
// given
|
||||
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");
|
||||
|
||||
// when
|
||||
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
|
||||
|
||||
final IDataSet databaseDataSet = getConnection().createDataSet();
|
||||
// then
|
||||
IDataSet databaseDataSet = getConnection().createDataSet();
|
||||
ITable actualTable = databaseDataSet.getTable("ITEMS");
|
||||
|
||||
Assertion.assertEquals(expectedTable, actualTable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception {
|
||||
final Connection connection = getConnection().getConnection();
|
||||
|
||||
try (final InputStream is = DataSourceDBUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) {
|
||||
try (InputStream is = DataSourceDBUnitTest.class.getClassLoader()
|
||||
.getResourceAsStream("dbunit/items_exp_rename.xml")) {
|
||||
// given
|
||||
ITable expectedTable = (new FlatXmlDataSetBuilder().build(is)).getTable("ITEMS");
|
||||
|
||||
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
|
||||
|
||||
final IDataSet databaseDataSet = getConnection().createDataSet();
|
||||
IDataSet databaseDataSet = getConnection().createDataSet();
|
||||
ITable actualTable = databaseDataSet.getTable("ITEMS");
|
||||
|
||||
Assertion.assertEquals(expectedTable, actualTable);
|
||||
@@ -140,22 +144,24 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenInsertUnexpectedData_thenFailOnAllUnexpectedValues() throws Exception {
|
||||
try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-multiple-failures.xml")) {
|
||||
final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
|
||||
final ITable expectedTable = expectedDataSet.getTable("ITEMS");
|
||||
final Connection conn = getDataSource().getConnection();
|
||||
final DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler();
|
||||
public void givenDataSet_whenInsertUnexpectedData_thenFail() throws Exception {
|
||||
try (InputStream is = getClass().getClassLoader()
|
||||
.getResourceAsStream("dbunit/expected-multiple-failures.xml")) {
|
||||
|
||||
conn.createStatement().executeUpdate(
|
||||
"INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')");
|
||||
final ITable actualData = getConnection().createDataSet().getTable("ITEMS");
|
||||
// given
|
||||
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
|
||||
ITable expectedTable = expectedDataSet.getTable("ITEMS");
|
||||
Connection conn = getDataSource().getConnection();
|
||||
DiffCollectingFailureHandler collectingHandler = new DiffCollectingFailureHandler();
|
||||
|
||||
// when
|
||||
conn.createStatement().executeUpdate("INSERT INTO ITEMS (title, price) VALUES ('Battery', '1000000')");
|
||||
ITable actualData = getConnection().createDataSet().getTable("ITEMS");
|
||||
|
||||
// then
|
||||
Assertion.assertEquals(expectedTable, actualData, collectingHandler);
|
||||
if (!collectingHandler.getDiffList().isEmpty()) {
|
||||
String message = (String) collectingHandler
|
||||
.getDiffList()
|
||||
.stream()
|
||||
String message = (String) collectingHandler.getDiffList().stream()
|
||||
.map(d -> formatDifference((Difference) d)).collect(joining("\n"));
|
||||
logger.error(() -> message);
|
||||
}
|
||||
@@ -163,6 +169,8 @@ public class DataSourceDBUnitTest extends DataSourceBasedDBTestCase {
|
||||
}
|
||||
|
||||
private static String formatDifference(Difference diff) {
|
||||
return "expected value in " + diff.getExpectedTable().getTableMetaData().getTableName() + "." + diff.getColumnName() + " row " + diff.getRowIndex() + ":" + diff.getExpectedValue() + ", but was: " + diff.getActualValue();
|
||||
return "expected value in " + diff.getExpectedTable().getTableMetaData().getTableName() + "." + diff
|
||||
.getColumnName() + " row " + diff.getRowIndex() + ":" + diff.getExpectedValue() + ", but was: " + diff
|
||||
.getActualValue();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,13 +31,15 @@ public class OldSchoolDbUnitTest {
|
||||
|
||||
private static IDatabaseTester tester = null;
|
||||
|
||||
private Connection connection;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws Exception {
|
||||
tester = initDatabaseTester();
|
||||
}
|
||||
|
||||
private static IDatabaseTester initDatabaseTester() throws Exception {
|
||||
final JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
|
||||
JdbcDatabaseTester tester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
|
||||
tester.setDataSet(initDataSet());
|
||||
tester.setSetUpOperation(DatabaseOperation.REFRESH);
|
||||
tester.setTearDownOperation(DatabaseOperation.DELETE_ALL);
|
||||
@@ -45,7 +47,7 @@ public class OldSchoolDbUnitTest {
|
||||
}
|
||||
|
||||
private static IDataSet initDataSet() throws Exception {
|
||||
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) {
|
||||
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/data.xml")) {
|
||||
return new FlatXmlDataSetBuilder().build(is);
|
||||
}
|
||||
}
|
||||
@@ -53,6 +55,7 @@ public class OldSchoolDbUnitTest {
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
tester.onSetup();
|
||||
connection = tester.getConnection().getConnection();
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -62,94 +65,100 @@ public class OldSchoolDbUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenSelect_thenFirstTitleIsGreyTShirt() throws Exception {
|
||||
final Connection connection = tester.getConnection().getConnection();
|
||||
|
||||
final ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
|
||||
ResultSet rs = connection.createStatement().executeQuery("select * from ITEMS where id = 1");
|
||||
|
||||
assertThat(rs.next()).isTrue();
|
||||
assertThat(rs.getString("title")).isEqualTo("Grey T-Shirt");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenInsert_thenGetResultsAreStillEqualIfIgnoringColumnsWithDifferentProduced() throws Exception {
|
||||
final Connection connection = tester.getConnection().getConnection();
|
||||
final String[] excludedColumns = { "id", "produced" };
|
||||
try (final InputStream is = getClass().getClassLoader().getResourceAsStream("dbunit/expected-ignoring-registered_at.xml")) {
|
||||
final IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
|
||||
final ITable expectedTable = DefaultColumnFilter.excludedColumnsTable(
|
||||
expectedDataSet.getTable("ITEMS"), excludedColumns);
|
||||
public void givenDataSet_whenInsert_thenGetResultsAreStillEqualIfIgnoringColumnsWithDifferentProduced()
|
||||
throws Exception {
|
||||
String[] excludedColumns = { "id", "produced" };
|
||||
try (InputStream is = getClass().getClassLoader()
|
||||
.getResourceAsStream("dbunit/expected-ignoring-registered_at.xml")) {
|
||||
// given
|
||||
IDataSet expectedDataSet = new FlatXmlDataSetBuilder().build(is);
|
||||
ITable expectedTable = DefaultColumnFilter
|
||||
.excludedColumnsTable(expectedDataSet.getTable("ITEMS"), excludedColumns);
|
||||
|
||||
connection.createStatement().executeUpdate(
|
||||
"INSERT INTO ITEMS (title, price, produced) VALUES('Necklace', 199.99, now())");
|
||||
|
||||
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
final ITable actualTable = DefaultColumnFilter.excludedColumnsTable(
|
||||
databaseDataSet.getTable("ITEMS"), excludedColumns);
|
||||
// when
|
||||
connection.createStatement()
|
||||
.executeUpdate("INSERT INTO ITEMS (title, price, produced) VALUES('Necklace', 199.99, now())");
|
||||
|
||||
// then
|
||||
IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
ITable actualTable = DefaultColumnFilter
|
||||
.excludedColumnsTable(databaseDataSet.getTable("ITEMS"), excludedColumns);
|
||||
Assertion.assertEquals(expectedTable, actualTable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenDelete_thenItemIsRemoved() throws Exception {
|
||||
final Connection connection = tester.getConnection().getConnection();
|
||||
|
||||
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete.xml")) {
|
||||
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
|
||||
.getResourceAsStream("dbunit/items_exp_delete.xml")) {
|
||||
// given
|
||||
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
|
||||
|
||||
// when
|
||||
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
|
||||
|
||||
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
// then
|
||||
IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
ITable actualTable = databaseDataSet.getTable("ITEMS");
|
||||
|
||||
assertEquals(expectedTable, actualTable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenDelete_thenItemIsRemovedAndResultsEqualIfProducedIsIgnored() throws Exception {
|
||||
final Connection connection = tester.getConnection().getConnection();
|
||||
|
||||
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) {
|
||||
final ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
|
||||
public void givenDataSet_whenProductIgnoredAndDelete_thenItemIsRemoved() throws Exception {
|
||||
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
|
||||
.getResourceAsStream("dbunit/items_exp_delete_no_produced.xml")) {
|
||||
// given
|
||||
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
|
||||
|
||||
// when
|
||||
connection.createStatement().executeUpdate("delete from ITEMS where id = 2");
|
||||
|
||||
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
// then
|
||||
IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
ITable actualTable = databaseDataSet.getTable("ITEMS");
|
||||
actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
|
||||
|
||||
assertEquals(expectedTable, actualTable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenUpdate_thenItemHasNewName() throws Exception {
|
||||
final Connection connection = tester.getConnection().getConnection();
|
||||
|
||||
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename.xml")) {
|
||||
final ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
|
||||
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
|
||||
.getResourceAsStream("dbunit/items_exp_rename.xml")) {
|
||||
// given
|
||||
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
|
||||
|
||||
// when
|
||||
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
|
||||
|
||||
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
// then
|
||||
IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
ITable actualTable = databaseDataSet.getTable("ITEMS");
|
||||
|
||||
assertEquals(expectedTable, actualTable);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDataSet_whenUpdateWithNoProduced_thenItemHasNewName() throws Exception {
|
||||
final Connection connection = tester.getConnection().getConnection();
|
||||
|
||||
try (final InputStream is = OldSchoolDbUnitTest.class.getClassLoader().getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) {
|
||||
try (InputStream is = OldSchoolDbUnitTest.class.getClassLoader()
|
||||
.getResourceAsStream("dbunit/items_exp_rename_no_produced.xml")) {
|
||||
// given
|
||||
ITable expectedTable = new FlatXmlDataSetBuilder().build(is).getTable("ITEMS");
|
||||
expectedTable = DefaultColumnFilter.excludedColumnsTable(expectedTable, new String[] { "produced" });
|
||||
|
||||
// when
|
||||
connection.createStatement().executeUpdate("update ITEMS set title='new name' where id = 1");
|
||||
|
||||
final IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
// then
|
||||
IDataSet databaseDataSet = tester.getConnection().createDataSet();
|
||||
ITable actualTable = databaseDataSet.getTable("ITEMS");
|
||||
actualTable = DefaultColumnFilter.excludedColumnsTable(actualTable, new String[] { "produced" });
|
||||
assertEquals(expectedTable, actualTable);
|
||||
|
||||
@@ -5,3 +5,4 @@ This module contains articles about Apache Maven. Please refer to its submodules
|
||||
### Relevant Articles
|
||||
|
||||
- [Apache Maven Tutorial](https://www.baeldung.com/maven)
|
||||
- [Find Unused Maven Dependencies](https://www.baeldung.com/maven-unused-dependencies)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [A Solid Guide to Solid Principles](https://www.baeldung.com/solid-principles)
|
||||
|
||||
- [Single Responsibility Principle in Java](https://www.baeldung.com/java-single-responsibility-principle)
|
||||
|
||||
|
||||
@@ -12,4 +12,5 @@ This module contains articles about the Java Persistence API (JPA) in Java.
|
||||
- [Combining JPA And/Or Criteria Predicates](https://www.baeldung.com/jpa-and-or-criteria-predicates)
|
||||
- [JPA Annotation for the PostgreSQL TEXT Type](https://www.baeldung.com/jpa-annotation-postgresql-text-type)
|
||||
- [Mapping a Single Entity to Multiple Tables in JPA](https://www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables)
|
||||
- [Constructing a JPA Query Between Unrelated Entities](https://www.baeldung.com/jpa-query-unrelated-entities)
|
||||
- More articles: [[<-- prev]](/java-jpa)
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [A Guide to Redis with Redisson](http://www.baeldung.com/redis-redisson)
|
||||
- [Introduction to Lettuce – the Java Redis Client](https://www.baeldung.com/java-redis-lettuce)
|
||||
- [List All Available Redis Keys](https://www.baeldung.com/redis-list-available-keys)
|
||||
- [Spring Data Redis’s Property-Based Configuration](https://www.baeldung.com/spring-data-redis-properties)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Spring JPA @Embedded and @EmbeddedId](TBD)
|
||||
- [Spring JPA @Embedded and @EmbeddedId](https://www.baeldung.com/spring-jpa-embedded-method-parameters)
|
||||
- [Generate Database Schema with Spring Data JPA](https://www.baeldung.com/spring-data-jpa-generate-db-schema)
|
||||
|
||||
### Eclipse Config
|
||||
After importing the project into Eclipse, you may see the following error:
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Spring JdbcTemplate Unit Testing](https://www.baeldung.com/spring-jdbctemplate-testing)
|
||||
- [Using a List of Values in a JdbcTemplate IN Clause](https://www.baeldung.com/spring-jdbctemplate-in-list)
|
||||
|
||||
@@ -9,3 +9,5 @@ This module contains articles about Spring Security 5
|
||||
- [New Password Storage In Spring Security 5](https://www.baeldung.com/spring-security-5-password-storage)
|
||||
- [Default Password Encoder in Spring Security 5](https://www.baeldung.com/spring-security-5-default-password-encoder)
|
||||
- [Guide to the AuthenticationManagerResolver in Spring Security](https://www.baeldung.com/spring-security-authenticationmanagerresolver)
|
||||
- [Disable Security for a Profile in Spring Boot](https://www.baeldung.com/spring-security-disable-profile)
|
||||
- [Manual Logout With Spring Security](https://www.baeldung.com/spring-security-manual-logout)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Building a Simple Web Application with Spring Boot and Groovy](https://www.baeldung.com/spring-boot-groovy-web-app)
|
||||
@@ -5,4 +5,4 @@ This module contains articles about logging in Spring Boot projects with Log4j 2
|
||||
### Relevant Articles:
|
||||
- [Logging in Spring Boot](https://www.baeldung.com/spring-boot-logging)
|
||||
- [Logging to Graylog with Spring Boot](https://www.baeldung.com/graylog-with-spring-boot)
|
||||
|
||||
- [Log Groups in Spring Boot 2.1](https://www.baeldung.com/spring-boot-log-groups)
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
- [A Guide To Caching in Spring](http://www.baeldung.com/spring-cache-tutorial)
|
||||
- [Spring Cache – Creating a Custom KeyGenerator](http://www.baeldung.com/spring-cache-custom-keygenerator)
|
||||
- [Cache Eviction in Spring Boot](https://www.baeldung.com/spring-boot-evict-cache)
|
||||
- [Using Multiple Cache Managers in Spring](https://www.baeldung.com/spring-multiple-cache-managers)
|
||||
|
||||
@@ -6,3 +6,4 @@ This module contains articles about Spring Cloud Gateway
|
||||
- [Exploring the new Spring Cloud Gateway](http://www.baeldung.com/spring-cloud-gateway)
|
||||
- [Writing Custom Spring Cloud Gateway Filters](https://www.baeldung.com/spring-cloud-custom-gateway-filters)
|
||||
- [Spring Cloud Gateway Routing Predicate Factories](https://www.baeldung.com/spring-cloud-gateway-routing-predicate-factories)
|
||||
- [Spring Cloud Gateway WebFilter Factories](https://www.baeldung.com/spring-cloud-gateway-webfilter-factories)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user