You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SQLContainerTest.java 106KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467
  1. package com.vaadin.v7.data.util.sqlcontainer;
  2. import static org.junit.Assert.assertArrayEquals;
  3. import static org.junit.Assert.assertEquals;
  4. import static org.junit.Assert.assertFalse;
  5. import static org.junit.Assert.assertNotNull;
  6. import static org.junit.Assert.assertNotSame;
  7. import static org.junit.Assert.assertNull;
  8. import static org.junit.Assert.assertSame;
  9. import static org.junit.Assert.assertTrue;
  10. import static org.junit.Assert.fail;
  11. import java.math.BigDecimal;
  12. import java.sql.Connection;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.List;
  18. import java.util.logging.Handler;
  19. import java.util.logging.LogRecord;
  20. import java.util.logging.Logger;
  21. import org.easymock.EasyMock;
  22. import org.easymock.IAnswer;
  23. import org.junit.After;
  24. import org.junit.Before;
  25. import org.junit.Test;
  26. import com.vaadin.v7.data.Container.Filter;
  27. import com.vaadin.v7.data.Container.ItemSetChangeEvent;
  28. import com.vaadin.v7.data.Container.ItemSetChangeListener;
  29. import com.vaadin.v7.data.Item;
  30. import com.vaadin.v7.data.util.filter.Compare.Equal;
  31. import com.vaadin.v7.data.util.filter.Like;
  32. import com.vaadin.v7.data.util.sqlcontainer.SQLTestsConstants.DB;
  33. import com.vaadin.v7.data.util.sqlcontainer.connection.JDBCConnectionPool;
  34. import com.vaadin.v7.data.util.sqlcontainer.query.FreeformQuery;
  35. import com.vaadin.v7.data.util.sqlcontainer.query.FreeformQueryDelegate;
  36. import com.vaadin.v7.data.util.sqlcontainer.query.FreeformStatementDelegate;
  37. import com.vaadin.v7.data.util.sqlcontainer.query.OrderBy;
  38. import com.vaadin.v7.data.util.sqlcontainer.query.ValidatingSimpleJDBCConnectionPool;
  39. import com.vaadin.v7.data.util.sqlcontainer.query.generator.MSSQLGenerator;
  40. import com.vaadin.v7.data.util.sqlcontainer.query.generator.OracleGenerator;
  41. import com.vaadin.v7.data.util.sqlcontainer.query.generator.SQLGenerator;
  42. import com.vaadin.v7.data.util.sqlcontainer.query.generator.StatementHelper;
  43. import com.vaadin.v7.data.util.sqlcontainer.query.generator.filter.QueryBuilder;
  44. public class SQLContainerTest {
  45. private static final int offset = SQLTestsConstants.offset;
  46. private JDBCConnectionPool connectionPool;
  47. @Before
  48. public void setUp() throws SQLException {
  49. try {
  50. connectionPool = new ValidatingSimpleJDBCConnectionPool(
  51. SQLTestsConstants.dbDriver, SQLTestsConstants.dbURL,
  52. SQLTestsConstants.dbUser, SQLTestsConstants.dbPwd, 2, 2);
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. fail(e.getMessage());
  56. }
  57. DataGenerator.addPeopleToDatabase(connectionPool);
  58. }
  59. @After
  60. public void tearDown() {
  61. if (connectionPool != null) {
  62. connectionPool.destroy();
  63. }
  64. }
  65. @Test
  66. public void constructor_withFreeformQuery_shouldSucceed()
  67. throws SQLException {
  68. new SQLContainer(new FreeformQuery("SELECT * FROM people",
  69. connectionPool, "ID"));
  70. }
  71. @Test(expected = SQLException.class)
  72. public void constructor_withIllegalFreeformQuery_shouldFail()
  73. throws SQLException {
  74. SQLContainer c = new SQLContainer(
  75. new FreeformQuery("SELECT * FROM asdf", connectionPool, "ID"));
  76. c.getItem(c.firstItemId());
  77. }
  78. @Test
  79. public void containsId_withFreeformQueryAndExistingId_returnsTrue()
  80. throws SQLException {
  81. SQLContainer container = new SQLContainer(new FreeformQuery(
  82. "SELECT * FROM people", connectionPool, "ID"));
  83. assertTrue(container.containsId(new RowId(new Object[] { 1 })));
  84. }
  85. @Test
  86. public void containsId_withFreeformQueryAndNonexistingId_returnsFalse()
  87. throws SQLException {
  88. SQLContainer container = new SQLContainer(new FreeformQuery(
  89. "SELECT * FROM people", connectionPool, "ID"));
  90. assertFalse(container.containsId(new RowId(new Object[] { 1337 })));
  91. }
  92. @Test
  93. public void getContainerProperty_freeformExistingItemIdAndPropertyId_returnsProperty()
  94. throws SQLException {
  95. SQLContainer container = new SQLContainer(new FreeformQuery(
  96. "SELECT * FROM people", connectionPool, "ID"));
  97. if (SQLTestsConstants.db == DB.ORACLE) {
  98. assertEquals("Ville",
  99. container.getContainerProperty(new RowId(
  100. new Object[] { new BigDecimal(0 + offset) }),
  101. "NAME").getValue());
  102. } else {
  103. assertEquals("Ville",
  104. container.getContainerProperty(
  105. new RowId(new Object[] { 0 + offset }), "NAME")
  106. .getValue());
  107. }
  108. }
  109. @Test
  110. public void getContainerProperty_freeformExistingItemIdAndNonexistingPropertyId_returnsNull()
  111. throws SQLException {
  112. SQLContainer container = new SQLContainer(new FreeformQuery(
  113. "SELECT * FROM people", connectionPool, "ID"));
  114. assertNull(container.getContainerProperty(
  115. new RowId(new Object[] { 1 + offset }), "asdf"));
  116. }
  117. @Test
  118. public void getContainerProperty_freeformNonexistingItemId_returnsNull()
  119. throws SQLException {
  120. SQLContainer container = new SQLContainer(new FreeformQuery(
  121. "SELECT * FROM people", connectionPool, "ID"));
  122. assertNull(container.getContainerProperty(
  123. new RowId(new Object[] { 1337 + offset }), "NAME"));
  124. }
  125. @Test
  126. public void getContainerPropertyIds_freeform_returnsIDAndNAME()
  127. throws SQLException {
  128. SQLContainer container = new SQLContainer(new FreeformQuery(
  129. "SELECT * FROM people", connectionPool, "ID"));
  130. Collection<?> propertyIds = container.getContainerPropertyIds();
  131. assertEquals(3, propertyIds.size());
  132. assertArrayEquals(new String[] { "ID", "NAME", "AGE" },
  133. propertyIds.toArray());
  134. }
  135. @Test
  136. public void getItem_freeformExistingItemId_returnsItem()
  137. throws SQLException {
  138. SQLContainer container = new SQLContainer(new FreeformQuery(
  139. "SELECT * FROM people", connectionPool, "ID"));
  140. Item item;
  141. if (SQLTestsConstants.db == DB.ORACLE) {
  142. item = container.getItem(
  143. new RowId(new Object[] { new BigDecimal(0 + offset) }));
  144. } else {
  145. item = container.getItem(new RowId(new Object[] { 0 + offset }));
  146. }
  147. assertNotNull(item);
  148. assertEquals("Ville", item.getItemProperty("NAME").getValue());
  149. }
  150. @Test
  151. public void nextItemNullAtEnd_freeformExistingItem() throws SQLException {
  152. SQLContainer container = new SQLContainer(new FreeformQuery(
  153. "SELECT * FROM people", connectionPool, "ID"));
  154. Object lastItemId = container.lastItemId();
  155. Object afterLast = container.nextItemId(lastItemId);
  156. assertNull(afterLast);
  157. }
  158. @Test
  159. public void prevItemNullAtStart_freeformExistingItem() throws SQLException {
  160. SQLContainer container = new SQLContainer(new FreeformQuery(
  161. "SELECT * FROM people", connectionPool, "ID"));
  162. Object firstItemId = container.firstItemId();
  163. Object beforeFirst = container.prevItemId(firstItemId);
  164. assertNull(beforeFirst);
  165. }
  166. @Test
  167. public void getItem_freeform5000RowsWithParameter1337_returnsItemWithId1337()
  168. throws SQLException {
  169. DataGenerator.addFiveThousandPeople(connectionPool);
  170. SQLContainer container = new SQLContainer(new FreeformQuery(
  171. "SELECT * FROM people", connectionPool, "ID"));
  172. Item item;
  173. if (SQLTestsConstants.db == DB.ORACLE) {
  174. item = container.getItem(
  175. new RowId(new Object[] { new BigDecimal(1337 + offset) }));
  176. assertNotNull(item);
  177. assertEquals(new BigDecimal(1337 + offset),
  178. item.getItemProperty("ID").getValue());
  179. } else {
  180. item = container.getItem(new RowId(new Object[] { 1337 + offset }));
  181. assertNotNull(item);
  182. assertEquals(1337 + offset, item.getItemProperty("ID").getValue());
  183. }
  184. assertEquals("Person 1337", item.getItemProperty("NAME").getValue());
  185. }
  186. @Test
  187. public void getItemIds_freeform_returnsItemIdsWithKeys0through3()
  188. throws SQLException {
  189. SQLContainer container = new SQLContainer(new FreeformQuery(
  190. "SELECT * FROM people", connectionPool, "ID"));
  191. Collection<?> itemIds = container.getItemIds();
  192. assertEquals(4, itemIds.size());
  193. RowId zero = new RowId(new Object[] { 0 + offset });
  194. RowId one = new RowId(new Object[] { 1 + offset });
  195. RowId two = new RowId(new Object[] { 2 + offset });
  196. RowId three = new RowId(new Object[] { 3 + offset });
  197. if (SQLTestsConstants.db == DB.ORACLE) {
  198. String[] correct = { "1", "2", "3", "4" };
  199. List<String> oracle = new ArrayList<String>();
  200. for (Object o : itemIds) {
  201. oracle.add(o.toString());
  202. }
  203. assertArrayEquals(correct, oracle.toArray());
  204. } else {
  205. assertArrayEquals(new Object[] { zero, one, two, three },
  206. itemIds.toArray());
  207. }
  208. }
  209. @Test
  210. public void getType_freeformNAMEPropertyId_returnsString()
  211. throws SQLException {
  212. SQLContainer container = new SQLContainer(new FreeformQuery(
  213. "SELECT * FROM people", connectionPool, "ID"));
  214. assertEquals(String.class, container.getType("NAME"));
  215. }
  216. @Test
  217. public void getType_freeformIDPropertyId_returnsInteger()
  218. throws SQLException {
  219. SQLContainer container = new SQLContainer(new FreeformQuery(
  220. "SELECT * FROM people", connectionPool, "ID"));
  221. if (SQLTestsConstants.db == DB.ORACLE) {
  222. assertEquals(BigDecimal.class, container.getType("ID"));
  223. } else {
  224. assertEquals(Integer.class, container.getType("ID"));
  225. }
  226. }
  227. @Test
  228. public void getType_freeformNonexistingPropertyId_returnsNull()
  229. throws SQLException {
  230. SQLContainer container = new SQLContainer(new FreeformQuery(
  231. "SELECT * FROM people", connectionPool, "ID"));
  232. assertNull(container.getType("asdf"));
  233. }
  234. @Test
  235. public void size_freeform_returnsFour() throws SQLException {
  236. SQLContainer container = new SQLContainer(new FreeformQuery(
  237. "SELECT * FROM people", connectionPool, "ID"));
  238. assertEquals(4, container.size());
  239. }
  240. @Test
  241. public void size_freeformOneAddedItem_returnsFive() throws SQLException {
  242. Connection conn = connectionPool.reserveConnection();
  243. Statement statement = conn.createStatement();
  244. if (SQLTestsConstants.db == DB.MSSQL) {
  245. statement.executeUpdate("insert into people values('Bengt', '42')");
  246. } else {
  247. statement.executeUpdate(
  248. "insert into people values(default, 'Bengt', '42')");
  249. }
  250. statement.close();
  251. conn.commit();
  252. connectionPool.releaseConnection(conn);
  253. SQLContainer container = new SQLContainer(new FreeformQuery(
  254. "SELECT * FROM people", connectionPool, "ID"));
  255. assertEquals(5, container.size());
  256. }
  257. @Test
  258. public void indexOfId_freeformWithParameterThree_returnsThree()
  259. throws SQLException {
  260. SQLContainer container = new SQLContainer(new FreeformQuery(
  261. "SELECT * FROM people", connectionPool, "ID"));
  262. if (SQLTestsConstants.db == DB.ORACLE) {
  263. assertEquals(3, container.indexOfId(
  264. new RowId(new Object[] { new BigDecimal(3 + offset) })));
  265. } else {
  266. assertEquals(3, container
  267. .indexOfId(new RowId(new Object[] { 3 + offset })));
  268. }
  269. }
  270. @Test
  271. public void indexOfId_freeform5000RowsWithParameter1337_returns1337()
  272. throws SQLException {
  273. DataGenerator.addFiveThousandPeople(connectionPool);
  274. SQLContainer container = new SQLContainer(
  275. new FreeformQuery("SELECT * FROM people ORDER BY \"ID\" ASC",
  276. connectionPool, "ID"));
  277. if (SQLTestsConstants.db == DB.ORACLE) {
  278. container.getItem(
  279. new RowId(new Object[] { new BigDecimal(1337 + offset) }));
  280. assertEquals(1337, container.indexOfId(
  281. new RowId(new Object[] { new BigDecimal(1337 + offset) })));
  282. } else {
  283. container.getItem(new RowId(new Object[] { 1337 + offset }));
  284. assertEquals(1337, container
  285. .indexOfId(new RowId(new Object[] { 1337 + offset })));
  286. }
  287. }
  288. @Test
  289. public void getIdByIndex_freeform5000rowsIndex1337_returnsRowId1337()
  290. throws SQLException {
  291. DataGenerator.addFiveThousandPeople(connectionPool);
  292. SQLContainer container = new SQLContainer(
  293. new FreeformQuery("SELECT * FROM people ORDER BY \"ID\" ASC",
  294. connectionPool, "ID"));
  295. Object itemId = container.getIdByIndex(1337);
  296. if (SQLTestsConstants.db == DB.ORACLE) {
  297. assertEquals(
  298. new RowId(new Object[] { new BigDecimal(1337 + offset) }),
  299. itemId);
  300. } else {
  301. assertEquals(new RowId(new Object[] { 1337 + offset }), itemId);
  302. }
  303. }
  304. @SuppressWarnings("unchecked")
  305. @Test
  306. public void getIdByIndex_freeformWithPaging5000rowsIndex1337_returnsRowId1337()
  307. throws SQLException {
  308. DataGenerator.addFiveThousandPeople(connectionPool);
  309. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  310. connectionPool, "ID");
  311. FreeformQueryDelegate delegate = EasyMock
  312. .createMock(FreeformQueryDelegate.class);
  313. EasyMock.expect(
  314. delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt()))
  315. .andAnswer(new IAnswer<String>() {
  316. @Override
  317. public String answer() throws Throwable {
  318. Object[] args = EasyMock.getCurrentArguments();
  319. int offset = (Integer) (args[0]);
  320. int limit = (Integer) (args[1]);
  321. if (SQLTestsConstants.db == DB.MSSQL) {
  322. int start = offset + 1;
  323. int end = offset + limit + 1;
  324. String q = "SELECT * FROM (SELECT row_number() OVER"
  325. + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)"
  326. + " AS a WHERE a.rownum BETWEEN " + start
  327. + " AND " + end;
  328. return q;
  329. } else if (SQLTestsConstants.db == DB.ORACLE) {
  330. int start = offset + 1;
  331. int end = offset + limit + 1;
  332. String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM"
  333. + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) "
  334. + " WHERE r BETWEEN " + start + " AND "
  335. + end;
  336. return q;
  337. } else {
  338. return "SELECT * FROM people LIMIT " + limit
  339. + " OFFSET " + offset;
  340. }
  341. }
  342. }).anyTimes();
  343. delegate.setFilters(null);
  344. EasyMock.expectLastCall().anyTimes();
  345. delegate.setFilters(EasyMock.isA(List.class));
  346. EasyMock.expectLastCall().anyTimes();
  347. delegate.setOrderBy(null);
  348. EasyMock.expectLastCall().anyTimes();
  349. delegate.setOrderBy(EasyMock.isA(List.class));
  350. EasyMock.expectLastCall().anyTimes();
  351. EasyMock.expect(delegate.getCountQuery())
  352. .andThrow(new UnsupportedOperationException()).anyTimes();
  353. EasyMock.replay(delegate);
  354. query.setDelegate(delegate);
  355. SQLContainer container = new SQLContainer(query);
  356. Object itemId = container.getIdByIndex(1337);
  357. if (SQLTestsConstants.db == DB.ORACLE) {
  358. assertEquals(new RowId(new Object[] { 1337 + offset }).toString(),
  359. itemId.toString());
  360. } else {
  361. assertEquals(new RowId(new Object[] { 1337 + offset }), itemId);
  362. }
  363. }
  364. @Test
  365. public void nextItemId_freeformCurrentItem1337_returnsItem1338()
  366. throws SQLException {
  367. DataGenerator.addFiveThousandPeople(connectionPool);
  368. SQLContainer container = new SQLContainer(
  369. new FreeformQuery("SELECT * FROM people ORDER BY \"ID\" ASC",
  370. connectionPool, "ID"));
  371. Object itemId = container.getIdByIndex(1337);
  372. if (SQLTestsConstants.db == DB.ORACLE) {
  373. assertEquals(new RowId(new Object[] { 1338 + offset }).toString(),
  374. container.nextItemId(itemId).toString());
  375. } else {
  376. assertEquals(new RowId(new Object[] { 1338 + offset }),
  377. container.nextItemId(itemId));
  378. }
  379. }
  380. @Test
  381. public void prevItemId_freeformCurrentItem1337_returns1336()
  382. throws SQLException {
  383. DataGenerator.addFiveThousandPeople(connectionPool);
  384. SQLContainer container = new SQLContainer(
  385. new FreeformQuery("SELECT * FROM people ORDER BY \"ID\" ASC",
  386. connectionPool, "ID"));
  387. Object itemId = container.getIdByIndex(1337);
  388. if (SQLTestsConstants.db == DB.ORACLE) {
  389. assertEquals(new RowId(new Object[] { 1336 + offset }).toString(),
  390. container.prevItemId(itemId).toString());
  391. } else {
  392. assertEquals(new RowId(new Object[] { 1336 + offset }),
  393. container.prevItemId(itemId));
  394. }
  395. }
  396. @Test
  397. public void firstItemId_freeform_returnsItemId0() throws SQLException {
  398. SQLContainer container = new SQLContainer(new FreeformQuery(
  399. "SELECT * FROM people", connectionPool, "ID"));
  400. if (SQLTestsConstants.db == DB.ORACLE) {
  401. assertEquals(new RowId(new Object[] { 0 + offset }).toString(),
  402. container.firstItemId().toString());
  403. } else {
  404. assertEquals(new RowId(new Object[] { 0 + offset }),
  405. container.firstItemId());
  406. }
  407. }
  408. @Test
  409. public void lastItemId_freeform5000Rows_returnsItemId4999()
  410. throws SQLException {
  411. DataGenerator.addFiveThousandPeople(connectionPool);
  412. SQLContainer container = new SQLContainer(
  413. new FreeformQuery("SELECT * FROM people ORDER BY \"ID\" ASC",
  414. connectionPool, "ID"));
  415. if (SQLTestsConstants.db == DB.ORACLE) {
  416. assertEquals(new RowId(new Object[] { 4999 + offset }).toString(),
  417. container.lastItemId().toString());
  418. } else {
  419. assertEquals(new RowId(new Object[] { 4999 + offset }),
  420. container.lastItemId());
  421. }
  422. }
  423. @Test
  424. public void isFirstId_freeformActualFirstId_returnsTrue()
  425. throws SQLException {
  426. SQLContainer container = new SQLContainer(new FreeformQuery(
  427. "SELECT * FROM people", connectionPool, "ID"));
  428. if (SQLTestsConstants.db == DB.ORACLE) {
  429. assertTrue(container.isFirstId(
  430. new RowId(new Object[] { new BigDecimal(0 + offset) })));
  431. } else {
  432. assertTrue(container
  433. .isFirstId(new RowId(new Object[] { 0 + offset })));
  434. }
  435. }
  436. @Test
  437. public void isFirstId_freeformSecondId_returnsFalse() throws SQLException {
  438. SQLContainer container = new SQLContainer(new FreeformQuery(
  439. "SELECT * FROM people", connectionPool, "ID"));
  440. if (SQLTestsConstants.db == DB.ORACLE) {
  441. assertFalse(container.isFirstId(
  442. new RowId(new Object[] { new BigDecimal(1 + offset) })));
  443. } else {
  444. assertFalse(container
  445. .isFirstId(new RowId(new Object[] { 1 + offset })));
  446. }
  447. }
  448. @Test
  449. public void isLastId_freeformSecondId_returnsFalse() throws SQLException {
  450. SQLContainer container = new SQLContainer(new FreeformQuery(
  451. "SELECT * FROM people", connectionPool, "ID"));
  452. if (SQLTestsConstants.db == DB.ORACLE) {
  453. assertFalse(container.isLastId(
  454. new RowId(new Object[] { new BigDecimal(1 + offset) })));
  455. } else {
  456. assertFalse(
  457. container.isLastId(new RowId(new Object[] { 1 + offset })));
  458. }
  459. }
  460. @Test
  461. public void isLastId_freeformLastId_returnsTrue() throws SQLException {
  462. SQLContainer container = new SQLContainer(new FreeformQuery(
  463. "SELECT * FROM people", connectionPool, "ID"));
  464. if (SQLTestsConstants.db == DB.ORACLE) {
  465. assertTrue(container.isLastId(
  466. new RowId(new Object[] { new BigDecimal(3 + offset) })));
  467. } else {
  468. assertTrue(
  469. container.isLastId(new RowId(new Object[] { 3 + offset })));
  470. }
  471. }
  472. @Test
  473. public void isLastId_freeform5000RowsLastId_returnsTrue()
  474. throws SQLException {
  475. DataGenerator.addFiveThousandPeople(connectionPool);
  476. SQLContainer container = new SQLContainer(
  477. new FreeformQuery("SELECT * FROM people ORDER BY \"ID\" ASC",
  478. connectionPool, "ID"));
  479. if (SQLTestsConstants.db == DB.ORACLE) {
  480. assertTrue(container.isLastId(
  481. new RowId(new Object[] { new BigDecimal(4999 + offset) })));
  482. } else {
  483. assertTrue(container
  484. .isLastId(new RowId(new Object[] { 4999 + offset })));
  485. }
  486. }
  487. @Test
  488. public void refresh_freeform_sizeShouldUpdate() throws SQLException {
  489. SQLContainer container = new SQLContainer(new FreeformQuery(
  490. "SELECT * FROM people", connectionPool, "ID"));
  491. assertEquals(4, container.size());
  492. DataGenerator.addFiveThousandPeople(connectionPool);
  493. container.refresh();
  494. assertEquals(5000, container.size());
  495. }
  496. @Test
  497. public void refresh_freeformWithoutCallingRefresh_sizeShouldNotUpdate()
  498. throws SQLException {
  499. // Yeah, this is a weird one. We're testing that the size doesn't update
  500. // after adding lots of items unless we call refresh inbetween. This to
  501. // make sure that the refresh method actually refreshes stuff and isn't
  502. // a NOP.
  503. SQLContainer container = new SQLContainer(new FreeformQuery(
  504. "SELECT * FROM people", connectionPool, "ID"));
  505. assertEquals(4, container.size());
  506. DataGenerator.addFiveThousandPeople(connectionPool);
  507. assertEquals(4, container.size());
  508. }
  509. @Test
  510. public void setAutoCommit_freeform_shouldSucceed() throws SQLException {
  511. SQLContainer container = new SQLContainer(new FreeformQuery(
  512. "SELECT * FROM people", connectionPool, "ID"));
  513. container.setAutoCommit(true);
  514. assertTrue(container.isAutoCommit());
  515. container.setAutoCommit(false);
  516. assertFalse(container.isAutoCommit());
  517. }
  518. @Test
  519. public void getPageLength_freeform_returnsDefault100() throws SQLException {
  520. SQLContainer container = new SQLContainer(new FreeformQuery(
  521. "SELECT * FROM people", connectionPool, "ID"));
  522. assertEquals(100, container.getPageLength());
  523. }
  524. @Test
  525. public void setPageLength_freeform_shouldSucceed() throws SQLException {
  526. SQLContainer container = new SQLContainer(new FreeformQuery(
  527. "SELECT * FROM people", connectionPool, "ID"));
  528. container.setPageLength(20);
  529. assertEquals(20, container.getPageLength());
  530. container.setPageLength(200);
  531. assertEquals(200, container.getPageLength());
  532. }
  533. @Test(expected = UnsupportedOperationException.class)
  534. public void addContainerProperty_normal_isUnsupported()
  535. throws SQLException {
  536. SQLContainer container = new SQLContainer(new FreeformQuery(
  537. "SELECT * FROM people", connectionPool, "ID"));
  538. container.addContainerProperty("asdf", String.class, "");
  539. }
  540. @Test(expected = UnsupportedOperationException.class)
  541. public void removeContainerProperty_normal_isUnsupported()
  542. throws SQLException {
  543. SQLContainer container = new SQLContainer(new FreeformQuery(
  544. "SELECT * FROM people", connectionPool, "ID"));
  545. container.removeContainerProperty("asdf");
  546. }
  547. @Test(expected = UnsupportedOperationException.class)
  548. public void addItemObject_normal_isUnsupported() throws SQLException {
  549. SQLContainer container = new SQLContainer(new FreeformQuery(
  550. "SELECT * FROM people", connectionPool, "ID"));
  551. container.addItem("asdf");
  552. }
  553. @Test(expected = UnsupportedOperationException.class)
  554. public void addItemAfterObjectObject_normal_isUnsupported()
  555. throws SQLException {
  556. SQLContainer container = new SQLContainer(new FreeformQuery(
  557. "SELECT * FROM people", connectionPool, "ID"));
  558. container.addItemAfter("asdf", "foo");
  559. }
  560. @Test(expected = UnsupportedOperationException.class)
  561. public void addItemAtIntObject_normal_isUnsupported() throws SQLException {
  562. SQLContainer container = new SQLContainer(new FreeformQuery(
  563. "SELECT * FROM people", connectionPool, "ID"));
  564. container.addItemAt(2, "asdf");
  565. }
  566. @Test(expected = UnsupportedOperationException.class)
  567. public void addItemAtInt_normal_isUnsupported() throws SQLException {
  568. SQLContainer container = new SQLContainer(new FreeformQuery(
  569. "SELECT * FROM people", connectionPool, "ID"));
  570. container.addItemAt(2);
  571. }
  572. @Test(expected = UnsupportedOperationException.class)
  573. public void addItemAfterObject_normal_isUnsupported() throws SQLException {
  574. SQLContainer container = new SQLContainer(new FreeformQuery(
  575. "SELECT * FROM people", connectionPool, "ID"));
  576. container.addItemAfter("asdf");
  577. }
  578. @Test
  579. public void addItem_freeformAddOneNewItem_returnsItemId()
  580. throws SQLException {
  581. SQLContainer container = new SQLContainer(new FreeformQuery(
  582. "SELECT * FROM people", connectionPool, "ID"));
  583. Object itemId = container.addItem();
  584. assertNotNull(itemId);
  585. }
  586. @Test
  587. public void addItem_freeformAddOneNewItem_shouldChangeSize()
  588. throws SQLException {
  589. SQLContainer container = new SQLContainer(new FreeformQuery(
  590. "SELECT * FROM people", connectionPool, "ID"));
  591. int size = container.size();
  592. container.addItem();
  593. assertEquals(size + 1, container.size());
  594. }
  595. @Test
  596. public void addItem_freeformAddTwoNewItems_shouldChangeSize()
  597. throws SQLException {
  598. SQLContainer container = new SQLContainer(new FreeformQuery(
  599. "SELECT * FROM people", connectionPool, "ID"));
  600. int size = container.size();
  601. Object id1 = container.addItem();
  602. Object id2 = container.addItem();
  603. assertEquals(size + 2, container.size());
  604. assertNotSame(id1, id2);
  605. assertFalse(id1.equals(id2));
  606. }
  607. @Test
  608. public void nextItemId_freeformNewlyAddedItem_returnsNewlyAdded()
  609. throws SQLException {
  610. SQLContainer container = new SQLContainer(new FreeformQuery(
  611. "SELECT * FROM people", connectionPool, "ID"));
  612. Object lastId = container.lastItemId();
  613. Object id = container.addItem();
  614. assertEquals(id, container.nextItemId(lastId));
  615. }
  616. @Test
  617. public void lastItemId_freeformNewlyAddedItem_returnsNewlyAdded()
  618. throws SQLException {
  619. SQLContainer container = new SQLContainer(new FreeformQuery(
  620. "SELECT * FROM people", connectionPool, "ID"));
  621. Object lastId = container.lastItemId();
  622. Object id = container.addItem();
  623. assertEquals(id, container.lastItemId());
  624. assertNotSame(lastId, container.lastItemId());
  625. }
  626. @Test
  627. public void indexOfId_freeformNewlyAddedItem_returnsFour()
  628. throws SQLException {
  629. SQLContainer container = new SQLContainer(new FreeformQuery(
  630. "SELECT * FROM people", connectionPool, "ID"));
  631. Object id = container.addItem();
  632. assertEquals(4, container.indexOfId(id));
  633. }
  634. @Test
  635. public void getItem_freeformNewlyAddedItem_returnsNewlyAdded()
  636. throws SQLException {
  637. SQLContainer container = new SQLContainer(new FreeformQuery(
  638. "SELECT * FROM people", connectionPool, "ID"));
  639. Object id = container.addItem();
  640. assertNotNull(container.getItem(id));
  641. }
  642. @Test
  643. public void getItem_freeformNewlyAddedItemAndFiltered_returnsNull()
  644. throws SQLException {
  645. SQLContainer container = new SQLContainer(new FreeformQuery(
  646. "SELECT * FROM people", connectionPool, "ID"));
  647. container.addContainerFilter(new Equal("NAME", "asdf"));
  648. Object id = container.addItem();
  649. assertNull(container.getItem(id));
  650. }
  651. @Test
  652. public void getItemUnfiltered_freeformNewlyAddedItemAndFiltered_returnsNewlyAdded()
  653. throws SQLException {
  654. SQLContainer container = new SQLContainer(new FreeformQuery(
  655. "SELECT * FROM people", connectionPool, "ID"));
  656. container.addContainerFilter(new Equal("NAME", "asdf"));
  657. Object id = container.addItem();
  658. assertNotNull(container.getItemUnfiltered(id));
  659. }
  660. @Test
  661. public void getItemIds_freeformNewlyAddedItem_containsNewlyAdded()
  662. throws SQLException {
  663. SQLContainer container = new SQLContainer(new FreeformQuery(
  664. "SELECT * FROM people", connectionPool, "ID"));
  665. Object id = container.addItem();
  666. assertTrue(container.getItemIds().contains(id));
  667. }
  668. @Test
  669. public void getContainerProperty_freeformNewlyAddedItem_returnsPropertyOfNewlyAddedItem()
  670. throws SQLException {
  671. SQLContainer container = new SQLContainer(new FreeformQuery(
  672. "SELECT * FROM people", connectionPool, "ID"));
  673. Object id = container.addItem();
  674. Item item = container.getItem(id);
  675. item.getItemProperty("NAME").setValue("asdf");
  676. assertEquals("asdf",
  677. container.getContainerProperty(id, "NAME").getValue());
  678. }
  679. @Test
  680. public void containsId_freeformNewlyAddedItem_returnsTrue()
  681. throws SQLException {
  682. SQLContainer container = new SQLContainer(new FreeformQuery(
  683. "SELECT * FROM people", connectionPool, "ID"));
  684. Object id = container.addItem();
  685. assertTrue(container.containsId(id));
  686. }
  687. @Test
  688. public void prevItemId_freeformTwoNewlyAddedItems_returnsFirstAddedItem()
  689. throws SQLException {
  690. SQLContainer container = new SQLContainer(new FreeformQuery(
  691. "SELECT * FROM people", connectionPool, "ID"));
  692. Object id1 = container.addItem();
  693. Object id2 = container.addItem();
  694. assertEquals(id1, container.prevItemId(id2));
  695. }
  696. @Test
  697. public void firstItemId_freeformEmptyResultSet_returnsFirstAddedItem()
  698. throws SQLException {
  699. DataGenerator.createGarbage(connectionPool);
  700. SQLContainer container = new SQLContainer(new FreeformQuery(
  701. "SELECT * FROM GARBAGE", connectionPool, "ID"));
  702. Object id = container.addItem();
  703. assertSame(id, container.firstItemId());
  704. }
  705. @Test
  706. public void isFirstId_freeformEmptyResultSet_returnsFirstAddedItem()
  707. throws SQLException {
  708. DataGenerator.createGarbage(connectionPool);
  709. SQLContainer container = new SQLContainer(new FreeformQuery(
  710. "SELECT * FROM GARBAGE", connectionPool, "ID"));
  711. Object id = container.addItem();
  712. assertTrue(container.isFirstId(id));
  713. }
  714. @Test
  715. public void isLastId_freeformOneItemAdded_returnsTrueForAddedItem()
  716. throws SQLException {
  717. SQLContainer container = new SQLContainer(new FreeformQuery(
  718. "SELECT * FROM people", connectionPool, "ID"));
  719. Object id = container.addItem();
  720. assertTrue(container.isLastId(id));
  721. }
  722. @Test
  723. public void isLastId_freeformTwoItemsAdded_returnsTrueForLastAddedItem()
  724. throws SQLException {
  725. SQLContainer container = new SQLContainer(new FreeformQuery(
  726. "SELECT * FROM people", connectionPool, "ID"));
  727. container.addItem();
  728. Object id2 = container.addItem();
  729. assertTrue(container.isLastId(id2));
  730. }
  731. @Test
  732. public void getIdByIndex_freeformOneItemAddedLastIndexInContainer_returnsAddedItem()
  733. throws SQLException {
  734. SQLContainer container = new SQLContainer(new FreeformQuery(
  735. "SELECT * FROM people", connectionPool, "ID"));
  736. Object id = container.addItem();
  737. assertEquals(id, container.getIdByIndex(container.size() - 1));
  738. }
  739. @Test
  740. public void removeItem_freeformNoAddedItems_removesItemFromContainer()
  741. throws SQLException {
  742. SQLContainer container = new SQLContainer(new FreeformQuery(
  743. "SELECT * FROM people", connectionPool, "ID"));
  744. int size = container.size();
  745. Object id = container.firstItemId();
  746. assertTrue(container.removeItem(id));
  747. assertNotSame(id, container.firstItemId());
  748. assertEquals(size - 1, container.size());
  749. }
  750. @Test
  751. public void containsId_freeformRemovedItem_returnsFalse()
  752. throws SQLException {
  753. SQLContainer container = new SQLContainer(new FreeformQuery(
  754. "SELECT * FROM people", connectionPool, "ID"));
  755. Object id = container.firstItemId();
  756. assertTrue(container.removeItem(id));
  757. assertFalse(container.containsId(id));
  758. }
  759. @Test
  760. public void containsId_unknownObject() throws SQLException {
  761. Handler ensureNoLogging = new Handler() {
  762. @Override
  763. public void publish(LogRecord record) {
  764. fail("No messages should be logged");
  765. }
  766. @Override
  767. public void flush() {
  768. }
  769. @Override
  770. public void close() throws SecurityException {
  771. }
  772. };
  773. SQLContainer container = new SQLContainer(new FreeformQuery(
  774. "SELECT * FROM people", connectionPool, "ID"));
  775. Logger logger = Logger.getLogger(SQLContainer.class.getName());
  776. logger.addHandler(ensureNoLogging);
  777. try {
  778. assertFalse(container.containsId(new Object()));
  779. } finally {
  780. logger.removeHandler(ensureNoLogging);
  781. }
  782. }
  783. @Test
  784. public void removeItem_freeformOneAddedItem_removesTheAddedItem()
  785. throws SQLException {
  786. SQLContainer container = new SQLContainer(new FreeformQuery(
  787. "SELECT * FROM people", connectionPool, "ID"));
  788. Object id = container.addItem();
  789. int size = container.size();
  790. assertTrue(container.removeItem(id));
  791. assertFalse(container.containsId(id));
  792. assertEquals(size - 1, container.size());
  793. }
  794. @Test
  795. public void getItem_freeformItemRemoved_returnsNull() throws SQLException {
  796. SQLContainer container = new SQLContainer(new FreeformQuery(
  797. "SELECT * FROM people", connectionPool, "ID"));
  798. Object id = container.firstItemId();
  799. assertTrue(container.removeItem(id));
  800. assertNull(container.getItem(id));
  801. }
  802. @Test
  803. public void getItem_freeformAddedItemRemoved_returnsNull()
  804. throws SQLException {
  805. SQLContainer container = new SQLContainer(new FreeformQuery(
  806. "SELECT * FROM people", connectionPool, "ID"));
  807. Object id = container.addItem();
  808. assertNotNull(container.getItem(id));
  809. assertTrue(container.removeItem(id));
  810. assertNull(container.getItem(id));
  811. }
  812. @Test
  813. public void getItemIds_freeformItemRemoved_shouldNotContainRemovedItem()
  814. throws SQLException {
  815. SQLContainer container = new SQLContainer(new FreeformQuery(
  816. "SELECT * FROM people", connectionPool, "ID"));
  817. Object id = container.firstItemId();
  818. assertTrue(container.getItemIds().contains(id));
  819. assertTrue(container.removeItem(id));
  820. assertFalse(container.getItemIds().contains(id));
  821. }
  822. @Test
  823. public void getItemIds_freeformAddedItemRemoved_shouldNotContainRemovedItem()
  824. throws SQLException {
  825. SQLContainer container = new SQLContainer(new FreeformQuery(
  826. "SELECT * FROM people", connectionPool, "ID"));
  827. Object id = container.addItem();
  828. assertTrue(container.getItemIds().contains(id));
  829. assertTrue(container.removeItem(id));
  830. assertFalse(container.getItemIds().contains(id));
  831. }
  832. @Test
  833. public void containsId_freeformItemRemoved_returnsFalse()
  834. throws SQLException {
  835. SQLContainer container = new SQLContainer(new FreeformQuery(
  836. "SELECT * FROM people", connectionPool, "ID"));
  837. Object id = container.firstItemId();
  838. assertTrue(container.containsId(id));
  839. assertTrue(container.removeItem(id));
  840. assertFalse(container.containsId(id));
  841. }
  842. @Test
  843. public void containsId_freeformAddedItemRemoved_returnsFalse()
  844. throws SQLException {
  845. SQLContainer container = new SQLContainer(new FreeformQuery(
  846. "SELECT * FROM people", connectionPool, "ID"));
  847. Object id = container.addItem();
  848. assertTrue(container.containsId(id));
  849. assertTrue(container.removeItem(id));
  850. assertFalse(container.containsId(id));
  851. }
  852. @Test
  853. public void nextItemId_freeformItemRemoved_skipsRemovedItem()
  854. throws SQLException {
  855. SQLContainer container = new SQLContainer(new FreeformQuery(
  856. "SELECT * FROM people", connectionPool, "ID"));
  857. Object first = container.getIdByIndex(0);
  858. Object second = container.getIdByIndex(1);
  859. Object third = container.getIdByIndex(2);
  860. assertTrue(container.removeItem(second));
  861. assertEquals(third, container.nextItemId(first));
  862. }
  863. @Test
  864. public void nextItemId_freeformAddedItemRemoved_skipsRemovedItem()
  865. throws SQLException {
  866. SQLContainer container = new SQLContainer(new FreeformQuery(
  867. "SELECT * FROM people", connectionPool, "ID"));
  868. Object first = container.lastItemId();
  869. Object second = container.addItem();
  870. Object third = container.addItem();
  871. assertTrue(container.removeItem(second));
  872. assertEquals(third, container.nextItemId(first));
  873. }
  874. @Test
  875. public void prevItemId_freeformItemRemoved_skipsRemovedItem()
  876. throws SQLException {
  877. SQLContainer container = new SQLContainer(new FreeformQuery(
  878. "SELECT * FROM people", connectionPool, "ID"));
  879. Object first = container.getIdByIndex(0);
  880. Object second = container.getIdByIndex(1);
  881. Object third = container.getIdByIndex(2);
  882. assertTrue(container.removeItem(second));
  883. assertEquals(first, container.prevItemId(third));
  884. }
  885. @Test
  886. public void prevItemId_freeformAddedItemRemoved_skipsRemovedItem()
  887. throws SQLException {
  888. SQLContainer container = new SQLContainer(new FreeformQuery(
  889. "SELECT * FROM people", connectionPool, "ID"));
  890. Object first = container.lastItemId();
  891. Object second = container.addItem();
  892. Object third = container.addItem();
  893. assertTrue(container.removeItem(second));
  894. assertEquals(first, container.prevItemId(third));
  895. }
  896. @Test
  897. public void firstItemId_freeformFirstItemRemoved_resultChanges()
  898. throws SQLException {
  899. SQLContainer container = new SQLContainer(new FreeformQuery(
  900. "SELECT * FROM people", connectionPool, "ID"));
  901. Object first = container.firstItemId();
  902. assertTrue(container.removeItem(first));
  903. assertNotSame(first, container.firstItemId());
  904. }
  905. @Test
  906. public void firstItemId_freeformNewlyAddedFirstItemRemoved_resultChanges()
  907. throws SQLException {
  908. DataGenerator.createGarbage(connectionPool);
  909. SQLContainer container = new SQLContainer(new FreeformQuery(
  910. "SELECT * FROM GARBAGE", connectionPool, "ID"));
  911. Object first = container.addItem();
  912. Object second = container.addItem();
  913. assertSame(first, container.firstItemId());
  914. assertTrue(container.removeItem(first));
  915. assertSame(second, container.firstItemId());
  916. }
  917. @Test
  918. public void lastItemId_freeformLastItemRemoved_resultChanges()
  919. throws SQLException {
  920. SQLContainer container = new SQLContainer(new FreeformQuery(
  921. "SELECT * FROM people", connectionPool, "ID"));
  922. Object last = container.lastItemId();
  923. assertTrue(container.removeItem(last));
  924. assertNotSame(last, container.lastItemId());
  925. }
  926. @Test
  927. public void lastItemId_freeformAddedLastItemRemoved_resultChanges()
  928. throws SQLException {
  929. SQLContainer container = new SQLContainer(new FreeformQuery(
  930. "SELECT * FROM people", connectionPool, "ID"));
  931. Object last = container.addItem();
  932. assertSame(last, container.lastItemId());
  933. assertTrue(container.removeItem(last));
  934. assertNotSame(last, container.lastItemId());
  935. }
  936. @Test
  937. public void isFirstId_freeformFirstItemRemoved_returnsFalse()
  938. throws SQLException {
  939. SQLContainer container = new SQLContainer(new FreeformQuery(
  940. "SELECT * FROM people", connectionPool, "ID"));
  941. Object first = container.firstItemId();
  942. assertTrue(container.removeItem(first));
  943. assertFalse(container.isFirstId(first));
  944. }
  945. @Test
  946. public void isFirstId_freeformAddedFirstItemRemoved_returnsFalse()
  947. throws SQLException {
  948. DataGenerator.createGarbage(connectionPool);
  949. SQLContainer container = new SQLContainer(new FreeformQuery(
  950. "SELECT * FROM GARBAGE", connectionPool, "ID"));
  951. Object first = container.addItem();
  952. container.addItem();
  953. assertSame(first, container.firstItemId());
  954. assertTrue(container.removeItem(first));
  955. assertFalse(container.isFirstId(first));
  956. }
  957. @Test
  958. public void isLastId_freeformLastItemRemoved_returnsFalse()
  959. throws SQLException {
  960. SQLContainer container = new SQLContainer(new FreeformQuery(
  961. "SELECT * FROM people", connectionPool, "ID"));
  962. Object last = container.lastItemId();
  963. assertTrue(container.removeItem(last));
  964. assertFalse(container.isLastId(last));
  965. }
  966. @Test
  967. public void isLastId_freeformAddedLastItemRemoved_returnsFalse()
  968. throws SQLException {
  969. SQLContainer container = new SQLContainer(new FreeformQuery(
  970. "SELECT * FROM people", connectionPool, "ID"));
  971. Object last = container.addItem();
  972. assertSame(last, container.lastItemId());
  973. assertTrue(container.removeItem(last));
  974. assertFalse(container.isLastId(last));
  975. }
  976. @Test
  977. public void indexOfId_freeformItemRemoved_returnsNegOne()
  978. throws SQLException {
  979. SQLContainer container = new SQLContainer(new FreeformQuery(
  980. "SELECT * FROM people", connectionPool, "ID"));
  981. Object id = container.getIdByIndex(2);
  982. assertTrue(container.removeItem(id));
  983. assertEquals(-1, container.indexOfId(id));
  984. }
  985. @Test
  986. public void indexOfId_freeformAddedItemRemoved_returnsNegOne()
  987. throws SQLException {
  988. SQLContainer container = new SQLContainer(new FreeformQuery(
  989. "SELECT * FROM people", connectionPool, "ID"));
  990. Object id = container.addItem();
  991. assertTrue(container.indexOfId(id) != -1);
  992. assertTrue(container.removeItem(id));
  993. assertEquals(-1, container.indexOfId(id));
  994. }
  995. @Test
  996. public void getIdByIndex_freeformItemRemoved_resultChanges()
  997. throws SQLException {
  998. SQLContainer container = new SQLContainer(new FreeformQuery(
  999. "SELECT * FROM people", connectionPool, "ID"));
  1000. Object id = container.getIdByIndex(2);
  1001. assertTrue(container.removeItem(id));
  1002. assertNotSame(id, container.getIdByIndex(2));
  1003. }
  1004. @Test
  1005. public void getIdByIndex_freeformAddedItemRemoved_resultChanges()
  1006. throws SQLException {
  1007. SQLContainer container = new SQLContainer(new FreeformQuery(
  1008. "SELECT * FROM people", connectionPool, "ID"));
  1009. Object id = container.addItem();
  1010. container.addItem();
  1011. int index = container.indexOfId(id);
  1012. assertTrue(container.removeItem(id));
  1013. assertNotSame(id, container.getIdByIndex(index));
  1014. }
  1015. @Test
  1016. public void removeAllItems_freeform_shouldSucceed() throws SQLException {
  1017. SQLContainer container = new SQLContainer(new FreeformQuery(
  1018. "SELECT * FROM people", connectionPool, "ID"));
  1019. assertTrue(container.removeAllItems());
  1020. assertEquals(0, container.size());
  1021. }
  1022. @Test
  1023. public void removeAllItems_freeformAddedItems_shouldSucceed()
  1024. throws SQLException {
  1025. SQLContainer container = new SQLContainer(new FreeformQuery(
  1026. "SELECT * FROM people", connectionPool, "ID"));
  1027. container.addItem();
  1028. container.addItem();
  1029. assertTrue(container.removeAllItems());
  1030. assertEquals(0, container.size());
  1031. }
  1032. @SuppressWarnings("unchecked")
  1033. @Test
  1034. public void commit_freeformAddedItem_shouldBeWrittenToDB()
  1035. throws SQLException {
  1036. FreeformQueryDelegate delegate = EasyMock
  1037. .createMock(FreeformQueryDelegate.class);
  1038. EasyMock.expect(delegate.storeRow(EasyMock.isA(Connection.class),
  1039. EasyMock.isA(RowItem.class))).andAnswer(new IAnswer<Integer>() {
  1040. @Override
  1041. public Integer answer() throws Throwable {
  1042. Connection conn = (Connection) EasyMock
  1043. .getCurrentArguments()[0];
  1044. RowItem item = (RowItem) EasyMock
  1045. .getCurrentArguments()[1];
  1046. Statement statement = conn.createStatement();
  1047. if (SQLTestsConstants.db == DB.MSSQL) {
  1048. statement
  1049. .executeUpdate("insert into people values('"
  1050. + item.getItemProperty("NAME")
  1051. .getValue()
  1052. + "', '"
  1053. + item.getItemProperty("AGE")
  1054. .getValue()
  1055. + "')");
  1056. } else {
  1057. statement.executeUpdate(
  1058. "insert into people values(default, '"
  1059. + item.getItemProperty("NAME")
  1060. .getValue()
  1061. + "', '"
  1062. + item.getItemProperty("AGE")
  1063. .getValue()
  1064. + "')");
  1065. }
  1066. statement.close();
  1067. conn.commit();
  1068. connectionPool.releaseConnection(conn);
  1069. return 1;
  1070. }
  1071. }).anyTimes();
  1072. EasyMock.expect(
  1073. delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt()))
  1074. .andAnswer(new IAnswer<String>() {
  1075. @Override
  1076. public String answer() throws Throwable {
  1077. Object[] args = EasyMock.getCurrentArguments();
  1078. int offset = (Integer) (args[0]);
  1079. int limit = (Integer) (args[1]);
  1080. if (SQLTestsConstants.db == DB.MSSQL) {
  1081. int start = offset + 1;
  1082. int end = offset + limit + 1;
  1083. String q = "SELECT * FROM (SELECT row_number() OVER"
  1084. + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)"
  1085. + " AS a WHERE a.rownum BETWEEN " + start
  1086. + " AND " + end;
  1087. return q;
  1088. } else if (SQLTestsConstants.db == DB.ORACLE) {
  1089. int start = offset + 1;
  1090. int end = offset + limit + 1;
  1091. String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM"
  1092. + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) "
  1093. + " WHERE r BETWEEN " + start + " AND "
  1094. + end;
  1095. return q;
  1096. } else {
  1097. return "SELECT * FROM people LIMIT " + limit
  1098. + " OFFSET " + offset;
  1099. }
  1100. }
  1101. }).anyTimes();
  1102. delegate.setFilters(null);
  1103. EasyMock.expectLastCall().anyTimes();
  1104. delegate.setFilters(EasyMock.isA(List.class));
  1105. EasyMock.expectLastCall().anyTimes();
  1106. delegate.setOrderBy(null);
  1107. EasyMock.expectLastCall().anyTimes();
  1108. delegate.setOrderBy(EasyMock.isA(List.class));
  1109. EasyMock.expectLastCall().anyTimes();
  1110. EasyMock.expect(delegate.getCountQuery())
  1111. .andThrow(new UnsupportedOperationException()).anyTimes();
  1112. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1113. connectionPool, "ID");
  1114. query.setDelegate(delegate);
  1115. EasyMock.replay(delegate);
  1116. SQLContainer container = new SQLContainer(query);
  1117. Object id = container.addItem();
  1118. container.getContainerProperty(id, "NAME").setValue("New Name");
  1119. container.getContainerProperty(id, "AGE").setValue(30);
  1120. assertTrue(id instanceof TemporaryRowId);
  1121. assertSame(id, container.lastItemId());
  1122. container.commit();
  1123. assertFalse(container.lastItemId() instanceof TemporaryRowId);
  1124. assertEquals("New Name",
  1125. container.getContainerProperty(container.lastItemId(), "NAME")
  1126. .getValue());
  1127. EasyMock.verify(delegate);
  1128. }
  1129. @SuppressWarnings("unchecked")
  1130. @Test
  1131. public void commit_freeformTwoAddedItems_shouldBeWrittenToDB()
  1132. throws SQLException {
  1133. FreeformQueryDelegate delegate = EasyMock
  1134. .createMock(FreeformQueryDelegate.class);
  1135. EasyMock.expect(delegate.storeRow(EasyMock.isA(Connection.class),
  1136. EasyMock.isA(RowItem.class))).andAnswer(new IAnswer<Integer>() {
  1137. @Override
  1138. public Integer answer() throws Throwable {
  1139. Connection conn = (Connection) EasyMock
  1140. .getCurrentArguments()[0];
  1141. RowItem item = (RowItem) EasyMock
  1142. .getCurrentArguments()[1];
  1143. Statement statement = conn.createStatement();
  1144. if (SQLTestsConstants.db == DB.MSSQL) {
  1145. statement
  1146. .executeUpdate("insert into people values('"
  1147. + item.getItemProperty("NAME")
  1148. .getValue()
  1149. + "', '"
  1150. + item.getItemProperty("AGE")
  1151. .getValue()
  1152. + "')");
  1153. } else {
  1154. statement.executeUpdate(
  1155. "insert into people values(default, '"
  1156. + item.getItemProperty("NAME")
  1157. .getValue()
  1158. + "', '"
  1159. + item.getItemProperty("AGE")
  1160. .getValue()
  1161. + "')");
  1162. }
  1163. statement.close();
  1164. conn.commit();
  1165. connectionPool.releaseConnection(conn);
  1166. return 1;
  1167. }
  1168. }).anyTimes();
  1169. EasyMock.expect(
  1170. delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt()))
  1171. .andAnswer(new IAnswer<String>() {
  1172. @Override
  1173. public String answer() throws Throwable {
  1174. Object[] args = EasyMock.getCurrentArguments();
  1175. int offset = (Integer) (args[0]);
  1176. int limit = (Integer) (args[1]);
  1177. if (SQLTestsConstants.db == DB.MSSQL) {
  1178. int start = offset + 1;
  1179. int end = offset + limit + 1;
  1180. String q = "SELECT * FROM (SELECT row_number() OVER"
  1181. + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)"
  1182. + " AS a WHERE a.rownum BETWEEN " + start
  1183. + " AND " + end;
  1184. return q;
  1185. } else if (SQLTestsConstants.db == DB.ORACLE) {
  1186. int start = offset + 1;
  1187. int end = offset + limit + 1;
  1188. String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM"
  1189. + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) "
  1190. + " WHERE r BETWEEN " + start + " AND "
  1191. + end;
  1192. return q;
  1193. } else {
  1194. return "SELECT * FROM people LIMIT " + limit
  1195. + " OFFSET " + offset;
  1196. }
  1197. }
  1198. }).anyTimes();
  1199. delegate.setFilters(null);
  1200. EasyMock.expectLastCall().anyTimes();
  1201. delegate.setFilters(EasyMock.isA(List.class));
  1202. EasyMock.expectLastCall().anyTimes();
  1203. delegate.setOrderBy(null);
  1204. EasyMock.expectLastCall().anyTimes();
  1205. delegate.setOrderBy(EasyMock.isA(List.class));
  1206. EasyMock.expectLastCall().anyTimes();
  1207. EasyMock.expect(delegate.getCountQuery())
  1208. .andThrow(new UnsupportedOperationException()).anyTimes();
  1209. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1210. connectionPool, "ID");
  1211. query.setDelegate(delegate);
  1212. EasyMock.replay(delegate);
  1213. SQLContainer container = new SQLContainer(query);
  1214. Object id = container.addItem();
  1215. Object id2 = container.addItem();
  1216. container.getContainerProperty(id, "NAME").setValue("Herbert");
  1217. container.getContainerProperty(id, "AGE").setValue(30);
  1218. container.getContainerProperty(id2, "NAME").setValue("Larry");
  1219. container.getContainerProperty(id2, "AGE").setValue(50);
  1220. assertTrue(id2 instanceof TemporaryRowId);
  1221. assertSame(id2, container.lastItemId());
  1222. container.commit();
  1223. Object nextToLast = container.getIdByIndex(container.size() - 2);
  1224. assertFalse(nextToLast instanceof TemporaryRowId);
  1225. assertEquals("Herbert",
  1226. container.getContainerProperty(nextToLast, "NAME").getValue());
  1227. assertFalse(container.lastItemId() instanceof TemporaryRowId);
  1228. assertEquals("Larry",
  1229. container.getContainerProperty(container.lastItemId(), "NAME")
  1230. .getValue());
  1231. EasyMock.verify(delegate);
  1232. }
  1233. @SuppressWarnings("unchecked")
  1234. @Test
  1235. public void commit_freeformRemovedItem_shouldBeRemovedFromDB()
  1236. throws SQLException {
  1237. FreeformQueryDelegate delegate = EasyMock
  1238. .createMock(FreeformQueryDelegate.class);
  1239. EasyMock.expect(delegate.removeRow(EasyMock.isA(Connection.class),
  1240. EasyMock.isA(RowItem.class))).andAnswer(new IAnswer<Boolean>() {
  1241. @Override
  1242. public Boolean answer() throws Throwable {
  1243. Connection conn = (Connection) EasyMock
  1244. .getCurrentArguments()[0];
  1245. RowItem item = (RowItem) EasyMock
  1246. .getCurrentArguments()[1];
  1247. Statement statement = conn.createStatement();
  1248. statement.executeUpdate(
  1249. "DELETE FROM people WHERE \"ID\"=" + item
  1250. .getItemProperty("ID").getValue());
  1251. statement.close();
  1252. return true;
  1253. }
  1254. }).anyTimes();
  1255. EasyMock.expect(
  1256. delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt()))
  1257. .andAnswer(new IAnswer<String>() {
  1258. @Override
  1259. public String answer() throws Throwable {
  1260. Object[] args = EasyMock.getCurrentArguments();
  1261. int offset = (Integer) (args[0]);
  1262. int limit = (Integer) (args[1]);
  1263. if (SQLTestsConstants.db == DB.MSSQL) {
  1264. int start = offset + 1;
  1265. int end = offset + limit + 1;
  1266. String q = "SELECT * FROM (SELECT row_number() OVER"
  1267. + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)"
  1268. + " AS a WHERE a.rownum BETWEEN " + start
  1269. + " AND " + end;
  1270. return q;
  1271. } else if (SQLTestsConstants.db == DB.ORACLE) {
  1272. int start = offset + 1;
  1273. int end = offset + limit + 1;
  1274. String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM"
  1275. + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) "
  1276. + " WHERE r BETWEEN " + start + " AND "
  1277. + end;
  1278. return q;
  1279. } else {
  1280. return "SELECT * FROM people LIMIT " + limit
  1281. + " OFFSET " + offset;
  1282. }
  1283. }
  1284. }).anyTimes();
  1285. delegate.setFilters(null);
  1286. EasyMock.expectLastCall().anyTimes();
  1287. delegate.setFilters(EasyMock.isA(List.class));
  1288. EasyMock.expectLastCall().anyTimes();
  1289. delegate.setOrderBy(null);
  1290. EasyMock.expectLastCall().anyTimes();
  1291. delegate.setOrderBy(EasyMock.isA(List.class));
  1292. EasyMock.expectLastCall().anyTimes();
  1293. EasyMock.expect(delegate.getCountQuery())
  1294. .andThrow(new UnsupportedOperationException()).anyTimes();
  1295. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1296. connectionPool, "ID");
  1297. query.setDelegate(delegate);
  1298. EasyMock.replay(delegate);
  1299. SQLContainer container = new SQLContainer(query);
  1300. Object last = container.lastItemId();
  1301. container.removeItem(last);
  1302. container.commit();
  1303. assertFalse(last.equals(container.lastItemId()));
  1304. EasyMock.verify(delegate);
  1305. }
  1306. @SuppressWarnings("unchecked")
  1307. @Test
  1308. public void commit_freeformLastItemUpdated_shouldUpdateRowInDB()
  1309. throws SQLException {
  1310. FreeformQueryDelegate delegate = EasyMock
  1311. .createMock(FreeformQueryDelegate.class);
  1312. EasyMock.expect(delegate.storeRow(EasyMock.isA(Connection.class),
  1313. EasyMock.isA(RowItem.class))).andAnswer(new IAnswer<Integer>() {
  1314. @Override
  1315. public Integer answer() throws Throwable {
  1316. Connection conn = (Connection) EasyMock
  1317. .getCurrentArguments()[0];
  1318. RowItem item = (RowItem) EasyMock
  1319. .getCurrentArguments()[1];
  1320. Statement statement = conn.createStatement();
  1321. statement.executeUpdate("UPDATE people SET \"NAME\"='"
  1322. + item.getItemProperty("NAME").getValue()
  1323. + "' WHERE \"ID\"="
  1324. + item.getItemProperty("ID").getValue());
  1325. statement.close();
  1326. conn.commit();
  1327. connectionPool.releaseConnection(conn);
  1328. return 1;
  1329. }
  1330. }).anyTimes();
  1331. EasyMock.expect(
  1332. delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt()))
  1333. .andAnswer(new IAnswer<String>() {
  1334. @Override
  1335. public String answer() throws Throwable {
  1336. Object[] args = EasyMock.getCurrentArguments();
  1337. int offset = (Integer) (args[0]);
  1338. int limit = (Integer) (args[1]);
  1339. if (SQLTestsConstants.db == DB.MSSQL) {
  1340. int start = offset + 1;
  1341. int end = offset + limit + 1;
  1342. String q = "SELECT * FROM (SELECT row_number() OVER"
  1343. + " ( ORDER BY \"ID\" ASC) AS rownum, * FROM people)"
  1344. + " AS a WHERE a.rownum BETWEEN " + start
  1345. + " AND " + end;
  1346. return q;
  1347. } else if (SQLTestsConstants.db == DB.ORACLE) {
  1348. int start = offset + 1;
  1349. int end = offset + limit + 1;
  1350. String q = "SELECT * FROM (SELECT x.*, ROWNUM AS r FROM"
  1351. + " (SELECT * FROM people ORDER BY \"ID\" ASC) x) "
  1352. + " WHERE r BETWEEN " + start + " AND "
  1353. + end;
  1354. return q;
  1355. } else {
  1356. return "SELECT * FROM people LIMIT " + limit
  1357. + " OFFSET " + offset;
  1358. }
  1359. }
  1360. }).anyTimes();
  1361. delegate.setFilters(null);
  1362. EasyMock.expectLastCall().anyTimes();
  1363. delegate.setFilters(EasyMock.isA(List.class));
  1364. EasyMock.expectLastCall().anyTimes();
  1365. delegate.setOrderBy(null);
  1366. EasyMock.expectLastCall().anyTimes();
  1367. delegate.setOrderBy(EasyMock.isA(List.class));
  1368. EasyMock.expectLastCall().anyTimes();
  1369. EasyMock.expect(delegate.getCountQuery())
  1370. .andThrow(new UnsupportedOperationException()).anyTimes();
  1371. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1372. connectionPool, "ID");
  1373. query.setDelegate(delegate);
  1374. EasyMock.replay(delegate);
  1375. SQLContainer container = new SQLContainer(query);
  1376. Object last = container.lastItemId();
  1377. container.getContainerProperty(last, "NAME").setValue("Donald");
  1378. container.commit();
  1379. assertEquals("Donald",
  1380. container.getContainerProperty(container.lastItemId(), "NAME")
  1381. .getValue());
  1382. EasyMock.verify(delegate);
  1383. }
  1384. @Test
  1385. public void rollback_freeformItemAdded_discardsAddedItem()
  1386. throws SQLException {
  1387. SQLContainer container = new SQLContainer(new FreeformQuery(
  1388. "SELECT * FROM people", connectionPool, "ID"));
  1389. int size = container.size();
  1390. Object id = container.addItem();
  1391. container.getContainerProperty(id, "NAME").setValue("foo");
  1392. assertEquals(size + 1, container.size());
  1393. container.rollback();
  1394. assertEquals(size, container.size());
  1395. assertFalse("foo".equals(
  1396. container.getContainerProperty(container.lastItemId(), "NAME")
  1397. .getValue()));
  1398. }
  1399. @Test
  1400. public void rollback_freeformItemRemoved_restoresRemovedItem()
  1401. throws SQLException {
  1402. SQLContainer container = new SQLContainer(new FreeformQuery(
  1403. "SELECT * FROM people", connectionPool, "ID"));
  1404. int size = container.size();
  1405. Object last = container.lastItemId();
  1406. container.removeItem(last);
  1407. assertEquals(size - 1, container.size());
  1408. container.rollback();
  1409. assertEquals(size, container.size());
  1410. assertEquals(last, container.lastItemId());
  1411. }
  1412. @Test
  1413. public void rollback_freeformItemChanged_discardsChanges()
  1414. throws SQLException {
  1415. SQLContainer container = new SQLContainer(new FreeformQuery(
  1416. "SELECT * FROM people", connectionPool, "ID"));
  1417. Object last = container.lastItemId();
  1418. container.getContainerProperty(last, "NAME").setValue("foo");
  1419. container.rollback();
  1420. assertFalse("foo".equals(
  1421. container.getContainerProperty(container.lastItemId(), "NAME")
  1422. .getValue()));
  1423. }
  1424. @Test
  1425. public void itemChangeNotification_freeform_isModifiedReturnsTrue()
  1426. throws SQLException {
  1427. SQLContainer container = new SQLContainer(new FreeformQuery(
  1428. "SELECT * FROM people", connectionPool, "ID"));
  1429. assertFalse(container.isModified());
  1430. RowItem last = (RowItem) container.getItem(container.lastItemId());
  1431. container.itemChangeNotification(last);
  1432. assertTrue(container.isModified());
  1433. }
  1434. @Test
  1435. public void itemSetChangeListeners_freeform_shouldFire()
  1436. throws SQLException {
  1437. SQLContainer container = new SQLContainer(new FreeformQuery(
  1438. "SELECT * FROM people", connectionPool, "ID"));
  1439. ItemSetChangeListener listener = EasyMock
  1440. .createMock(ItemSetChangeListener.class);
  1441. listener.containerItemSetChange(EasyMock.isA(ItemSetChangeEvent.class));
  1442. EasyMock.replay(listener);
  1443. container.addListener(listener);
  1444. container.addItem();
  1445. EasyMock.verify(listener);
  1446. }
  1447. @Test
  1448. public void itemSetChangeListeners_freeformItemRemoved_shouldFire()
  1449. throws SQLException {
  1450. SQLContainer container = new SQLContainer(new FreeformQuery(
  1451. "SELECT * FROM people", connectionPool, "ID"));
  1452. ItemSetChangeListener listener = EasyMock
  1453. .createMock(ItemSetChangeListener.class);
  1454. listener.containerItemSetChange(EasyMock.isA(ItemSetChangeEvent.class));
  1455. EasyMock.expectLastCall().anyTimes();
  1456. EasyMock.replay(listener);
  1457. container.addListener(listener);
  1458. container.removeItem(container.lastItemId());
  1459. EasyMock.verify(listener);
  1460. }
  1461. @Test
  1462. public void removeListener_freeform_shouldNotFire() throws SQLException {
  1463. SQLContainer container = new SQLContainer(new FreeformQuery(
  1464. "SELECT * FROM people", connectionPool, "ID"));
  1465. ItemSetChangeListener listener = EasyMock
  1466. .createMock(ItemSetChangeListener.class);
  1467. EasyMock.replay(listener);
  1468. container.addListener(listener);
  1469. container.removeListener(listener);
  1470. container.addItem();
  1471. EasyMock.verify(listener);
  1472. }
  1473. @Test
  1474. public void isModified_freeformRemovedItem_returnsTrue()
  1475. throws SQLException {
  1476. SQLContainer container = new SQLContainer(new FreeformQuery(
  1477. "SELECT * FROM people", connectionPool, "ID"));
  1478. assertFalse(container.isModified());
  1479. container.removeItem(container.lastItemId());
  1480. assertTrue(container.isModified());
  1481. }
  1482. @Test
  1483. public void isModified_freeformAddedItem_returnsTrue() throws SQLException {
  1484. SQLContainer container = new SQLContainer(new FreeformQuery(
  1485. "SELECT * FROM people", connectionPool, "ID"));
  1486. assertFalse(container.isModified());
  1487. container.addItem();
  1488. assertTrue(container.isModified());
  1489. }
  1490. @Test
  1491. public void isModified_freeformChangedItem_returnsTrue()
  1492. throws SQLException {
  1493. SQLContainer container = new SQLContainer(new FreeformQuery(
  1494. "SELECT * FROM people", connectionPool, "ID"));
  1495. assertFalse(container.isModified());
  1496. container.getContainerProperty(container.lastItemId(), "NAME")
  1497. .setValue("foo");
  1498. assertTrue(container.isModified());
  1499. }
  1500. @Test
  1501. public void getSortableContainerPropertyIds_freeform_returnsAllPropertyIds()
  1502. throws SQLException {
  1503. SQLContainer container = new SQLContainer(new FreeformQuery(
  1504. "SELECT * FROM people", connectionPool, "ID"));
  1505. Collection<?> sortableIds = container.getSortableContainerPropertyIds();
  1506. assertTrue(sortableIds.contains("ID"));
  1507. assertTrue(sortableIds.contains("NAME"));
  1508. assertTrue(sortableIds.contains("AGE"));
  1509. assertEquals(3, sortableIds.size());
  1510. }
  1511. @SuppressWarnings("unchecked")
  1512. @Test
  1513. public void addOrderBy_freeform_shouldReorderResults() throws SQLException {
  1514. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1515. connectionPool, "ID");
  1516. FreeformQueryDelegate delegate = EasyMock
  1517. .createMock(FreeformQueryDelegate.class);
  1518. final List<OrderBy> orderBys = new ArrayList<OrderBy>();
  1519. delegate.setFilters(null);
  1520. EasyMock.expectLastCall().anyTimes();
  1521. delegate.setFilters(EasyMock.isA(List.class));
  1522. EasyMock.expectLastCall().anyTimes();
  1523. delegate.setOrderBy(null);
  1524. EasyMock.expectLastCall().anyTimes();
  1525. delegate.setOrderBy(EasyMock.isA(List.class));
  1526. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  1527. @Override
  1528. public Object answer() throws Throwable {
  1529. List<OrderBy> orders = (List<OrderBy>) EasyMock
  1530. .getCurrentArguments()[0];
  1531. orderBys.clear();
  1532. orderBys.addAll(orders);
  1533. return null;
  1534. }
  1535. }).anyTimes();
  1536. EasyMock.expect(
  1537. delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt()))
  1538. .andAnswer(new IAnswer<String>() {
  1539. @Override
  1540. public String answer() throws Throwable {
  1541. Object[] args = EasyMock.getCurrentArguments();
  1542. int offset = (Integer) (args[0]);
  1543. int limit = (Integer) (args[1]);
  1544. if (SQLTestsConstants.db == DB.MSSQL) {
  1545. SQLGenerator gen = new MSSQLGenerator();
  1546. if (orderBys == null || orderBys.isEmpty()) {
  1547. List<OrderBy> ob = new ArrayList<OrderBy>();
  1548. ob.add(new OrderBy("ID", true));
  1549. return gen
  1550. .generateSelectQuery("people", null, ob,
  1551. offset, limit, null)
  1552. .getQueryString();
  1553. } else {
  1554. return gen
  1555. .generateSelectQuery("people", null,
  1556. orderBys, offset, limit, null)
  1557. .getQueryString();
  1558. }
  1559. } else if (SQLTestsConstants.db == DB.ORACLE) {
  1560. SQLGenerator gen = new OracleGenerator();
  1561. if (orderBys == null || orderBys.isEmpty()) {
  1562. List<OrderBy> ob = new ArrayList<OrderBy>();
  1563. ob.add(new OrderBy("ID", true));
  1564. return gen
  1565. .generateSelectQuery("people", null, ob,
  1566. offset, limit, null)
  1567. .getQueryString();
  1568. } else {
  1569. return gen
  1570. .generateSelectQuery("people", null,
  1571. orderBys, offset, limit, null)
  1572. .getQueryString();
  1573. }
  1574. } else {
  1575. StringBuilder query = new StringBuilder(
  1576. "SELECT * FROM people");
  1577. if (!orderBys.isEmpty()) {
  1578. query.append(" ORDER BY ");
  1579. for (OrderBy orderBy : orderBys) {
  1580. query.append(
  1581. "\"" + orderBy.getColumn() + "\"");
  1582. if (orderBy.isAscending()) {
  1583. query.append(" ASC");
  1584. } else {
  1585. query.append(" DESC");
  1586. }
  1587. }
  1588. }
  1589. query.append(" LIMIT ").append(limit)
  1590. .append(" OFFSET ").append(offset);
  1591. return query.toString();
  1592. }
  1593. }
  1594. }).anyTimes();
  1595. EasyMock.expect(delegate.getCountQuery())
  1596. .andThrow(new UnsupportedOperationException()).anyTimes();
  1597. EasyMock.replay(delegate);
  1598. query.setDelegate(delegate);
  1599. SQLContainer container = new SQLContainer(query);
  1600. // Ville, Kalle, Pelle, Börje
  1601. assertEquals("Ville",
  1602. container.getContainerProperty(container.firstItemId(), "NAME")
  1603. .getValue());
  1604. assertEquals("Börje",
  1605. container.getContainerProperty(container.lastItemId(), "NAME")
  1606. .getValue());
  1607. container.addOrderBy(new OrderBy("NAME", true));
  1608. // Börje, Kalle, Pelle, Ville
  1609. assertEquals("Börje",
  1610. container.getContainerProperty(container.firstItemId(), "NAME")
  1611. .getValue());
  1612. assertEquals("Ville",
  1613. container.getContainerProperty(container.lastItemId(), "NAME")
  1614. .getValue());
  1615. EasyMock.verify(delegate);
  1616. }
  1617. @Test(expected = IllegalArgumentException.class)
  1618. public void addOrderBy_freeformIllegalColumn_shouldFail()
  1619. throws SQLException {
  1620. SQLContainer container = new SQLContainer(new FreeformQuery(
  1621. "SELECT * FROM people", connectionPool, "ID"));
  1622. container.addOrderBy(new OrderBy("asdf", true));
  1623. }
  1624. @SuppressWarnings("unchecked")
  1625. @Test
  1626. public void sort_freeform_sortsByName() throws SQLException {
  1627. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1628. connectionPool, "ID");
  1629. FreeformQueryDelegate delegate = EasyMock
  1630. .createMock(FreeformQueryDelegate.class);
  1631. final List<OrderBy> orderBys = new ArrayList<OrderBy>();
  1632. delegate.setFilters(null);
  1633. EasyMock.expectLastCall().anyTimes();
  1634. delegate.setFilters(EasyMock.isA(List.class));
  1635. EasyMock.expectLastCall().anyTimes();
  1636. delegate.setOrderBy(null);
  1637. EasyMock.expectLastCall().anyTimes();
  1638. delegate.setOrderBy(EasyMock.isA(List.class));
  1639. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  1640. @Override
  1641. public Object answer() throws Throwable {
  1642. List<OrderBy> orders = (List<OrderBy>) EasyMock
  1643. .getCurrentArguments()[0];
  1644. orderBys.clear();
  1645. orderBys.addAll(orders);
  1646. return null;
  1647. }
  1648. }).anyTimes();
  1649. EasyMock.expect(
  1650. delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt()))
  1651. .andAnswer(new IAnswer<String>() {
  1652. @Override
  1653. public String answer() throws Throwable {
  1654. Object[] args = EasyMock.getCurrentArguments();
  1655. int offset = (Integer) (args[0]);
  1656. int limit = (Integer) (args[1]);
  1657. if (SQLTestsConstants.db == DB.MSSQL) {
  1658. SQLGenerator gen = new MSSQLGenerator();
  1659. if (orderBys == null || orderBys.isEmpty()) {
  1660. List<OrderBy> ob = new ArrayList<OrderBy>();
  1661. ob.add(new OrderBy("ID", true));
  1662. return gen
  1663. .generateSelectQuery("people", null, ob,
  1664. offset, limit, null)
  1665. .getQueryString();
  1666. } else {
  1667. return gen
  1668. .generateSelectQuery("people", null,
  1669. orderBys, offset, limit, null)
  1670. .getQueryString();
  1671. }
  1672. } else if (SQLTestsConstants.db == DB.ORACLE) {
  1673. SQLGenerator gen = new OracleGenerator();
  1674. if (orderBys == null || orderBys.isEmpty()) {
  1675. List<OrderBy> ob = new ArrayList<OrderBy>();
  1676. ob.add(new OrderBy("ID", true));
  1677. return gen
  1678. .generateSelectQuery("people", null, ob,
  1679. offset, limit, null)
  1680. .getQueryString();
  1681. } else {
  1682. return gen
  1683. .generateSelectQuery("people", null,
  1684. orderBys, offset, limit, null)
  1685. .getQueryString();
  1686. }
  1687. } else {
  1688. StringBuilder query = new StringBuilder(
  1689. "SELECT * FROM people");
  1690. if (!orderBys.isEmpty()) {
  1691. query.append(" ORDER BY ");
  1692. for (OrderBy orderBy : orderBys) {
  1693. query.append(
  1694. "\"" + orderBy.getColumn() + "\"");
  1695. if (orderBy.isAscending()) {
  1696. query.append(" ASC");
  1697. } else {
  1698. query.append(" DESC");
  1699. }
  1700. }
  1701. }
  1702. query.append(" LIMIT ").append(limit)
  1703. .append(" OFFSET ").append(offset);
  1704. return query.toString();
  1705. }
  1706. }
  1707. }).anyTimes();
  1708. EasyMock.expect(delegate.getCountQuery())
  1709. .andThrow(new UnsupportedOperationException()).anyTimes();
  1710. EasyMock.replay(delegate);
  1711. query.setDelegate(delegate);
  1712. SQLContainer container = new SQLContainer(query);
  1713. // Ville, Kalle, Pelle, Börje
  1714. assertEquals("Ville",
  1715. container.getContainerProperty(container.firstItemId(), "NAME")
  1716. .getValue());
  1717. assertEquals("Börje",
  1718. container.getContainerProperty(container.lastItemId(), "NAME")
  1719. .getValue());
  1720. container.sort(new Object[] { "NAME" }, new boolean[] { true });
  1721. // Börje, Kalle, Pelle, Ville
  1722. assertEquals("Börje",
  1723. container.getContainerProperty(container.firstItemId(), "NAME")
  1724. .getValue());
  1725. assertEquals("Ville",
  1726. container.getContainerProperty(container.lastItemId(), "NAME")
  1727. .getValue());
  1728. EasyMock.verify(delegate);
  1729. }
  1730. @SuppressWarnings("unchecked")
  1731. @Test
  1732. public void addFilter_freeform_filtersResults() throws SQLException {
  1733. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1734. connectionPool, "ID");
  1735. FreeformStatementDelegate delegate = EasyMock
  1736. .createMock(FreeformStatementDelegate.class);
  1737. final List<Filter> filters = new ArrayList<Filter>();
  1738. delegate.setFilters(null);
  1739. EasyMock.expectLastCall().anyTimes();
  1740. delegate.setOrderBy(EasyMock.isA(List.class));
  1741. EasyMock.expectLastCall().anyTimes();
  1742. delegate.setOrderBy(null);
  1743. EasyMock.expectLastCall().anyTimes();
  1744. delegate.setFilters(EasyMock.isA(List.class));
  1745. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  1746. @Override
  1747. public Object answer() throws Throwable {
  1748. List<Filter> orders = (List<Filter>) EasyMock
  1749. .getCurrentArguments()[0];
  1750. filters.clear();
  1751. filters.addAll(orders);
  1752. return null;
  1753. }
  1754. }).anyTimes();
  1755. EasyMock.expect(delegate.getQueryStatement(EasyMock.anyInt(),
  1756. EasyMock.anyInt())).andAnswer(new IAnswer<StatementHelper>() {
  1757. @Override
  1758. public StatementHelper answer() throws Throwable {
  1759. Object[] args = EasyMock.getCurrentArguments();
  1760. int offset = (Integer) (args[0]);
  1761. int limit = (Integer) (args[1]);
  1762. return FreeformQueryUtil.getQueryWithFilters(filters,
  1763. offset, limit);
  1764. }
  1765. }).anyTimes();
  1766. EasyMock.expect(delegate.getCountStatement())
  1767. .andAnswer(new IAnswer<StatementHelper>() {
  1768. @Override
  1769. public StatementHelper answer() throws Throwable {
  1770. StatementHelper sh = new StatementHelper();
  1771. StringBuilder query = new StringBuilder(
  1772. "SELECT COUNT(*) FROM people");
  1773. if (!filters.isEmpty()) {
  1774. query.append(QueryBuilder
  1775. .getWhereStringForFilters(filters, sh));
  1776. }
  1777. sh.setQueryString(query.toString());
  1778. return sh;
  1779. }
  1780. }).anyTimes();
  1781. EasyMock.replay(delegate);
  1782. query.setDelegate(delegate);
  1783. SQLContainer container = new SQLContainer(query);
  1784. // Ville, Kalle, Pelle, Börje
  1785. assertEquals(4, container.size());
  1786. assertEquals("Börje",
  1787. container.getContainerProperty(container.lastItemId(), "NAME")
  1788. .getValue());
  1789. container.addContainerFilter(new Like("NAME", "%lle"));
  1790. // Ville, Kalle, Pelle
  1791. assertEquals(3, container.size());
  1792. assertEquals("Pelle",
  1793. container.getContainerProperty(container.lastItemId(), "NAME")
  1794. .getValue());
  1795. EasyMock.verify(delegate);
  1796. }
  1797. @SuppressWarnings("unchecked")
  1798. @Test
  1799. public void addContainerFilter_filtersResults() throws SQLException {
  1800. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1801. connectionPool, "ID");
  1802. FreeformStatementDelegate delegate = EasyMock
  1803. .createMock(FreeformStatementDelegate.class);
  1804. final List<Filter> filters = new ArrayList<Filter>();
  1805. delegate.setFilters(null);
  1806. EasyMock.expectLastCall().anyTimes();
  1807. delegate.setOrderBy(null);
  1808. EasyMock.expectLastCall().anyTimes();
  1809. delegate.setOrderBy(EasyMock.isA(List.class));
  1810. EasyMock.expectLastCall().anyTimes();
  1811. delegate.setFilters(EasyMock.isA(List.class));
  1812. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  1813. @Override
  1814. public Object answer() throws Throwable {
  1815. List<Filter> orders = (List<Filter>) EasyMock
  1816. .getCurrentArguments()[0];
  1817. filters.clear();
  1818. filters.addAll(orders);
  1819. return null;
  1820. }
  1821. }).anyTimes();
  1822. EasyMock.expect(delegate.getQueryStatement(EasyMock.anyInt(),
  1823. EasyMock.anyInt())).andAnswer(new IAnswer<StatementHelper>() {
  1824. @Override
  1825. public StatementHelper answer() throws Throwable {
  1826. Object[] args = EasyMock.getCurrentArguments();
  1827. int offset = (Integer) (args[0]);
  1828. int limit = (Integer) (args[1]);
  1829. return FreeformQueryUtil.getQueryWithFilters(filters,
  1830. offset, limit);
  1831. }
  1832. }).anyTimes();
  1833. EasyMock.expect(delegate.getCountStatement())
  1834. .andAnswer(new IAnswer<StatementHelper>() {
  1835. @Override
  1836. public StatementHelper answer() throws Throwable {
  1837. StatementHelper sh = new StatementHelper();
  1838. StringBuilder query = new StringBuilder(
  1839. "SELECT COUNT(*) FROM people");
  1840. if (!filters.isEmpty()) {
  1841. query.append(QueryBuilder
  1842. .getWhereStringForFilters(filters, sh));
  1843. }
  1844. sh.setQueryString(query.toString());
  1845. return sh;
  1846. }
  1847. }).anyTimes();
  1848. EasyMock.replay(delegate);
  1849. query.setDelegate(delegate);
  1850. SQLContainer container = new SQLContainer(query);
  1851. // Ville, Kalle, Pelle, Börje
  1852. assertEquals(4, container.size());
  1853. container.addContainerFilter("NAME", "Vi", false, false);
  1854. // Ville
  1855. assertEquals(1, container.size());
  1856. assertEquals("Ville",
  1857. container.getContainerProperty(container.lastItemId(), "NAME")
  1858. .getValue());
  1859. EasyMock.verify(delegate);
  1860. }
  1861. @SuppressWarnings("unchecked")
  1862. @Test
  1863. public void addContainerFilter_ignoreCase_filtersResults()
  1864. throws SQLException {
  1865. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1866. connectionPool, "ID");
  1867. FreeformStatementDelegate delegate = EasyMock
  1868. .createMock(FreeformStatementDelegate.class);
  1869. final List<Filter> filters = new ArrayList<Filter>();
  1870. delegate.setFilters(null);
  1871. EasyMock.expectLastCall().anyTimes();
  1872. delegate.setOrderBy(EasyMock.isA(List.class));
  1873. EasyMock.expectLastCall().anyTimes();
  1874. delegate.setOrderBy(null);
  1875. EasyMock.expectLastCall().anyTimes();
  1876. delegate.setFilters(EasyMock.isA(List.class));
  1877. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  1878. @Override
  1879. public Object answer() throws Throwable {
  1880. List<Filter> orders = (List<Filter>) EasyMock
  1881. .getCurrentArguments()[0];
  1882. filters.clear();
  1883. filters.addAll(orders);
  1884. return null;
  1885. }
  1886. }).anyTimes();
  1887. EasyMock.expect(delegate.getQueryStatement(EasyMock.anyInt(),
  1888. EasyMock.anyInt())).andAnswer(new IAnswer<StatementHelper>() {
  1889. @Override
  1890. public StatementHelper answer() throws Throwable {
  1891. Object[] args = EasyMock.getCurrentArguments();
  1892. int offset = (Integer) (args[0]);
  1893. int limit = (Integer) (args[1]);
  1894. return FreeformQueryUtil.getQueryWithFilters(filters,
  1895. offset, limit);
  1896. }
  1897. }).anyTimes();
  1898. EasyMock.expect(delegate.getCountStatement())
  1899. .andAnswer(new IAnswer<StatementHelper>() {
  1900. @Override
  1901. public StatementHelper answer() throws Throwable {
  1902. StatementHelper sh = new StatementHelper();
  1903. StringBuilder query = new StringBuilder(
  1904. "SELECT COUNT(*) FROM people");
  1905. if (!filters.isEmpty()) {
  1906. query.append(QueryBuilder
  1907. .getWhereStringForFilters(filters, sh));
  1908. }
  1909. sh.setQueryString(query.toString());
  1910. return sh;
  1911. }
  1912. }).anyTimes();
  1913. EasyMock.replay(delegate);
  1914. query.setDelegate(delegate);
  1915. SQLContainer container = new SQLContainer(query);
  1916. // Ville, Kalle, Pelle, Börje
  1917. assertEquals(4, container.size());
  1918. // FIXME LIKE %asdf% doesn't match a string that begins with asdf
  1919. container.addContainerFilter("NAME", "vi", true, true);
  1920. // Ville
  1921. assertEquals(1, container.size());
  1922. assertEquals("Ville",
  1923. container.getContainerProperty(container.lastItemId(), "NAME")
  1924. .getValue());
  1925. EasyMock.verify(delegate);
  1926. }
  1927. @SuppressWarnings("unchecked")
  1928. @Test
  1929. public void removeAllContainerFilters_freeform_noFiltering()
  1930. throws SQLException {
  1931. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  1932. connectionPool, "ID");
  1933. FreeformStatementDelegate delegate = EasyMock
  1934. .createMock(FreeformStatementDelegate.class);
  1935. final List<Filter> filters = new ArrayList<Filter>();
  1936. delegate.setFilters(null);
  1937. EasyMock.expectLastCall().anyTimes();
  1938. delegate.setOrderBy(EasyMock.isA(List.class));
  1939. EasyMock.expectLastCall().anyTimes();
  1940. delegate.setOrderBy(null);
  1941. EasyMock.expectLastCall().anyTimes();
  1942. delegate.setFilters(EasyMock.isA(List.class));
  1943. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  1944. @Override
  1945. public Object answer() throws Throwable {
  1946. List<Filter> orders = (List<Filter>) EasyMock
  1947. .getCurrentArguments()[0];
  1948. filters.clear();
  1949. filters.addAll(orders);
  1950. return null;
  1951. }
  1952. }).anyTimes();
  1953. EasyMock.expect(delegate.getQueryStatement(EasyMock.anyInt(),
  1954. EasyMock.anyInt())).andAnswer(new IAnswer<StatementHelper>() {
  1955. @Override
  1956. public StatementHelper answer() throws Throwable {
  1957. Object[] args = EasyMock.getCurrentArguments();
  1958. int offset = (Integer) (args[0]);
  1959. int limit = (Integer) (args[1]);
  1960. return FreeformQueryUtil.getQueryWithFilters(filters,
  1961. offset, limit);
  1962. }
  1963. }).anyTimes();
  1964. EasyMock.expect(delegate.getCountStatement())
  1965. .andAnswer(new IAnswer<StatementHelper>() {
  1966. @Override
  1967. public StatementHelper answer() throws Throwable {
  1968. StatementHelper sh = new StatementHelper();
  1969. StringBuilder query = new StringBuilder(
  1970. "SELECT COUNT(*) FROM people");
  1971. if (!filters.isEmpty()) {
  1972. query.append(QueryBuilder
  1973. .getWhereStringForFilters(filters, sh));
  1974. }
  1975. sh.setQueryString(query.toString());
  1976. return sh;
  1977. }
  1978. }).anyTimes();
  1979. EasyMock.replay(delegate);
  1980. query.setDelegate(delegate);
  1981. SQLContainer container = new SQLContainer(query);
  1982. // Ville, Kalle, Pelle, Börje
  1983. assertEquals(4, container.size());
  1984. container.addContainerFilter("NAME", "Vi", false, false);
  1985. // Ville
  1986. assertEquals(1, container.size());
  1987. assertEquals("Ville",
  1988. container.getContainerProperty(container.lastItemId(), "NAME")
  1989. .getValue());
  1990. container.removeAllContainerFilters();
  1991. assertEquals(4, container.size());
  1992. assertEquals("Börje",
  1993. container.getContainerProperty(container.lastItemId(), "NAME")
  1994. .getValue());
  1995. EasyMock.verify(delegate);
  1996. }
  1997. @SuppressWarnings("unchecked")
  1998. @Test
  1999. public void removeContainerFilters_freeform_noFiltering()
  2000. throws SQLException {
  2001. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  2002. connectionPool, "ID");
  2003. FreeformStatementDelegate delegate = EasyMock
  2004. .createMock(FreeformStatementDelegate.class);
  2005. final List<Filter> filters = new ArrayList<Filter>();
  2006. delegate.setFilters(null);
  2007. EasyMock.expectLastCall().anyTimes();
  2008. delegate.setOrderBy(EasyMock.isA(List.class));
  2009. EasyMock.expectLastCall().anyTimes();
  2010. delegate.setOrderBy(null);
  2011. EasyMock.expectLastCall().anyTimes();
  2012. delegate.setFilters(EasyMock.isA(List.class));
  2013. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  2014. @Override
  2015. public Object answer() throws Throwable {
  2016. List<Filter> orders = (List<Filter>) EasyMock
  2017. .getCurrentArguments()[0];
  2018. filters.clear();
  2019. filters.addAll(orders);
  2020. return null;
  2021. }
  2022. }).anyTimes();
  2023. EasyMock.expect(delegate.getQueryStatement(EasyMock.anyInt(),
  2024. EasyMock.anyInt())).andAnswer(new IAnswer<StatementHelper>() {
  2025. @Override
  2026. public StatementHelper answer() throws Throwable {
  2027. Object[] args = EasyMock.getCurrentArguments();
  2028. int offset = (Integer) (args[0]);
  2029. int limit = (Integer) (args[1]);
  2030. return FreeformQueryUtil.getQueryWithFilters(filters,
  2031. offset, limit);
  2032. }
  2033. }).anyTimes();
  2034. EasyMock.expect(delegate.getCountStatement())
  2035. .andAnswer(new IAnswer<StatementHelper>() {
  2036. @Override
  2037. public StatementHelper answer() throws Throwable {
  2038. StatementHelper sh = new StatementHelper();
  2039. StringBuilder query = new StringBuilder(
  2040. "SELECT COUNT(*) FROM people");
  2041. if (!filters.isEmpty()) {
  2042. query.append(QueryBuilder
  2043. .getWhereStringForFilters(filters, sh));
  2044. }
  2045. sh.setQueryString(query.toString());
  2046. return sh;
  2047. }
  2048. }).anyTimes();
  2049. EasyMock.replay(delegate);
  2050. query.setDelegate(delegate);
  2051. SQLContainer container = new SQLContainer(query);
  2052. // Ville, Kalle, Pelle, Börje
  2053. assertEquals(4, container.size());
  2054. container.addContainerFilter("NAME", "Vi", false, true);
  2055. // Ville
  2056. assertEquals(1, container.size());
  2057. assertEquals("Ville",
  2058. container.getContainerProperty(container.lastItemId(), "NAME")
  2059. .getValue());
  2060. container.removeContainerFilters("NAME");
  2061. assertEquals(4, container.size());
  2062. assertEquals("Börje",
  2063. container.getContainerProperty(container.lastItemId(), "NAME")
  2064. .getValue());
  2065. EasyMock.verify(delegate);
  2066. }
  2067. @SuppressWarnings("unchecked")
  2068. @Test
  2069. public void addFilter_freeformBufferedItems_alsoFiltersBufferedItems()
  2070. throws SQLException {
  2071. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  2072. connectionPool, "ID");
  2073. FreeformStatementDelegate delegate = EasyMock
  2074. .createMock(FreeformStatementDelegate.class);
  2075. final List<Filter> filters = new ArrayList<Filter>();
  2076. delegate.setFilters(null);
  2077. EasyMock.expectLastCall().anyTimes();
  2078. delegate.setOrderBy(EasyMock.isA(List.class));
  2079. EasyMock.expectLastCall().anyTimes();
  2080. delegate.setOrderBy(null);
  2081. EasyMock.expectLastCall().anyTimes();
  2082. delegate.setFilters(EasyMock.isA(List.class));
  2083. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  2084. @Override
  2085. public Object answer() throws Throwable {
  2086. List<Filter> orders = (List<Filter>) EasyMock
  2087. .getCurrentArguments()[0];
  2088. filters.clear();
  2089. filters.addAll(orders);
  2090. return null;
  2091. }
  2092. }).anyTimes();
  2093. EasyMock.expect(delegate.getQueryStatement(EasyMock.anyInt(),
  2094. EasyMock.anyInt())).andAnswer(new IAnswer<StatementHelper>() {
  2095. @Override
  2096. public StatementHelper answer() throws Throwable {
  2097. Object[] args = EasyMock.getCurrentArguments();
  2098. int offset = (Integer) (args[0]);
  2099. int limit = (Integer) (args[1]);
  2100. return FreeformQueryUtil.getQueryWithFilters(filters,
  2101. offset, limit);
  2102. }
  2103. }).anyTimes();
  2104. EasyMock.expect(delegate.getCountStatement())
  2105. .andAnswer(new IAnswer<StatementHelper>() {
  2106. @Override
  2107. public StatementHelper answer() throws Throwable {
  2108. StatementHelper sh = new StatementHelper();
  2109. StringBuilder query = new StringBuilder(
  2110. "SELECT COUNT(*) FROM people");
  2111. if (!filters.isEmpty()) {
  2112. query.append(QueryBuilder
  2113. .getWhereStringForFilters(filters, sh));
  2114. }
  2115. sh.setQueryString(query.toString());
  2116. return sh;
  2117. }
  2118. }).anyTimes();
  2119. EasyMock.replay(delegate);
  2120. query.setDelegate(delegate);
  2121. SQLContainer container = new SQLContainer(query);
  2122. // Ville, Kalle, Pelle, Börje
  2123. assertEquals(4, container.size());
  2124. assertEquals("Börje",
  2125. container.getContainerProperty(container.lastItemId(), "NAME")
  2126. .getValue());
  2127. Object id1 = container.addItem();
  2128. container.getContainerProperty(id1, "NAME").setValue("Palle");
  2129. Object id2 = container.addItem();
  2130. container.getContainerProperty(id2, "NAME").setValue("Bengt");
  2131. container.addContainerFilter(new Like("NAME", "%lle"));
  2132. // Ville, Kalle, Pelle, Palle
  2133. assertEquals(4, container.size());
  2134. assertEquals("Ville",
  2135. container
  2136. .getContainerProperty(container.getIdByIndex(0), "NAME")
  2137. .getValue());
  2138. assertEquals("Kalle",
  2139. container
  2140. .getContainerProperty(container.getIdByIndex(1), "NAME")
  2141. .getValue());
  2142. assertEquals("Pelle",
  2143. container
  2144. .getContainerProperty(container.getIdByIndex(2), "NAME")
  2145. .getValue());
  2146. assertEquals("Palle",
  2147. container
  2148. .getContainerProperty(container.getIdByIndex(3), "NAME")
  2149. .getValue());
  2150. try {
  2151. container.getIdByIndex(4);
  2152. fail("SQLContainer.getIdByIndex() returned a value for an index beyond the end of the container");
  2153. } catch (IndexOutOfBoundsException e) {
  2154. // should throw exception - item is filtered out
  2155. }
  2156. container.nextItemId(container.getIdByIndex(3));
  2157. assertFalse(container.containsId(id2));
  2158. assertFalse(container.getItemIds().contains(id2));
  2159. assertNull(container.getItem(id2));
  2160. assertEquals(-1, container.indexOfId(id2));
  2161. assertNotSame(id2, container.lastItemId());
  2162. assertSame(id1, container.lastItemId());
  2163. EasyMock.verify(delegate);
  2164. }
  2165. @SuppressWarnings("unchecked")
  2166. @Test
  2167. public void sort_freeformBufferedItems_sortsBufferedItemsLastInOrderAdded()
  2168. throws SQLException {
  2169. FreeformQuery query = new FreeformQuery("SELECT * FROM people",
  2170. connectionPool, "ID");
  2171. FreeformQueryDelegate delegate = EasyMock
  2172. .createMock(FreeformQueryDelegate.class);
  2173. final List<OrderBy> orderBys = new ArrayList<OrderBy>();
  2174. delegate.setFilters(null);
  2175. EasyMock.expectLastCall().anyTimes();
  2176. delegate.setFilters(EasyMock.isA(List.class));
  2177. EasyMock.expectLastCall().anyTimes();
  2178. delegate.setOrderBy(null);
  2179. EasyMock.expectLastCall().anyTimes();
  2180. delegate.setOrderBy(EasyMock.isA(List.class));
  2181. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  2182. @Override
  2183. public Object answer() throws Throwable {
  2184. List<OrderBy> orders = (List<OrderBy>) EasyMock
  2185. .getCurrentArguments()[0];
  2186. orderBys.clear();
  2187. orderBys.addAll(orders);
  2188. return null;
  2189. }
  2190. }).anyTimes();
  2191. EasyMock.expect(
  2192. delegate.getQueryString(EasyMock.anyInt(), EasyMock.anyInt()))
  2193. .andAnswer(new IAnswer<String>() {
  2194. @Override
  2195. public String answer() throws Throwable {
  2196. Object[] args = EasyMock.getCurrentArguments();
  2197. int offset = (Integer) (args[0]);
  2198. int limit = (Integer) (args[1]);
  2199. if (SQLTestsConstants.db == DB.MSSQL) {
  2200. SQLGenerator gen = new MSSQLGenerator();
  2201. if (orderBys == null || orderBys.isEmpty()) {
  2202. List<OrderBy> ob = new ArrayList<OrderBy>();
  2203. ob.add(new OrderBy("ID", true));
  2204. return gen
  2205. .generateSelectQuery("people", null, ob,
  2206. offset, limit, null)
  2207. .getQueryString();
  2208. } else {
  2209. return gen
  2210. .generateSelectQuery("people", null,
  2211. orderBys, offset, limit, null)
  2212. .getQueryString();
  2213. }
  2214. } else if (SQLTestsConstants.db == DB.ORACLE) {
  2215. SQLGenerator gen = new OracleGenerator();
  2216. if (orderBys == null || orderBys.isEmpty()) {
  2217. List<OrderBy> ob = new ArrayList<OrderBy>();
  2218. ob.add(new OrderBy("ID", true));
  2219. return gen
  2220. .generateSelectQuery("people", null, ob,
  2221. offset, limit, null)
  2222. .getQueryString();
  2223. } else {
  2224. return gen
  2225. .generateSelectQuery("people", null,
  2226. orderBys, offset, limit, null)
  2227. .getQueryString();
  2228. }
  2229. } else {
  2230. StringBuilder query = new StringBuilder(
  2231. "SELECT * FROM people");
  2232. if (!orderBys.isEmpty()) {
  2233. query.append(" ORDER BY ");
  2234. for (OrderBy orderBy : orderBys) {
  2235. query.append(
  2236. "\"" + orderBy.getColumn() + "\"");
  2237. if (orderBy.isAscending()) {
  2238. query.append(" ASC");
  2239. } else {
  2240. query.append(" DESC");
  2241. }
  2242. }
  2243. }
  2244. query.append(" LIMIT ").append(limit)
  2245. .append(" OFFSET ").append(offset);
  2246. return query.toString();
  2247. }
  2248. }
  2249. }).anyTimes();
  2250. EasyMock.expect(delegate.getCountQuery())
  2251. .andThrow(new UnsupportedOperationException()).anyTimes();
  2252. EasyMock.replay(delegate);
  2253. query.setDelegate(delegate);
  2254. SQLContainer container = new SQLContainer(query);
  2255. // Ville, Kalle, Pelle, Börje
  2256. assertEquals("Ville",
  2257. container.getContainerProperty(container.firstItemId(), "NAME")
  2258. .getValue());
  2259. assertEquals("Börje",
  2260. container.getContainerProperty(container.lastItemId(), "NAME")
  2261. .getValue());
  2262. Object id1 = container.addItem();
  2263. container.getContainerProperty(id1, "NAME").setValue("Wilbert");
  2264. Object id2 = container.addItem();
  2265. container.getContainerProperty(id2, "NAME").setValue("Albert");
  2266. container.sort(new Object[] { "NAME" }, new boolean[] { true });
  2267. // Börje, Kalle, Pelle, Ville, Wilbert, Albert
  2268. assertEquals("Börje",
  2269. container.getContainerProperty(container.firstItemId(), "NAME")
  2270. .getValue());
  2271. assertEquals("Wilbert",
  2272. container.getContainerProperty(
  2273. container.getIdByIndex(container.size() - 2), "NAME")
  2274. .getValue());
  2275. assertEquals("Albert",
  2276. container.getContainerProperty(container.lastItemId(), "NAME")
  2277. .getValue());
  2278. EasyMock.verify(delegate);
  2279. }
  2280. }