github.com/apache/beam/sdks/v2@v2.48.2/java/container/pathingjar_test.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one or more 2 // contributor license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright ownership. 4 // The ASF licenses this file to You under the Apache License, Version 2.0 5 // (the "License"); you may not use this file except in compliance with 6 // the License. You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 package main 17 18 import ( 19 "archive/zip" 20 "bufio" 21 "bytes" 22 "io" 23 "strings" 24 "testing" 25 ) 26 27 func TestWritePathingJar(t *testing.T) { 28 var buf bytes.Buffer 29 input := []string{"a.jar", "b.jar", "c.jar", "thisVeryLongJarNameIsOverSeventyTwoCharactersLongAndNeedsToBeSplitCorrectly.jar"} 30 err := writePathingJar(input, &buf) 31 if err != nil { 32 t.Errorf("writePathingJar(%v) = %v, want nil", input, err) 33 } 34 35 zr, err := zip.NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len())) 36 if err != nil { 37 t.Errorf("zip.NewReader() = %v, want nil", err) 38 } 39 40 f := getManifest(zr) 41 if f == nil { 42 t.Fatalf("Jar didn't contain manifest") 43 } 44 45 { 46 fr, err := f.Open() 47 if err != nil { 48 t.Errorf("(%v).Open() = %v, want nil", f.Name, err) 49 } 50 all, err := io.ReadAll(fr) 51 if err != nil { 52 t.Errorf("(%v).Open() = %v, want nil", f.Name, err) 53 } 54 fr.Close() 55 want := "\nClass-Path: file:a.jar file:b.jar file:c.jar" 56 if !strings.Contains(string(all), want) { 57 t.Errorf("%v = %v, want nil", f.Name, err) 58 } 59 } 60 61 { 62 fr, err := f.Open() 63 if err != nil { 64 t.Errorf("(%v).Open() = %v, want nil", f.Name, err) 65 } 66 defer fr.Close() 67 fs := bufio.NewScanner(fr) 68 fs.Split(bufio.ScanLines) 69 70 for fs.Scan() { 71 if got, want := len(fs.Bytes()), 72; got > want { 72 t.Errorf("Manifest line exceeds limit got %v:, want %v line: %q", got, want, fs.Text()) 73 } 74 } 75 } 76 77 } 78 79 // getManifest extracts the java manifest from the zip file. 80 func getManifest(zr *zip.Reader) *zip.File { 81 for _, f := range zr.File { 82 if f.Name != "META-INF/MANIFEST.MF" { 83 continue 84 } 85 return f 86 } 87 return nil 88 }