From 7c7a6d4407c728e1790896e54f5d9ed1483d30ff Mon Sep 17 00:00:00 2001 From: lor6 Date: Tue, 24 Oct 2017 00:16:19 +0300 Subject: [PATCH 1/5] BAEL-1274 custom method in multiple repositories ex (#2766) * custom method in multiple repositories ex * change var name * refactor packages * change to assertj, change method name --- spring-jpa/pom.xml | 6 ++ .../baeldung/config/StudentJPAH2Config.java | 69 +++++++++++++++++++ .../org/baeldung/config/StudentJpaConfig.java | 4 +- .../persistence/dao/ExtendedRepository.java | 12 ++++ .../dao/ExtendedRepositoryImpl.java | 36 ++++++++++ .../dao/ExtendedStudentRepository.java | 6 ++ .../persistence/dao/StudentRepository.java | 5 +- .../persistence/model/Student.java | 2 +- .../persistence-student-h2.properties | 12 ++++ ...endedStudentRepositoryIntegrationTest.java | 39 +++++++++++ .../repository/InMemoryDBIntegrationTest.java | 4 +- 11 files changed, 187 insertions(+), 8 deletions(-) create mode 100644 spring-jpa/src/main/java/org/baeldung/config/StudentJPAH2Config.java create mode 100644 spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedRepository.java create mode 100644 spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedRepositoryImpl.java create mode 100644 spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedStudentRepository.java rename spring-jpa/src/main/java/org/baeldung/{ => inmemory}/persistence/dao/StudentRepository.java (57%) rename spring-jpa/src/main/java/org/baeldung/{ => inmemory}/persistence/model/Student.java (91%) create mode 100644 spring-jpa/src/main/resources/persistence-student-h2.properties create mode 100644 spring-jpa/src/test/java/org/baeldung/persistence/repository/ExtendedStudentRepositoryIntegrationTest.java diff --git a/spring-jpa/pom.xml b/spring-jpa/pom.xml index 6a67d44b48..960dcbc588 100644 --- a/spring-jpa/pom.xml +++ b/spring-jpa/pom.xml @@ -113,6 +113,11 @@ guava ${guava.version} + + org.assertj + assertj-core + ${assertj.version} + @@ -180,6 +185,7 @@ 21.0 3.5 + 3.8.0 4.4.5 4.5.2 diff --git a/spring-jpa/src/main/java/org/baeldung/config/StudentJPAH2Config.java b/spring-jpa/src/main/java/org/baeldung/config/StudentJPAH2Config.java new file mode 100644 index 0000000000..439c6cb602 --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/config/StudentJPAH2Config.java @@ -0,0 +1,69 @@ +package org.baeldung.config; + +import java.util.Properties; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +import org.baeldung.extended.persistence.dao.ExtendedRepositoryImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.jdbc.datasource.DriverManagerDataSource; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableJpaRepositories(basePackages = "org.baeldung.extended.persistence.dao", repositoryBaseClass = ExtendedRepositoryImpl.class) +@PropertySource("persistence-student-h2.properties") +@EnableTransactionManagement +public class StudentJPAH2Config { + + @Autowired + private Environment env; + + @Bean + public DataSource dataSource() { + final DriverManagerDataSource dataSource = new DriverManagerDataSource(); + dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); + dataSource.setUrl(env.getProperty("jdbc.url")); + dataSource.setUsername(env.getProperty("jdbc.user")); + dataSource.setPassword(env.getProperty("jdbc.pass")); + + return dataSource; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); + em.setDataSource(dataSource()); + em.setPackagesToScan(new String[] { "org.baeldung.inmemory.persistence.model" }); + em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); + em.setJpaProperties(additionalProperties()); + return em; + } + + @Bean + JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(entityManagerFactory); + return transactionManager; + } + + final Properties additionalProperties() { + final Properties hibernateProperties = new Properties(); + + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql")); + hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache")); + hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache")); + + return hibernateProperties; + } +} diff --git a/spring-jpa/src/main/java/org/baeldung/config/StudentJpaConfig.java b/spring-jpa/src/main/java/org/baeldung/config/StudentJpaConfig.java index a40f180a62..8021691716 100644 --- a/spring-jpa/src/main/java/org/baeldung/config/StudentJpaConfig.java +++ b/spring-jpa/src/main/java/org/baeldung/config/StudentJpaConfig.java @@ -18,7 +18,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration -@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao") +@EnableJpaRepositories(basePackages = "org.baeldung.inmemory.persistence.dao") @PropertySource("persistence-student.properties") @EnableTransactionManagement public class StudentJpaConfig { @@ -41,7 +41,7 @@ public class StudentJpaConfig { public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource()); - em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" }); + em.setPackagesToScan(new String[] { "org.baeldung.inmemory.persistence.model" }); em.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); em.setJpaProperties(additionalProperties()); return em; diff --git a/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedRepository.java b/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedRepository.java new file mode 100644 index 0000000000..9c9c12029a --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedRepository.java @@ -0,0 +1,12 @@ +package org.baeldung.extended.persistence.dao; + +import java.io.Serializable; +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.repository.NoRepositoryBean; + +@NoRepositoryBean +public interface ExtendedRepository extends JpaRepository { + public List findByAttributeContainsText(String attributeName, String text); +} diff --git a/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedRepositoryImpl.java b/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedRepositoryImpl.java new file mode 100644 index 0000000000..0dd32757d7 --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedRepositoryImpl.java @@ -0,0 +1,36 @@ +package org.baeldung.extended.persistence.dao; + +import java.io.Serializable; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import javax.transaction.Transactional; + +import org.springframework.data.jpa.repository.support.JpaEntityInformation; +import org.springframework.data.jpa.repository.support.SimpleJpaRepository; + +public class ExtendedRepositoryImpl extends SimpleJpaRepository implements ExtendedRepository { + + private EntityManager entityManager; + + public ExtendedRepositoryImpl(JpaEntityInformation entityInformation, EntityManager entityManager) { + super(entityInformation, entityManager); + this.entityManager = entityManager; + } + + @Transactional + public List findByAttributeContainsText(String attributeName, String text) { + CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + CriteriaQuery query = builder.createQuery(getDomainClass()); + Root root = query.from(getDomainClass()); + query.select(root) + .where(builder.like(root. get(attributeName), "%" + text + "%")); + TypedQuery q = entityManager.createQuery(query); + return q.getResultList(); + } + +} diff --git a/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedStudentRepository.java b/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedStudentRepository.java new file mode 100644 index 0000000000..7e2efc72bc --- /dev/null +++ b/spring-jpa/src/main/java/org/baeldung/extended/persistence/dao/ExtendedStudentRepository.java @@ -0,0 +1,6 @@ +package org.baeldung.extended.persistence.dao; + +import org.baeldung.inmemory.persistence.model.Student; + +public interface ExtendedStudentRepository extends ExtendedRepository { +} diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/dao/StudentRepository.java b/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java similarity index 57% rename from spring-jpa/src/main/java/org/baeldung/persistence/dao/StudentRepository.java rename to spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java index af484b442c..bfcf6f5cdc 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/dao/StudentRepository.java +++ b/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java @@ -1,8 +1,7 @@ -package org.baeldung.persistence.dao; +package org.baeldung.inmemory.persistence.dao; +import org.baeldung.inmemory.persistence.model.Student; import org.springframework.data.jpa.repository.JpaRepository; -import org.baeldung.persistence.model.Student; - public interface StudentRepository extends JpaRepository { } diff --git a/spring-jpa/src/main/java/org/baeldung/persistence/model/Student.java b/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java similarity index 91% rename from spring-jpa/src/main/java/org/baeldung/persistence/model/Student.java rename to spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java index 437eeac5bb..b50fe9122e 100644 --- a/spring-jpa/src/main/java/org/baeldung/persistence/model/Student.java +++ b/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java @@ -1,4 +1,4 @@ -package org.baeldung.persistence.model; +package org.baeldung.inmemory.persistence.model; import javax.persistence.Entity; import javax.persistence.Id; diff --git a/spring-jpa/src/main/resources/persistence-student-h2.properties b/spring-jpa/src/main/resources/persistence-student-h2.properties new file mode 100644 index 0000000000..e1d6bfa45a --- /dev/null +++ b/spring-jpa/src/main/resources/persistence-student-h2.properties @@ -0,0 +1,12 @@ +# jdbc.X +jdbc.driverClassName=org.h2.Driver +jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 +jdbc.user=sa +# jdbc.pass= + +# hibernate.X +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=create-drop +hibernate.cache.use_second_level_cache=false +hibernate.cache.use_query_cache=false \ No newline at end of file diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/repository/ExtendedStudentRepositoryIntegrationTest.java b/spring-jpa/src/test/java/org/baeldung/persistence/repository/ExtendedStudentRepositoryIntegrationTest.java new file mode 100644 index 0000000000..0970daa0ee --- /dev/null +++ b/spring-jpa/src/test/java/org/baeldung/persistence/repository/ExtendedStudentRepositoryIntegrationTest.java @@ -0,0 +1,39 @@ +package org.baeldung.persistence.repository; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.List; + +import javax.annotation.Resource; + +import org.baeldung.config.StudentJPAH2Config; +import org.baeldung.extended.persistence.dao.ExtendedStudentRepository; +import org.baeldung.inmemory.persistence.model.Student; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { StudentJPAH2Config.class}) +public class ExtendedStudentRepositoryIntegrationTest { + @Resource + private ExtendedStudentRepository extendedStudentRepository; + + @Before + public void setup(){ + Student student = new Student(1, "john"); + extendedStudentRepository.save(student); + Student student2 = new Student(2, "johnson"); + extendedStudentRepository.save(student2); + Student student3 = new Student(3, "tom"); + extendedStudentRepository.save(student3); + } + + @Test + public void givenStudents_whenFindByName_thenGetOk(){ + List students = extendedStudentRepository.findByAttributeContainsText("name", "john"); + assertThat(students.size()).isEqualTo(2); + } +} diff --git a/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java b/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java index 1fcc4be45d..8380ab5434 100644 --- a/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java +++ b/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java @@ -1,8 +1,8 @@ package org.baeldung.persistence.repository; import org.baeldung.config.StudentJpaConfig; -import org.baeldung.persistence.dao.StudentRepository; -import org.baeldung.persistence.model.Student; +import org.baeldung.inmemory.persistence.dao.StudentRepository; +import org.baeldung.inmemory.persistence.model.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; From 1c896ec494ec84a1565ee3c748657ec258cb215a Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Mon, 23 Oct 2017 18:44:18 -0500 Subject: [PATCH 2/5] Update README (#2852) * BAEL-973: updated README * BAEL-1069: Updated README * BAEL-817: add README file * BAEL-1084: README update * BAEL-960: Update README * BAEL-1155: updated README * BAEL-1041: updated README * BAEL-973: Updated README --- asm/README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/asm/README.md b/asm/README.md index ff12555376..50d9c34324 100644 --- a/asm/README.md +++ b/asm/README.md @@ -1 +1,3 @@ -## Relevant articles: +### Relevant Articles: + +- [A Guide to Java Bytecode Manipulation with ASM](http://www.baeldung.com/java-asm) From f4c346935295cd6365c11112f919ce8c1eef43e8 Mon Sep 17 00:00:00 2001 From: chrisoberle Date: Mon, 23 Oct 2017 19:55:12 -0400 Subject: [PATCH 3/5] update test method names per BAEL-1187 (#2851) * BAEL-1187 - Mapping Nested Values with Jackson * minor naming and formatting changes to align with standards * update formatting and jackson version * update test method names --- .../nested/DeserializeWithNestedPropertiesUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/DeserializeWithNestedPropertiesUnitTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/DeserializeWithNestedPropertiesUnitTest.java index 6e762caa64..037bc7e880 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/DeserializeWithNestedPropertiesUnitTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/nested/DeserializeWithNestedPropertiesUnitTest.java @@ -15,7 +15,7 @@ public class DeserializeWithNestedPropertiesUnitTest { private String SOURCE_JSON = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"name\":\"The Best Product\",\"brand\":{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"name\":\"ACME Products\",\"owner\":{\"id\":\"b21a80b1-0c09-4be3-9ebd-ea3653511c13\",\"name\":\"Ultimate Corp, Inc.\"}}}"; @Test - public void whenUsingJacksonAnnotations_thenOk() throws IOException { + public void whenUsingAnnotations_thenOk() throws IOException { Product product = new ObjectMapper().readerFor(Product.class) .readValue(SOURCE_JSON); @@ -25,7 +25,7 @@ public class DeserializeWithNestedPropertiesUnitTest { } @Test - public void whenUsingJacksonJsonNode_thenOk() throws IOException { + public void whenUsingJsonNode_thenOk() throws IOException { JsonNode productNode = new ObjectMapper().readTree(SOURCE_JSON); Product product = new Product(); @@ -47,7 +47,7 @@ public class DeserializeWithNestedPropertiesUnitTest { } @Test - public void whenUsingJacksonDeserializerManuallyRegistered_thenOk() throws IOException { + public void whenUsingDeserializerManuallyRegistered_thenOk() throws IOException { ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addDeserializer(Product.class, new ProductDeserializer()); @@ -60,7 +60,7 @@ public class DeserializeWithNestedPropertiesUnitTest { } @Test - public void whenUsingJacksonDeserializerAutoRegistered_thenOk() throws IOException { + public void whenUsingDeserializerAutoRegistered_thenOk() throws IOException { ObjectMapper mapper = new ObjectMapper(); Product product = mapper.readValue(SOURCE_JSON, Product.class); assertEquals(product.getName(), "The Best Product"); From 5d39af398fbf50777948741ca1a12951d492d381 Mon Sep 17 00:00:00 2001 From: deep20jain Date: Tue, 24 Oct 2017 09:06:28 +0530 Subject: [PATCH 4/5] BAEL 1143 - Edit Distance - deep20jain@gmail.com (#2718) * Calculate edit distance * Fixing formatting * Making variable local to method --- .../editdistance/EditDistanceBase.java | 22 +++++++++++++ .../EditDistanceDynamicProgramming.java | 25 +++++++++++++++ .../editdistance/EditDistanceRecursive.java | 20 ++++++++++++ .../EditDistanceDataProvider.java | 21 +++++++++++++ .../editdistance/EditDistanceTest.java | 31 +++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java create mode 100644 algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java create mode 100644 algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java create mode 100644 algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceTest.java diff --git a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java b/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java new file mode 100644 index 0000000000..e884c576c1 --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceBase.java @@ -0,0 +1,22 @@ +package com.baeldung.algorithms.editdistance; + +public class EditDistanceBase { + + public static int costOfSubstitution(char a, char b) { + if (a == b) { + return 0; + } + return 1; + } + + public static int min(int... numbers) { + int min = Integer.MAX_VALUE; + + for (int x : numbers) { + if (x < min) + min = x; + } + + return min; + } +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java b/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java new file mode 100644 index 0000000000..163714002b --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceDynamicProgramming.java @@ -0,0 +1,25 @@ +package com.baeldung.algorithms.editdistance; + +public class EditDistanceDynamicProgramming extends EditDistanceBase { + + public static int calculate(String x, String y) { + int[][] dp = new int[x.length() + 1][y.length() + 1]; + + for (int i = 0; i <= x.length(); i++) { + for (int j = 0; j <= y.length(); j++) { + if (i == 0) + dp[i][j] = j; + + else if (j == 0) + dp[i][j] = i; + + else { + dp[i][j] = min(dp[i - 1][j - 1] + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)), dp[i - 1][j] + 1, dp[i][j - 1] + 1); + } + } + } + + return dp[x.length()][y.length()]; + } + +} diff --git a/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java b/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java new file mode 100644 index 0000000000..68e470147e --- /dev/null +++ b/algorithms/src/main/java/com/baeldung/algorithms/editdistance/EditDistanceRecursive.java @@ -0,0 +1,20 @@ +package com.baeldung.algorithms.editdistance; + +public class EditDistanceRecursive extends EditDistanceBase { + + public static int calculate(String x, String y) { + + if (x.isEmpty()) + return y.length(); + + if (y.isEmpty()) + return x.length(); + + int substitution = calculate(x.substring(1), y.substring(1)) + costOfSubstitution(x.charAt(0), y.charAt(0)); + int insertion = calculate(x, y.substring(1)) + 1; + int deletion = calculate(x.substring(1), y) + 1; + + return min(substitution, insertion, deletion); + } + +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java b/algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java new file mode 100644 index 0000000000..89bd871616 --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceDataProvider.java @@ -0,0 +1,21 @@ +package com.baeldung.algorithms.editdistance; + +import org.junit.runners.Parameterized.Parameters; + +import java.util.Arrays; +import java.util.Collection; + +public class EditDistanceDataProvider { + + @Parameters + public static Collection getLists() { + return Arrays.asList(new Object[][] { + { "", "", 0 }, + { "ago", "", 3 }, + { "", "do", 2 }, + { "abc", "adc", 1 }, + { "peek", "pesek", 1 }, + { "sunday", "saturday", 3 } + }); + } +} diff --git a/algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceTest.java b/algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceTest.java new file mode 100644 index 0000000000..1594f73a73 --- /dev/null +++ b/algorithms/src/test/java/com/baeldung/algorithms/editdistance/EditDistanceTest.java @@ -0,0 +1,31 @@ +package com.baeldung.algorithms.editdistance; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public class EditDistanceTest extends EditDistanceDataProvider { + + String x; + String y; + int result; + + public EditDistanceTest(String a, String b, int res) { + super(); + x = a; + y = b; + result = res; + } + + @Test + public void testEditDistance_RecursiveImplementation() { + Assert.assertEquals(result, EditDistanceRecursive.calculate(x, y)); + } + + @Test + public void testEditDistance_givenDynamicProgrammingImplementation() { + Assert.assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y)); + } +} From a5e2357033cd5f14f31a226139dd2becd2628219 Mon Sep 17 00:00:00 2001 From: Ahmed Tawila Date: Tue, 24 Oct 2017 18:47:40 +0200 Subject: [PATCH 5/5] BAEL-1250 Initializing Arrays in Java (#2811) * Evaluation article: Different Types of Bean Injection in Spring * added tests & changed configuration to Java-based config * removed xml config files * rename unit tests * BAEL-972 - Apache Commons Text * remove code from evaluation article * remove code from evaluation article * BAEL-972 - Apache Commons Text - added another example * BAEL-972 - Apache Commons Text - just indentation * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java * BAEL-994 - TemporalAdjuster in Java - fix problems * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * BAEL-1033 Introduction to StreamUtils * fix formatting * BAEL-1033 minor refactor * BAEL-1035 Introduction to Eclipse Collections * format * BAEL-1035 Introduction to Eclipse Collections * BAEL-1035 Introduction to Eclipse Collections * BAEL-1035 Introduction to Eclipse Collections * cleanup * cleanup * BAEL-1109 Introduction to JCache * BAEL-1109 Introduction to JCache * remove unneeded property in pom.xml * fix formatting * close cache instances properly * remove latest commit * BAEL-1057 Introduction to rxjava-jdbc * refactor rxjava-jdbc * Refactor rxjava-jdbc * Refactoring rxjava-jdbc * BAEL-1171 java.lang.String API * refactor rxjava-jdbc * refactor String * String API - move multiple classes into a single class * move class into test package * BAEL-1171 String.lang.String API * BAEL-1171 java.lang.String API * BAEL-1250 Initializing Arrays in Java * BAEL-1250 Initializing Arrays in Java --- .../com/baeldung/array/ArrayInitializer.java | 74 +++++++++++++++++++ .../baeldung/array/ArrayInitializerTest.java | 68 +++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/array/ArrayInitializer.java create mode 100644 core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java diff --git a/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java b/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java new file mode 100644 index 0000000000..fd00c74e7f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/array/ArrayInitializer.java @@ -0,0 +1,74 @@ +package com.baeldung.array; + +import java.util.Arrays; + +public class ArrayInitializer { + + public static int[] initializeArrayInLoop() { + int array[] = new int[5]; + for (int i = 0; i < array.length; i++) + array[i] = i + 2; + return array; + } + + public static int[][] initializeMultiDimensionalArrayInLoop() { + int array[][] = new int[2][5]; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 5; j++) + array[i][j] = j + 1; + return array; + } + + public static String[] initializeArrayAtTimeOfDeclarationMethod1() { + String array[] = new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" }; + return array; + } + + public static int[] initializeArrayAtTimeOfDeclarationMethod2() { + int[] array = new int[] { 1, 2, 3, 4, 5 }; + return array; + } + + public static int[] initializeArrayAtTimeOfDeclarationMethod3() { + int array[] = { 1, 2, 3, 4, 5 }; + return array; + } + + public static long[] initializeArrayUsingArraysFill() { + long array[] = new long[5]; + Arrays.fill(array, 30); + return array; + } + + public static int[] initializeArrayRangeUsingArraysFill() { + int array[] = new int[5]; + Arrays.fill(array, 0, 3, -50); + return array; + } + + public static int[] initializeArrayUsingArraysCopy() { + int array[] = { 1, 2, 3, 4, 5 }; + int[] copy = Arrays.copyOf(array, 5); + return copy; + } + + public static int[] initializeLargerArrayUsingArraysCopy() { + int array[] = { 1, 2, 3, 4, 5 }; + int[] copy = Arrays.copyOf(array, 6); + return copy; + } + + public static int[] initializeArrayUsingArraysSetAll() { + int[] array = new int[20]; + + for (int i = 0; i < 20; i++) { + Arrays.setAll(array, p -> { + if (p > 9) + return 0; + else + return p; + }); + } + return array; + } +} diff --git a/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java b/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java new file mode 100644 index 0000000000..d3afad7b00 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/array/ArrayInitializerTest.java @@ -0,0 +1,68 @@ +package com.baeldung.array; + +import static com.baeldung.array.ArrayInitializer.initializeArrayAtTimeOfDeclarationMethod1; +import static com.baeldung.array.ArrayInitializer.initializeArrayAtTimeOfDeclarationMethod2; +import static com.baeldung.array.ArrayInitializer.initializeArrayAtTimeOfDeclarationMethod3; +import static com.baeldung.array.ArrayInitializer.initializeArrayInLoop; +import static com.baeldung.array.ArrayInitializer.initializeArrayRangeUsingArraysFill; +import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysCopy; +import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysFill; +import static com.baeldung.array.ArrayInitializer.initializeArrayUsingArraysSetAll; +import static com.baeldung.array.ArrayInitializer.initializeLargerArrayUsingArraysCopy; +import static com.baeldung.array.ArrayInitializer.initializeMultiDimensionalArrayInLoop; +import static org.junit.Assert.assertArrayEquals; + +import org.junit.Test; + +public class ArrayInitializerTest { + + @Test + public void whenInitializeArrayInLoop_thenCorrect() { + assertArrayEquals(new int[] { 2, 3, 4, 5, 6 }, initializeArrayInLoop()); + } + + @Test + public void whenInitializeMultiDimensionalArrayInLoop_thenCorrect() { + assertArrayEquals(new int[][] { { 1, 2, 3, 4, 5 }, { 1, 2, 3, 4, 5 } }, initializeMultiDimensionalArrayInLoop()); + } + + @Test + public void whenInitializeArrayAtTimeOfDeclarationMethod1_thenCorrect() { + assertArrayEquals(new String[] { "Toyota", "Mercedes", "BMW", "Volkswagen", "Skoda" }, initializeArrayAtTimeOfDeclarationMethod1()); + } + + @Test + public void whenInitializeArrayAtTimeOfDeclarationMethod2_thenCorrect() { + assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayAtTimeOfDeclarationMethod2()); + } + + @Test + public void whenInitializeArrayAtTimeOfDeclarationMethod3_thenCorrect() { + assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayAtTimeOfDeclarationMethod3()); + } + + @Test + public void whenInitializeArrayUsingArraysFill_thenCorrect() { + assertArrayEquals(new long[] { 30, 30, 30, 30, 30 }, initializeArrayUsingArraysFill()); + } + + @Test + public void whenInitializeArrayRangeUsingArraysFill_thenCorrect() { + assertArrayEquals(new int[] { -50, -50, -50, 0, 0 }, initializeArrayRangeUsingArraysFill()); + } + + @Test + public void whenInitializeArrayRangeUsingArraysCopy_thenCorrect() { + assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, initializeArrayUsingArraysCopy()); + } + + @Test + public void whenInitializeLargerArrayRangeUsingArraysCopy_thenCorrect() { + assertArrayEquals(new int[] { 1, 2, 3, 4, 5, 0 }, initializeLargerArrayUsingArraysCopy()); + } + + @Test + public void whenInitializeLargerArrayRangeUsingArraysSetAll_thenCorrect() { + assertArrayEquals(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, initializeArrayUsingArraysSetAll()); + } +}