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.

ClobTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright 2004-2011 H2 Group.
  3. * Copyright 2011 James Moger.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.iciql.test;
  18. import static org.junit.Assert.assertEquals;
  19. import static com.iciql.Define.primaryKey;
  20. import static com.iciql.Define.tableName;
  21. import java.text.MessageFormat;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import org.junit.Test;
  25. import com.iciql.Db;
  26. import com.iciql.Iciql;
  27. /**
  28. * Tests if converting a CLOB to a String works.
  29. */
  30. public class ClobTest {
  31. @Test
  32. public void testClob() throws Exception {
  33. String create = "CREATE TABLE CLOB_TEST(ID INT PRIMARY KEY, WORDS {0})";
  34. Db db = Db.open("jdbc:h2:mem:", "sa", "sa");
  35. db.executeUpdate(MessageFormat.format(create, "VARCHAR(255)"));
  36. db.insertAll(StringRecord.getList());
  37. testSimpleUpdate(db, "VARCHAR fail");
  38. db.close();
  39. db = Db.open("jdbc:h2:mem:", "sa", "sa");
  40. db.executeUpdate(MessageFormat.format(create, "TEXT"));
  41. db.insertAll(StringRecord.getList());
  42. testSimpleUpdate(db, "CLOB fail because of single quote artifacts");
  43. db.close();
  44. }
  45. private void testSimpleUpdate(Db db, String failureMsg) {
  46. String newWords = "I changed the words";
  47. StringRecord r = new StringRecord();
  48. StringRecord originalRecord = db.from(r).where(r.id).is(2).selectFirst();
  49. String oldWords = originalRecord.words;
  50. originalRecord.words = newWords;
  51. db.update(originalRecord);
  52. StringRecord r2 = new StringRecord();
  53. StringRecord revisedRecord = db.from(r2).where(r2.id).is(2).selectFirst();
  54. assertEquals(failureMsg, newWords, revisedRecord.words);
  55. // undo update
  56. originalRecord.words = oldWords;
  57. db.update(originalRecord);
  58. }
  59. /**
  60. * A simple class used in this test.
  61. */
  62. public static class StringRecord implements Iciql {
  63. public Integer id;
  64. public String words;
  65. public StringRecord() {
  66. // public constructor
  67. }
  68. private StringRecord(int id, String words) {
  69. this.id = id;
  70. this.words = words;
  71. }
  72. public void defineIQ() {
  73. tableName("CLOB_TEST");
  74. primaryKey(id);
  75. }
  76. private static StringRecord create(int id, String words) {
  77. return new StringRecord(id, words);
  78. }
  79. public static List<StringRecord> getList() {
  80. StringRecord[] list = { create(1, "Once upon a midnight dreary, while I pondered weak and weary,"),
  81. create(2, "Over many a quaint and curious volume of forgotten lore,"),
  82. create(3, "While I nodded, nearly napping, suddenly there came a tapping,"),
  83. create(4, "As of some one gently rapping, rapping at my chamber door."),
  84. create(5, "`'Tis some visitor,' I muttered, `tapping at my chamber door -"),
  85. create(6, "Only this, and nothing more.'") };
  86. return Arrays.asList(list);
  87. }
  88. public String toString() {
  89. return id + ": " + words;
  90. }
  91. }
  92. }