1 #!/bin/sh
   2 
   3 #
   4 # Copyright (c) 2010, 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 
  27 # @test
  28 # @bug 6964018
  29 # @summary Unit test for common tools infrastructure.
  30 #
  31 # @build SimpleApplication SleeperApplication ShutdownSimpleApplication
  32 # @run shell CommonTests.sh
  33 
  34 . ${TESTSRC}/CommonSetup.sh
  35 . ${TESTSRC}/ApplicationSetup.sh
  36 
  37 # hope for the best:
  38 status=0
  39 
  40 
  41 # Test program path constants from CommonSetup.sh:
  42 #
  43 for name in JAVA JHAT JINFO JMAP JPS JSTACK; do
  44     eval value=$`echo $name`
  45 
  46     echo "INFO: $name=$value"
  47     if [ -x "$value" ]; then
  48         echo "INFO: '$value' is executable."
  49     else
  50         echo "ERROR: '$value' is not executable." >&2
  51         status=1
  52     fi
  53 done
  54 
  55 
  56 # Display flag values from CommonSetup.sh:
  57 #
  58 for name in isCygwin isMKS isLinux isSolaris isUnknownOS isWindows; do
  59     eval value=$`echo $name`
  60     echo "INFO: flag $name=$value"
  61 done
  62 
  63 
  64 # Test OS constant from CommonSetup.sh:
  65 #
  66 if [ -z "$OS" ]; then
  67     echo "ERROR: OS constant cannot be empty." >&2
  68     status=1
  69 fi
  70 
  71 
  72 # Display the PATTERN_EOL value:
  73 #
  74 echo "INFO: PATTERN_EOL="`echo "$PATTERN_EOL" | od -c`
  75 
  76 
  77 # Test PATTERN_EOL with 'grep' for a regular line.
  78 #
  79 TESTOUT="${TESTCLASSES}/testout.grep_reg_line_eol"
  80 set +e
  81 echo 'regular line' | grep "line${PATTERN_EOL}" > "$TESTOUT"
  82 set -e
  83 if [ -s "$TESTOUT" ]; then
  84     echo "INFO: PATTERN_EOL works for regular line with grep."
  85 else
  86     echo "ERROR: PATTERN_EOL does not work for regular line with grep." >&2
  87     status=1
  88 fi
  89 
  90 
  91 if $isWindows; then
  92     # Test PATTERN_EOL with 'grep' for a CR line.
  93     #
  94     TESTOUT="${TESTCLASSES}/testout.grep_cr_line_eol"
  95     set +e
  96     echo 'CR line
' | grep "line${PATTERN_EOL}" > "$TESTOUT"
  97     set -e
  98     if [ -s "$TESTOUT" ]; then
  99         echo "INFO: PATTERN_EOL works for CR line with grep."
 100     else
 101         echo "ERROR: PATTERN_EOL does not work for CR line with grep." >&2
 102         status=1
 103     fi
 104 fi
 105 
 106 
 107 # Test PATTERN_EOL with 'sed' for a regular line.
 108 #
 109 TESTOUT="${TESTCLASSES}/testout.sed_reg_line_eol"
 110 echo 'regular line' | sed -n "/line${PATTERN_EOL}/p" > "$TESTOUT"
 111 if [ -s "$TESTOUT" ]; then
 112     echo "INFO: PATTERN_EOL works for regular line with sed."
 113 else
 114     echo "ERROR: PATTERN_EOL does not work for regular line with sed." >&2
 115     status=1
 116 fi
 117 
 118 
 119 if $isWindows; then
 120     # Test PATTERN_EOL with 'sed' for a CR line.
 121     #
 122     TESTOUT="${TESTCLASSES}/testout.sed_cr_line_eol"
 123     echo 'CR line
