Skip to content

Commit 1570e42

Browse files
author
Dassi Orleando
committed
Merge branch 'master' of https://github.com/eugenp/tutorials
2 parents 5f3abae + 3459d3a commit 1570e42

107 files changed

Lines changed: 2932 additions & 715 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎activejdbc/pom.xml‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
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>com.baeldung</groupId>
5+
<artifactId>activejdbc</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<name>activejdbc</name>
9+
<url>http://maven.apache.org</url>
10+
<properties>
11+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
12+
<activejdbc.version>1.4.13</activejdbc.version>
13+
<environments>development.test,development</environments>
14+
</properties>
15+
<build>
16+
<plugins>
17+
<plugin>
18+
<groupId>org.apache.maven.plugins</groupId>
19+
<artifactId>maven-compiler-plugin</artifactId>
20+
<version>3.6.0</version>
21+
<configuration>
22+
<source>1.8</source>
23+
<target>1.8</target>
24+
<encoding>UTF-8</encoding>
25+
</configuration>
26+
</plugin>
27+
<plugin>
28+
<groupId>org.javalite</groupId>
29+
<artifactId>activejdbc-instrumentation</artifactId>
30+
<version>${activejdbc.version}</version>
31+
<executions>
32+
<execution>
33+
<phase>process-classes</phase>
34+
<goals>
35+
<goal>instrument</goal>
36+
</goals>
37+
</execution>
38+
</executions>
39+
</plugin>
40+
<plugin>
41+
<groupId>org.javalite</groupId>
42+
<artifactId>db-migrator-maven-plugin</artifactId>
43+
<version>${activejdbc.version}</version>
44+
<configuration>
45+
<configFile>${project.basedir}/src/main/resources/database.properties</configFile>
46+
<environments>${environments}</environments>
47+
</configuration>
48+
<dependencies>
49+
<dependency>
50+
<groupId>mysql</groupId>
51+
<artifactId>mysql-connector-java</artifactId>
52+
<version>5.1.34</version>
53+
</dependency>
54+
</dependencies>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-surefire-plugin</artifactId>
59+
<version>2.18.1</version>
60+
<configuration>
61+
<reportFormat>brief</reportFormat>
62+
<trimStackTrace>true</trimStackTrace>
63+
<useFile>false</useFile>
64+
<includes>
65+
<include>**/*Spec*.java</include>
66+
<include>**/*Test*.java</include>
67+
</includes>
68+
<excludes>
69+
<exclude>**/helpers/*</exclude>
70+
<exclude>**/*$*</exclude>
71+
</excludes>
72+
</configuration>
73+
</plugin>
74+
</plugins>
75+
</build>
76+
<dependencies>
77+
<dependency>
78+
<groupId>junit</groupId>
79+
<artifactId>junit</artifactId>
80+
<version>4.12</version>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.javalite</groupId>
85+
<artifactId>activejdbc</artifactId>
86+
<version>${activejdbc.version}</version>
87+
<exclusions>
88+
<exclusion>
89+
<groupId>opensymphony</groupId>
90+
<artifactId>oscache</artifactId>
91+
</exclusion>
92+
</exclusions>
93+
</dependency>
94+
<dependency>
95+
<groupId>mysql</groupId>
96+
<artifactId>mysql-connector-java</artifactId>
97+
<version>5.1.34</version>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.slf4j</groupId>
101+
<artifactId>slf4j-simple</artifactId>
102+
<version>1.7.9</version>
103+
</dependency>
104+
</dependencies>
105+
<repositories>
106+
<repository>
107+
<id>snapshots1</id>
108+
<name>JavaLite Snapshots1</name>
109+
<url>http://repo.javalite.io/</url>
110+
<snapshots>
111+
<enabled>true</enabled>
112+
<updatePolicy>always</updatePolicy>
113+
<checksumPolicy>warn</checksumPolicy>
114+
</snapshots>
115+
</repository>
116+
</repositories>
117+
<pluginRepositories>
118+
<pluginRepository>
119+
<id>snapshots2</id>
120+
<name>JavaLite Snapshots2</name>
121+
<url>http://repo.javalite.io/</url>
122+
<snapshots>
123+
<enabled>true</enabled>
124+
<updatePolicy>always</updatePolicy>
125+
<checksumPolicy>warn</checksumPolicy>
126+
</snapshots>
127+
</pluginRepository>
128+
</pluginRepositories>
129+
</project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung;
2+
3+
4+
import com.baeldung.model.Employee;
5+
import com.baeldung.model.Role;
6+
import org.javalite.activejdbc.Base;
7+
import org.javalite.activejdbc.LazyList;
8+
import org.javalite.activejdbc.Model;
9+
10+
public class ActiveJDBCApp
11+
{
12+
public static void main( String[] args )
13+
{
14+
try {
15+
Base.open();
16+
ActiveJDBCApp app = new ActiveJDBCApp();
17+
app.create();
18+
app.update();
19+
app.delete();
20+
app.deleteCascade();
21+
} catch (Exception e) {
22+
e.printStackTrace();
23+
} finally {
24+
Base.close();
25+
}
26+
}
27+
28+
protected void create() {
29+
Employee employee = new Employee("Hugo","C","M","BN");
30+
employee.saveIt();
31+
employee.add(new Role("Java Developer","BN"));
32+
LazyList<Model> all = Employee.findAll();
33+
System.out.println(all.size());
34+
}
35+
36+
protected void update() {
37+
Employee employee = Employee.findFirst("first_name = ?","Hugo");
38+
employee.set("last_namea","Choi").saveIt();
39+
employee = Employee.findFirst("last_name = ?","Choi");
40+
System.out.println(employee.getString("first_name") + " " + employee.getString("last_name"));
41+
}
42+
43+
protected void delete() {
44+
Employee employee = Employee.findFirst("first_name = ?","Hugo");
45+
employee.delete();
46+
employee = Employee.findFirst("last_name = ?","Choi");
47+
if(null == employee){
48+
System.out.println("No such Employee found!");
49+
}
50+
}
51+
52+
protected void deleteCascade() {
53+
create();
54+
Employee employee = Employee.findFirst("first_name = ?","Hugo");
55+
employee.deleteCascade();
56+
employee = Employee.findFirst("last_name = ?","C");
57+
if(null == employee){
58+
System.out.println("No such Employee found!");
59+
}
60+
}
61+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.model;
2+
3+
4+
import org.javalite.activejdbc.Model;
5+
6+
public class Employee extends Model {
7+
8+
public Employee(){
9+
10+
}
11+
12+
public Employee(String firstName, String lastName, String gender, String createdBy) {
13+
set("first_name1",firstName);
14+
set("last_name",lastName);
15+
set("gender",gender);
16+
set("created_by",createdBy);
17+
}
18+
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.model;
2+
3+
4+
import org.javalite.activejdbc.Model;
5+
import org.javalite.activejdbc.annotations.Table;
6+
7+
@Table("EMP_ROLES")
8+
public class Role extends Model {
9+
10+
public Role(){
11+
12+
}
13+
14+
public Role(String role,String createdBy){
15+
set("role_name",role);
16+
set("created_by",createdBy);
17+
}
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# noinspection SqlNoDataSourceInspectionForFile
2+
3+
create table organisation.employees
4+
(
5+
id int not null auto_increment
6+
primary key,
7+
first_name varchar(100) not null,
8+
last_name varchar(100) not null,
9+
gender varchar(1) not null,
10+
created_at datetime not null,
11+
updated_at datetime null,
12+
created_by varchar(100) not null,
13+
updated_by varchar(100) null
14+
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
15+
16+
create table organisation.emp_roles
17+
(
18+
id int not null auto_increment primary key,
19+
employee_id int not null,
20+
role_name varchar(100) not null,
21+
created_at datetime not null,
22+
updated_at datetime null,
23+
created_by varchar(100) not null,
24+
updated_by varchar(100) null
25+
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
development.driver=com.mysql.jdbc.Driver
2+
development.username=root
3+
development.password=123456
4+
development.url=jdbc:mysql://localhost/organisation
5+
6+
7+
development.test.driver=com.mysql.jdbc.Driver
8+
development.test.username=root
9+
development.test.password=123456
10+
development.test.url=jdbc:mysql://localhost/organisation_test
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung;
2+
3+
import com.baeldung.model.Employee;
4+
import com.baeldung.model.Role;
5+
import org.javalite.activejdbc.test.DBSpec;
6+
import org.junit.Test;
7+
8+
import java.util.List;
9+
10+
public class ActiveJDBCAppTest extends DBSpec
11+
{
12+
@Test
13+
public void ifEmployeeCreated_thenIsValid() {
14+
Employee employee = new Employee("B", "N", "M", "BN");
15+
the(employee).shouldBe("valid");
16+
}
17+
18+
@Test
19+
public void ifEmployeeCreatedWithRoles_thenShouldPersist() {
20+
Employee employee = new Employee("B", "N", "M", "BN");
21+
employee.saveIt();
22+
employee.add(new Role("Java Developer","BN"));
23+
employee.add(new Role("Lead Java Developer","BN"));
24+
a(Role.count()).shouldBeEqual(2);
25+
List<Role> roles = employee.getAll(Role.class).orderBy("created_at");
26+
the(roles.get(0).getRoleName()).shouldBeEqual("Java Developer");
27+
the(roles.get(1).getRoleName()).shouldBeEqual("Lead Java Developer");
28+
}
29+
30+
@Test
31+
public void ifEmployeeCreatedWithRoles_whenNameUpdated_thenShouldShowNewName() {
32+
Employee employee = new Employee("Binesh", "N", "M", "BN");
33+
employee.saveIt();
34+
employee.add(new Role("Java Developer","BN"));
35+
employee.add(new Role("Lead Java Developer","BN"));
36+
employee = Employee.findFirst("first_name = ?", "Binesh");
37+
employee.set("last_name","Narayanan").saveIt();
38+
Employee updated = Employee.findFirst("first_name = ?", "Binesh");
39+
the(updated.getLastName()).shouldBeEqual("Narayanan");
40+
}
41+
42+
@Test
43+
public void ifEmployeeCreatedWithRoles_whenDeleted_thenShouldNotBeFound() {
44+
Employee employee = new Employee("Binesh", "N", "M", "BN");
45+
employee.saveIt();
46+
employee.add(new Role("Java Developer","BN"));
47+
employee.delete();
48+
employee = Employee.findFirst("first_name = ?", "Binesh");
49+
the(employee).shouldBeNull();
50+
}
51+
}
Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,19 @@
11
package com.baeldung.spliteratorAPI;
22

3-
import java.util.Arrays;
43
import java.util.List;
5-
import java.util.Spliterator;
6-
import java.util.concurrent.ExecutorService;
7-
import java.util.concurrent.Executors;
84
import java.util.stream.Collectors;
9-
import java.util.stream.IntStream;
105
import java.util.stream.Stream;
11-
import java.util.stream.StreamSupport;
126

137
public class Executor {
14-
public void executeCustomSpliterator() {
15-
Article article = new Article(Arrays.asList(new Author("Ahmad", 0), new Author("Eugen", 0), new Author("Alice", 1), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1),
16-
new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0), new Author("Alice", 1), new Author("Mike", 0),
17-
new Author("Alice", 1), new Author("Mike", 0), new Author("Michał", 0), new Author("Loredana", 1)), 0);
18-
Stream<Author> stream = IntStream.range(0, article.getListOfAuthors()
19-
.size())
20-
.mapToObj(article.getListOfAuthors()::get);
21-
System.out.println("count= " + countAutors(stream.parallel()));
22-
Spliterator<Author> spliterator = new RelatedAuthorSpliterator(article.getListOfAuthors());
23-
Stream<Author> stream2 = StreamSupport.stream(spliterator, true);
24-
System.out.println("count= " + countAutors(stream2.parallel()));
25-
}
268

27-
public void executeSpliterator() {
28-
Spliterator<Article> split1 = generateElements().spliterator();
29-
Spliterator<Article> split2 = split1.trySplit();
30-
ExecutorService service = Executors.newCachedThreadPool();
31-
service.execute(new Task(split1));
32-
service.execute(new Task(split2));
33-
}
9+
public static int countAutors(Stream<Author> stream) {
10+
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true),
11+
RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
12+
return wordCounter.getCounter();
13+
}
3414

35-
private static int countAutors(Stream<Author> stream) {
36-
RelatedAuthorCounter wordCounter = stream.reduce(new RelatedAuthorCounter(0, true), RelatedAuthorCounter::accumulate, RelatedAuthorCounter::combine);
37-
return wordCounter.getCounter();
38-
}
15+
public static List<Article> generateElements() {
16+
return Stream.generate(() -> new Article("Java")).limit(35000).collect(Collectors.toList());
17+
}
3918

40-
private List<Article> generateElements() {
41-
return Stream.generate(() -> new Article("Java"))
42-
.limit(35000)
43-
.collect(Collectors.toList());
44-
}
45-
}
19+
}

0 commit comments

Comments
 (0)