Skip to content

Commit 7b618af

Browse files
committed
Merge remote-tracking branch 'eugenp/master'
2 parents 691f929 + 75803f1 commit 7b618af

16 files changed

Lines changed: 633 additions & 0 deletions

File tree

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
package com.baeldung.apachecayenne;
2+
3+
import com.baeldung.apachecayenne.persistent.Author;
4+
import org.apache.cayenne.ObjectContext;
5+
import org.apache.cayenne.QueryResponse;
6+
import org.apache.cayenne.configuration.server.ServerRuntime;
7+
import org.apache.cayenne.exp.Expression;
8+
import org.apache.cayenne.exp.ExpressionFactory;
9+
import org.apache.cayenne.query.*;
10+
import org.junit.After;
11+
import org.junit.Before;
12+
import org.junit.BeforeClass;
13+
import org.junit.Test;
14+
15+
import java.util.Arrays;
16+
import java.util.Collections;
17+
import java.util.List;
18+
19+
import static junit.framework.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
22+
public class CayenneAdvancedOperationTests {
23+
private static ObjectContext context = null;
24+
25+
@BeforeClass
26+
public static void setupTheCayenneContext() {
27+
ServerRuntime cayenneRuntime = ServerRuntime.builder()
28+
.addConfig("cayenne-project.xml")
29+
.build();
30+
context = cayenneRuntime.newContext();
31+
}
32+
33+
@Before
34+
public void saveThreeAuthors() {
35+
Author authorOne = context.newObject(Author.class);
36+
authorOne.setName("Paul Xavier");
37+
Author authorTwo = context.newObject(Author.class);
38+
authorTwo.setName("pAuL Smith");
39+
Author authorThree = context.newObject(Author.class);
40+
authorThree.setName("Vicky Sarra");
41+
context.commitChanges();
42+
}
43+
44+
@After
45+
public void deleteAllAuthors() {
46+
SQLTemplate deleteAuthors = new SQLTemplate(Author.class, "delete from author");
47+
context.performGenericQuery(deleteAuthors);
48+
}
49+
50+
@Test
51+
public void givenAuthors_whenFindAllSQLTmplt_thenWeGetThreeAuthors() {
52+
SQLTemplate select = new SQLTemplate(Author.class, "select * from Author");
53+
List<Author> authors = context.performQuery(select);
54+
55+
assertEquals(authors.size(), 3);
56+
}
57+
58+
@Test
59+
public void givenAuthors_whenFindByNameSQLTmplt_thenWeGetOneAuthor() {
60+
SQLTemplate select = new SQLTemplate(Author.class, "select * from Author where name = 'Vicky Sarra'");
61+
List<Author> authors = context.performQuery(select);
62+
Author author = authors.get(0);
63+
64+
assertEquals(authors.size(), 1);
65+
assertEquals(author.getName(), "Vicky Sarra");
66+
}
67+
68+
@Test
69+
public void givenAuthors_whenLikeSltQry_thenWeGetOneAuthor() {
70+
Expression qualifier = ExpressionFactory.likeExp(Author.NAME.getName(), "Paul%");
71+
SelectQuery query = new SelectQuery(Author.class, qualifier);
72+
List<Author> authorsTwo = context.performQuery(query);
73+
74+
assertEquals(authorsTwo.size(), 1);
75+
}
76+
77+
@Test
78+
public void givenAuthors_whenCtnsIgnorCaseSltQry_thenWeGetTwoAuthors() {
79+
Expression qualifier = ExpressionFactory.containsIgnoreCaseExp(Author.NAME.getName(), "Paul");
80+
SelectQuery query = new SelectQuery(Author.class, qualifier);
81+
List<Author> authors = context.performQuery(query);
82+
83+
assertEquals(authors.size(), 2);
84+
}
85+
86+
@Test
87+
public void givenAuthors_whenCtnsIgnorCaseEndsWSltQry_thenWeGetTwoAuthors() {
88+
Expression qualifier = ExpressionFactory.containsIgnoreCaseExp(Author.NAME.getName(), "Paul")
89+
.andExp(ExpressionFactory.endsWithExp(Author.NAME.getName(), "h"));
90+
SelectQuery query = new SelectQuery(Author.class, qualifier);
91+
List<Author> authors = context.performQuery(query);
92+
93+
Author author = authors.get(0);
94+
95+
assertEquals(authors.size(), 1);
96+
assertEquals(author.getName(), "pAuL Smith");
97+
}
98+
99+
@Test
100+
public void givenAuthors_whenAscOrderingSltQry_thenWeGetOrderedAuthors() {
101+
SelectQuery query = new SelectQuery(Author.class);
102+
query.addOrdering(Author.NAME.asc());
103+
104+
List<Author> authors = query.select(context);
105+
Author firstAuthor = authors.get(0);
106+
107+
assertEquals(authors.size(), 3);
108+
assertEquals(firstAuthor.getName(), "Paul Xavier");
109+
}
110+
111+
@Test
112+
public void givenAuthors_whenDescOrderingSltQry_thenWeGetOrderedAuthors() {
113+
SelectQuery query = new SelectQuery(Author.class);
114+
query.addOrdering(Author.NAME.desc());
115+
116+
List<Author> authors = query.select(context);
117+
Author firstAuthor = authors.get(0);
118+
119+
assertEquals(authors.size(), 3);
120+
assertEquals(firstAuthor.getName(), "pAuL Smith");
121+
}
122+
123+
@Test
124+
public void givenAuthors_onContainsObjS_thenWeGetOneRecord() {
125+
List<Author> authors = ObjectSelect.query(Author.class)
126+
.where(Author.NAME.contains("Paul"))
127+
.select(context);
128+
129+
assertEquals(authors.size(), 1);
130+
}
131+
132+
@Test
133+
public void givenAuthors_whenLikeObjS_thenWeGetTwoAuthors() {
134+
List<Author> authors = ObjectSelect.query(Author.class)
135+
.where(Author.NAME.likeIgnoreCase("Paul%"))
136+
.select(context);
137+
138+
assertEquals(authors.size(), 2);
139+
}
140+
141+
@Test
142+
public void givenTwoAuthor_whenEndsWithObjS_thenWeGetOrderedAuthors() {
143+
List<Author> authors = ObjectSelect.query(Author.class)
144+
.where(Author.NAME.endsWith("Sarra"))
145+
.select(context);
146+
Author firstAuthor = authors.get(0);
147+
148+
assertEquals(authors.size(), 1);
149+
assertEquals(firstAuthor.getName(), "Vicky Sarra");
150+
}
151+
152+
@Test
153+
public void givenTwoAuthor_whenInObjS_thenWeGetAuthors() {
154+
String [] args = {"Paul Xavier", "pAuL Smith", "Vicky Sarra"};
155+
List<Author> authors = ObjectSelect.query(Author.class)
156+
.where(Author.NAME.in(Arrays.asList(args)))
157+
.select(context);
158+
159+
assertEquals(authors.size(), 3);
160+
}
161+
162+
@Test
163+
public void givenTwoAuthor_whenNinObjS_thenWeGetAuthors() {
164+
String [] args = {"Paul Xavier", "pAuL Smith"};
165+
List<Author> authors = ObjectSelect.query(Author.class)
166+
.where(Author.NAME.nin(Arrays.asList(args)))
167+
.select(context);
168+
Author author = authors.get(0);
169+
170+
assertEquals(authors.size(), 1);
171+
assertEquals(author.getName(), "Vicky Sarra");
172+
}
173+
174+
@Test
175+
public void givenTwoAuthor_whenIsNotNullObjS_thenWeGetAuthors() {
176+
List<Author> authors = ObjectSelect.query(Author.class)
177+
.where(Author.NAME.isNotNull())
178+
.select(context);
179+
180+
assertEquals(authors.size(), 3);
181+
}
182+
183+
@Test
184+
public void givenAuthors_whenFindAllEJBQL_thenWeGetThreeAuthors() {
185+
EJBQLQuery query = new EJBQLQuery("select a FROM Author a");
186+
List<Author> authors = context.performQuery(query);
187+
188+
assertEquals(authors.size(), 3);
189+
}
190+
191+
@Test
192+
public void givenAuthors_whenFindByNameEJBQL_thenWeGetOneAuthor() {
193+
EJBQLQuery query = new EJBQLQuery("select a FROM Author a WHERE a.name = 'Vicky Sarra'");
194+
List<Author> authors = context.performQuery(query);
195+
Author author = authors.get(0);
196+
197+
assertEquals(authors.size(), 1);
198+
assertEquals(author.getName(), "Vicky Sarra");
199+
}
200+
201+
@Test
202+
public void givenAuthors_whenUpdadingByNameEJBQL_thenWeGetTheUpdatedAuthor() {
203+
EJBQLQuery query = new EJBQLQuery("UPDATE Author AS a SET a.name = 'Vicky Edison' WHERE a.name = 'Vicky Sarra'");
204+
QueryResponse queryResponse = context.performGenericQuery(query);
205+
206+
EJBQLQuery queryUpdatedAuthor = new EJBQLQuery("select a FROM Author a WHERE a.name = 'Vicky Edison'");
207+
List<Author> authors = context.performQuery(queryUpdatedAuthor);
208+
Author author = authors.get(0);
209+
210+
assertNotNull(author);
211+
}
212+
213+
@Test
214+
public void givenAuthors_whenSeletingNamesEJBQL_thenWeGetListWithSizeThree() {
215+
String [] args = {"Paul Xavier", "pAuL Smith", "Vicky Sarra"};
216+
List<String> names = Arrays.asList(args);
217+
EJBQLQuery query = new EJBQLQuery("select a.name FROM Author a");
218+
List<String> nameList = context.performQuery(query);
219+
220+
Collections.sort(names);
221+
Collections.sort(nameList);
222+
223+
assertEquals(names.size(), 3);
224+
assertEquals(nameList.size(), 3);
225+
assertEquals(names, nameList);
226+
}
227+
228+
@Test
229+
public void givenAuthors_whenDeletingAllWithEJB_thenWeGetNoAuthor() {
230+
EJBQLQuery deleteQuery = new EJBQLQuery("delete FROM Author");
231+
EJBQLQuery findAllQuery = new EJBQLQuery("select a FROM Author a");
232+
233+
context.performQuery(deleteQuery);
234+
List<Author> objects = context.performQuery(findAllQuery);
235+
236+
assertEquals(objects.size(), 0);
237+
}
238+
239+
@Test
240+
public void givenAuthors_whenInsertingSQLExec_thenWeGetNewAuthor() {
241+
int inserted = SQLExec
242+
.query("INSERT INTO Author (name) VALUES ('Baeldung')")
243+
.update(context);
244+
245+
assertEquals(inserted, 1);
246+
}
247+
248+
@Test
249+
public void givenAuthors_whenUpdatingSQLExec_thenItsUpdated() {
250+
int updated = SQLExec
251+
.query("UPDATE Author SET name = 'Baeldung' WHERE name = 'Vicky Sarra'")
252+
.update(context);
253+
254+
assertEquals(updated, 1);
255+
}
256+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.baeldung;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.OptionalInt;
6+
7+
import javax.validation.constraints.Min;
8+
import javax.validation.constraints.NotBlank;
9+
import javax.validation.constraints.PositiveOrZero;
10+
11+
public class Customer {
12+
13+
@NotBlank(message="Name cannot be empty")
14+
private String name;
15+
16+
private List<@NotBlank(message="Address must not be blank") String> addresses;
17+
18+
private Integer age;
19+
20+
@PositiveOrZero
21+
private OptionalInt numberOfOrders;
22+
23+
//@NotBlank
24+
private Profile profile;
25+
26+
public String getName() {
27+
return name;
28+
}
29+
30+
public void setName(String name) {
31+
this.name = name;
32+
}
33+
34+
public List<String> getAddresses() {
35+
return addresses;
36+
}
37+
38+
public void setAddresses(List<String> addresses) {
39+
this.addresses = addresses;
40+
}
41+
42+
public Optional<@Min(18) Integer> getAge() {
43+
return Optional.ofNullable(age);
44+
}
45+
46+
public void setAge(Integer age) {
47+
this.age = age;
48+
}
49+
50+
public OptionalInt getNumberOfOrders() {
51+
return numberOfOrders;
52+
}
53+
54+
public void setNumberOfOrders(OptionalInt numberOfOrders) {
55+
this.numberOfOrders = numberOfOrders;
56+
}
57+
58+
public Profile getProfile() {
59+
return profile;
60+
}
61+
62+
public void setProfile(Profile profile) {
63+
this.profile = profile;
64+
}
65+
66+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung;
2+
3+
import java.util.Map;
4+
5+
import javax.validation.constraints.Email;
6+
import javax.validation.constraints.NotNull;
7+
8+
public class CustomerMap {
9+
10+
private Map<@Email(message="Must be a valid email") String, @NotNull Customer> customers;
11+
12+
public Map<String, Customer> getCustomers() {
13+
return customers;
14+
}
15+
16+
public void setCustomers(Map<String, Customer> customers) {
17+
this.customers = customers;
18+
}
19+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.baeldung;
2+
3+
public class Profile {
4+
private String companyName;
5+
6+
public String getCompanyName() {
7+
return companyName;
8+
}
9+
10+
public void setCompanyName(String companyName) {
11+
this.companyName = companyName;
12+
}
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.valueextractors;
2+
3+
import javax.validation.valueextraction.ExtractedValue;
4+
import javax.validation.valueextraction.UnwrapByDefault;
5+
import javax.validation.valueextraction.ValueExtractor;
6+
7+
import org.baeldung.Profile;
8+
9+
@UnwrapByDefault
10+
public class ProfileValueExtractor implements ValueExtractor<@ExtractedValue(type = String.class) Profile> {
11+
12+
@Override
13+
public void extractValues(Profile originalValue, ValueExtractor.ValueReceiver receiver) {
14+
receiver.value(null, originalValue.getCompanyName());
15+
}
16+
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.baeldung.valueextractors.ProfileValueExtractor

0 commit comments

Comments
 (0)