1 /*
   2  * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25  * @summary Basic test for PersistentTreeMap
  26  */
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import org.openjdk.jigsaw.PersistentTreeMap;
  31 import org.openjdk.jigsaw.PersistentTreeMap.StringAndInt;
  32 
  33 public class Basic
  34 {
  35     static final boolean debug = true;
  36     static int fail = 0;
  37     static String filename = "Basic_db.db";
  38 
  39     public static void main(String[] args) throws IOException {
  40         debug("Using database: " + filename + "\n");
  41         File dbFile = new File(filename);
  42         try {
  43             test(dbFile);
  44         } finally {
  45             dbFile.delete();
  46         }
  47     }
  48 
  49     static void test(File dbFile) throws IOException {
  50         // test put(String,String), get(String)
  51         try (PersistentTreeMap pmap = PersistentTreeMap.create(dbFile)) {
  52             // verify null for non existent key
  53             if (get("non_existent_key", pmap) != null)
  54                 throw new RuntimeException("Failed: key:" + "non_existent_key" +
  55                                            ", expected: " + null);
  56 
  57             put("helloThereKey", "helloThereValue", pmap);
  58             check("helloThereKey", "helloThereValue", pmap);
  59             put("chegar.Key", "chegar.Value", pmap);
  60             check("chegar.Key", "chegar.Value", pmap);
  61             put("foo.bar.com", "123456789", pmap);
  62             check("foo.bar.com", "123456789", pmap);
  63             put("emptyKey",  "", pmap);
  64             check("emptyKey", "", pmap);
  65 
  66             // multiple put/gets
  67             put("testKeyA1", "testKeyA1Value", pmap);
  68             put("testKeyA2", "testKeyA2Value", pmap);
  69             put("testKeyA3", "testKeyA3Value", pmap);
  70             put("testKeyA4", "testKeyA4Value", pmap);
  71             put("testKeyA5", "testKeyA5Value", pmap);
  72             put("testKeyA6", "testKeyA6Value", pmap);
  73             put("testKeyA7", "testKeyA7Value", pmap);
  74             // duplicate key
  75             put("testKeyA7", "duplicate", pmap);
  76             check("testKeyA3", "testKeyA3Value", pmap);
  77             check("testKeyA4", "testKeyA4Value", pmap);
  78             check("testKeyA5", "testKeyA5Value", pmap);
  79             check("testKeyA6", "testKeyA6Value", pmap);
  80             check("testKeyA7", "duplicate", pmap);
  81             check("testKeyA2", "testKeyA2Value", pmap);
  82             check("testKeyA1", "testKeyA1Value", pmap);
  83         }
  84 
  85         // verify persistence
  86         try (PersistentTreeMap pmap = PersistentTreeMap.open(dbFile)) {
  87             // verify null for non existent key
  88             if (get("non_existent_key", pmap) != null)
  89                 throw new RuntimeException("Failed: key:" + "non_existent_key" +
  90                                            ", expected: " + null);
  91 
  92             check("helloThereKey", "helloThereValue", pmap);
  93             check("chegar.Key", "chegar.Value", pmap);
  94             check("foo.bar.com", "123456789", pmap);
  95             check("emptyKey", "", pmap);
  96             check("testKeyA3", "testKeyA3Value", pmap);
  97             check("testKeyA4", "testKeyA4Value", pmap);
  98             check("testKeyA5", "testKeyA5Value", pmap);
  99             check("testKeyA6", "testKeyA6Value", pmap);
 100             check("testKeyA7", "duplicate", pmap);
 101             check("testKeyA2", "testKeyA2Value", pmap);
 102             check("testKeyA1", "testKeyA1Value", pmap);
 103         }
 104 
 105         // test put(String,int), getInt(String)
 106         try (PersistentTreeMap pmap = PersistentTreeMap.create(dbFile)) {
 107             // verify -1 for non existent key
 108             check("non_existent_key", -1, pmap);
 109 
 110             put("smallKey", 5, pmap);
 111             check("smallKey", 5, pmap);
 112             put("big.Key", 12345678, pmap);
 113             check("big.Key", 12345678, pmap);
 114             put("t.i.n.y.K.e.y", 0, pmap);
 115             check("t.i.n.y.K.e.y", 0, pmap);
 116 
 117             // multiple put/gets
 118             put("testIntKeyA1", 34567, pmap);
 119             put("testIntKeyA2", 34568, pmap);
 120             put("testIntKeyA3", 34569, pmap);
 121             put("testIntKeyA4", 34570, pmap);
 122             put("testIntKeyA5", 34571, pmap);
 123             put("testIntKeyA6", 34572, pmap);
 124             put("testIntKeyA7", 98765, pmap);
 125             //duplicate
 126             put("testIntKeyA7", 34573, pmap);
 127             check("testIntKeyA3", 34569, pmap);
 128             check("testIntKeyA4", 34570, pmap);
 129             check("testIntKeyA5", 34571, pmap);
 130             check("testIntKeyA6", 34572, pmap);
 131             check("testIntKeyA7", 34573, pmap);
 132             check("testIntKeyA2", 34568, pmap);
 133             check("testIntKeyA1", 34567, pmap);
 134         }
 135 
 136         // verify persistence
 137         try (PersistentTreeMap pmap = PersistentTreeMap.open(dbFile)) {
 138             // verify -1 for non existent key
 139             check("non_existent_key", -1, pmap);
 140 
 141             check("testIntKeyA3", 34569, pmap);
 142             check("testIntKeyA4", 34570, pmap);
 143             check("testIntKeyA5", 34571, pmap);
 144             check("testIntKeyA6", 34572, pmap);
 145             check("testIntKeyA7", 34573, pmap);
 146             check("testIntKeyA2", 34568, pmap);
 147             check("testIntKeyA1", 34567, pmap);
 148             check("smallKey", 5, pmap);
 149             check("big.Key", 12345678, pmap);
 150             check("t.i.n.y.K.e.y", 0, pmap);
 151         }
 152 
 153         // test put(String,String,int), getStringAndInt(String)
 154         try (PersistentTreeMap pmap = PersistentTreeMap.create(dbFile)) {
 155             // verify null for non existent key
 156             if (pmap.getStringAndInt("non_existent_key") != null)
 157                 throw new RuntimeException("Failed: key:" + "non_existent_key" +
 158                                            ", expected: " + null);
 159 
 160             put("stringAndIntKey", "stringAndIntValue", 5, pmap);
 161             check("stringAndIntKey", "stringAndIntValue", 5, pmap);
 162             put("hello.There.Key", "helloThereValue", 4, pmap);
 163             check("hello.There.Key", "helloThereValue", 4, pmap);
 164             put("chegar.Key", "chegar.Value", 56, pmap);
 165             check("chegar.Key", "chegar.Value", 56, pmap);
 166             put("foo.bar.com", "123456789", 987654321, pmap);
 167             check("foo.bar.com", "123456789", 987654321, pmap);
 168             put("e.m.p.t.y.K.e.y",  "", 78, pmap);
 169             check("e.m.p.t.y.K.e.y", "", 78, pmap);
 170 
 171             // multiple put/gets
 172             put("testKeyA1", "testKeyA1Value", 45, pmap);
 173             put("testKeyA2", "testKeyA2Value", 46, pmap);
 174             put("testKeyA3", "testKeyA3Value", 47, pmap);
 175             put("testKeyA4", "testKeyA4Value", 48, pmap);
 176             put("testKeyA5", "testKeyA5Value", 49, pmap);
 177             put("testKeyA6", "testKeyA6Value", 50, pmap);
 178             put("testKeyA7", "testKeyA7Value", 51, pmap);
 179             //duplicate
 180             put("testKeyA7",  "duplicate", 15, pmap);
 181             check("testKeyA3", "testKeyA3Value", 47, pmap);
 182             check("testKeyA4", "testKeyA4Value", 48, pmap);
 183             check("testKeyA5", "testKeyA5Value", 49, pmap);
 184             check("testKeyA6", "testKeyA6Value", 50, pmap);
 185             check("testKeyA7", "duplicate", 15, pmap);
 186             check("testKeyA2", "testKeyA2Value", 46, pmap);
 187             check("testKeyA1", "testKeyA1Value", 45, pmap);
 188         }
 189 
 190         // verify persistence, read only (open)
 191         try (PersistentTreeMap pmap = PersistentTreeMap.open(dbFile)) {
 192             // verify null for non existent key
 193             if (pmap.getStringAndInt("non_existent_key") != null)
 194                 throw new RuntimeException("Failed: key:" + "non_existent_key" +
 195                                            ", expected: " + null);
 196 
 197             check("stringAndIntKey", "stringAndIntValue", 5, pmap);
 198             check("hello.There.Key", "helloThereValue", 4, pmap);
 199             check("chegar.Key", "chegar.Value", 56, pmap);
 200             check("foo.bar.com", "123456789", 987654321, pmap);
 201             check("e.m.p.t.y.K.e.y", "", 78, pmap);
 202             check("testKeyA3", "testKeyA3Value", 47, pmap);
 203             check("testKeyA4", "testKeyA4Value", 48, pmap);
 204             check("testKeyA5", "testKeyA5Value", 49, pmap);
 205             check("testKeyA6", "testKeyA6Value", 50, pmap);
 206             check("testKeyA7", "duplicate", 15, pmap);
 207             check("testKeyA2", "testKeyA2Value", 46, pmap);
 208             check("testKeyA1", "testKeyA1Value", 45, pmap);
 209 
 210             // Read only
 211             checkThrow("anyKey", "anyValue", pmap);
 212             checkThrow("anyKey", 56, pmap);
 213             checkThrow("anyKey", "anyValue", 56, pmap);
 214         }
 215 
 216         if (fail > 0)
 217             throw new RuntimeException("Failed: " + fail + " tests failed. " +
 218                                        "Check output");
 219     }
 220 
 221     static void put(String key, String value, PersistentTreeMap pmap)
 222         throws IOException
 223     {
 224         debug("putting: " + key + ", " + value + "\n");
 225         pmap.put(key, value);
 226     }
 227 
 228     static void put(String key, int value, PersistentTreeMap pmap)
 229         throws IOException
 230     {
 231         debug("putting: " + key + ", " + value + "\n");
 232         pmap.put(key, value);
 233     }
 234 
 235     static void put(String key, String sval, int ival, PersistentTreeMap pmap)
 236         throws IOException
 237     {
 238         debug("putting: " + key + ", [" + sval + "," + ival + "]" + "\n");
 239         pmap.put(key, sval, ival);
 240     }
 241 
 242     static String get(String key, PersistentTreeMap pmap)
 243         throws IOException
 244     {
 245         debug("getting: " + key + ", ");
 246         String value =  pmap.get(key);
 247         debug(value + "\n");
 248         return value;
 249     }
 250 
 251     static int getInt(String key, PersistentTreeMap pmap)
 252         throws IOException
 253     {
 254         debug("getting: " + key + ", ");
 255         int value =  pmap.getInt(key);
 256         debug(value + "\n");
 257         return value;
 258     }
 259 
 260     static StringAndInt getStringAndInt(String key, PersistentTreeMap pmap)
 261         throws IOException
 262     {
 263         debug("getting: " + key + ", ");
 264         StringAndInt value =  pmap.getStringAndInt(key);
 265         debug("[" + value.s + "," + value.i + "]" + "\n");
 266         return value;
 267     }
 268 
 269     static void check(String key, String expected, PersistentTreeMap pmap)
 270         throws IOException
 271     {
 272         String value = get(key, pmap);
 273         if (!expected.equals(value)) {
 274             fail("Failed: key:" + key + ", expected: " +
 275                  expected + ", got:" + value);
 276         }
 277     }
 278 
 279     static void check(String key, int expected, PersistentTreeMap pmap)
 280         throws IOException
 281     {
 282         int value = getInt(key, pmap);
 283         if (expected != value) {
 284             fail("Failed: key:" + key + ", expected: " +
 285                  expected + ", got:" + value);
 286         }
 287     }
 288 
 289     static void check(String key, String expectedString,
 290                       int expectedInt, PersistentTreeMap pmap)
 291         throws IOException
 292     {
 293         StringAndInt value = getStringAndInt(key, pmap);
 294         if (!expectedString.equals(value.s) || expectedInt != value.i) {
 295             fail("Failed: key:" + key + ", expected: " +
 296                  "[" + expectedString + "," + expectedInt + "]" + ", got:" +
 297                  "[" + value.s + "," + value.i + "]");
 298         }
 299     }
 300 
 301     static void checkThrow(String key, String value, PersistentTreeMap pmap)
 302         throws IOException
 303     {
 304         try {
 305             pmap.put(key, value);
 306             fail("Failed: expected throw when putting to read only database");
 307         } catch (IOException e) {
 308             debug("[expected] caught " + e + "\n");
 309         }
 310     }
 311 
 312     static void checkThrow(String key, int value, PersistentTreeMap pmap)
 313         throws IOException
 314     {
 315         try {
 316             pmap.put(key, value);
 317             fail("Failed: expected throw when putting to read only database");
 318         } catch (IOException e) {
 319             debug("[expected] caught " + e + "\n");
 320         }
 321     }
 322 
 323     static void checkThrow(String key, String sval,
 324                            int ival, PersistentTreeMap pmap)
 325         throws IOException
 326     {
 327         try {
 328             pmap.put(key, sval, ival);
 329             fail("Failed: expected throw when putting to read only database");
 330         } catch (IOException e) {
 331             debug("[expected] caught " + e + "\n");
 332         }
 333     }
 334 
 335     static void debug(String message) {
 336         if (debug)
 337             System.out.print(message);
 338     }
 339 
 340     static void fail(String message) {
 341         System.err.println(message);
 342         fail++;
 343         //Thread.dumpStack();
 344     }
 345 }