Renamed scanDir to sourceDir, reworked sourcedir test.

This commit is contained in:
Tyler Sommer
2016-04-13 16:20:24 -06:00
parent 0f3a4b7b8f
commit 6e81294963
6 changed files with 60 additions and 62 deletions

View File

@ -64,33 +64,12 @@ 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) {
if (args.length > 0) { SeqTest test = new SeqTest();
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(pattern, method.getName())) { if (!method.getName().startsWith("test") || !Pattern.matches(args[0], method.getName())) {
continue; continue;
} }
@ -104,14 +83,8 @@ 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);
}
} }
} }

View File

@ -13,7 +13,7 @@ The same set of types are supported. Details on how the binding works can be fou
-o string -o string
Path to write the generated jar file. (default "libgojava.jar") Path to write the generated jar file. (default "libgojava.jar")
-s string -s string
Additional path to scan for Java files. These files will be compiled and Additional path to scan for Java source code. These files will be compiled and
included in the final jar. included in the final jar.
-v Verbose output. -v Verbose output.
``` ```

View File

@ -10,10 +10,10 @@ Usage
-o string -o string
Path to write the generated jar file. (default "libgojava.jar") Path to write the generated jar file. (default "libgojava.jar")
-s string -s string
Additional path to scan for Java files. These files will be compiled and Additional path to scan for Java source code. These files will be compiled and
included in the final jar. included in the final jar.
-v Verbose output. -v Verbose output.
*/ */
package main package main
import ( import (
@ -133,19 +133,19 @@ func bindPackages(bindDir, javaDir string, pkgs []*types.Package) ([]string, err
return javaFiles, nil return javaFiles, nil
} }
func addExtraFiles(javaDir, scanDir string) ([]string, error) { func addExtraFiles(javaDir, sourceDir string) ([]string, error) {
if scanDir == "" { if sourceDir == "" {
return nil, nil return nil, nil
} }
extraFiles := make([]string, 0) extraFiles := make([]string, 0)
err := filepath.Walk(scanDir, func(path string, info os.FileInfo, walkErr error) error { err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil { if walkErr != nil {
return walkErr return walkErr
} }
if info.IsDir() { if info.IsDir() {
return nil return nil
} }
fileName, err := filepath.Rel(scanDir, path) fileName, err := filepath.Rel(sourceDir, path)
if err != nil { if err != nil {
return err return err
} }
@ -160,7 +160,7 @@ func addExtraFiles(javaDir, scanDir string) ([]string, error) {
return nil, err return nil, err
} }
if len(extraFiles) == 0 { if len(extraFiles) == 0 {
verbosef("warning: argument -s was passed on command line, but no .java files were found in '%s'\n", scanDir) verbosef("warning: argument -s was passed on command line, but no .java files were found in '%s'\n", sourceDir)
} }
return extraFiles, nil return extraFiles, nil
} }
@ -259,7 +259,7 @@ func createJar(target, jarDir string) error {
return nil return nil
} }
func bindToJar(target string, scanDir string, pkgs ...string) error { func bindToJar(target string, sourceDir string, pkgs ...string) error {
tmpDir, cleanup, err := initBuild() tmpDir, cleanup, err := initBuild()
if err != nil { if err != nil {
return err return err
@ -286,7 +286,7 @@ func bindToJar(target string, scanDir string, pkgs ...string) error {
if err != nil { if err != nil {
return err return err
} }
extraFiles, err := addExtraFiles(javaDir, scanDir) extraFiles, err := addExtraFiles(javaDir, sourceDir)
if err != nil { if err != nil {
return err return err
} }
@ -378,7 +378,7 @@ 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 source code.")
flag.BoolVar(&verbose, "v", false, "Verbose output.") flag.BoolVar(&verbose, "v", false, "Verbose output.")
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintln(os.Stderr, usage) fmt.Fprintln(os.Stderr, usage)

View File

@ -13,15 +13,18 @@ import (
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 init() {
verbose = testing.Verbose() verbose = testing.Verbose()
}
func TestJavaBind(t *testing.T) {
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",
@ -46,3 +49,23 @@ func TestJavaBind(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
} }
func TestSourceDir(t *testing.T) {
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",
); err != nil {
t.Fatal(err)
}
cmd := exec.Command("java", "-cp", jar, "go.SourceDirTest")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
t.Fatal(err)
}
}

5
testdata/Dummy.java vendored
View File

@ -1,5 +0,0 @@
package go;
public class Dummy {
public int fortyTwo = 42;
}

7
testdata/SourceDirTest.java vendored Normal file
View File

@ -0,0 +1,7 @@
package go;
public class SourceDirTest {
public static void main(String[] args) {
System.out.println("testSourceDirInclusion PASS");
}
}