1 # 
   2 # Copyright (c) 2007 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20 # CA 95054 USA or visit www.sun.com if you need additional information or
  21 # have any questions.
  22 #
  23 # @test
  24 # @bug 4212439 5102289 6272156
  25 # @summary Tests for expiration control and reloading expired resource bundles.
  26 # @build ExpirationTest
  27 # @run shell/timeout=300 ExpirationTest.sh
  28 
  29 #
  30 # Timings of the test sequence
  31 #
  32 # 0         5    7      10             20             40  [seconds]
  33 # +---------+----+------+------//------+------//------+--
  34 # g         X    g      X              U              g   [event]
  35 #
  36 #  0 g - java starts; the first getBundle call gets "German";
  37 #        sleep for 7 sec
  38 #  5 X - the bundle expires (every 5 seconds)
  39 #  7 g - java wakes up; the second getBundle call still gets "German";
  40 #        sleep for 33 sec
  41 # 10 X - the bundle expires in the cache
  42 # 20 U - shell script updates DE and add AT
  43 # 40 g - java wakes up; third getBundle call; gets "Deutsch"
  44 #
  45 # event: g - getBundle, X - eXpire, U - Update
  46 #
  47 #
  48 # ExpirationTest.java uses 3 exit values.
  49 #  0 - passed
  50 #  1 - failed
  51 #  2 - can't proceed due to slow platform
  52 #
  53 
  54 # Check environment variables
  55 if [ "x$TESTJAVA" = "x" ]; then
  56     1>&2 echo "No TESTJAVA defined. exiting..."
  57     exit 1
  58 fi
  59 
  60 # Make sure that this test is run in C locale
  61 LANG=C
  62 export LANG
  63 LC_ALL=
  64 export LC_ALL
  65 
  66 : ${TESTCLASSES:=.}
  67 
  68 # YES if the platform has %s support in date
  69 HAS_S=NO
  70 
  71 case "`uname`" in
  72 Windows*)
  73     DEL=";"
  74     ;;
  75 SunOS)
  76     DEL=":"
  77     ;;
  78 Linux)
  79     DEL=":"
  80     HAS_S=YES
  81     ;;
  82 esac
  83 
  84 # Interval until resources are updated
  85 INTERVAL=20
  86 
  87 DATA=ExpirationData
  88 
  89 ROOT=${DATA}.properties
  90 JA=${DATA}_ja.properties
  91 DE=${DATA}_de.properties
  92 AT=${DATA}_de_AT.properties
  93 
  94 JARFILE=data.jar
  95 
  96 createProperties() {
  97     rm -f ${DATA}*.properties
  98     echo "data: English" > $ROOT
  99     (echo "data: Japanese"; echo "january: 1gatsu") > $JA
 100     (echo "data: German"; echo "january: Januar") > $DE
 101     echo "Properties files have been created at `date +%T`"
 102 }
 103 
 104 createJar() {
 105     if [ "$FORMAT" = "properties" ]; then
 106         createProperties
 107         F="${DATA}*.properties"
 108     else
 109         createClasses
 110         F="-C classes ${ROOT}.class -C classes ${JA}.class -C classes ${DE}.class"
 111     fi
 112     ${TESTJAVA}/bin/jar cf $JARFILE $F
 113     ${TESTJAVA}/bin/jar tvf $JARFILE
 114     rm -f ${DATA}*.properties
 115     echo "Jar created at `date +%T`"
 116 }
 117 
 118 createClasses() {
 119     rm -f ${DATA}*.java
 120     rm -rf classes
 121     mkdir classes
 122     createJava $ROOT English
 123     createJava $JA Japanese
 124     createJava $DE German Januar
 125     ${TESTJAVA}/bin/javac -d classes ${ROOT}.java ${JA}.java ${DE}.java
 126     echo "Created" classes/*.class "at `date +%T`"
 127 }
 128 
 129 createJava() {
 130     (echo "
 131 import java.util.*;
 132 
 133 public class $1 extends ListResourceBundle {
 134     public Object[][] getContents() {
 135         return new Object[][] {
 136             { \"data\", \"$2\" },"
 137     if [ "x$3" != "x" ]; then
 138         echo "      { \"january\", \"$3\" },"
 139     fi
 140 echo "  };
 141     }
 142 }") >$1.java
 143 }
 144 
 145 updateDEaddAT() {
 146     rm -f $DE
 147     (echo "data=Deutsch"; echo "january=Januar") > $DE
 148     # add de_AT
 149     echo "january=J\u00e4nner" > $AT
 150     echo "Updated '"${DE}"' and added '"${AT}"' at `date +%T`"
 151 }
 152 
 153 updateClassDEaddClassAT() {
 154     rm -f $DE.java classes/$DE.class
 155     createJava $DE Deutsch Januar
 156     ${TESTJAVA}/bin/javac -d classes ${DE}.java
 157     createJava $AT Deutsch "J\\u00e4nner"
 158     ${TESTJAVA}/bin/javac -d classes ${AT}.java
 159     echo "Updated '"${DE}"' class and added '"${AT}"' class at `date +%T`"
 160 }
 161 
 162 updateJar() {
 163     if [ "$FORMAT" = "properties" ]; then
 164         updateDEaddAT
 165         F="$DE $AT"
 166     else
 167         updateClassDEaddClassAT
 168         F="-C classes ${DE}.class -C classes ${AT}.class"
 169     fi
 170     ${TESTJAVA}/bin/jar uf $JARFILE $F
 171     rm -f $DE $AT
 172     echo "Updated '"${JARFILE}"' at `date +%T`"
 173     ${TESTJAVA}/bin/jar tvf $JARFILE
 174 }
 175 
 176 getSeconds() {
 177     if [ "$HAS_S" = "YES" ]; then
 178         date '+%s'
 179     else
 180         # Returns an approximation of the offset from the Epoch in
 181         # seconds.
 182         date -u '+%Y %j %H %M %S' | \
 183         awk '{d=($1-1970)*365.2425; print int(((((((d+$2-1)*24)+$3)*60)+$3)*60)+$5);}'
 184     fi
 185 }
 186 
 187 #
 188 # Execute $1 and check how long it takes
 189 #
 190 timedExec() {
 191     S=`getSeconds`
 192     eval $1
 193     E=`getSeconds`
 194     D=`expr $E - $S`
 195     #
 196     # If this machine is too slow, give up the further testing.
 197     #
 198     if [ "$D" -gt $2 ]; then
 199         1>&2 echo "This machine took $D seconds to prepare test data," \
 200                   "which is too slow to proceed. Exiting..."
 201         exit 0
 202     fi
 203     unset S
 204     unset E
 205     unset D
 206 }
 207 
 208 checkStatus() {
 209     if [ $1 = 0 ]; then
 210         echo "$2: PASSED"
 211     elif [ $1 != 2 ]; then
 212         echo "$2: FAILED"
 213         exit 1
 214     else
 215         # Just we should't proceed to avoid timing issues.
 216         exit 0
 217     fi
 218 }
 219 
 220 #
 221 # Before starting tests, check the latency with Thread.sleep().
 222 #
 223 ${TESTJAVA}/bin/java -cp "${TESTCLASSES}${DEL}." ExpirationTest -latency
 224 STATUS=$?
 225 if [ $STATUS = 2 ]; then
 226     exit 0
 227 fi
 228 
 229 #
 230 # Tests for properties
 231 #
 232 FORMAT=properties
 233 
 234 #
 235 # Test with plain files (properties)
 236 #
 237 echo "Starting test with properties files at `date +%T`"
 238 
 239 #
 240 # Creates properties files
 241 #
 242 timedExec createProperties 10
 243 
 244 #
 245 # Execute a child process which will update files in $INTERVAL seconds.
 246 #
 247 (sleep $INTERVAL; updateDEaddAT; exit 0) &
 248 
 249 ${TESTJAVA}/bin/java -cp "${TESTCLASSES}${DEL}." ExpirationTest properties file
 250 STATUS=$?
 251 wait
 252 checkStatus $STATUS "Test with properties files"
 253 
 254 #
 255 # Test with jar file if jar is available (properties)
 256 #
 257 if [ -x ${TESTJAVA}/bin/jar ] || [ -x ${TESTJAVA}/bin/jar.exe ]; then
 258     HASJAR=YES
 259 else
 260     HASJAR=NO
 261 fi
 262 
 263 if [ $HASJAR = YES  ]; then
 264     echo ""
 265     echo "Starting test with a jar file (properties) at `date +%T`"
 266 
 267     #
 268     # Create a jar files with properties
 269     #
 270     timedExec createJar 10
 271 
 272     (sleep $INTERVAL; updateJar; exit 0) &
 273     ${TESTJAVA}/bin/java -cp "${TESTCLASSES}${DEL}${JARFILE}" ExpirationTest properties jar
 274     STATUS=$?
 275     wait
 276     checkStatus $STATUS "Test with a jar file (properties)"
 277 fi
 278 
 279 #
 280 # Test for classes
 281 #
 282 
 283 # Note: class-based resource bundles can't be reloaded due to the
 284 # cache support in class loaders. So the results of the test cases
 285 # below are not checked. (Test cases always pass.)
 286 
 287 # If there's no javac available, then give up the test with
 288 # class-based properties.
 289 if [ ! -x ${TESTJAVA}/bin/javac ] && [ ! -x ${TESTJAVA}/bin/javac.exe ]; then
 290     exit 0
 291 fi
 292 
 293 rm -f ${DATA}*.properties $JARFILE
 294 
 295 FORMAT=class
 296 ROOT=`basename $ROOT .properties`
 297 JA=`basename $JA .properties`
 298 DE=`basename $DE .properties`
 299 AT=`basename $AT .properties`
 300 
 301 echo ""
 302 echo "Starting test with class files at `date +%T`"
 303 
 304 #
 305 # Create class files
 306 #
 307 timedExec createClasses 10
 308 
 309 (sleep $INTERVAL; updateClassDEaddClassAT; exit 0) &
 310 ${TESTJAVA}/bin/java -cp "${TESTCLASSES}${DEL}classes" ExpirationTest class file
 311 STATUS=$?
 312 wait
 313 checkStatus $STATUS "Test with class files"
 314 
 315 if [ $HASJAR = YES ]; then
 316     echo ""
 317     echo "Starting test with a jar file (class) at `date +%T`"
 318 
 319     #
 320     # Create a jar file with class files
 321     #
 322     timedExec createJar 10
 323 
 324     (sleep $INTERVAL; updateJar; exit 0) &
 325     ${TESTJAVA}/bin/java -cp "${TESTCLASSES}${DEL}${JARFILE}" ExpirationTest class jar
 326     STATUS=$?
 327     wait
 328     checkStatus $STATUS "Test with a jar file (class)"
 329 fi
 330 
 331 exit 0