This commit is contained in:
Dassi Orleando 2017-12-14 17:13:33 +01:00
commit 8caf1a6d4f
19 changed files with 2 additions and 422 deletions

View File

@ -10,9 +10,9 @@ import java.util.concurrent.*;
import static junit.framework.TestCase.assertTrue;
public class WaitingForThreadsToFinishTest {
public class WaitingForThreadsToFinishManualTest {
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishTest.class);
private static final Logger LOG = LoggerFactory.getLogger(WaitingForThreadsToFinishManualTest.class);
private final static ExecutorService WORKER_THREAD_POOL = Executors.newFixedThreadPool(10);
public void awaitTerminationAfterShutdown(ExecutorService threadPool) {
@ -142,66 +142,4 @@ public class WaitingForThreadsToFinishTest {
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
}
}
@Test
public void givenMultipleThreads_whenUsingCompletableFutures_thenMainThreadShouldWaitForAllToFinish() {
CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello";
});
CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Beautiful";
});
CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "World";
});
long startProcessingTime = System.currentTimeMillis();
CompletableFuture<Void> combinedFuture = CompletableFuture.allOf(future1, future2, future3);
combinedFuture.join();
long totalProcessingTime = System.currentTimeMillis() - startProcessingTime;
assertTrue(totalProcessingTime >= 5000 && totalProcessingTime < 6000);
LOG.debug("Responses from all threads are available after " + totalProcessingTime + " milliseconds");
try {
String thread1Response = future1.get();
assertTrue(thread1Response.equals("Hello"));
String thread2Response = future2.get();
assertTrue(thread2Response.equals("Beautiful"));
String thread3Response = future3.get();
assertTrue(thread3Response.equals("World"));
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
awaitTerminationAfterShutdown(WORKER_THREAD_POOL);
}
}

View File

@ -1,52 +0,0 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>flyway</artifactId>
<version>1.0</version>
<name>flyway</name>
<packaging>pom</packaging>
<description>A sample project to demonstrate Flyway migrations</description>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>spring-flyway</module>
</modules>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>${flyway-maven-plugin.version}</version>
</plugin>
</plugins>
</build>
<properties>
<mysql.version>6.0.5</mysql.version>
<flyway-maven-plugin.version>4.0.3</flyway-maven-plugin.version>
<spring.boot.version>1.5.8.RELEASE</spring.boot.version>
</properties>
</project>

View File

@ -1,24 +0,0 @@
target/
!.mvn/wrapper/maven-wrapper.jar
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### NetBeans ###
nbproject/private/
build/
nbbuild/
dist/
nbdist/
.nb-gradle/

View File

@ -1,57 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-flyway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-flyway</name>
<description>Spring Boot Test Flyway Migrations</description>
<parent>
<artifactId>flyway</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0</version>
<relativePath>../../flyway</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,12 +0,0 @@
package com.baeldung.springflyway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringFlywayApplication {
public static void main(String[] args) {
SpringApplication.run(SpringFlywayApplication.class, args);
}
}

View File

@ -1,28 +0,0 @@
package com.baeldung.springflyway.entities;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Customer {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String email;
}

View File

@ -1,14 +0,0 @@
package com.baeldung.springflyway.migration;
import org.flywaydb.core.api.migration.spring.SpringJdbcMigration;
import org.springframework.jdbc.core.JdbcTemplate;
public class V2__uk_lastname_customer implements SpringJdbcMigration {
final String CUSTOMER_LASTNAME_UK = "ALTER TABLE customer ADD CONSTRAINT uk_customer_lastname UNIQUE(last_name);";
@Override
public void migrate(final JdbcTemplate jdbcTemplate) throws Exception {
jdbcTemplate.execute(CUSTOMER_LASTNAME_UK);
}
}

View File

@ -1,11 +0,0 @@
package com.baeldung.springflyway.repositories;
import com.baeldung.springflyway.entities.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
Optional<Customer> findByEmail(String email);
}

View File

