11package com .baeldung .jooq .introduction ;
22
3- import static org .junit .Assert .assertEquals ;
4-
5- import org .junit .Test ;
6- import org .junit .runner .RunWith ;
7-
3+ import com .baeldung .jooq .introduction .db .public_ .tables .Author ;
4+ import com .baeldung .jooq .introduction .db .public_ .tables .AuthorBook ;
5+ import com .baeldung .jooq .introduction .db .public_ .tables .Book ;
86import org .jooq .DSLContext ;
97import org .jooq .Record3 ;
108import org .jooq .Result ;
119import org .jooq .impl .DSL ;
12-
10+ import org .junit .Test ;
11+ import org .junit .runner .RunWith ;
1312import org .springframework .beans .factory .annotation .Autowired ;
1413import org .springframework .dao .DataAccessException ;
1514import org .springframework .test .context .ContextConfiguration ;
1615import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
1716import org .springframework .transaction .annotation .Transactional ;
1817
19- import com .baeldung .jooq .introduction .db .public_ .tables .Author ;
20- import com .baeldung .jooq .introduction .db .public_ .tables .AuthorBook ;
21- import com .baeldung .jooq .introduction .db .public_ .tables .Book ;
18+ import static org .junit .Assert .assertEquals ;
2219
2320@ ContextConfiguration (classes = PersistenceContext .class )
2421@ Transactional (transactionManager = "transactionManager" )
2522@ RunWith (SpringJUnit4ClassRunner .class )
2623public class QueryTest {
24+
2725 @ Autowired
28- private DSLContext create ;
26+ private DSLContext dsl ;
2927
3028 Author author = Author .AUTHOR ;
3129 Book book = Book .BOOK ;
3230 AuthorBook authorBook = AuthorBook .AUTHOR_BOOK ;
3331
3432 @ Test
3533 public void givenValidData_whenInserting_thenSucceed () {
36- create .insertInto (author ).set (author .ID , 4 ).set (author .FIRST_NAME , "Herbert" ).set (author .LAST_NAME , "Schildt" ).execute ();
37- create .insertInto (book ).set (book .ID , 4 ).set (book .TITLE , "A Beginner's Guide" ).execute ();
38- create .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 4 ).execute ();
39- Result <Record3 <Integer , String , Integer >> result = create .select (author .ID , author .LAST_NAME , DSL .count ()).from (author ).join (authorBook ).on (author .ID .equal (authorBook .AUTHOR_ID )).join (book ).on (authorBook .BOOK_ID .equal (book .ID ))
34+ dsl .insertInto (author ).set (author .ID , 4 ).set (author .FIRST_NAME , "Herbert" ).set (author .LAST_NAME , "Schildt" ).execute ();
35+ dsl .insertInto (book ).set (book .ID , 4 ).set (book .TITLE , "A Beginner's Guide" ).execute ();
36+ dsl .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 4 ).execute ();
37+ Result <Record3 <Integer , String , Integer >> result = dsl .select (author .ID , author .LAST_NAME , DSL .count ()).from (author ).join (authorBook ).on (author .ID .equal (authorBook .AUTHOR_ID )).join (book ).on (authorBook .BOOK_ID .equal (book .ID ))
4038 .groupBy (author .LAST_NAME ).fetch ();
4139
4240 assertEquals (3 , result .size ());
@@ -48,15 +46,15 @@ public void givenValidData_whenInserting_thenSucceed() {
4846
4947 @ Test (expected = DataAccessException .class )
5048 public void givenInvalidData_whenInserting_thenFail () {
51- create .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 5 ).execute ();
49+ dsl .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 5 ).execute ();
5250 }
5351
5452 @ Test
5553 public void givenValidData_whenUpdating_thenSucceed () {
56- create .update (author ).set (author .LAST_NAME , "Baeldung" ).where (author .ID .equal (3 )).execute ();
57- create .update (book ).set (book .TITLE , "Building your REST API with Spring" ).where (book .ID .equal (3 )).execute ();
58- create .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 3 ).set (authorBook .BOOK_ID , 3 ).execute ();
59- Result <Record3 <Integer , String , String >> result = create .select (author .ID , author .LAST_NAME , book .TITLE ).from (author ).join (authorBook ).on (author .ID .equal (authorBook .AUTHOR_ID )).join (book ).on (authorBook .BOOK_ID .equal (book .ID )).where (author .ID .equal (3 ))
54+ dsl .update (author ).set (author .LAST_NAME , "Baeldung" ).where (author .ID .equal (3 )).execute ();
55+ dsl .update (book ).set (book .TITLE , "Building your REST API with Spring" ).where (book .ID .equal (3 )).execute ();
56+ dsl .insertInto (authorBook ).set (authorBook .AUTHOR_ID , 3 ).set (authorBook .BOOK_ID , 3 ).execute ();
57+ Result <Record3 <Integer , String , String >> result = dsl .select (author .ID , author .LAST_NAME , book .TITLE ).from (author ).join (authorBook ).on (author .ID .equal (authorBook .AUTHOR_ID )).join (book ).on (authorBook .BOOK_ID .equal (book .ID )).where (author .ID .equal (3 ))
6058 .fetch ();
6159
6260 assertEquals (1 , result .size ());
@@ -67,13 +65,13 @@ public void givenValidData_whenUpdating_thenSucceed() {
6765
6866 @ Test (expected = DataAccessException .class )
6967 public void givenInvalidData_whenUpdating_thenFail () {
70- create .update (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 5 ).execute ();
68+ dsl .update (authorBook ).set (authorBook .AUTHOR_ID , 4 ).set (authorBook .BOOK_ID , 5 ).execute ();
7169 }
7270
7371 @ Test
7472 public void givenValidData_whenDeleting_thenSucceed () {
75- create .delete (author ).where (author .ID .lt (3 )).execute ();
76- Result <Record3 <Integer , String , String >> result = create .select (author .ID , author .FIRST_NAME , author .LAST_NAME ).from (author ).fetch ();
73+ dsl .delete (author ).where (author .ID .lt (3 )).execute ();
74+ Result <Record3 <Integer , String , String >> result = dsl .select (author .ID , author .FIRST_NAME , author .LAST_NAME ).from (author ).fetch ();
7775
7876 assertEquals (1 , result .size ());
7977 assertEquals ("Bryan" , result .getValue (0 , author .FIRST_NAME ));
@@ -82,6 +80,6 @@ public void givenValidData_whenDeleting_thenSucceed() {
8280
8381 @ Test (expected = DataAccessException .class )
8482 public void givenInvalidData_whenDeleting_thenFail () {
85- create .delete (book ).where (book .ID .equal (1 )).execute ();
83+ dsl .delete (book ).where (book .ID .equal (1 )).execute ();
8684 }
8785}
0 commit comments