1 package com.softmotions.ejdb2; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.File; 5 import java.util.LinkedHashMap; 6 import java.util.Map; 7 import java.util.Objects; 8 9 import com.softmotions.ejdb2.JSON.ObjectBuilder; 10 import com.softmotions.ejdb2.JSON.ValueType; 11 12 /** 13 * @author Adamansky Anton (adamansky@softmotions.com) 14 */ 15 public class TestEJDB2 { 16 TestEJDB2()17 private TestEJDB2() { 18 } 19 jsonBasicTest()20 private static void jsonBasicTest() throws Exception { 21 JSON json = JSON.fromString("{\"foo\":\"bar\"}"); 22 assert json.type == ValueType.OBJECT; 23 assert json.value != null; 24 Map<String, Object> map = (Map<String, Object>) json.value; 25 assert map.get("foo").equals("bar"); 26 27 ObjectBuilder b = JSON.buildObject(); 28 b.put("foo", "bar").putArray("baz").add(1).add("one").toJSON(); 29 30 json = b.toJSON().at("/baz/1"); 31 assert json.isString(); 32 assert "one".equals(json.value); 33 } 34 dbTest()35 private static void dbTest() throws Exception { 36 try (EJDB2 db = new EJDB2Builder("test.db").truncate().withWAL().open()) { 37 EJDB2Exception exception = null; 38 String json = "{'foo':'bar'}".replace('\'', '"'); 39 String patch = "[{'op':'add', 'path':'/baz', 'value':'qux'}]".replace('\'', '"'); 40 41 long id = db.put("c1", json); 42 assert (id == 1L); 43 44 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 45 db.get("c1", 1L, bos); 46 assert (bos.toString().equals(json)); 47 48 db.patch("c1", patch, id); 49 bos.reset(); 50 db.get("c1", 1L, bos); 51 assert (bos.toString().equals("{'foo':'bar','baz':'qux'}".replace('\'', '"'))); 52 bos.reset(); 53 54 db.del("c1", id); 55 56 exception = null; 57 try { 58 db.get("c1", id, bos); 59 } catch (EJDB2Exception e) { 60 exception = e; 61 } 62 assert (exception != null); 63 assert (exception.getMessage().contains("IWKV_ERROR_NOTFOUND")); 64 65 // JQL resources can be closed explicitly or garbage collected 66 JQL q = db.createQuery("@mycoll/*"); 67 assert (q.getDb() != null); 68 assert (Objects.equals(q.getCollection(), "mycoll")); 69 70 id = db.put("mycoll", "{'foo':'bar'}".replace('\'', '"')); 71 assert (id == 1); 72 73 exception = null; 74 try { 75 db.put("mycoll", "{\""); 76 } catch (EJDB2Exception e) { 77 exception = e; 78 } 79 assert (exception != null && exception.getMessage() != null); 80 assert (exception.getCode() == 86005); 81 assert (exception.getMessage().contains("JBL_ERROR_PARSE_UNQUOTED_STRING")); 82 83 db.put("mycoll", "{'foo':'baz'}".replace('\'', '"')); 84 85 Map<Long, String> results = new LinkedHashMap<>(); 86 q.executeRaw((docId, doc) -> { 87 assert (docId > 0 && doc != null); 88 results.put(docId, doc); 89 return 1; 90 }); 91 assert (results.size() == 2); 92 assert (Objects.equals(results.get(1L), "{\"foo\":\"bar\"}")); 93 assert (Objects.equals(results.get(2L), "{\"foo\":\"baz\"}")); 94 results.clear(); 95 96 try (JQL q2 = db.createQuery("/[foo=:?]", "mycoll").setString(0, "zaz")) { 97 q2.executeRaw((docId, doc) -> { 98 results.put(docId, doc); 99 return 1; 100 }); 101 } 102 assert (results.isEmpty()); 103 104 try (JQL q2 = db.createQuery("/[foo=:val]", "mycoll").setString("val", "bar")) { 105 q2.executeRaw((docId, doc) -> { 106 results.put(docId, doc); 107 return 1; 108 }); 109 } 110 assert (results.size() == 1); 111 assert (Objects.equals(results.get(1L), "{\"foo\":\"bar\"}")); 112 113 exception = null; 114 try { 115 db.createQuery("@mycoll/["); 116 } catch (EJDB2Exception e) { 117 exception = e; 118 } 119 assert (exception != null && exception.getMessage() != null); 120 assert (exception.getCode() == 87001); 121 assert (exception.getMessage().contains("@mycoll/[ <---")); 122 123 long count = db.createQuery("@mycoll/* | count").executeScalarInt(); 124 assert (count == 2); 125 126 q.withExplain().execute(); 127 assert (q.getExplainLog().contains("[INDEX] NO [COLLECTOR] PLAIN")); 128 129 json = db.infoAsString(); 130 assert (json != null); 131 assert (json.contains("{\"name\":\"mycoll\",\"dbid\":4,\"rnum\":2,\"indexes\":[]}")); 132 133 // Indexes 134 db.ensureStringIndex("mycoll", "/foo", true); 135 json = db.infoAsString(); 136 assert (json.contains("\"indexes\":[{\"ptr\":\"/foo\",\"mode\":5,\"idbf\":0,\"dbid\":5,\"rnum\":2}]")); 137 138 db.patch("mycoll", patch, 2); 139 140 json = db.createQuery("@mycoll/[foo=:?] and /[baz=:?]").setString(0, "baz").setString(1, "qux").firstValue(); 141 assert ("{\"foo\":\"baz\",\"baz\":\"qux\"}".equals(json)); 142 143 json = db.createQuery("@mycoll/[foo re :?]").setRegexp(0, ".*").firstValue(); 144 assert ("{\"foo\":\"baz\",\"baz\":\"qux\"}".equals(json)); 145 146 db.removeStringIndex("mycoll", "/foo", true); 147 json = db.infoAsString(); 148 assert (json.contains("{\"name\":\"mycoll\",\"dbid\":4,\"rnum\":2,\"indexes\":[]}")); 149 150 db.removeCollection("mycoll"); 151 db.removeCollection("c1"); 152 json = db.infoAsString(); 153 assert (json.contains("\"collections\":[]")); 154 155 // Test rename collection 156 bos.reset(); 157 db.put("cc1", "{\"foo\": 1}"); 158 db.renameCollection("cc1", "cc2"); 159 db.get("cc2", 1, bos); 160 assert (bos.toString().equals("{\"foo\":1}")); 161 162 // Check limit 163 q = db.createQuery("@mycoll/* | limit 2 skip 3"); 164 assert (q.getLimit() == 2); 165 assert (q.getSkip() == 3); 166 167 long ts0 = System.currentTimeMillis(); 168 long ts = db.onlineBackup("test-bkp.db"); 169 assert (ts > ts0); 170 assert (new File("test-bkp.db").exists()); 171 } 172 173 try (EJDB2 db = new EJDB2Builder("test-bkp.db").withWAL().open()) { 174 String val = db.getAsString("cc2", 1); 175 assert (val.equals("{\"foo\":1}")); 176 } 177 } 178 main(String[] args)179 public static void main(String[] args) throws Exception { 180 jsonBasicTest(); 181 dbTest(); 182 } 183 } 184