@ -1,28 +0,0 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertNotNull;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryInitialMigrationIntegrationTest {
@Autowired CustomerRepository customerRepository;
@Test
public void givenSchemaCreationMigration_whenTryingToCreateACustomer_thenSuccess() {
Customer customer = customerRepository.save(Customer
.builder()
.email("customer@email.com")
.build());
assertNotNull(customer.getId());
}
}

View File

@ -1,35 +0,0 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryInsertDataIntegrationTest {
@Autowired CustomerRepository customerRepository;
@Test
public void givenASetInsertData_whenRunningMigrationsWithSuccess_thenASpecificCustomerIsFound() {
Optional<Customer> customerOptional = customerRepository.findByEmail("email@email.com");
assertTrue(customerOptional.isPresent());
}
@Test
public void givenASetInsertData_whenRunningMigrationsWithSuccess_thenASetOfCustomersIsFound() {
List<Customer> customers = customerRepository.findAll();
assertNotNull(customers);
assertEquals(customers.size(), 6);
}
}

View File

@ -1,25 +0,0 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryNotNullConstraintMigrationIntegrationTest {
@Autowired CustomerRepository customerRepository;
@Test(expected = DataIntegrityViolationException.class)
public void givenTheNotNullConstraintMigrations_whenInsertingACustomerWithNullEmail_thenThrowException() {
customerRepository.save(Customer
.builder()
.build());
}
}

View File

@ -1,29 +0,0 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {
"flyway.locations[0]=db/migration", "flyway.locations[1]=com/baeldung/springflyway/migration"
})
public class CustomerRepositoryUniqueConstraintJavaMigrationIntegrationTest {
@Autowired CustomerRepository customerRepository;
@Test(expected = DataIntegrityViolationException.class)
public void givenTheUniqueConstraintMigrations_whenInsertingAnExistingLastNameCustomer_thenThrowException() {
customerRepository.save(Customer
.builder()
.lastName("LastName")
.build());
}
}

View File

@ -1,27 +0,0 @@
package com.baeldung.springflyway;
import com.baeldung.springflyway.entities.Customer;
import com.baeldung.springflyway.repositories.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CustomerRepositoryUniqueConstraintMigrationIntegrationTest {
@Autowired CustomerRepository customerRepository;
@Test(expected = DataIntegrityViolationException.class)
public void givenTheUniqueConstraintMigrations_whenInsertingAnExistingEmailCustomer_thenThrowException() {
customerRepository.save(Customer
.builder()
.email("email@email.com")
.build());
}
}

View File

@ -1 +0,0 @@
spring.jpa.hibernate.ddl-auto=validate

View File

@ -1,6 +0,0 @@
create table if not exists customer (
id bigint AUTO_INCREMENT not null primary key,
first_name varchar(255) ,
last_name varchar(255) ,
email varchar(255)
);

View File

@ -1,6 +0,0 @@
insert into customer (first_name, last_name, email) values ('FirstName', 'LastName', 'email@email.com');
insert into customer (first_name, last_name, email) values ('FirstName1', 'LastName1', 'email1@email.com');
insert into customer (first_name, last_name, email) values ('FirstName2', 'LastName2', 'email2@email.com');
insert into customer (first_name, last_name, email) values ('FirstName3', 'LastName3', 'email3@email.com');
insert into customer (first_name, last_name, email) values ('FirstName4', 'LastName4', 'email4@email.com');
insert into customer (first_name, last_name, email) values ('FirstName5', 'LastName5', 'email5@email.com');

View File

@ -1 +0,0 @@
ALTER TABLE customer ALTER email SET NOT NULL;

View File

@ -1 +0,0 @@
ALTER TABLE customer ADD CONSTRAINT uk_customer_email UNIQUE(email);

View File

@ -58,7 +58,6 @@
<!--<module>ejb</module>-->
<module>feign</module>
<module>flyway</module>
<!-- <module>testing-modules/gatling</module> --> <!-- not meant to run as part of the standard build -->