mirror of
https://github.com/go-i2p/gojava.git
synced 2025-07-05 02:23:56 -04:00
Added test for scan directory feature and updated readme.
This commit is contained in:
@ -64,12 +64,33 @@ public class MoreAsserts {
|
|||||||
throw new RuntimeException(msg);
|
throw new RuntimeException(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean failed = false;
|
||||||
|
|
||||||
|
private static String pattern = ".*";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SeqTest test = new SeqTest();
|
if (args.length > 0) {
|
||||||
|
pattern = args[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
new TestRunner(new SeqTest()).runTests();
|
||||||
|
new TestRunner(new ExtraTest()).runTests();
|
||||||
|
|
||||||
|
// NOTE: We need to call System.exit to force all go threads to exit.
|
||||||
|
System.exit(failed ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TestRunner<T> {
|
||||||
|
private T test;
|
||||||
|
|
||||||
|
public TestRunner(T test) {
|
||||||
|
this.test = test;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void runTests() {
|
||||||
Class c = test.getClass();
|
Class c = test.getClass();
|
||||||
boolean failed = false;
|
|
||||||
for (Method method : c.getDeclaredMethods()) {
|
for (Method method : c.getDeclaredMethods()) {
|
||||||
if (!method.getName().startsWith("test") || !Pattern.matches(args[0], method.getName())) {
|
if (!method.getName().startsWith("test") || !Pattern.matches(pattern, method.getName())) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,8 +104,14 @@ public class MoreAsserts {
|
|||||||
failed = true;
|
failed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// NOTE: We need to call System.exit to force all go threads to exit.
|
}
|
||||||
System.exit(failed ? 1 : 0);
|
}
|
||||||
|
|
||||||
|
private static class ExtraTest {
|
||||||
|
public void testCopyFromScanDir() {
|
||||||
|
Dummy d = new Dummy();
|
||||||
|
assertEquals("Values must match", 42, d.fortyTwo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,12 +6,16 @@ The same set of types are supported. Details on how the binding works can be fou
|
|||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
gojava build [-o <jar>] [<pkg1>, [<pkg2>...]]
|
gojava [-v] [-o <jar>] [-s <dir>] build [<pkg1>, [<pkg2>...]]
|
||||||
|
|
||||||
This generates a jar containing Java bindings to the specified Go packages.
|
This generates a jar containing Java bindings to the specified Go packages.
|
||||||
|
|
||||||
-o string
|
-o string
|
||||||
Path to the generated jar file (default "libgojava.jar")
|
Path to write the generated jar file. (default "libgojava.jar")
|
||||||
|
-s string
|
||||||
|
Additional path to scan for Java files. These files will be compiled and
|
||||||
|
included in the final jar.
|
||||||
|
-v Verbose output.
|
||||||
```
|
```
|
||||||
|
|
||||||
You can include the generated jar in your build using the build tool of your choice.
|
You can include the generated jar in your build using the build tool of your choice.
|
||||||
|
60
gojava.go
60
gojava.go
@ -1,3 +1,19 @@
|
|||||||
|
/*
|
||||||
|
Command gojava is a tool for creating Java bindings to Go packages.
|
||||||
|
|
||||||
|
Usage
|
||||||
|
|
||||||
|
gojava [-v] [-o <jar>] [-s <dir>] build [<pkg1>, [<pkg2>...]]
|
||||||
|
|
||||||
|
This generates a jar containing Java bindings to the specified Go packages.
|
||||||
|
|
||||||
|
-o string
|
||||||
|
Path to write the generated jar file. (default "libgojava.jar")
|
||||||
|
-s string
|
||||||
|
Additional path to scan for Java files. These files will be compiled and
|
||||||
|
included in the final jar.
|
||||||
|
-v Verbose output.
|
||||||
|
*/
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -19,6 +35,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"flag"
|
"flag"
|
||||||
|
|
||||||
"github.com/sridharv/gomobile-java/bind"
|
"github.com/sridharv/gomobile-java/bind"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -31,6 +48,14 @@ func runCommand(cmd string, args ...string) error {
|
|||||||
|
|
||||||
var javaHome = os.Getenv("JAVA_HOME")
|
var javaHome = os.Getenv("JAVA_HOME")
|
||||||
var cwd string
|
var cwd string
|
||||||
|
var verbose = false
|
||||||
|
|
||||||
|
func verbosef(format string, a ...interface{}) {
|
||||||
|
if !verbose {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Printf(format, a...)
|
||||||
|
}
|
||||||
|
|
||||||
func initBuild() (string, func(), error) {
|
func initBuild() (string, func(), error) {
|
||||||
if javaHome == "" {
|
if javaHome == "" {
|
||||||
@ -108,12 +133,12 @@ func bindPackages(bindDir, javaDir string, pkgs []*types.Package) ([]string, err
|
|||||||
return javaFiles, nil
|
return javaFiles, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func addExtraFiles(javaDir, scanDir string) error {
|
func addExtraFiles(javaDir, scanDir string) ([]string, error) {
|
||||||
if scanDir == "" {
|
if scanDir == "" {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
var files []filePair
|
extraFiles := make([]string, 0)
|
||||||
filepath.Walk(scanDir+"/", func(path string, info os.FileInfo, walkErr error) error {
|
err := filepath.Walk(scanDir, func(path string, info os.FileInfo, walkErr error) error {
|
||||||
if walkErr != nil {
|
if walkErr != nil {
|
||||||
return walkErr
|
return walkErr
|
||||||
}
|
}
|
||||||
@ -125,11 +150,19 @@ func addExtraFiles(javaDir, scanDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(fileName, ".java") {
|
if strings.HasSuffix(fileName, ".java") {
|
||||||
files = append(files, filePair{filepath.Join(javaDir, fileName), path})
|
p := filepath.Join(javaDir, fileName)
|
||||||
|
extraFiles = append(extraFiles, p)
|
||||||
|
return copyFile(p, path)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
return copyFiles(files)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if len(extraFiles) == 0 {
|
||||||
|
verbosef("warning: argument -s was passed on command line, but no .java files were found in '%s'\n", scanDir)
|
||||||
|
}
|
||||||
|
return extraFiles, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSupportFiles(bindDir, javaDir, mainFile string) error {
|
func createSupportFiles(bindDir, javaDir, mainFile string) error {
|
||||||
@ -188,7 +221,7 @@ func createJar(target, jarDir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w := zip.NewWriter(t)
|
w := zip.NewWriter(t)
|
||||||
fmt.Printf("Building %s\n", target)
|
verbosef("Building %s\n", target)
|
||||||
if err := filepath.Walk(jarDir, func(path string, info os.FileInfo, walkErr error) error {
|
if err := filepath.Walk(jarDir, func(path string, info os.FileInfo, walkErr error) error {
|
||||||
if walkErr != nil {
|
if walkErr != nil {
|
||||||
return walkErr
|
return walkErr
|
||||||
@ -197,7 +230,7 @@ func createJar(target, jarDir string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
fileName, err := filepath.Rel(jarDir, path)
|
fileName, err := filepath.Rel(jarDir, path)
|
||||||
fmt.Printf("Adding %s\n", fileName)
|
verbosef("Adding %s\n", fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -253,9 +286,11 @@ func bindToJar(target string, scanDir string, pkgs ...string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := addExtraFiles(javaDir, scanDir); err != nil {
|
extraFiles, err := addExtraFiles(javaDir, scanDir)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
javaFiles = append(javaFiles, extraFiles...)
|
||||||
if err := createSupportFiles(bindDir, javaDir, mainFile); err != nil {
|
if err := createSupportFiles(bindDir, javaDir, mainFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -336,14 +371,15 @@ const usage = `gojava is a tool for creating Java bindings to Go
|
|||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
gojava [-o <jar>] [-s <dir>] build [<pkg1>, [<pkg2>...]]
|
gojava [-v] [-o <jar>] [-s <dir>] build [<pkg1>, [<pkg2>...]]
|
||||||
|
|
||||||
This generates a jar containing Java bindings to the specified Go packages.
|
This generates a jar containing Java bindings to the specified Go packages.
|
||||||
`
|
`
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
o := flag.String("o", "libgojava.jar", "Path to the generated jar file")
|
o := flag.String("o", "libgojava.jar", "Path to the generated jar file.")
|
||||||
s := flag.String("s", "", "Additional path to scan for Java files")
|
s := flag.String("s", "", "Additional path to scan for Java files.")
|
||||||
|
flag.BoolVar(&verbose, "v", false, "Verbose output.")
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Fprintln(os.Stderr, usage)
|
fmt.Fprintln(os.Stderr, usage)
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
|
@ -4,23 +4,24 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"flag"
|
"flag"
|
||||||
|
"go/build"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"go/build"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var javaTest = flag.String("javatest", ".*", "Run only java tests matching the regular expression")
|
var javaTest = flag.String("javatest", ".*", "Run only java tests matching the regular expression")
|
||||||
|
|
||||||
func TestJavaBind(t *testing.T) {
|
func TestJavaBind(t *testing.T) {
|
||||||
|
verbose = testing.Verbose()
|
||||||
tmpDir, err := ioutil.TempDir("", "gojavatest")
|
tmpDir, err := ioutil.TempDir("", "gojavatest")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
jar := filepath.Join(tmpDir, "gojavatest.jar")
|
jar := filepath.Join(tmpDir, "gojavatest.jar")
|
||||||
if err := bindToJar(jar,
|
if err := bindToJar(jar,
|
||||||
"",
|
"testdata",
|
||||||
"github.com/sridharv/gomobile-java/bind/testpkg",
|
"github.com/sridharv/gomobile-java/bind/testpkg",
|
||||||
"github.com/sridharv/gomobile-java/bind/testpkg/secondpkg",
|
"github.com/sridharv/gomobile-java/bind/testpkg/secondpkg",
|
||||||
"github.com/sridharv/gomobile-java/bind/testpkg/simplepkg",
|
"github.com/sridharv/gomobile-java/bind/testpkg/simplepkg",
|
||||||
|
5
testdata/Dummy.java
vendored
Normal file
5
testdata/Dummy.java
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package go;
|
||||||
|
|
||||||
|
public class Dummy {
|
||||||
|
public int fortyTwo = 42;
|
||||||
|
}
|
Reference in New Issue
Block a user