1// Copyright 2018 The Bazel Authors. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package rjar 16 17import ( 18 "archive/zip" 19 "io/ioutil" 20 "os" 21 "path" 22 "path/filepath" 23 "testing" 24) 25 26var ( 27 expectedClasses = []string{"R.class", "R$attr.class", "R$id.class", "R$layout.class", "R$string.class"} 28) 29 30const ( 31 java = "local_jdk/bin/java" 32 testDataBase = "rules_android/src/tools/ak/rjar/testdata" 33) 34 35func TestCreateRJar(t *testing.T) { 36 tmpDir, err := ioutil.TempDir("", "rjartest") 37 if err != nil { 38 t.Fatalf("Error creating temp directory: %v", err) 39 } 40 defer os.RemoveAll(tmpDir) 41 42 out := filepath.Join(tmpDir, "R.jar") 43 jarDexer := path.Join(os.Getenv("TEST_SRCDIR"), "remote_java_tools_for_rules_android/java_tools/JavaBuilder_deploy.jar") 44 inJava := dataPath("R.java") 45 pkgs := dataPath("pkgs.txt") 46 targetLabel := "//test:test" 47 48 if err := doWork(inJava, pkgs, out, path.Join(os.Getenv("TEST_SRCDIR"), java), jarDexer, targetLabel); err != nil { 49 t.Fatalf("Error creating R.jar: %v", err) 50 } 51 52 z, err := zip.OpenReader(out) 53 if err != nil { 54 t.Fatalf("Error opening output jar: %v", err) 55 } 56 defer z.Close() 57 58 for _, class := range expectedClasses { 59 if !zipContains(z, filepath.Join("android/support/v7", class)) { 60 t.Errorf("R.jar does not contain %s", filepath.Join("android/support/v7", class)) 61 } 62 if !zipContains(z, filepath.Join("com/google/android/samples/skeletonapp", class)) { 63 t.Errorf("R.jar does not contain %s", filepath.Join("com/google/android/samples/skeletonapp", class)) 64 } 65 if zipContains(z, filepath.Join("com/google/android/package/test", class)) { 66 t.Errorf("R.jar contains %s", filepath.Join("com/google/android/package/test", class)) 67 } 68 } 69} 70 71func dataPath(fn string) string { 72 return filepath.Join(os.Getenv("TEST_SRCDIR"), testDataBase, fn) 73} 74 75func zipContains(z *zip.ReadCloser, fn string) bool { 76 for _, f := range z.File { 77 if fn == f.Name { 78 return true 79 } 80 } 81 return false 82} 83