' | sed -n "/line${PATTERN_EOL}/p" > "$TESTOUT"
 124     if [ -s "$TESTOUT" ]; then
 125         echo "INFO: PATTERN_EOL works for CR line with sed."
 126     else
 127         echo "ERROR: PATTERN_EOL does not work for CR line with sed." >&2
 128         status=1
 129     fi
 130 fi
 131 
 132 
 133 # Display the PATTERN_WS value:
 134 #
 135 echo "INFO: PATTERN_WS="`echo "$PATTERN_WS" | od -c`
 136 
 137 
 138 # Test PATTERN_WS with 'grep' for a blank.
 139 #
 140 TESTOUT="${TESTCLASSES}/testout.grep_blank"
 141 set +e
 142 echo 'blank: ' | grep "$PATTERN_WS" > "$TESTOUT"
 143 set -e
 144 if [ -s "$TESTOUT" ]; then
 145     echo "INFO: PATTERN_WS works for blanks with grep."
 146 else
 147     echo "ERROR: PATTERN_WS does not work for blanks with grep." >&2
 148     status=1
 149 fi
 150 
 151 
 152 # Test PATTERN_WS with 'grep' for a tab.
 153 #
 154 TESTOUT="${TESTCLASSES}/testout.grep_tab"
 155 set +e
 156 echo 'tab:      ' | grep "$PATTERN_WS" > "$TESTOUT"
 157 set -e
 158 if [ -s "$TESTOUT" ]; then
 159     echo "INFO: PATTERN_WS works for tabs with grep."
 160 else
 161     echo "ERROR: PATTERN_WS does not work for tabs with grep." >&2
 162     status=1
 163 fi
 164 
 165 
 166 # Test PATTERN_WS with 'sed' for a blank.
 167 #
 168 TESTOUT="${TESTCLASSES}/testout.sed_blank"
 169 echo 'blank: ' | sed -n "/$PATTERN_WS/p" > "$TESTOUT"
 170 if [ -s "$TESTOUT" ]; then
 171     echo "INFO: PATTERN_WS works for blanks with sed."
 172 else
 173     echo "ERROR: PATTERN_WS does not work for blanks with sed." >&2
 174     status=1
 175 fi
 176 
 177 
 178 # Test PATTERN_WS with 'sed' for a tab.
 179 #
 180 TESTOUT="${TESTCLASSES}/testout.sed_tab"
 181 echo 'tab:      ' | sed -n "/$PATTERN_WS/p" > "$TESTOUT"
 182 if [ -s "$TESTOUT" ]; then
 183     echo "INFO: PATTERN_WS works for tabs with sed."
 184 else
 185     echo "ERROR: PATTERN_WS does not work for tabs with sed." >&2
 186     status=1
 187 fi
 188 
 189 
 190 # Test startApplication and use PORTFILE for coordination
 191 # The app sleeps for 30 seconds.
 192 #
 193 PORTFILE="${TESTCLASSES}"/shutdown.port
 194 startApplication SleeperApplication "${PORTFILE}" 30
 195 
 196 
 197 # Test appJavaPid in "ps" cmd output.
 198 #
 199 TESTOUT="${TESTCLASSES}/testout.ps_app"
 200 set +e
 201 if $isCygwin; then
 202     # On Cygwin, appJavaPid is the Windows pid for the Java process
 203     # and appOtherPid is the Cygwin pid for the Java process.
 204     ps -p "$appOtherPid" \
 205         | grep "${PATTERN_WS}${appJavaPid}${PATTERN_WS}" > "$TESTOUT"
 206 else
 207     # output only pid and comm columns to avoid mismatches
 208     ps -eo pid,comm \
 209         | grep "^${PATTERN_WS}*${appJavaPid}${PATTERN_WS}" > "$TESTOUT"
 210 fi
 211 set -e
 212 if [ -s "$TESTOUT" ]; then
 213     echo "INFO: begin appJavaPid=$appJavaPid in 'ps' cmd output:"
 214     cat "$TESTOUT"
 215     echo "INFO: end appJavaPid=$appJavaPid in 'ps' cmd output."
 216 else
 217     echo "ERROR: 'ps' cmd should show appJavaPid=$appJavaPid." >&2
 218     status=1
 219 fi
 220 
 221 if [ -n "$appOtherPid" ]; then
 222     # Test appOtherPid in "ps" cmd output, if we have one.
 223     #
 224     TESTOUT="${TESTCLASSES}/testout.ps_other"
 225     set +e
 226     if $isCygwin; then
 227         ps -p "$appOtherPid" \
 228             | grep "${PATTERN_WS}${appOtherPid}${PATTERN_WS}" > "$TESTOUT"
 229     else
 230         # output only pid and comm columns to avoid mismatches
 231         ps -eo pid,comm \
 232             | grep "^${PATTERN_WS}*${appOtherPid}${PATTERN_WS}" > "$TESTOUT"
 233     fi
 234     set -e
 235     if [ -s "$TESTOUT" ]; then
 236         echo "INFO: begin appOtherPid=$appOtherPid in 'ps' cmd output:"
 237         cat "$TESTOUT"
 238         echo "INFO: end appOtherPid=$appOtherPid in 'ps' cmd output."
 239     else
 240         echo "ERROR: 'ps' cmd should show appOtherPid=$appOtherPid." >&2
 241         status=1
 242     fi
 243 fi
 244 
 245 
 246 # Test stopApplication and PORTFILE for coordination
 247 #
 248 stopApplication "${PORTFILE}"
 249 
 250 
 251 # Test application still running after stopApplication.
 252 #
 253 # stopApplication just lets the app know that it can stop, but the
 254 # app might still be doing work. This test just demonstrates that
 255 # fact and doesn't fail if the app is already done.
 256 #
 257 TESTOUT="${TESTCLASSES}/testout.after_stop"
 258 set +e
 259 if $isCygwin; then
 260     # On Cygwin, appJavaPid is the Windows pid for the Java process
 261     # and appOtherPid is the Cygwin pid for the Java process.
 262     ps -p "$appOtherPid" \
 263         | grep "${PATTERN_WS}${appJavaPid}${PATTERN_WS}" > "$TESTOUT"
 264 else
 265     # output only pid and comm columns to avoid mismatches
 266     ps -eo pid,comm \
 267         | grep "^${PATTERN_WS}*${appJavaPid}${PATTERN_WS}" > "$TESTOUT"
 268 fi
 269 set -e
 270 if [ -s "$TESTOUT" ]; then
 271     echo "INFO: it is okay for appJavaPid=$appJavaPid to still be running" \
 272         "after stopApplication() is called."
 273     echo "INFO: begin 'after_stop' output:"
 274     cat "$TESTOUT"
 275     echo "INFO: end 'after_stop' output."
 276 fi
 277 
 278 
 279 # Test waitForApplication
 280 #
 281 # The app might already be gone so this function shouldn't generate
 282 # a fatal error in either call.
 283 #
 284 waitForApplication
 285 
 286 if [ $isWindows = false ]; then
 287     # Windows can recycle pids quickly so we can't use this test there
 288     TESTOUT="${TESTCLASSES}/testout.after_kill"
 289     set +e
 290     # output only pid and comm columns to avoid mismatches
 291     ps -eo pid,comm \
 292         | grep "^${PATTERN_WS}*${appJavaPid}${PATTERN_WS}" > "$TESTOUT"
 293     set -e
 294     if [ -s "$TESTOUT" ]; then
 295         echo "ERROR: 'ps' cmd should not show appJavaPid." >&2
 296         echo "ERROR: begin 'after_kill' output:" >&2
 297         cat "$TESTOUT" >&2
 298         echo "ERROR: end 'after_kill' output." >&2
 299         status=1
 300     else
 301         echo "INFO: 'ps' cmd does not show appJavaPid after" \
 302             "waitForApplication() is called."
 303     fi
 304 fi
 305 
 306 
 307 # Test killApplication
 308 #
 309 # The app is already be gone so this function shouldn't generate
 310 # a fatal error.
 311 #
 312 killApplication
 313 
 314 exit $status