RyanSkraba commented on code in PR #20835: URL: https://github.com/apache/flink/pull/20835#discussion_r971112886
########## flink-test-utils-parent/flink-test-utils/src/test/java/org/apache/flink/packaging/PackagingTestUtilsTest.java: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.packaging; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.net.URI; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class PackagingTestUtilsTest { + + @Test + void testAssertJarContainsOnlyFilesMatching(@TempDir Path tmp) throws Exception { + Path jar = createJar(tmp, Entry.fileEntry("", Arrays.asList("META-INF", "NOTICES"))); + + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/")); + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICES")); + + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF"))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICE"))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICES/"))) + .isInstanceOf(AssertionError.class); + } + + @Test + void testAssertJarContainsServiceEntry(@TempDir Path tmp) throws Exception { + final String service = PackagingTestUtilsTest.class.getName(); + Path jar = + createJar(tmp, Entry.fileEntry("", Arrays.asList("META-INF", "services", service))); + + PackagingTestUtils.assertJarContainsServiceEntry(jar, PackagingTestUtilsTest.class); + + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsServiceEntry( + jar, PackagingTestUtils.class)); Review Comment: Minor nit: Missing the `.isInstanceOf(AssertionError.class)` here. ########## flink-test-utils-parent/flink-test-utils/src/test/java/org/apache/flink/packaging/PackagingTestUtilsTest.java: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.packaging; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.net.URI; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class PackagingTestUtilsTest { + + @Test + void testAssertJarContainsOnlyFilesMatching(@TempDir Path tmp) throws Exception { + Path jar = createJar(tmp, Entry.fileEntry("", Arrays.asList("META-INF", "NOTICES"))); + + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/")); + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICES")); + + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF"))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICE"))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICES/"))) + .isInstanceOf(AssertionError.class); + } + + @Test + void testAssertJarContainsServiceEntry(@TempDir Path tmp) throws Exception { + final String service = PackagingTestUtilsTest.class.getName(); + Path jar = + createJar(tmp, Entry.fileEntry("", Arrays.asList("META-INF", "services", service))); + + PackagingTestUtils.assertJarContainsServiceEntry(jar, PackagingTestUtilsTest.class); + + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsServiceEntry( + jar, PackagingTestUtils.class)); + } + + private static class Entry { + final String contents; + final List<String> path; + final boolean isDirectory; + + public static Entry directoryEntry(List<String> path) { + return new Entry("", path, true); + } + + public static Entry fileEntry(String contents, List<String> path) { + return new Entry(contents, path, false); + } + + private Entry(String contents, List<String> path, boolean isDirectory) { + this.contents = contents; + this.path = path; + this.isDirectory = isDirectory; + } + } + + private static Path createJar(Path tempDir, Entry... entries) throws Exception { + final Path path = tempDir.resolve(UUID.randomUUID().toString() + ".jar"); + + final URI uriWithoutScheme = path.toUri(); + + final URI uri = + new URI( + "jar:file", + uriWithoutScheme.getHost(), + uriWithoutScheme.getPath(), + uriWithoutScheme.getFragment()); + + // causes FileSystems#newFileSystem to automatically create a valid zip file + // this is easier than manually creating a valid empty zip file manually + final Map<String, String> env = new HashMap<>(); + env.put("create", "true"); + + try (FileSystem zip = FileSystems.newFileSystem(uri, env)) { + // shortcut to getting the single root + final Path root = zip.getPath("").toAbsolutePath(); Review Comment: From my reading, this can only ever have the value `/` -- can we get rid of it and the unnecessary `.resolve(...)` ? ########## flink-connectors/flink-sql-connector-kafka/src/test/java/org/apache/flink/connectors/kafka/PackagingTest.java: ########## @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connectors.kafka; + +import org.apache.flink.packaging.PackagingTestUtils; +import org.apache.flink.table.factories.Factory; +import org.apache.flink.test.resources.ResourceTestUtils; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; +import java.util.Arrays; + +class PackagingTest { + + @Test + void testPackaging() throws Exception { + final Path jar = ResourceTestUtils.getResource("target/flink-sql-kafka*.jar"); Review Comment: ```suggestion final Path jar = ResourceTestUtils.getResource("target/flink-sql-connector-kafka.*\\.jar"); ``` These should be a regex, and this is probably the name of the jar that you are looking for! ########## flink-connectors/flink-sql-connector-kinesis/src/test/java/org/apache/flink/connectors/kinesis/PackagingTest.java: ########## @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.connectors.kinesis; + +import org.apache.flink.packaging.PackagingTestUtils; +import org.apache.flink.table.factories.Factory; +import org.apache.flink.test.resources.ResourceTestUtils; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; +import java.util.Arrays; + +class PackagingTest { + + @Test + void testPackaging() throws Exception { + final Path jar = ResourceTestUtils.getResource("target/flink-sql-kinesis*.jar"); Review Comment: ```suggestion final Path jar = ResourceTestUtils.getResource("target/flink-sql-connector-kinesis.*\\.jar"); ``` ########## flink-test-utils-parent/flink-test-utils/src/test/java/org/apache/flink/packaging/PackagingTestUtilsTest.java: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.packaging; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.net.URI; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class PackagingTestUtilsTest { + + @Test + void testAssertJarContainsOnlyFilesMatching(@TempDir Path tmp) throws Exception { + Path jar = createJar(tmp, Entry.fileEntry("", Arrays.asList("META-INF", "NOTICES"))); + + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/")); + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICES")); + + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF"))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICE"))) + .isInstanceOf(AssertionError.class); + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsOnlyFilesMatching( + jar, Collections.singleton("META-INF/NOTICES/"))) + .isInstanceOf(AssertionError.class); + } + + @Test + void testAssertJarContainsServiceEntry(@TempDir Path tmp) throws Exception { + final String service = PackagingTestUtilsTest.class.getName(); + Path jar = + createJar(tmp, Entry.fileEntry("", Arrays.asList("META-INF", "services", service))); + + PackagingTestUtils.assertJarContainsServiceEntry(jar, PackagingTestUtilsTest.class); + + assertThatThrownBy( + () -> + PackagingTestUtils.assertJarContainsServiceEntry( + jar, PackagingTestUtils.class)); + } + + private static class Entry { + final String contents; + final List<String> path; + final boolean isDirectory; + + public static Entry directoryEntry(List<String> path) { + return new Entry("", path, true); + } + + public static Entry fileEntry(String contents, List<String> path) { + return new Entry(contents, path, false); + } + + private Entry(String contents, List<String> path, boolean isDirectory) { + this.contents = contents; + this.path = path; + this.isDirectory = isDirectory; + } + } + + private static Path createJar(Path tempDir, Entry... entries) throws Exception { + final Path path = tempDir.resolve(UUID.randomUUID().toString() + ".jar"); + + final URI uriWithoutScheme = path.toUri(); Review Comment: Nit: I guess *technically* it does have a scheme: `file:` ########## flink-formats/flink-sql-avro/src/test/java/org/apache/flink/formats/avro/PackagingTest.java: ########## @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.formats.avro; + +import org.apache.flink.packaging.PackagingTestUtils; +import org.apache.flink.table.factories.Factory; +import org.apache.flink.test.resources.ResourceTestUtils; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; +import java.util.Arrays; + +class PackagingTest { + + @Test + void testPackaging() throws Exception { + final Path jar = ResourceTestUtils.getResource("target/flink-sql-avro*.jar"); Review Comment: ```suggestion final Path jar = ResourceTestUtils.getResource("target/flink-sql-avro.*\\.jar"); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
