|
| 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 | +} |
0 commit comments