mirror of
https://github.com/go-i2p/gojava.git
synced 2025-07-03 17:59:42 -04:00
Added test for scan directory feature and updated readme.
This commit is contained in:
@ -64,27 +64,54 @@ public class MoreAsserts {
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SeqTest test = new SeqTest();
|
||||
Class c = test.getClass();
|
||||
boolean failed = false;
|
||||
for (Method method : c.getDeclaredMethods()) {
|
||||
if (!method.getName().startsWith("test") || !Pattern.matches(args[0], method.getName())) {
|
||||
continue;
|
||||
}
|
||||
private static boolean failed = false;
|
||||
|
||||
System.out.print(method.getName());
|
||||
try {
|
||||
method.invoke(test);
|
||||
System.out.println(" PASS");
|
||||
} catch (Exception ex) {
|
||||
System.out.println(" FAIL");
|
||||
ex.printStackTrace();
|
||||
failed = true;
|
||||
}
|
||||
private static String pattern = ".*";
|
||||
|
||||
public static void main(String[] args) {
|
||||
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();
|
||||
for (Method method : c.getDeclaredMethods()) {
|
||||
if (!method.getName().startsWith("test") || !Pattern.matches(pattern, method.getName())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.print(method.getName());
|
||||
try {
|
||||
method.invoke(test);
|
||||
System.out.println(" PASS");
|
||||
} catch (Exception ex) {
|
||||
System.out.println(" FAIL");
|
||||
ex.printStackTrace();
|
||||
failed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class ExtraTest {
|
||||
public void testCopyFromScanDir() {
|
||||
Dummy d = new Dummy();
|
||||
assertEquals("Values must match", 42, d.fortyTwo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
16
README.md
16
README.md
@ -6,12 +6,16 @@ The same set of types are supported. Details on how the binding works can be fou
|
||||
### Usage
|
||||
|
||||
```
|
||||
gojava build [-o <jar>] [<pkg1>, [<pkg2>...]]
|
||||
|
||||
This generates a jar containing Java bindings to the specified Go packages.
|
||||
|
||||
-o string
|
||||
Path to the generated jar file (default "libgojava.jar")
|
||||
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.
|
||||
```
|
||||
|
||||
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
|
||||
|
||||
import (
|
||||
@ -19,6 +35,7 @@ import (
|
||||
"runtime"
|
||||
|
||||
"flag"
|
||||
|
||||
"github.com/sridharv/gomobile-java/bind"
|
||||
)
|
||||
|
||||
@ -31,6 +48,14 @@ func runCommand(cmd string, args ...string) error {
|
||||
|
||||
var javaHome = os.Getenv("JAVA_HOME")
|
||||
var cwd string
|
||||
var verbose = false
|
||||
|
||||
func verbosef(format string, a ...interface{}) {
|
||||
if !verbose {
|
||||
return
|
||||
}
|
||||
fmt.Printf(format, a...)
|
||||
}
|
||||
|
||||
func initBuild() (string, func(), error) {
|
||||
if javaHome == "" {
|
||||
@ -108,12 +133,12 @@ func bindPackages(bindDir, javaDir string, pkgs []*types.Package) ([]string, err
|
||||
return javaFiles, nil
|
||||
}
|
||||
|
||||
func addExtraFiles(javaDir, scanDir string) error {
|
||||
func addExtraFiles(javaDir, scanDir string) ([]string, error) {
|
||||
if scanDir == "" {
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
var files []filePair
|
||||
filepath.Walk(scanDir+"/", func(path string, info os.FileInfo, walkErr error) error {
|
||||
extraFiles := make([]string, 0)
|
||||
err := filepath.Walk(scanDir, func(path string, info os.FileInfo, walkErr error) error {
|
||||
if walkErr != nil {
|
||||
return walkErr
|
||||
}
|
||||
@ -125,11 +150,19 @@ func addExtraFiles(javaDir, scanDir string) error {
|
||||
return err
|
||||
}
|
||||
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 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 {
|
||||
@ -188,7 +221,7 @@ func createJar(target, jarDir string) error {
|
||||
return err
|
||||
}
|
||||
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 walkErr != nil {
|
||||
return walkErr
|
||||
@ -197,7 +230,7 @@ func createJar(target, jarDir string) error {
|
||||
return nil
|
||||
}
|
||||
fileName, err := filepath.Rel(jarDir, path)
|
||||
fmt.Printf("Adding %s\n", fileName)
|
||||
verbosef("Adding %s\n", fileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -253,9 +286,11 @@ func bindToJar(target string, scanDir string, pkgs ...string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := addExtraFiles(javaDir, scanDir); err != nil {
|
||||
extraFiles, err := addExtraFiles(javaDir, scanDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
javaFiles = append(javaFiles, extraFiles...)
|
||||
if err := createSupportFiles(bindDir, javaDir, mainFile); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -336,14 +371,15 @@ const usage = `gojava is a tool for creating Java bindings to Go
|
||||
|
||||
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.
|
||||
`
|
||||
|
||||
func main() {
|
||||
o := flag.String("o", "libgojava.jar", "Path to the generated jar file")
|
||||
s := flag.String("s", "", "Additional path to scan for Java files")
|
||||
o := flag.String("o", "libgojava.jar", "Path to the generated jar file.")
|
||||
s := flag.String("s", "", "Additional path to scan for Java files.")
|
||||
flag.BoolVar(&verbose, "v", false, "Verbose output.")
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintln(os.Stderr, usage)
|
||||
flag.PrintDefaults()
|
||||
|
@ -4,23 +4,24 @@ import (
|
||||
"testing"
|
||||
|
||||
"flag"
|
||||
"go/build"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"go/build"
|
||||
)
|
||||
|
||||
var javaTest = flag.String("javatest", ".*", "Run only java tests matching the regular expression")
|
||||
|
||||
func TestJavaBind(t *testing.T) {
|
||||
verbose = testing.Verbose()
|
||||
tmpDir, err := ioutil.TempDir("", "gojavatest")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
jar := filepath.Join(tmpDir, "gojavatest.jar")
|
||||
if err := bindToJar(jar,
|
||||
"",
|
||||
"testdata",
|
||||
"github.com/sridharv/gomobile-java/bind/testpkg",
|
||||
"github.com/sridharv/gomobile-java/bind/testpkg/secondpkg",
|
||||
"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