JAVA-10516: Revisit spring-boot module inside spring-boot-modules folder (#12038)
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.demo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class DemoApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("spring.config.name", "demo");
|
||||
SpringApplication.run(DemoApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.demo.components;
|
||||
|
||||
import com.baeldung.demo.model.Foo;
|
||||
import com.baeldung.demo.repository.FooRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class FooService {
|
||||
|
||||
@Autowired
|
||||
private FooRepository fooRepository;
|
||||
|
||||
public Foo getFooWithId(Integer id) throws Exception {
|
||||
return fooRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public Foo getFooWithName(String name) {
|
||||
return fooRepository.findByName(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.demo.exceptions;
|
||||
|
||||
public class CommonException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 3080004140659213332L;
|
||||
|
||||
public CommonException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.demo.exceptions;
|
||||
|
||||
public class FooNotFoundException extends RuntimeException {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final long serialVersionUID = 9042200028456133589L;
|
||||
|
||||
public FooNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.demo.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Foo implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Foo() {
|
||||
}
|
||||
|
||||
public Foo(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Foo(Integer id, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.demo.repository;
|
||||
|
||||
import com.baeldung.demo.model.Foo;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface FooRepository extends JpaRepository<Foo, Integer> {
|
||||
public Foo findByName(String name);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.demo.service;
|
||||
|
||||
import com.baeldung.demo.model.Foo;
|
||||
import com.baeldung.demo.components.FooService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class FooController {
|
||||
|
||||
@Autowired
|
||||
private FooService fooService;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Foo getFooWithId(@PathVariable Integer id) throws Exception {
|
||||
return fooService.getFooWithId(id);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public Foo getFooWithName(@RequestParam String name) throws Exception {
|
||||
return fooService.getFooWithName(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.session.exception;
|
||||
|
||||
import com.baeldung.demo.model.Foo;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
|
||||
@EntityScan(basePackageClasses = Foo.class)
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("spring.config.name", "exception");
|
||||
System.setProperty("spring.profiles.active", "exception");
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.session.exception.repository;
|
||||
|
||||
import com.baeldung.demo.model.Foo;
|
||||
|
||||
public interface FooRepository {
|
||||
|
||||
void save(Foo foo);
|
||||
|
||||
Foo get(Integer id);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.session.exception.repository;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
import com.baeldung.demo.model.Foo;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Profile("exception")
|
||||
@Repository
|
||||
public class FooRepositoryImpl implements FooRepository {
|
||||
@Autowired
|
||||
private EntityManagerFactory emf;
|
||||
|
||||
@Override
|
||||
public void save(Foo foo) {
|
||||
emf.unwrap(SessionFactory.class).getCurrentSession().saveOrUpdate(foo);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Foo get(Integer id) {
|
||||
return emf.unwrap(SessionFactory.class).getCurrentSession().get(Foo.class, id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.boot;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.session.exception.Application;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
@TestPropertySource("classpath:exception.properties")
|
||||
public class ApplicationIntegrationTest {
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.baeldung.boot.repository;
|
||||
|
||||
import com.baeldung.demo.DemoApplicationIntegrationTest;
|
||||
import com.baeldung.demo.model.Foo;
|
||||
import com.baeldung.demo.repository.FooRepository;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@Transactional
|
||||
public class FooRepositoryIntegrationTest extends DemoApplicationIntegrationTest {
|
||||
@Autowired
|
||||
private FooRepository fooRepository;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
fooRepository.save(new Foo("Foo"));
|
||||
fooRepository.save(new Foo("Bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindByName() {
|
||||
Foo foo = fooRepository.findByName("Bar");
|
||||
|
||||
assertThat(foo, notNullValue());
|
||||
assertThat(foo.getId(), notNullValue());
|
||||
assertThat(foo.getName(), is("Bar"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.boot.repository;
|
||||
|
||||
import com.baeldung.demo.DemoApplicationIntegrationTest;
|
||||
import com.baeldung.demo.model.Foo;
|
||||
import com.baeldung.demo.repository.FooRepository;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
@Transactional
|
||||
public class HibernateSessionIntegrationTest extends DemoApplicationIntegrationTest {
|
||||
@Autowired
|
||||
private FooRepository fooRepository;
|
||||
|
||||
@Test
|
||||
public void whenSavingWithCurrentSession_thenThrowNoException() {
|
||||
fooRepository.save(new Foo("Exception Solved"));
|
||||
|
||||
Foo foo = fooRepository.findByName("Exception Solved");
|
||||
|
||||
assertThat(foo, notNullValue());
|
||||
assertThat(foo.getId(), notNullValue());
|
||||
assertThat(foo.getName(), is("Exception Solved"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.boot.repository;
|
||||
|
||||
import com.baeldung.boot.ApplicationIntegrationTest;
|
||||
import com.baeldung.demo.model.Foo;
|
||||
import com.baeldung.session.exception.repository.FooRepository;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Transactional
|
||||
@TestPropertySource("classpath:exception-hibernate.properties")
|
||||
public class NoHibernateSessionIntegrationTest extends ApplicationIntegrationTest {
|
||||
@Autowired
|
||||
private FooRepository fooRepository;
|
||||
|
||||
@Test(expected = HibernateException.class)
|
||||
public void whenSavingWithoutCurrentSession_thenThrowException() {
|
||||
Foo foo = new Foo("Exception Thrown");
|
||||
fooRepository.save(foo);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.demo;
|
||||
|
||||
import com.baeldung.demo.DemoApplication;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public class DemoApplicationIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.profiles.active=exception
|
||||
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
|
||||
@@ -0,0 +1,2 @@
|
||||
spring.dao.exceptiontranslation.enabled=false
|
||||
spring.profiles.active=exception
|
||||
Reference in New Issue
Block a user