Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-13600
This commit is contained in:
commit
79596848ce
@ -2,4 +2,6 @@
|
||||
|
||||
## Relevant articles:
|
||||
|
||||
- [String Matching in Groovy](http://www.baeldung.com/)
|
||||
- [String Matching in Groovy](http://www.baeldung.com/)
|
||||
- [Groovy def Keyword]
|
||||
|
||||
|
@ -14,12 +14,33 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy-all.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-dateutil</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-sql</artifactId>
|
||||
<version>${groovy-sql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
@ -43,12 +64,39 @@
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>addSources</goal>
|
||||
<goal>addTestSources</goal>
|
||||
<goal>compile</goal>
|
||||
<goal>compileTests</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-failsafe-plugin</artifactId>
|
||||
<version>${maven-failsafe-plugin.version}</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-surefire-provider</artifactId>
|
||||
<version>${junit.platform.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>junit5</id>
|
||||
<goals>
|
||||
<goal>integration-test</goal>
|
||||
<goal>verify</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Test5.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.20.1</version>
|
||||
@ -71,10 +119,13 @@
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<groovy.version>2.5.6</groovy.version>
|
||||
<groovy-all.version>2.5.6</groovy-all.version>
|
||||
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.3-groovy-2.5</spock-core.version>
|
||||
<gmavenplus-plugin.version>1.6.3</gmavenplus-plugin.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
|
@ -0,0 +1,79 @@
|
||||
package com.baeldung.defkeyword
|
||||
|
||||
import org.codehaus.groovy.runtime.NullObject
|
||||
import org.codehaus.groovy.runtime.typehandling.GroovyCastException
|
||||
|
||||
import groovy.transform.TypeChecked
|
||||
import groovy.transform.TypeCheckingMode
|
||||
|
||||
@TypeChecked
|
||||
class DefUnitTest extends GroovyTestCase {
|
||||
|
||||
def id
|
||||
def firstName = "Samwell"
|
||||
def listOfCountries = ['USA', 'UK', 'FRANCE', 'INDIA']
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
def multiply(x, y) {
|
||||
return x*y
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
void testDefVariableDeclaration() {
|
||||
|
||||
def list
|
||||
assert list.getClass() == org.codehaus.groovy.runtime.NullObject
|
||||
assert list.is(null)
|
||||
|
||||
list = [1,2,4]
|
||||
assert list instanceof ArrayList
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
void testTypeVariables() {
|
||||
int rate = 200
|
||||
try {
|
||||
rate = [12] //GroovyCastException
|
||||
rate = "nill" //GroovyCastException
|
||||
} catch(GroovyCastException) {
|
||||
println "Cannot assign anything other than integer"
|
||||
}
|
||||
}
|
||||
|
||||
@TypeChecked(TypeCheckingMode.SKIP)
|
||||
void testDefVariableMultipleAssignment() {
|
||||
def rate
|
||||
assert rate == null
|
||||
assert rate.getClass() == org.codehaus.groovy.runtime.NullObject
|
||||
|
||||
rate = 12
|
||||
assert rate instanceof Integer
|
||||
|
||||
rate = "Not Available"
|
||||
assert rate instanceof String
|
||||
|
||||
rate = [1, 4]
|
||||
assert rate instanceof List
|
||||
|
||||
assert divide(12, 3) instanceof BigDecimal
|
||||
assert divide(1, 0) instanceof String
|
||||
|
||||
}
|
||||
|
||||
def divide(int x, int y) {
|
||||
if(y==0) {
|
||||
return "Should not divide by 0"
|
||||
} else {
|
||||
return x/y
|
||||
}
|
||||
}
|
||||
|
||||
def greetMsg() {
|
||||
println "Hello! I am Groovy"
|
||||
}
|
||||
|
||||
void testDefVsType() {
|
||||
def int count
|
||||
assert count instanceof Integer
|
||||
}
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
<?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">
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>spring-data-jpa-2</artifactId>
|
||||
<name>spring-data-jpa</name>
|
||||
|
||||
<name>spring-data-jpa</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@ -19,12 +20,22 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-oxm</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.repository.init.Jackson2RepositoryPopulatorFactoryBean;
|
||||
import org.springframework.data.repository.init.UnmarshallerRepositoryPopulatorFactoryBean;
|
||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||
|
||||
import com.baeldung.entity.Fruit;
|
||||
|
||||
@Configuration
|
||||
public class JpaPopulators {
|
||||
|
||||
@Bean
|
||||
public Jackson2RepositoryPopulatorFactoryBean getRespositoryPopulator() throws Exception {
|
||||
Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
|
||||
factory.setResources(new Resource[] { new ClassPathResource("fruit-data.json") });
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public UnmarshallerRepositoryPopulatorFactoryBean repositoryPopulator() {
|
||||
|
||||
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
|
||||
unmarshaller.setClassesToBeBound(Fruit.class);
|
||||
|
||||
UnmarshallerRepositoryPopulatorFactoryBean factory = new UnmarshallerRepositoryPopulatorFactoryBean();
|
||||
factory.setUnmarshaller(unmarshaller);
|
||||
factory.setResources(new Resource[] { new ClassPathResource("apple-fruit-data.xml"), new ClassPathResource("guava-fruit-data.xml") });
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
@ -2,7 +2,9 @@ package com.baeldung.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
|
||||
@XmlRootElement
|
||||
@Entity
|
||||
public class Fruit {
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fruit>
|
||||
<id>1</id>
|
||||
<name>apple</name>
|
||||
<color>red</color>
|
||||
</fruit>
|
@ -0,0 +1,14 @@
|
||||
[
|
||||
{
|
||||
"_class": "com.baeldung.entity.Fruit",
|
||||
"name": "apple",
|
||||
"color": "red",
|
||||
"id": 1
|
||||
},
|
||||
{
|
||||
"_class": "com.baeldung.entity.Fruit",
|
||||
"name": "guava",
|
||||
"color": "green",
|
||||
"id": 2
|
||||
}
|
||||
]
|
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<fruit>
|
||||
<id>2</id>
|
||||
<name>guava</name>
|
||||
<color>green</color>
|
||||
</fruit>
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.repository;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
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 com.baeldung.entity.Fruit;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class FruitPopulatorTest {
|
||||
|
||||
@Autowired
|
||||
private FruitRepository fruitRepository;
|
||||
|
||||
@Test
|
||||
public void givenFruitJsonPopulatorThenShouldInsertRecordOnStart() {
|
||||
|
||||
List<Fruit> fruits = fruitRepository.findAll();
|
||||
assertEquals("record count is not matching", 2, fruits.size());
|
||||
|
||||
fruits.forEach(fruit -> {
|
||||
if (1 == fruit.getId()) {
|
||||
assertEquals("apple", fruit.getName());
|
||||
assertEquals("red", fruit.getColor());
|
||||
} else if (2 == fruit.getId()) {
|
||||
assertEquals("guava", fruit.getName());
|
||||
assertEquals("green", fruit.getColor());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
2
pom.xml
2
pom.xml
@ -376,7 +376,7 @@
|
||||
<module>cdi</module>
|
||||
<module>checker-plugin</module>
|
||||
<module>core-groovy</module>
|
||||
<module>core-groovy-2</module>
|
||||
<module>core-groovy-2</module>
|
||||
<!-- <module>core-java-10</module> --> <!-- We haven't upgraded to java 10. Fixing in BAEL-10841 -->
|
||||
<!-- <module>core-java-11</module> --> <!-- We haven't upgraded to java 11. Fixing in BAEL-10841 -->
|
||||
<!-- <module>core-java-12</module> --> <!-- We haven't upgraded to java 12. Fixing in BAEL-10841 -->
|
||||
|
@ -7,12 +7,10 @@ Module for the articles that are part of the Spring REST E-book:
|
||||
5. [Entity To DTO Conversion for a Spring REST API](https://www.baeldung.com/entity-to-and-from-dto-for-a-java-spring-application)
|
||||
6. [Error Handling for REST with Spring](http://www.baeldung.com/exception-handling-for-rest-with-spring)
|
||||
7. [REST API Discoverability and HATEOAS](http://www.baeldung.com/restful-web-service-discoverability)
|
||||
8.
|
||||
9. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring)
|
||||
10. [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
|
||||
|
||||
- [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring)
|
||||
- [Versioning a REST API](http://www.baeldung.com/rest-versioning)
|
||||
- [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring)
|
||||
- [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
- [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
|
||||
8. [REST Pagination in Spring](http://www.baeldung.com/rest-api-pagination-in-spring)
|
||||
9. [Test a REST API with Java](http://www.baeldung.com/integration-testing-a-rest-api)
|
||||
10. [HATEOAS for a Spring REST Service](http://www.baeldung.com/rest-api-discoverability-with-spring)
|
||||
11. [Versioning a REST API](http://www.baeldung.com/rest-versioning)
|
||||
12. [ETags for REST with Spring](http://www.baeldung.com/etags-for-rest-with-spring)
|
||||
13. [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
14. [Testing Web APIs with Postman Collections](https://www.baeldung.com/postman-testing-collections)
|
||||
|
Loading…
x
Reference in New Issue
Block a user