1 #!/bin/sh
   2 
   3 #
   4 # Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
   5 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   6 #
   7 # This code is free software; you can redistribute it and/or modify it
   8 # under the terms of the GNU General Public License version 2 only, as
   9 # published by the Free Software Foundation.
  10 #
  11 # This code is distributed in the hope that it will be useful, but WITHOUT
  12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14 # version 2 for more details (a copy is included in the LICENSE file that
  15 # accompanied this code).
  16 #
  17 # You should have received a copy of the GNU General Public License version
  18 # 2 along with this work; if not, write to the Free Software Foundation,
  19 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20 #
  21 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22 # or visit www.oracle.com if you need additional information or have any
  23 # questions.
  24 #
  25 
  26 # @test
  27 # @bug 4991526 6514993
  28 # @summary Unit test for Preferences jar providers
  29 #
  30 # @build PrefsSpi
  31 # @run shell PrefsSpi.sh
  32 # @author Martin Buchholz
  33 
  34 # Command-line usage: sh PrefsSpi.sh /path/to/build
  35 
  36 if [ -z "$TESTJAVA" ]; then
  37     if [ $# -lt 1 ]; then exit 1; fi
  38     TESTJAVA="$1"; shift
  39     TESTSRC="`pwd`"
  40     TESTCLASSES="`pwd`"
  41 fi
  42 
  43  java="$TESTJAVA/bin/java"
  44 javac="$TESTJAVA/bin/javac"
  45   jar="$TESTJAVA/bin/jar"
  46 
  47 Die() { printf "%s\n" "$*"; exit 1; }
  48 
  49 Sys() {
  50     printf "%s\n" "$*"; "$@"; rc="$?";
  51     test "$rc" -eq 0 || Die "Command \"$*\" failed with exitValue $rc";
  52 }
  53 
  54 cat > StubPreferences.java <<'EOF'
  55 import java.util.prefs.*;
  56 
  57 public class StubPreferences extends AbstractPreferences {
  58     public StubPreferences() { super(null, ""); }
  59     public String              getSpi(String x)           { return null; }
  60     public void                putSpi(String x, String y) { }
  61     public void                removeSpi(String x)        { }
  62     public AbstractPreferences childSpi(String x)         { return null; }
  63     public void                removeNodeSpi()            { }
  64     public String[]            keysSpi()                  { return null; }
  65     public String[]            childrenNamesSpi()         { return null; }
  66     public void                syncSpi()                  { }
  67     public void                flushSpi()                 { }
  68 }
  69 EOF
  70 
  71 cat > StubPreferencesFactory.java <<'EOF'
  72 import java.util.prefs.*;
  73 
  74 public class StubPreferencesFactory implements PreferencesFactory {
  75     public Preferences userRoot()   { return new StubPreferences(); }
  76     public Preferences systemRoot() { return new StubPreferences(); }
  77 }
  78 EOF
  79 
  80 Sys rm -rf jarDir extDir
  81 Sys mkdir -p jarDir/META-INF/services extDir
  82 echo "StubPreferencesFactory" \
  83   > "jarDir/META-INF/services/java.util.prefs.PreferencesFactory"
  84 Sys "$javac" -d jarDir StubPreferencesFactory.java StubPreferences.java
  85 
  86 (cd jarDir && "$jar" "cf" "../extDir/PrefsSpi.jar" ".")
  87 
  88 case "`uname`" in Windows*|CYGWIN* ) CPS=';';; *) CPS=':';; esac
  89 
  90 Sys "$java" "-cp" "$TESTCLASSES${CPS}extDir/PrefsSpi.jar" \
  91     -Djava.util.prefs.PreferencesFactory=StubPreferencesFactory \
  92     -Djava.util.prefs.userRoot=. \
  93     PrefsSpi "StubPreferences"
  94 Sys "$java" "-cp" "$TESTCLASSES" \
  95     -Djava.util.prefs.userRoot=. \
  96     PrefsSpi "java.util.prefs.*"
  97 Sys "$java" "-cp" "$TESTCLASSES${CPS}extDir/PrefsSpi.jar" \
  98     -Djava.util.prefs.userRoot=. \
  99     PrefsSpi "StubPreferences"
 100 Sys "$java" "-cp" "$TESTCLASSES" "-Djava.ext.dirs=extDir" \
 101     -Djava.util.prefs.userRoot=. \
 102     PrefsSpi "StubPreferences"
 103 
 104 rm -rf jarDir extDir