1 #! /bin/sh
   2 
   3 # Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   4 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 #
   6 # This code is free software; you can redistribute it and/or modify it
   7 # under the terms of the GNU General Public License version 2 only, as
   8 # published by the Free Software Foundation.
   9 #
  10 # This code is distributed in the hope that it will be useful, but WITHOUT
  11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13 # version 2 for more details (a copy is included in the LICENSE file that
  14 # accompanied this code).
  15 #
  16 # You should have received a copy of the GNU General Public License version
  17 # 2 along with this work; if not, write to the Free Software Foundation,
  18 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19 #
  20 # Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21 # or visit www.oracle.com if you need additional information or have any
  22 # questions.
  23 
  24 # @test
  25 # @summary Hello
  26 
  27 set -e
  28 
  29 sh ${TESTSRC:-.}/tester.sh $0
  30 
  31 BIN=${TESTJAVA:-../../../../build}/bin
  32 
  33 mkdir -p z.test/native/src z.test/native/lib
  34 $BIN/javah -classpath z.test/modules/org.astro -d z.test/native/src \
  35   org.astro.World
  36 
  37 cat >z.test/native/src/org_astro_World.c <<___
  38 
  39 #include <string.h>
  40 #include "org_astro_World.h"
  41 
  42 JNIEXPORT jbyteArray JNICALL
  43 Java_org_astro_World_getName(JNIEnv *env, jclass cl)
  44 {
  45     const char *s = "native World";
  46     int n = strlen(s);
  47     jbyteArray jb;
  48     jb = (*env)->NewByteArray(env, n);
  49     (*env)->SetByteArrayRegion(env, jb, 0, n, (jbyte *)s);
  50     return jb;
  51 }
  52 
  53 ___
  54 
  55 
  56 # Build native library in a regression test.
  57 # Temporary leave with the dependency on the C compiler
  58 # until other test harness covers the functionality for
  59 # native libraries support.
  60 OS=`uname -s`
  61 case "$OS" in
  62   SunOS )
  63     (cd z.test/native/src;
  64      cc -G -o ../lib/libworld.so -I$TESTJAVA/include -I$TESTJAVA/include/solaris org_astro_World.c -lc)
  65     ;;
  66   Linux )
  67     (cd z.test/native/src;
  68      gcc -o ../lib/libworld.so -I$TESTJAVA/include -I$TESTJAVA/include/linux -shared org_astro_World.c -static -lc)
  69     ;;
  70   Windows* )
  71     (cd z.test/native/src;
  72      cl /LD /Fe../lib/world.dll /I$TESTJAVA/include /I$TESTJAVA/include/win32 org_astro_World.c)
  73     ;;
  74   CYGWIN* )
  75     (cd z.test/native/src;
  76      cl /LD /Fe../lib/world.dll /I$TESTJAVA/include /I$TESTJAVA/include/win32 org_astro_World.c)
  77     ;;
  78   * )
  79     echo "Unrecognized system!"
  80     exit 1
  81 esac
  82 
  83 
  84 mkdir -p z.test/module-files
  85 $BIN/jpkg -d z.test/module-files -m z.test/modules/com.greetings \
  86           jmod com.greetings
  87 $BIN/jpkg -d z.test/module-files -m z.test/modules/org.astro \
  88           --natlib z.test/native/lib jmod org.astro
  89 $BIN/jmod -L z.lib create
  90 $BIN/jmod -L z.lib install z.test/module-files/*
  91 $BIN/java -L z.lib -m com.greetings
  92 
  93 exit 0
  94 
  95 : hello pass compile
  96 
  97 module com.greetings @ 0.1 {
  98     requires org.astro @ 1.2;
  99     class com.greetings.Hello;
 100 }
 101 
 102 package com.greetings;
 103 import org.astro.World;
 104 public class Hello {
 105     public static void main(String[] args) {
 106         System.out.println("Hello, " + World.name() + "!");
 107     }
 108 }
 109 
 110 module org.astro @ 1.2 { }
 111 
 112 package org.astro;
 113 public class World {
 114     private static native byte[] getName();
 115     static {
 116         System.loadLibrary("world");
 117     }
 118     public static String name() {
 119         return new String(getName());
 120     }
 121 }