From 6080639682e964fbcffb19c8dfae78c18de5455d Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Fri, 18 Dec 2015 15:34:34 -0500 Subject: [PATCH] INTSAMPLES-148: Make JPA Sample as Boot App JIRA: https://jira.spring.io/browse/INTSAMPLES-148 Upgrade several dependencies, especially OpenJPA-2.4.0 to support Java 8 Class Transformation Upgrade to DSL-1.1.2 and other polishing Upgrade to SI-4.3.0.M1 --- .../ftp/FtpInboundChannelAdapterSample.java | 8 ++- .../ftp/FtpOutboundChannelAdapterSample.java | 8 ++- .../integration/samples/ftp/TestSuite.java | 8 ++- basic/jpa/README.md | 12 ++-- .../jpa/EclipseLinkAutoConfiguration.java | 64 +++++++++++++++++++ .../integration/samples/jpa/Main.java | 36 +++++------ .../samples/jpa/OpenJpaAutoConfiguration.java | 64 +++++++++++++++++++ .../samples/jpa/domain/Person.java | 15 ++--- .../jpa/src/main/resources/H2-DropTables.sql | 2 - .../src/main/resources/H2-PopulateData.sql | 2 - .../spring/integration/commonJpa-context.xml | 48 -------------- .../integration/eclipselink-context.xml | 22 ------- .../spring/integration/hibernate-context.xml | 22 ------- .../spring/integration/openjpa-context.xml | 22 ------- basic/jpa/src/main/resources/application.yml | 8 +++ basic/jpa/src/main/resources/data.sql | 3 + basic/jpa/src/main/resources/log4j.xml | 28 -------- .../{H2-CreateTables.sql => schema.sql} | 8 ++- basic/jpa/src/main/resources/setup-tables.sql | 6 -- .../spring-integration-context.xml | 20 +----- ...OutboundGatewayTest.java => JpaTests.java} | 44 +++++++------ build.gradle | 34 +++++----- 22 files changed, 234 insertions(+), 250 deletions(-) create mode 100644 basic/jpa/src/main/java/org/springframework/integration/samples/jpa/EclipseLinkAutoConfiguration.java create mode 100644 basic/jpa/src/main/java/org/springframework/integration/samples/jpa/OpenJpaAutoConfiguration.java delete mode 100644 basic/jpa/src/main/resources/H2-DropTables.sql delete mode 100644 basic/jpa/src/main/resources/H2-PopulateData.sql delete mode 100644 basic/jpa/src/main/resources/META-INF/spring/integration/commonJpa-context.xml delete mode 100644 basic/jpa/src/main/resources/META-INF/spring/integration/eclipselink-context.xml delete mode 100644 basic/jpa/src/main/resources/META-INF/spring/integration/hibernate-context.xml delete mode 100644 basic/jpa/src/main/resources/META-INF/spring/integration/openjpa-context.xml create mode 100644 basic/jpa/src/main/resources/application.yml create mode 100644 basic/jpa/src/main/resources/data.sql delete mode 100644 basic/jpa/src/main/resources/log4j.xml rename basic/jpa/src/main/resources/{H2-CreateTables.sql => schema.sql} (61%) delete mode 100644 basic/jpa/src/main/resources/setup-tables.sql rename basic/jpa/src/main/resources/{META-INF/spring/integration => }/spring-integration-context.xml (57%) rename basic/jpa/src/test/java/org/springframework/integration/samples/jpa/{OutboundGatewayTest.java => JpaTests.java} (53%) diff --git a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpInboundChannelAdapterSample.java b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpInboundChannelAdapterSample.java index 4ab72281..caeed7eb 100644 --- a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpInboundChannelAdapterSample.java +++ b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpInboundChannelAdapterSample.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,8 +18,10 @@ package org.springframework.integration.samples.ftp; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import org.apache.log4j.Logger; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.messaging.Message; @@ -33,7 +35,7 @@ import org.springframework.messaging.PollableChannel; */ public class FtpInboundChannelAdapterSample { - private static final Logger LOGGER = Logger.getLogger(FtpInboundChannelAdapterSample.class); + private static final Logger LOGGER = LoggerFactory.getLogger(FtpInboundChannelAdapterSample.class); @Test public void runDemo() throws Exception{ diff --git a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundChannelAdapterSample.java b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundChannelAdapterSample.java index 19cd4b1b..8ef70e7b 100644 --- a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundChannelAdapterSample.java +++ b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/FtpOutboundChannelAdapterSample.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,10 +19,12 @@ import static org.junit.Assert.assertTrue; import java.io.File; import java.io.InputStream; -import org.apache.log4j.Logger; import org.apache.commons.io.FileUtils; import org.junit.After; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.messaging.Message; @@ -37,7 +39,7 @@ import org.springframework.integration.support.MessageBuilder; */ public class FtpOutboundChannelAdapterSample { - private static final Logger LOGGER = Logger.getLogger(FtpOutboundChannelAdapterSample.class); + private static final Logger LOGGER = LoggerFactory.getLogger(FtpOutboundChannelAdapterSample.class); private final File baseFolder = new File("target" + File.separator + "toSend"); diff --git a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/TestSuite.java b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/TestSuite.java index 5f6e0f7f..132c7e8a 100644 --- a/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/TestSuite.java +++ b/basic/ftp/src/test/java/org/springframework/integration/samples/ftp/TestSuite.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2016 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,13 +24,15 @@ import org.apache.ftpserver.FtpServer; import org.apache.ftpserver.FtpServerFactory; import org.apache.ftpserver.ftplet.FtpException; import org.apache.ftpserver.listener.ListenerFactory; -import org.apache.log4j.Logger; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; import org.junit.runners.Suite; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.springframework.integration.samples.ftp.support.TestUserManager; import org.springframework.integration.test.util.SocketUtils; @@ -50,7 +52,7 @@ import org.springframework.integration.test.util.SocketUtils; }) public class TestSuite { - private static final Logger LOGGER = Logger.getLogger(TestSuite.class); + private static final Logger LOGGER = LoggerFactory.getLogger(TestSuite.class); public static final String FTP_ROOT_DIR = "target" + File.separator + "ftproot"; public static final String LOCAL_FTP_TEMP_DIR = "target" + File.separator + "local-ftp-temp"; diff --git a/basic/jpa/README.md b/basic/jpa/README.md index 76d5a36e..a2802e49 100644 --- a/basic/jpa/README.md +++ b/basic/jpa/README.md @@ -8,7 +8,7 @@ This sample illustrates how the JPA Components can be used. The example presente * List all people from the database * Create a new Person record in the database -The first example demonstrates the use of an JPA Outbound gateway to retrieve a list of people. The second example uses an JPA Outbound Gateway in order to create a new Person record and then return the newly created Person record. +The first example demonstrates the use of an JPA Outbound gateway to retrieve a list of people. The second example uses an JPA Outbound Gateway in order to create a new Person record and then return the newly created Person record. You have the option to choose between the following 3 persistence providers: @@ -18,18 +18,18 @@ You have the option to choose between the following 3 persistence providers: # Getting Started -Hibernate works out of the box and there are 2 options on how to execute the sample: +Hibernate works out of the box and there are 2 options on how to execute the sample: * running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application) * or from the command line: $ gradlew :jpa:run -For **OpenJPA** and **EclipseLink** to work, you must provide a [Java Agent](http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html). -When using the Gradle Application Plugin, this is taken care of for you behind the scenes automatically. However, +For **OpenJPA** and **EclipseLink** to work, you must provide a [Java Agent](http://docs.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html). +When using the Gradle Application Plugin, this is taken care of for you behind the scenes automatically. However, when running the sample from within STS start the Main class with the following JVM flags: - -javaagent:/path/to/.m2/repository/org/springframework/spring-instrument/3.1.1.RELEASE/spring-instrument-3.1.1.RELEASE.jar - -javaagent:/path/to/.m2/repository/org/apache/openjpa/openjpa/2.2.0/openjpa-2.2.0.jar + -javaagent:/path/to/.m2/repository/org/springframework/spring-instrument/4.2.4.RELEASE/spring-instrument-4.2.4.RELEASE.jar + -javaagent:/path/to/.m2/repository/org/apache/openjpa/openjpa/2.4.0/openjpa-2.4.0.jar With these flags you will be able to use all 3 persistence providers at once. diff --git a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/EclipseLinkAutoConfiguration.java b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/EclipseLinkAutoConfiguration.java new file mode 100644 index 00000000..9bc70e0c --- /dev/null +++ b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/EclipseLinkAutoConfiguration.java @@ -0,0 +1,64 @@ +package org.springframework.integration.samples.jpa; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.EntityManager; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; +import org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.util.ClassUtils; + +/** + * @author Artem Bilan + * @since 4.2 + */ +@Configuration +@ConditionalOnClass({LocalContainerEntityManagerFactoryBean.class, + EnableTransactionManagement.class, EntityManager.class}) +@Conditional(EclipseLinkAutoConfiguration.EclipseLinkEntityManagerCondition.class) +@AutoConfigureAfter({DataSourceAutoConfiguration.class}) +@Profile("eclipseLink") +public class EclipseLinkAutoConfiguration extends JpaBaseConfiguration { + + @Override + protected AbstractJpaVendorAdapter createJpaVendorAdapter() { + return new EclipseLinkJpaVendorAdapter(); + } + + @Override + protected Map getVendorProperties() { + return new HashMap(); + } + + + @Order(Ordered.HIGHEST_PRECEDENCE + 20) + static class EclipseLinkEntityManagerCondition extends SpringBootCondition { + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + if (ClassUtils.isPresent("org.eclipse.persistence.jpa.JpaEntityManager", context.getClassLoader())) { + return ConditionOutcome.match("found JpaEntityManager class"); + } + else { + return ConditionOutcome.noMatch("did not find JpaEntityManager class"); + } + } + + } + +} diff --git a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/Main.java b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/Main.java index 65533b77..f4a09fab 100644 --- a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/Main.java +++ b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/Main.java @@ -13,13 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.samples.jpa; import java.text.SimpleDateFormat; import java.util.List; import java.util.Scanner; -import org.springframework.context.support.GenericXmlApplicationContext; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.ImportResource; import org.springframework.integration.samples.jpa.domain.Person; import org.springframework.integration.samples.jpa.service.PersonService; import org.springframework.util.StringUtils; @@ -30,18 +35,16 @@ import org.springframework.util.StringUtils; * @author Gunnar Hillert * @author Amol Nayak * @author Gary Russell + * @author Artem Bilan * @version 1.0 * */ -public final class Main { +@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class) +@ImportResource("spring-integration-context.xml") +public class Main { private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss"); - /** - * Prevent instantiation. - */ - private Main() {} - /** * Load the Spring Integration Application Context * @@ -68,19 +71,19 @@ public final class Main { System.out.println("\tq. Quit the application"); System.out.print("Enter you choice: "); - final GenericXmlApplicationContext context = new GenericXmlApplicationContext(); + SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(Main.class).web(false); while (true) { final String input = scanner.nextLine(); if("1".equals(input.trim())) { - context.getEnvironment().setActiveProfiles("hibernate"); + springApplicationBuilder.sources(HibernateJpaAutoConfiguration.class); break; } else if("2".equals(input.trim())) { - context.getEnvironment().setActiveProfiles("openjpa"); + springApplicationBuilder.profiles("openJpa"); break; } else if("3".equals(input.trim())) { - context.getEnvironment().setActiveProfiles("eclipselink"); + springApplicationBuilder.profiles("eclipseLink"); break; } else if("q".equals(input.trim())) { System.out.println("Exiting application...bye."); @@ -91,9 +94,7 @@ public final class Main { } } - context.load("classpath:META-INF/spring/integration/*-context.xml"); - context.registerShutdownHook(); - context.refresh(); + ConfigurableApplicationContext context = springApplicationBuilder.run(args); final PersonService personService = context.getBean(PersonService.class); @@ -161,10 +162,7 @@ public final class Main { } } } - /** - * @param service - * @param input - */ + private static void findPeople(final PersonService service) { System.out.println("ID NAME CREATED"); @@ -183,6 +181,6 @@ public final class Main { } System.out.println("==================================\n\n"); - } + } diff --git a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/OpenJpaAutoConfiguration.java b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/OpenJpaAutoConfiguration.java new file mode 100644 index 00000000..7b184687 --- /dev/null +++ b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/OpenJpaAutoConfiguration.java @@ -0,0 +1,64 @@ +package org.springframework.integration.samples.jpa; + +import java.util.HashMap; +import java.util.Map; +import javax.persistence.EntityManager; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; +import org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter; +import org.springframework.transaction.annotation.EnableTransactionManagement; +import org.springframework.util.ClassUtils; + +/** + * @author Artem Bilan + * @since 4.2 + */ +@Configuration +@ConditionalOnClass({LocalContainerEntityManagerFactoryBean.class, + EnableTransactionManagement.class, EntityManager.class}) +@Conditional(OpenJpaAutoConfiguration.OpenJpaEntityManagerCondition.class) +@AutoConfigureAfter({DataSourceAutoConfiguration.class}) +@Profile("openJpa") +public class OpenJpaAutoConfiguration extends JpaBaseConfiguration { + + @Override + protected AbstractJpaVendorAdapter createJpaVendorAdapter() { + return new OpenJpaVendorAdapter(); + } + + @Override + protected Map getVendorProperties() { + return new HashMap(); + } + + + @Order(Ordered.HIGHEST_PRECEDENCE + 20) + static class OpenJpaEntityManagerCondition extends SpringBootCondition { + + @Override + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { + if (ClassUtils.isPresent("org.apache.openjpa.persistence.OpenJPAEntityManager", context.getClassLoader())) { + return ConditionOutcome.match("found OpenJPAEntityManager class"); + } + else { + return ConditionOutcome.noMatch("did not find OpenJPAEntityManager class"); + } + } + + } + +} diff --git a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/domain/Person.java b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/domain/Person.java index 0553b4ab..16d401e5 100644 --- a/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/domain/Person.java +++ b/basic/jpa/src/main/java/org/springframework/integration/samples/jpa/domain/Person.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.samples.jpa.domain; import java.util.Date; @@ -28,32 +29,29 @@ import javax.persistence.TemporalType; /** * A simple POJO representing a Person. - * * @author Amol Nayak * @author Gunnar Hillert - * + * @author Artem Bilan */ @Entity -@Table(name="PEOPLE") +@Table(name = "PEOPLE") public class Person { @Id - @GeneratedValue(strategy=GenerationType.AUTO) + @GeneratedValue(strategy = GenerationType.AUTO) private int id; private String name; @Temporal(TemporalType.TIMESTAMP) - @Column(name="CREATED_DATE_TIME") + @Column(name = "CREATED_DATE_TIME") private Date createdDateTime; public Person() { - super(); this.createdDateTime = new Date(); } public Person(String name) { - super(); this.name = name; this.createdDateTime = new Date(); } @@ -106,7 +104,8 @@ public class Person { if (other.name != null) { return false; } - } else if (!name.equals(other.name)) { + } + else if (!name.equals(other.name)) { return false; } return true; diff --git a/basic/jpa/src/main/resources/H2-DropTables.sql b/basic/jpa/src/main/resources/H2-DropTables.sql deleted file mode 100644 index 1891c098..00000000 --- a/basic/jpa/src/main/resources/H2-DropTables.sql +++ /dev/null @@ -1,2 +0,0 @@ -drop table OPENJPA_SEQUENCE_TABLE; -drop table PEOPLE; diff --git a/basic/jpa/src/main/resources/H2-PopulateData.sql b/basic/jpa/src/main/resources/H2-PopulateData.sql deleted file mode 100644 index 5f6eda40..00000000 --- a/basic/jpa/src/main/resources/H2-PopulateData.sql +++ /dev/null @@ -1,2 +0,0 @@ -insert into PEOPLE(id, name, CREATED_DATE_TIME) -values ('1001', 'Cartman', NOW()); diff --git a/basic/jpa/src/main/resources/META-INF/spring/integration/commonJpa-context.xml b/basic/jpa/src/main/resources/META-INF/spring/integration/commonJpa-context.xml deleted file mode 100644 index f386a4b3..00000000 --- a/basic/jpa/src/main/resources/META-INF/spring/integration/commonJpa-context.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/basic/jpa/src/main/resources/META-INF/spring/integration/eclipselink-context.xml b/basic/jpa/src/main/resources/META-INF/spring/integration/eclipselink-context.xml deleted file mode 100644 index 7f58f5bf..00000000 --- a/basic/jpa/src/main/resources/META-INF/spring/integration/eclipselink-context.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - diff --git a/basic/jpa/src/main/resources/META-INF/spring/integration/hibernate-context.xml b/basic/jpa/src/main/resources/META-INF/spring/integration/hibernate-context.xml deleted file mode 100644 index 31008364..00000000 --- a/basic/jpa/src/main/resources/META-INF/spring/integration/hibernate-context.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - diff --git a/basic/jpa/src/main/resources/META-INF/spring/integration/openjpa-context.xml b/basic/jpa/src/main/resources/META-INF/spring/integration/openjpa-context.xml deleted file mode 100644 index 153580ce..00000000 --- a/basic/jpa/src/main/resources/META-INF/spring/integration/openjpa-context.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - diff --git a/basic/jpa/src/main/resources/application.yml b/basic/jpa/src/main/resources/application.yml new file mode 100644 index 00000000..4ccb5c9e --- /dev/null +++ b/basic/jpa/src/main/resources/application.yml @@ -0,0 +1,8 @@ +spring: + jpa: + generate-ddl: false + hibernate: + ddl-auto: false + show-sql: true + main: + banner-mode: "off" diff --git a/basic/jpa/src/main/resources/data.sql b/basic/jpa/src/main/resources/data.sql new file mode 100644 index 00000000..ff37c67c --- /dev/null +++ b/basic/jpa/src/main/resources/data.sql @@ -0,0 +1,3 @@ +insert into PEOPLE(id, name, CREATED_DATE_TIME) values ('1001', 'Cartman', NOW()); + +INSERT INTO SEQUENCE(SEQ_NAME, SEQ_COUNT) values ('SEQ_GEN', 0); diff --git a/basic/jpa/src/main/resources/log4j.xml b/basic/jpa/src/main/resources/log4j.xml deleted file mode 100644 index 40d94ca5..00000000 --- a/basic/jpa/src/main/resources/log4j.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/basic/jpa/src/main/resources/H2-CreateTables.sql b/basic/jpa/src/main/resources/schema.sql similarity index 61% rename from basic/jpa/src/main/resources/H2-CreateTables.sql rename to basic/jpa/src/main/resources/schema.sql index 7d4e55e1..6e2fa513 100644 --- a/basic/jpa/src/main/resources/H2-CreateTables.sql +++ b/basic/jpa/src/main/resources/schema.sql @@ -1,6 +1,8 @@ -drop table PEOPLE; -DROP TABLE OPENJPA_SEQUENCE_TABLE; +drop table if EXISTS PEOPLE; +DROP TABLE if EXISTS OPENJPA_SEQUENCE_TABLE; CREATE TABLE OPENJPA_SEQUENCE_TABLE (ID TINYINT NOT NULL, SEQUENCE_VALUE BIGINT, PRIMARY KEY (ID)); -CREATE TABLE PEOPLE (id BIGINT generated by default as identity, name VARCHAR(255), CREATED_DATE_TIME TIMESTAMP, PRIMARY KEY (id)); +CREATE TABLE SEQUENCE (SEQ_NAME VARCHAR(50), SEQ_COUNT DECIMAL(15)); + +CREATE TABLE PEOPLE (id BIGINT generated by default as identity, name VARCHAR(255), CREATED_DATE_TIME TIMESTAMP, PRIMARY KEY (id)); diff --git a/basic/jpa/src/main/resources/setup-tables.sql b/basic/jpa/src/main/resources/setup-tables.sql deleted file mode 100644 index dee1726a..00000000 --- a/basic/jpa/src/main/resources/setup-tables.sql +++ /dev/null @@ -1,6 +0,0 @@ -create table IF NOT EXISTS USERS(USERNAME varchar(100),PASSWORD varchar(100), EMAIL varchar(100)); -create table IF NOT EXISTS DUMMY(DUMMY_VALUE varchar(10)); -create table IF NOT EXISTS Person(id integer identity primary key , name varchar(100), gender varchar(1), dateOfBirth date); -INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('a', 'secret', 'spring-integration@awesome.com'); -INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('b', 's3cr3t', 'spring@rocks.com'); -INSERT INTO USERS(USERNAME, PASSWORD, EMAIL) VALUES ('foo', 'bar', 'foo@bar.de'); \ No newline at end of file diff --git a/basic/jpa/src/main/resources/META-INF/spring/integration/spring-integration-context.xml b/basic/jpa/src/main/resources/spring-integration-context.xml similarity index 57% rename from basic/jpa/src/main/resources/META-INF/spring/integration/spring-integration-context.xml rename to basic/jpa/src/main/resources/spring-integration-context.xml index 10133d42..e1d3a8e5 100644 --- a/basic/jpa/src/main/resources/META-INF/spring/integration/spring-integration-context.xml +++ b/basic/jpa/src/main/resources/spring-integration-context.xml @@ -3,17 +3,12 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xmlns:int-jpa="http://www.springframework.org/schema/integration/jpa" - xmlns:jdbc="http://www.springframework.org/schema/jdbc" - xmlns:tx="http://www.springframework.org/schema/tx" - xmlns:context="http://www.springframework.org/schema/context" - xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd - http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd + xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd - http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd - http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/integration/jpa http://www.springframework.org/schema/integration/jpa/spring-integration-jpa.xsd"> + - - - - - - - - - - - diff --git a/basic/jpa/src/test/java/org/springframework/integration/samples/jpa/OutboundGatewayTest.java b/basic/jpa/src/test/java/org/springframework/integration/samples/jpa/JpaTests.java similarity index 53% rename from basic/jpa/src/test/java/org/springframework/integration/samples/jpa/OutboundGatewayTest.java rename to basic/jpa/src/test/java/org/springframework/integration/samples/jpa/JpaTests.java index 474a78b8..f41295aa 100644 --- a/basic/jpa/src/test/java/org/springframework/integration/samples/jpa/OutboundGatewayTest.java +++ b/basic/jpa/src/test/java/org/springframework/integration/samples/jpa/JpaTests.java @@ -15,44 +15,48 @@ */ package org.springframework.integration.samples.jpa; -import java.util.Calendar; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Calendar; +import java.util.List; -import org.apache.log4j.Logger; -import org.junit.Assert; import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; +import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.integration.samples.jpa.domain.Person; import org.springframework.integration.samples.jpa.service.PersonService; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * * @author Amol Nayak + * @author Artem Bilan * */ -public class OutboundGatewayTest { +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = {Main.class, HibernateJpaAutoConfiguration.class}) +public class JpaTests { - private static final Logger LOGGER = Logger.getLogger(OutboundGatewayTest.class); + @Autowired + private PersonService personService; @Test public void insertPersonRecord() { - - final ApplicationContext context = new ClassPathXmlApplicationContext( - "classpath:/META-INF/spring/integration/spring-integration-context.xml"); - - final PersonService service = context.getBean(PersonService.class); - - LOGGER.info("Creating person Instance"); - - final Person person = new Person(); + Person person = new Person(); Calendar createdDateTime = Calendar.getInstance(); - createdDateTime.set(1980, 0, 1); + createdDateTime.set(1980, Calendar.JANUARY, 1); person.setCreatedDateTime(createdDateTime.getTime()); person.setName("Name Of The Person"); - final Person persistedPerson = service.createPerson(person); - Assert.assertNotNull("Expected a non null instance of Person, got null", persistedPerson); - LOGGER.info("\n\tGenerated person with id: " + persistedPerson.getId() + ", with name: " + persistedPerson.getName()); + this.personService.createPerson(person); + List people = this.personService.findPeople(); + assertNotNull(people); + assertEquals(2, people.size()); + assertEquals(person.getName(), people.get(1).getName()); } } diff --git a/build.gradle b/build.gradle index 152e9e31..4d273b64 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,7 @@ allprojects { // mavenLocal() maven { url 'http://repo.spring.io/libs-snapshot' } maven { url 'http://repo.spring.io/libs-milestone' } -// maven { url 'http://repo.spring.io/libs-staging-local' } + maven { url 'http://repo.spring.io/libs-staging-local' } } } @@ -162,8 +162,9 @@ subprojects { subproject -> commonsPoolVersion = '1.5.4' c3p0Version = '0.9.1.2' derbyVersion = '10.10.1.1' + eclipseLinkVersion = '2.4.2' hamcrestVersion = '1.3' - hibernateVersion = '4.2.11.Final' + hibernateVersion = '4.3.11.Final' hibernateValidatorVersion = '4.2.0.Final' ftpServerVersion = '1.0.6' flexjsonVersion = '2.0' @@ -180,13 +181,14 @@ subprojects { subproject -> jtdsVersion = '1.2.6' jmsApiVersion = '1.1-rev-1' jrubyVersion = '1.7.12' + jpa21ApiVersion = '1.0.0.Final' jpaApiVersion = '2.0.0' jstlVersion = '1.2' junitVersion = '4.11' jythonVersion = '2.5.3' log4jVersion = '1.2.17' mockitoVersion = '1.9.5' - openJpaVersion = '2.3.0' + openJpaVersion = '2.4.0' oracleDriverVersion = '11.2.0.3' reactorVersion = '2.0.7.RELEASE' postgresVersion = '9.1-901-1.jdbc4' @@ -196,7 +198,7 @@ subprojects { subproject -> springIntegrationDslVersion = '1.1.2.RELEASE' springIntegrationKafkaVersion = '1.3.0.RELEASE' springIntegrationSplunkVersion = '1.1.0.RELEASE' - springVersion = '4.2.3.RELEASE' + springVersion = '4.2.5.RELEASE' springSecurityVersion = '4.0.2.RELEASE' springWebFlowVersion = '2.3.3.RELEASE' tilesJspVersion = '2.2.1' @@ -758,31 +760,34 @@ project('jpa') { description = 'JPA Basic Sample' apply plugin: 'application' + apply plugin: 'spring-boot' mainClassName = 'org.springframework.integration.samples.jpa.Main' dependencies { + compile 'org.springframework.boot:spring-boot-starter-data-jpa' compile "org.springframework.integration:spring-integration-jpa:$springIntegrationVersion" compile "com.h2database:h2:$h2Version" - compile "org.hibernate:hibernate-entitymanager:$hibernateVersion" - compile "org.eclipse.persistence:javax.persistence:$jpaApiVersion" - compile "commons-lang:commons-lang:$commonsLangVersion" - compile "log4j:log4j:$log4jVersion" + compile "org.hibernate.javax.persistence:hibernate-jpa-2.1-api:$jpa21ApiVersion" runtime "org.springframework:spring-instrument:$springVersion" runtime "org.apache.openjpa:openjpa:$openJpaVersion" - + runtime "org.hibernate:hibernate-entitymanager:$hibernateVersion" + runtime "org.eclipse.persistence:org.eclipse.persistence.jpa:$eclipseLinkVersion" } //Suppress openjpa annotation processor warnings compileTestJava.options.compilerArgs = ["${xLintArg},-processing"] - run { - def javaAgents = configurations.testRuntime.resolvedConfiguration.resolvedArtifacts - .findAll { it.name in ['spring-instrument', 'openjpa'] } - .collect { "-javaagent:${it.file}" } - jvmArgs javaAgents + springBoot { + mainClass = 'org.springframework.integration.samples.jpa.Main' } + + tasks.withType(JavaExec) { + standardInput = System.in + jvmArgs classpath.files.findAll{it.name ==~ /(spring-instrument.+)|(openjpa-${openJpaVersion}.+)/}.collect{"-javaagent:$it"} + } + } project('mail') { @@ -799,7 +804,6 @@ project('mail') { task runGmailInboundPop3AdapterTestApp(type: JavaExec) { main 'org.springframework.integration.samples.mail.imapidle.GmailInboundPop3AdapterTestApp' classpath = sourceSets.main.runtimeClasspath - } task runGmailInboundImapIdleAdapterTestApp(type: JavaExec) {