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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 com.iciql.Db;
  19. import com.iciql.Iciql;
  20. import org.junit.Test;
  21. import java.text.MessageFormat;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import static com.iciql.Define.primaryKey;
  25. import static com.iciql.Define.tableName;
  26. import static org.junit.Assert.assertEquals;
  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 = IciqlSuite.openNewDb();
  35. db.executeUpdate(MessageFormat.format(create, "VARCHAR(255)"));
  36. db.insertAll(StringRecord.getList());
  37. testSimpleUpdate(db, "VARCHAR fail");
  38. db.executeUpdate("DROP TABLE CLOB_TEST");
  39. db.close();
  40. db = IciqlSuite.openNewDb();
  41. db.executeUpdate(MessageFormat.format(create, db.getDialect().convertSqlType("CLOB")));
  42. db.insertAll(StringRecord.getList());
  43. testSimpleUpdate(db, "CLOB fail because of single quote artifacts");
  44. db.executeUpdate("DROP TABLE CLOB_TEST");
  45. db.close();
  46. }
  47. private void testSimpleUpdate(Db db, String failureMsg) {
  48. String newWords = "I changed the words";
  49. StringRecord r = new StringRecord();
  50. StringRecord originalRecord = db.from(r).where(r.id).is(2).selectFirst();
  51. String oldWords = originalRecord.words;
  52. originalRecord.words = newWords;
  53. db.update(originalRecord);
  54. StringRecord r2 = new StringRecord();
  55. StringRecord revisedRecord = db.from(r2).where(r2.id).is(2).selectFirst();
  56. assertEquals(failureMsg, newWords, revisedRecord.words);
  57. // undo update
  58. originalRecord.words = oldWords;
  59. db.update(originalRecord);
  60. }
  61. /**
  62. * A simple class used in this test.
  63. */
  64. public static class StringRecord implements Iciql {
  65. public Integer id;
  66. public String words;
  67. public StringRecord() {
  68. // public constructor
  69. }
  70. private StringRecord(int id, String words) {
  71. this.id = id;
  72. this.words = words;
  73. }
  74. public void defineIQ() {
  75. tableName("CLOB_TEST");
  76. primaryKey(id);
  77. }
  78. private static StringRecord create(int id, String words) {
  79. return new StringRecord(id, words);
  80. }
  81. public static List<StringRecord> getList() {
  82. StringRecord[] list = {
  83. create(1, "Once upon a midnight dreary, while I pondered weak and weary,"),
  84. create(2, "Over many a quaint and curious volume of forgotten lore,"),
  85. create(3, "While I nodded, nearly napping, suddenly there came a tapping,"),
  86. create(4, "As of some one gently rapping, rapping at my chamber door."),
  87. create(5, "`'Tis some visitor,' I muttered, `tapping at my chamber door -"),
  88. create(6, "Only this, and nothing more.'")};
  89. return Arrays.asList(list);
  90. }
  91. public String toString() {
  92. return id + ": " + words;
  93. }
  94. }
  95. }