1 /*
   2  * Copyright (c) 2011, 2013, 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 /**
  25  @test
  26  @summary Tests for the 1.4 prefs package
  27  @summary com.apple.junit.java.util.prefs;
  28  @run main Prefs02
  29  */
  30 
  31 import junit.framework.*;
  32 
  33 import java.util.prefs.Preferences;
  34 
  35 public class Prefs02 extends TestCase {
  36 
  37     public static Test suite() {
  38             return new TestSuite(Prefs02.class);
  39     }
  40 
  41     public static void main (String[] args) throws RuntimeException {
  42         TestResult tr = junit.textui.TestRunner.run(suite());
  43         if((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
  44             throw new RuntimeException("### Unexpected JUnit errors or failures.");
  45         }
  46     }
  47 
  48     // Preference key name
  49     final static String STRING_KEY = "KEY_STRING";          // String
  50     final static String BOOLEAN_KEY = "KEY_BOOLEAN";        // boolean
  51     final static String INT_KEY = "KEY_INT";                // int
  52     final static String LONG_KEY = "KEY_LONG";              // long
  53     final static String FLOAT_KEY = "KEY_FLOAT";            // float
  54     final static String DOUBLE_KEY = "KEY_DOUBLE";          // double
  55     final static String BYTE_ARRAY_KEY = "KEY_BYTE_ARRAY";  // byte[]
  56 
  57     final static String sString = "a string";               // String
  58     final static boolean sBoolean = true;                   // boolean
  59     final static int sInt = 123;                            // int
  60     final static long sLong = 123L;                         // long
  61     final static float sFloat = 12.3F;                      // float
  62     final static double sDouble =  12.3;                    // double
  63     final static byte[] sByteArray = new byte[1024];        // byte[]
  64 
  65     private Preferences prefs = Preferences.userNodeForPackage( Prefs02.class ).node("Prefs02");
  66 
  67     public void testPrefs02 () throws Exception {
  68 
  69         // Initialize the byte array
  70         for(int i = 0; i < 1024; i++) {
  71             sByteArray[i] = 90;
  72         }
  73 
  74         // Write out some preference values
  75         prefs.put(STRING_KEY, sString);                  // String
  76         prefs.putBoolean(BOOLEAN_KEY, sBoolean);         // boolean
  77         prefs.putInt(INT_KEY, sInt);                     // int
  78         prefs.putLong(LONG_KEY, sLong);                  // long
  79         prefs.putFloat(FLOAT_KEY, sFloat);               // float
  80         prefs.putDouble(DOUBLE_KEY, sDouble);            // double
  81         prefs.putByteArray(BYTE_ARRAY_KEY, sByteArray);  // byte[]
  82         prefs.flush();
  83 
  84 
  85         // System.out.println("Checking the integrity of all the values we stored.");
  86         byte[] bytes = new byte[10];
  87 
  88         assertEquals( prefs.get(STRING_KEY, "foo"),         sString );
  89         assertEquals( prefs.getBoolean(BOOLEAN_KEY, false), sBoolean );
  90         assertEquals( prefs.getInt(INT_KEY, 0),             sInt );
  91         assertEquals( prefs.getLong(LONG_KEY, 0L),          sLong );
  92         assertTrue( prefs.getFloat(FLOAT_KEY, 0.0F) ==      sFloat );
  93         assertTrue( prefs.getDouble(DOUBLE_KEY, 0.0) ==     sDouble );
  94         assertTrue( prefs.getLong(LONG_KEY, 0L) ==          sLong );
  95 
  96         bytes = prefs.getByteArray(BYTE_ARRAY_KEY, bytes);
  97 
  98         assertEquals( bytes.length,    sByteArray.length );
  99         for(int index = 0; index < bytes.length; index ++) {
 100             assertEquals( "Byte value at index " + index + "does not match original value.", bytes[index], sByteArray[index]);
 101         }
 102 
 103 
 104         // Clean up after ourselves. Remove the node
 105         prefs.removeNode();
 106         assertFalse("nodeExists() returned true true after node was supposedly removed.", Preferences.userRoot().nodeExists("/com/apple/java/test/harness/tests/java/util/prefs/Prefs02"));
 107     }
 108 
 109 }
 110 
 111 
 112