Add java file resources and flag to include sources

Add a properties to allow including files as resources, including
support for filegroups.  Also add a flag that causes module sources
to be included in the final jar.

Test: java_test.go TestResources
Change-Id: Ida8ee59b28df9fe66952170f46470d3a09fd5d65
diff --git a/java/java_test.go b/java/java_test.go
index a757a9f..7154f5e 100644
--- a/java/java_test.go
+++ b/java/java_test.go
@@ -16,6 +16,7 @@
 
 import (
 	"android/soong/android"
+	"android/soong/genrule"
 	"fmt"
 	"io/ioutil"
 	"os"
@@ -59,6 +60,7 @@
 	ctx.RegisterModuleType("java_library_host", android.ModuleFactoryAdaptor(LibraryHostFactory))
 	ctx.RegisterModuleType("java_import", android.ModuleFactoryAdaptor(ImportFactory))
 	ctx.RegisterModuleType("java_defaults", android.ModuleFactoryAdaptor(defaultsFactory))
+	ctx.RegisterModuleType("filegroup", android.ModuleFactoryAdaptor(genrule.FileGroupFactory))
 	ctx.PreArchMutators(android.RegisterPrebuiltsPreArchMutators)
 	ctx.PreArchMutators(android.RegisterPrebuiltsPostDepsMutators)
 	ctx.PreArchMutators(android.RegisterDefaultsPreArchMutators)
@@ -92,6 +94,8 @@
 		"c.java":     nil,
 		"a.jar":      nil,
 		"b.jar":      nil,
+		"res/a":      nil,
+		"res/b":      nil,
 		"prebuilts/sdk/14/android.jar":    nil,
 		"prebuilts/sdk/14/framework.aidl": nil,
 	})
@@ -360,6 +364,76 @@
 	}
 }
 
+func TestResources(t *testing.T) {
+	var table = []struct {
+		name  string
+		prop  string
+		extra string
+		args  string
+	}{
+		{
+			// Test that a module with java_resource_dirs includes a file list file
+			name: "resource dirs",
+			prop: `java_resource_dirs: ["res"]`,
+			args: "-C res -l ",
+		},
+		{
+			// Test that a module with java_resources includes the files
+			name: "resource files",
+			prop: `java_resources: ["res/a", "res/b"]`,
+			args: "-C . -f res/a -C . -f res/b",
+		},
+		{
+			// Test that a module with a filegroup in java_resources includes the files with the
+			// path prefix
+			name: "resource filegroup",
+			prop: `java_resources: [":foo-res"]`,
+			extra: `
+				filegroup {
+					name: "foo-res",
+					path: "res",
+					srcs: ["res/a", "res/b"],
+				}`,
+			args: "-C res -f res/a -C res -f res/b",
+		},
+		{
+			// Test that a module with "include_srcs: true" includes its source files in the resources jar
+			name: "include sources",
+			prop: `include_srcs: true`,
+			args: "-C . -f a.java -C . -f b.java -C . -f c.java",
+		},
+	}
+
+	for _, test := range table {
+		t.Run(test.name, func(t *testing.T) {
+			ctx := testJava(t, `
+				java_library {
+					name: "foo",
+					srcs: [
+						"a.java",
+						"b.java",
+						"c.java",
+					],
+					`+test.prop+`,
+				}
+			`+test.extra)
+
+			foo := ctx.ModuleForTests("foo", "android_common").Output("classes.jar")
+			fooRes := ctx.ModuleForTests("foo", "android_common").Output("res.jar")
+
+			if !inList(fooRes.Output.String(), foo.Inputs.Strings()) {
+				t.Errorf("foo combined jars %v does not contain %q",
+					foo.Inputs.Strings(), fooRes.Output.String())
+			}
+
+			if !strings.Contains(fooRes.Args["jarArgs"], test.args) {
+				t.Errorf("foo resource jar args %q does not contain %q",
+					fooRes.Args["jarArgs"], test.args)
+			}
+		})
+	}
+}
+
 func fail(t *testing.T, errs []error) {
 	if len(errs) > 0 {
 		for _, err := range errs {