This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit c3777f6d9bf1c1cf8998d5d298d9ef5a1f6d104a Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed May 21 10:50:52 2025 -0400 Add org.apache.commons.compress.archivers.zip.ZipEncodingHelperTest - Internal refactoring - Javadoc --- .../compress/archivers/zip/ZipEncodingHelper.java | 13 ++++--- .../archivers/zip/ZipEncodingHelperTest.java | 42 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java index 56377a8cf..025db4fa6 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java @@ -102,13 +102,18 @@ private static boolean isUTF8Alias(final String actual) { return UTF_8.name().equalsIgnoreCase(actual) || UTF_8.aliases().stream().anyMatch(actual::equalsIgnoreCase); } + /** + * Returns a Charset for the named charset. If the name cannot find a charset, return {@link Charset#defaultCharset()}. + * + * @param name The name of the requested charset, may be null. + * @return a Charset for the named charset. + * @see Charset#defaultCharset() + */ private static Charset toSafeCharset(final String name) { - Charset charset = Charset.defaultCharset(); try { - charset = Charsets.toCharset(name); + return Charsets.toCharset(name); } catch (final UnsupportedCharsetException ignored) { - // Use the default encoding instead. + return Charset.defaultCharset(); } - return charset; } } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelperTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelperTest.java new file mode 100644 index 000000000..e9f8a60dc --- /dev/null +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelperTest.java @@ -0,0 +1,42 @@ +/* + * 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 + * + * https://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.commons.compress.archivers.zip; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.nio.charset.Charset; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link ZipEncodingHelper}. + */ +public class ZipEncodingHelperTest { + + @Test + public void testGetZipEncodingForDefault() { + assertEquals(Charset.defaultCharset(), ((NioZipEncoding) ZipEncodingHelper.getZipEncoding(Charset.defaultCharset().name())).getCharset()); + } + + @Test + public void testGetZipEncodingForUnknown() { + assertEquals(Charset.defaultCharset(), ((NioZipEncoding) ZipEncodingHelper.getZipEncoding("X")).getCharset()); + } +}