Merge pull request #11458 from hkhan/JAVA-6455-spring-boot-with-h2
[JAVA-6455] Update H2 code with latest Spring Boot changes
This commit is contained in:
commit
d96250ac89
|
@ -2,8 +2,10 @@ package com.baeldung.h2db.springboot;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
|
@PropertySource("classpath:application-h2.properties")
|
||||||
public class SpringBootH2Application {
|
public class SpringBootH2Application {
|
||||||
|
|
||||||
public static void main(String... args) {
|
public static void main(String... args) {
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.baeldung.h2db.springboot.daos;
|
||||||
|
|
||||||
|
import com.baeldung.h2db.springboot.models.Country;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
public interface CountryRepository extends CrudRepository<Country, Integer> {
|
||||||
|
}
|
|
@ -1,10 +0,0 @@
|
||||||
package com.baeldung.h2db.springboot.daos;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import com.baeldung.h2db.springboot.models.User;
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
|
||||||
|
|
||||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
|
||||||
}
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
package com.baeldung.h2db.springboot.models;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Table(name = "countries")
|
||||||
|
@Entity
|
||||||
|
public class Country {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o)
|
||||||
|
return true;
|
||||||
|
if (o == null || getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
|
Country country = (Country) o;
|
||||||
|
return id == country.id && name.equals(country.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Country{" +
|
||||||
|
"id=" + id +
|
||||||
|
", name='" + name + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,54 +0,0 @@
|
||||||
package com.baeldung.h2db.springboot.models;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
|
|
||||||
@Table(name = "users")
|
|
||||||
@Entity
|
|
||||||
public class User {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue
|
|
||||||
private int id;
|
|
||||||
|
|
||||||
private String firstName;
|
|
||||||
|
|
||||||
private String lastName;
|
|
||||||
|
|
||||||
public User() { }
|
|
||||||
|
|
||||||
public int getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getFirstName() {
|
|
||||||
return firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setFirstName(String firstName) {
|
|
||||||
this.firstName = firstName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLastName() {
|
|
||||||
return lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLastName(String lastName) {
|
|
||||||
this.lastName = lastName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "User{" +
|
|
||||||
"id=" + id +
|
|
||||||
", firstName='" + firstName + '\'' +
|
|
||||||
", lastName='" + lastName + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1 @@
|
||||||
|
spring.sql.init.data-locations=data-h2.sql
|
|
@ -0,0 +1,5 @@
|
||||||
|
INSERT INTO countries (id, name) VALUES (1, 'USA');
|
||||||
|
INSERT INTO countries (id, name) VALUES (2, 'France');
|
||||||
|
INSERT INTO countries (id, name) VALUES (3, 'Brazil');
|
||||||
|
INSERT INTO countries (id, name) VALUES (4, 'Italy');
|
||||||
|
INSERT INTO countries (id, name) VALUES (5, 'Canada');
|
|
@ -1,17 +1,3 @@
|
||||||
DROP TABLE IF EXISTS billionaires;
|
|
||||||
|
|
||||||
CREATE TABLE billionaires (
|
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
first_name VARCHAR(250) NOT NULL,
|
|
||||||
last_name VARCHAR(250) NOT NULL,
|
|
||||||
career VARCHAR(250) DEFAULT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
INSERT INTO billionaires (first_name, last_name, career) VALUES
|
|
||||||
('Aliko', 'Dangote', 'Billionaire Industrialist'),
|
|
||||||
('Bill', 'Gates', 'Billionaire Tech Entrepreneur'),
|
|
||||||
('Folrunsho', 'Alakija', 'Billionaire Oil Magnate');
|
|
||||||
|
|
||||||
insert into USER values (101, 'user1', 'comment1');
|
insert into USER values (101, 'user1', 'comment1');
|
||||||
insert into USER values (102, 'user2', 'comment2');
|
insert into USER values (102, 'user2', 'comment2');
|
||||||
insert into USER values (103, 'user3', 'comment3');
|
insert into USER values (103, 'user3', 'comment3');
|
||||||
|
|
|
@ -1,50 +1,39 @@
|
||||||
package com.baeldung;
|
package com.baeldung;
|
||||||
|
|
||||||
import com.baeldung.h2db.springboot.SpringBootH2Application;
|
import com.baeldung.h2db.springboot.SpringBootH2Application;
|
||||||
import com.baeldung.h2db.springboot.daos.UserRepository;
|
import com.baeldung.h2db.springboot.daos.CountryRepository;
|
||||||
import com.baeldung.h2db.springboot.models.User;
|
import com.baeldung.h2db.springboot.models.Country;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import java.util.List;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes = SpringBootH2Application.class)
|
@SpringBootTest(classes = SpringBootH2Application.class)
|
||||||
public class SpringBootH2IntegrationTest {
|
public class SpringBootH2IntegrationTest {
|
||||||
|
|
||||||
|
private static final Country AN_EXPECTED_COUNTRY = buildCountry();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository userRepository;
|
private CountryRepository countryRepository;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contextLoads() { }
|
public void givenInitData_whenApplicationStarts_thenDataIsLoaded() {
|
||||||
|
Iterable<Country> users = countryRepository.findAll();
|
||||||
|
|
||||||
@Test
|
assertThat(users)
|
||||||
public void givenUserProfile_whenAddUser_thenCreateNewUser() {
|
.hasSize(5)
|
||||||
User user = new User();
|
.contains(AN_EXPECTED_COUNTRY);
|
||||||
user.setFirstName("John");
|
}
|
||||||
user.setLastName("Doe");
|
|
||||||
userRepository.save(user);
|
|
||||||
List<User> users = (List<User>) userRepository.findAll();
|
|
||||||
assertFalse(users.isEmpty());
|
|
||||||
|
|
||||||
String firstName = "Aliko";
|
private static Country buildCountry() {
|
||||||
String lastName = "Dangote";
|
Country c = new Country();
|
||||||
User user1 = userRepository.findById(users.get(0).getId()).get();
|
c.setId(5);
|
||||||
user1.setLastName(lastName);
|
c.setName("Canada");
|
||||||
user1.setFirstName(firstName);
|
return c;
|
||||||
userRepository.save(user1);
|
|
||||||
|
|
||||||
user = userRepository.findById(user.getId()).get();
|
|
||||||
assertEquals(user.getFirstName(), firstName);
|
|
||||||
assertEquals(user.getLastName(), lastName);
|
|
||||||
|
|
||||||
userRepository.deleteById(user.getId());
|
|
||||||
assertTrue( ((List<User>) userRepository.findAll()).isEmpty());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue