This commit is contained in:
Ahmed Tawila
2017-10-26 10:17:37 +02:00
124 changed files with 1267 additions and 191 deletions
@@ -1,22 +1,15 @@
package com.baeldung.algorithms.editdistance;
import java.util.Arrays;
public class EditDistanceBase {
public static int costOfSubstitution(char a, char b) {
if (a == b) {
return 0;
}
return 1;
static int costOfSubstitution(char a, char b) {
return a == b ? 0 : 1;
}
public static int min(int... numbers) {
int min = Integer.MAX_VALUE;
for (int x : numbers) {
if (x < min)
min = x;
}
return min;
static int min(int... numbers) {
return Arrays.stream(numbers)
.min().orElse(Integer.MAX_VALUE);
}
}
@@ -2,7 +2,7 @@ package com.baeldung.algorithms.editdistance;
public class EditDistanceDynamicProgramming extends EditDistanceBase {
public static int calculate(String x, String y) {
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++) {
@@ -14,12 +14,13 @@ public class EditDistanceDynamicProgramming extends EditDistanceBase {
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);
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()];
}
}
@@ -2,13 +2,15 @@ package com.baeldung.algorithms.editdistance;
public class EditDistanceRecursive extends EditDistanceBase {
public static int calculate(String x, String y) {
static int calculate(String x, String y) {
if (x.isEmpty())
if (x.isEmpty()) {
return y.length();
}
if (y.isEmpty())
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;
@@ -16,5 +18,4 @@ public class EditDistanceRecursive extends EditDistanceBase {
return min(substitution, insertion, deletion);
}
}
@@ -1,16 +1,17 @@
package com.baeldung.algorithms.editdistance;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class EditDistanceTest extends EditDistanceDataProvider {
String x;
String y;
int result;
private String x;
private String y;
private int result;
public EditDistanceTest(String a, String b, int res) {
super();
@@ -21,11 +22,11 @@ public class EditDistanceTest extends EditDistanceDataProvider {
@Test
public void testEditDistance_RecursiveImplementation() {
Assert.assertEquals(result, EditDistanceRecursive.calculate(x, y));
assertEquals(result, EditDistanceRecursive.calculate(x, y));
}
@Test
public void testEditDistance_givenDynamicProgrammingImplementation() {
Assert.assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
}
}
+70
View File
@@ -106,5 +106,75 @@
</pluginRepository>
</pluginRepositories>
<profiles>
<profile>
<id>integration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/AutoconfigurationTest.java</exclude>
</excludes>
<includes>
<include>**/*IntegrationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>autoconfiguration</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<phase>integration-test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<excludes>
<exclude>**/*LiveTest.java</exclude>
<exclude>**/*IntegrationTest.java</exclude>
</excludes>
<includes>
<include>**/AutoconfigurationTest.java</include>
</includes>
</configuration>
</execution>
</executions>
<configuration>
<systemPropertyVariables>
<test.mime>json</test.mime>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
@@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CasSecuredAppApplicationTests {
public class CasSecuredAppApplicationIntegrationTest {
@Test
public void contextLoads() {
@@ -0,0 +1,110 @@
package com.baeldung.concurrent.runnable;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.commons.lang3.RandomUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RunnableVsThreadTest {
private static Logger log =
LoggerFactory.getLogger(RunnableVsThreadTest.class);
@Test
public void givenARunnable_whenRunIt_thenResult() throws Exception{
Thread thread = new Thread(new SimpleRunnable(
"SimpleRunnable executed using Thread"));
thread.start();
thread.join();
ExecutorService executorService =
Executors.newCachedThreadPool();
executorService.submit(new SimpleRunnable(
"SimpleRunnable executed using ExecutorService")).get();
executorService.submit(()->
log.info("Lambda runnable executed!!!")).get();
executorService.shutdown();
}
@Test
public void givenAThread_whenRunIt_thenResult() throws Exception{
Thread thread = new SimpleThread(
"SimpleThread executed using Thread");
thread.start();
thread.join();
ExecutorService executorService =
Executors.newCachedThreadPool();
executorService.submit(new SimpleThread(
"SimpleThread executed using ExecutorService")).get();
}
@Test
public void givenACallable_whenRunIt_thenResult() throws Exception {
ExecutorService executorService =
Executors.newCachedThreadPool();
Future<Integer> future = executorService.submit(new SimpleCallable());
log.info("Result from callable: {}", future.get());
future = executorService.submit(() -> {
return RandomUtils.nextInt(0, 100);
});
log.info("Result from callable: {}", future.get());
}
}
class SimpleThread extends Thread{
private static final Logger log =
LoggerFactory.getLogger(SimpleThread.class);
private String message;
public SimpleThread(String message) {
this.message = message;
}
@Override
public void run() {
log.info(message);
}
}
class SimpleRunnable implements Runnable {
private static final Logger log =
LoggerFactory.getLogger(SimpleRunnable.class);
private String message;
public SimpleRunnable(String message) {
this.message = message;
}
@Override
public void run() {
log.info(message);
}
}
class SimpleCallable implements Callable<Integer> {
@Override
public Integer call() throws Exception {
return RandomUtils.nextInt(0, 100);
}
}
@@ -7,12 +7,12 @@ class CalculatorTest5 {
private val calculator = Calculator()
@Test
fun testAddition() {
fun whenAdding1and3_thenAnswerIs4() {
Assertions.assertEquals(4, calculator.add(1, 3))
}
@Test
fun testDivideByZero() {
fun whenDividingBy0_thenErrorOccurs() {
val exception = Assertions.assertThrows(DivideByZeroException::class.java) {
calculator.divide(5, 0)
}
@@ -21,7 +21,7 @@ class CalculatorTest5 {
}
@Test
fun testSquares() {
fun whenSquaringNumbers_thenCorrectAnswerGiven() {
Assertions.assertAll(
Executable { Assertions.assertEquals(1, calculator.square(1)) },
Executable { Assertions.assertEquals(4, calculator.square(2)) },
@@ -31,9 +31,9 @@ class CalculatorTest5 {
@TestFactory
fun testSquaresFactory() = listOf(
DynamicTest.dynamicTest("1 squared") { Assertions.assertEquals(1,calculator.square(1))},
DynamicTest.dynamicTest("2 squared") { Assertions.assertEquals(4,calculator.square(2))},
DynamicTest.dynamicTest("3 squared") { Assertions.assertEquals(9,calculator.square(3))}
DynamicTest.dynamicTest("when I calculate 1^2 then I get 1") { Assertions.assertEquals(1,calculator.square(1))},
DynamicTest.dynamicTest("when I calculate 2^2 then I get 4") { Assertions.assertEquals(4,calculator.square(2))},
DynamicTest.dynamicTest("when I calculate 3^2 then I get 9") { Assertions.assertEquals(9,calculator.square(3))}
)
@TestFactory
@@ -44,7 +44,7 @@ class CalculatorTest5 {
4 to 16,
5 to 25)
.map { (input, expected) ->
DynamicTest.dynamicTest("$input squared") {
DynamicTest.dynamicTest("when I calculate $input^2 then I get $expected") {
Assertions.assertEquals(expected, calculator.square(input))
}
}
@@ -59,14 +59,14 @@ class CalculatorTest5 {
@TestFactory
fun testSquaresFactory3() = squaresTestData
.map { (input, expected) ->
DynamicTest.dynamicTest("$input squared") {
DynamicTest.dynamicTest("when I calculate $input^2 then I get $expected") {
Assertions.assertEquals(expected, calculator.square(input))
}
}
@TestFactory
fun testSquareRootsFactory3() = squaresTestData
.map { (expected, input) ->
DynamicTest.dynamicTest("Square root of $input") {
DynamicTest.dynamicTest("I calculate the square root of $input then I get $expected") {
Assertions.assertEquals(expected.toDouble(), calculator.squareRoot(input))
}
}
@@ -76,7 +76,7 @@ class CalculatorTest5 {
Tag("logarithms")
)
@Test
fun testLogarithms() {
fun whenIcalculateLog2Of8_thenIget3() {
Assertions.assertEquals(3.0, calculator.log(2, 8))
}
}
@@ -6,14 +6,14 @@ import org.junit.jupiter.api.Test
class SimpleTest5 {
@Test
fun testEmpty() {
fun whenEmptyList_thenListIsEmpty() {
val list = listOf<String>()
Assertions.assertTrue(list::isEmpty)
}
@Test
@Disabled
fun testMessage() {
fun when3equals4_thenTestFails() {
Assertions.assertEquals(3, 4) {
"Three does not equal four"
}
+1
View File
@@ -35,3 +35,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Jackson – Unmarshall to Collection/Array](http://www.baeldung.com/jackson-collection-array)
- [Jackson – Change Name of Field](http://www.baeldung.com/jackson-name-of-property)
- [Serialize Only Fields that meet a Custom Criteria with Jackson](http://www.baeldung.com/jackson-serialize-field-custom-criteria)
- [Mapping Nested Values with Jackson](http://www.baeldung.com/jackson-nested-values)
@@ -16,7 +16,7 @@ import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
import com.j256.ormlite.table.TableUtils;
public class ORMLiteTest {
public class ORMLiteIntegrationTest {
private static JdbcPooledConnectionSource connectionSource;
private static Dao<Library, Long> libraryDao;
@@ -9,7 +9,7 @@ import org.junit.Test;
import com.gs.fw.common.mithra.test.ConnectionManagerForTests;
import com.gs.fw.common.mithra.test.MithraTestResource;
public class ReladomoTest {
public class ReladomoIntegrationTest {
private MithraTestResource mithraTestResource;
@Before
@@ -1,6 +1,5 @@
package com.baeldung.awaitility;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.junit.Before;
import org.junit.Test;
+2 -1
View File
@@ -192,6 +192,8 @@
<module>spring-quartz</module>
<module>spring-rest-angular</module>
<module>spring-rest-docs</module>
<module>spring-rest-full</module>
<module>spring-rest-query-language</module>
<module>spring-rest</module>
<module>spring-rest-simple</module>
<module>spring-security-cache-control</module>
@@ -213,7 +215,6 @@
<module>spring-security-mvc-socket</module>
<module>spring-security-rest-basic-auth</module>
<module>spring-security-rest-custom</module>
<module>spring-security-rest-full</module>
<module>spring-security-rest</module>
<module>spring-security-sso</module>
<module>spring-security-x509</module>
@@ -1,6 +1,6 @@
=========
## REST Example Project with Spring Security
## REST Example Project with Spring
### Courses
The "REST With Spring" Classes: http://bit.ly/restwithspring
@@ -15,15 +15,9 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com
- [Integration Testing with the Maven Cargo plugin](http://www.baeldung.com/2011/10/16/how-to-set-up-integration-testing-with-the-maven-cargo-plugin/)
- [Introduction to Spring Data JPA](http://www.baeldung.com/2011/12/22/the-persistence-layer-with-spring-data-jpa/)
- [Project Configuration with Spring](http://www.baeldung.com/2012/03/12/project-configuration-with-spring/)
- [REST Query Language with Spring and JPA Criteria](http://www.baeldung.com/rest-search-language-spring-jpa-criteria)
- [REST Query Language with Spring Data JPA Specifications](http://www.baeldung.com/rest-api-search-language-spring-data-specifications)
- [REST Query Language with Spring Data JPA and QueryDSL](http://www.baeldung.com/rest-api-search-language-spring-data-querydsl)
- [REST Query Language – Advanced Search Operations](http://www.baeldung.com/rest-api-query-search-language-more-operations)
- [Metrics for your Spring REST API](http://www.baeldung.com/spring-rest-api-metrics)
- [REST Query Language with RSQL](http://www.baeldung.com/rest-api-search-language-rsql-fiql)
- [Spring RestTemplate Tutorial](http://www.baeldung.com/rest-template)
- [Bootstrap a Web Application with Spring 4](http://www.baeldung.com/bootstraping-a-web-application-with-spring-and-java-based-configuration)
- [REST Query Language – Implementing OR Operation](http://www.baeldung.com/rest-api-query-search-or-operation)
@@ -46,5 +40,5 @@ mysql -u root -p
### Use the REST Service
```
curl http://localhost:8080/spring-security-rest-full/foos
curl http://localhost:8080/spring-rest-full/foos
```
File diff suppressed because it is too large Load Diff
@@ -2,13 +2,10 @@ package org.baeldung.persistence.dao;
import org.baeldung.persistence.model.Foo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface IFooDao extends JpaRepository<Foo, Long>, JpaSpecificationExecutor<Foo> {
public interface IFooDao extends JpaRepository<Foo, Long> {
@Query("SELECT f FROM Foo f WHERE LOWER(f.name) = LOWER(:name)")
Foo retrieveByName(@Param("name") String name);
}
@@ -16,7 +16,7 @@ import org.springframework.web.filter.ShallowEtagHeaderFilter;
/**
* Main Application Class - uses Spring Boot. Just run this as a normal Java
* class to run up a Jetty Server (on http://localhost:8082/spring-security-rest-full)
* class to run up a Jetty Server (on http://localhost:8082/spring-rest-full)
*
*/
@EnableScheduling

Some files were not shown because too many files have changed in this diff Show More