Skip to content

Commit b2b5349

Browse files
asevans48adamd1985
authored andcommitted
Javaslang Fetched Upstream and Updated (eugenp#1368)
* javaslang updates * groovy * update and clean from upstream * upate from upstream and clean * update * cleanup * clean
1 parent dc142e8 commit b2b5349

8 files changed

Lines changed: 367 additions & 0 deletions

File tree

‎spring-data-javaslang/.gitignore‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/target/
2+
/project/
3+
.idea
4+
.classpath
5+
.eclipse

‎spring-data-javaslang/pom.xml‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>spring-data-javaslangb</groupId>
5+
<artifactId>spring-data-javaslangb</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<properties>
8+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
9+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
10+
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
11+
<groovy-eclipse-compiler.version>2.9.1-01</groovy-eclipse-compiler.version>
12+
<groovy-eclipse-batch.version>2.3.7-01</groovy-eclipse-batch.version>
13+
<org.codehouse.groovy.version>2.4.8</org.codehouse.groovy.version>
14+
<javaslang.version>2.0.5</javaslang.version>
15+
<junit.version>4.12</junit.version>
16+
<spring-boot.version>1.5.1.RELEASE</spring-boot.version>
17+
<spring-context.version>4.3.6.RELEASE</spring-context.version>
18+
<spring-core.version>4.3.6.RELEASE</spring-core.version>
19+
<spring-test.version>4.3.6.RELEASE</spring-test.version>
20+
<project.build.testSourceDirectory>${basedir}/src/test/java</project.build.testSourceDirectory>
21+
</properties>
22+
<parent>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-parent</artifactId>
25+
<version>1.5.1.RELEASE</version>
26+
</parent>
27+
<profiles>
28+
<profile>
29+
<id>test-app</id>
30+
<build>
31+
<defaultGoal>verify</defaultGoal>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-maven-plugin</artifactId>
36+
<executions>
37+
<execution>
38+
<id>spring-boot-run</id>
39+
<phase>verify</phase>
40+
<goals>
41+
<goal>run</goal>
42+
</goals>
43+
<inherited>false</inherited>
44+
</execution>
45+
</executions>
46+
</plugin>
47+
</plugins>
48+
</build>
49+
</profile>
50+
</profiles>
51+
<build>
52+
<plugins>
53+
<plugin>
54+
<artifactId>maven-compiler-plugin</artifactId>
55+
<version>3.5.1</version>
56+
<configuration>
57+
<source>1.8</source>
58+
<target>1.8</target>
59+
</configuration>
60+
</plugin>
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-surefire-plugin</artifactId>
64+
<version>${maven-surefire-plugin.version}</version>
65+
<configuration>
66+
<testSourceDirectory>${project.build.testSourceDirectory}</testSourceDirectory>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
<dependencies>
72+
<dependency>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-devtools</artifactId>
75+
<optional>true</optional>
76+
</dependency>
77+
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
78+
<dependency>
79+
<groupId>com.h2database</groupId>
80+
<artifactId>h2</artifactId>
81+
<version>1.4.193</version>
82+
</dependency>
83+
<!-- https://mvnrepository.com/artifact/io.javaslang/javaslang -->
84+
<dependency>
85+
<groupId>io.javaslang</groupId>
86+
<artifactId>javaslang</artifactId>
87+
<version>${javaslang.version}</version>
88+
</dependency>
89+
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
90+
<dependency>
91+
<groupId>org.springframework.data</groupId>
92+
<artifactId>spring-data-jpa</artifactId>
93+
<version>1.11.0.RELEASE</version>
94+
</dependency>
95+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot -->
96+
<dependency>
97+
<groupId>org.springframework.boot</groupId>
98+
<artifactId>spring-boot</artifactId>
99+
<version>${spring-boot.version}</version>
100+
</dependency>
101+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
102+
<dependency>
103+
<groupId>org.springframework.boot</groupId>
104+
<artifactId>spring-boot-starter-data-jpa</artifactId>
105+
<version>${spring-boot.version}</version>
106+
</dependency>
107+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
108+
<dependency>
109+
<groupId>org.springframework.boot</groupId>
110+
<artifactId>spring-boot-starter-test</artifactId>
111+
<version>${spring-boot.version}</version>
112+
</dependency>
113+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
114+
<dependency>
115+
<groupId>org.springframework</groupId>
116+
<artifactId>spring-context</artifactId>
117+
<version>${spring-context.version}</version>
118+
</dependency>
119+
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
120+
<dependency>
121+
<groupId>org.springframework</groupId>
122+
<artifactId>spring-core</artifactId>
123+
<version>${spring-core.version}</version>
124+
</dependency>
125+
<!-- https://mvnrepository.com/artifact/junit/junit -->
126+
<dependency>
127+
<groupId>junit</groupId>
128+
<artifactId>junit</artifactId>
129+
<version>${junit.version}</version>
130+
<scope>test</scope>
131+
</dependency>
132+
</dependencies>
133+
</project>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.spring_data.model;
2+
3+
import javaslang.collection.Seq;
4+
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
import javax.persistence.Table;
8+
import javax.persistence.CollectionTable;
9+
import javax.persistence.Column;
10+
import javax.persistence.ElementCollection;
11+
import javax.persistence.Entity;
12+
13+
@Entity
14+
@Table(name = "book")
15+
public class Book {
16+
17+
@GeneratedValue
18+
@Id
19+
private Long id;
20+
21+
private String title;
22+
23+
private Seq<String> authors;
24+
25+
26+
public void setTitle(String title){
27+
this.title = title;
28+
}
29+
30+
public String getTitle(){
31+
return this.title;
32+
}
33+
34+
public Long getId(){
35+
return this.id;
36+
}
37+
38+
public void setAuthors(Seq authors){
39+
this.authors = authors;
40+
}
41+
42+
public Seq getAuthors(){
43+
return this.authors;
44+
}
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.spring_data.model;
2+
3+
import javax.persistence.ElementCollection;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
import javax.persistence.Table;
8+
9+
import java.util.List;
10+
11+
@Entity
12+
@Table(name = "java_book")
13+
public class JavaBook {
14+
15+
@GeneratedValue
16+
@Id
17+
private Long id;
18+
19+
private String title;
20+
21+
@ElementCollection
22+
private List<String> authors;
23+
24+
25+
public void setAuthors(List<String> authors){
26+
this.authors = authors;
27+
}
28+
29+
public void setTitle(String title){
30+
this.title = title;
31+
}
32+
33+
public String getTitle(){
34+
return this.title;
35+
}
36+
37+
public Long getId(){
38+
return this.id;
39+
}
40+
}
41+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.spring_data.repository;
2+
3+
import com.baeldung.spring_data.model.Book;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
import javaslang.collection.Seq;
9+
import javaslang.control.Option;
10+
11+
@Repository
12+
public interface BookRepository extends JpaRepository<Book,Long>{
13+
Book save(Book book);
14+
15+
Option<Book> findById(Long id);
16+
17+
Option<Seq<Book>> findByTitleContaining(String title);
18+
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.spring_data.repository;
2+
3+
import com.baeldung.spring_data.model.JavaBook;
4+
5+
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.stereotype.Repository;
7+
8+
import java.util.List;
9+
10+
@Repository
11+
public interface JavaBookRepository extends JpaRepository<JavaBook,Long>{
12+
JavaBook save(JavaBook book);
13+
14+
JavaBook findById(Long id);
15+
16+
List<JavaBook> findByTitleContaining(String title);
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.spring_data_app;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.autoconfigure.domain.EntityScan;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
10+
import org.springframework.transaction.annotation.EnableTransactionManagement;
11+
12+
@Configuration
13+
@EnableJpaRepositories("com.baeldung.spring_data.repository")
14+
@EnableTransactionManagement
15+
@EntityScan("com.baeldung.spring_data.model")
16+
@SpringBootApplication
17+
public class MainApp {
18+
public static void main(String[] args){
19+
SpringApplication.run(MainApp.class, args);
20+
}
21+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.spring_data_tests;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
6+
import com.baeldung.spring_data_app.MainApp;
7+
import com.baeldung.spring_data.model.Book;
8+
import com.baeldung.spring_data.model.JavaBook;
9+
import com.baeldung.spring_data.repository.BookRepository;
10+
import com.baeldung.spring_data.repository.JavaBookRepository;
11+
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
15+
import org.springframework.test.context.junit4.SpringRunner;
16+
17+
import javaslang.collection.Seq;
18+
import javaslang.collection.List;
19+
import javaslang.control.Option;
20+
21+
import java.util.ArrayList;
22+
23+
@RunWith(SpringRunner.class)
24+
@SpringBootTest(classes = MainApp.class,webEnvironment = WebEnvironment.NONE)
25+
public class SpringTests {
26+
27+
@Autowired
28+
JavaBookRepository javaRepository;
29+
30+
@Autowired
31+
BookRepository repository;
32+
33+
@Test
34+
public void should_return_seq(){
35+
Seq authors = List.of("author1","author2");
36+
Book testBook = new Book();
37+
testBook.setTitle("Javaslang in Spring Data Seq Test Return");
38+
testBook.setAuthors(authors);
39+
Book book = repository.save(testBook);
40+
Option<Seq<Book>> books = repository.findByTitleContaining("Seq Test");
41+
assert(!books.isEmpty());
42+
}
43+
44+
45+
@Test
46+
public void should_return_option_with_book(){
47+
Seq authors = List.of("author1","author2");
48+
Book testBook = new Book();
49+
testBook.setTitle("Javaslang in Spring Data");
50+
testBook.setAuthors(authors);
51+
Book book = repository.save(testBook);
52+
Option<Book> retBook = repository.findById(1L);
53+
assert(retBook.isDefined() && !retBook.isEmpty());
54+
assert(retBook.get() != null);
55+
}
56+
57+
@Test
58+
public void should_return_list(){
59+
ArrayList<String> authors = new ArrayList<String>();
60+
authors.add("author1");
61+
authors.add("author2");
62+
JavaBook testBook = new JavaBook();
63+
testBook.setTitle("Javaslang in Spring Data Seq Return");
64+
testBook.setAuthors(authors);
65+
JavaBook book = javaRepository.save(testBook);
66+
java.util.List<JavaBook> books = javaRepository.findByTitleContaining("Seq");
67+
assert(!books.isEmpty());
68+
assert(books.size() == 1);
69+
assert(books.get(0).getTitle().equals("Javaslang in Spring Data Seq Return"));
70+
}
71+
72+
@Test
73+
public void should_return_book(){
74+
ArrayList<String> authors = new ArrayList<String>();
75+
authors.add("author1");
76+
authors.add("author2");
77+
JavaBook testBook = new JavaBook();
78+
testBook.setTitle("Javaslang in Spring Data");
79+
testBook.setAuthors(authors);
80+
JavaBook book = javaRepository.save(testBook);
81+
JavaBook retBook = javaRepository.findById(1L);
82+
assert(retBook != null);
83+
assert(retBook.getId() == 1L);
84+
assert(retBook.getTitle().contains("Data"));
85+
}
86+
}

0 commit comments

Comments
 (0)