hanicz commented on code in PR #1393: URL: https://github.com/apache/knox/pull/1393#discussion_r4023651079
########## gateway-util-common/src/test/java/org/apache/knox/gateway/util/MimeTypesTest.java: ########## @@ -0,0 +1,271 @@ +/* + * 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.knox.gateway.util; + +import jakarta.activation.MimeType; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MimeTypesTest { + + @Test + public void testCreateReturnsNullForNullBaseType() { + assertThat(MimeTypes.create(null, StandardCharsets.UTF_8.name()), nullValue()); + } + + @Test + public void testCreateAddsEncodingWhenCharsetIsMissing() { + MimeType type = MimeTypes.create("application/json", StandardCharsets.UTF_8.name()); + + assertThat(type.getBaseType(), is("application/json")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreateAddsCustomEncodingWhenCharsetIsMissing() { + MimeType type = MimeTypes.create("text/plain", "ISO-8859-1"); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateSupportsWildcardMimeType() { + MimeType type = MimeTypes.create("text/*", StandardCharsets.UTF_8.name()); + + assertThat(type.getBaseType(), is("text/*")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreatePreservesExistingCharset() { + MimeType type = MimeTypes.create("application/json; charset=ISO-8859-1", StandardCharsets.UTF_8.name()); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateReadsQuotedCharsetValue() { + MimeType type = MimeTypes.create("text/plain; charset=\"ISO-8859-1\"", null); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreatePreservesOtherMimeTypeParameters() { + MimeType type = MimeTypes.create("application/json; version=1", StandardCharsets.UTF_8.name()); + + assertThat(type.getParameter("version"), is("1")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreatePreservesOtherParametersWhenCharsetAlreadyExists() { + MimeType type = MimeTypes.create( + "application/json; version=1; charset=ISO-8859-1", StandardCharsets.UTF_8.name()); + + assertThat(type.getParameter("version"), is("1")); + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateRecognizesCharsetParameterRegardlessOfParameterNameCase() { + MimeType type = MimeTypes.create("text/plain; CHARSET=ISO-8859-1", StandardCharsets.UTF_8.name()); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateLeavesCharsetUnsetWhenEncodingIsNull() { + MimeType type = MimeTypes.create("application/json", null); + + assertThat(MimeTypes.getCharset(type, null), nullValue()); + } + + @Test + public void testCreateLeavesCharsetUnsetForWildcardTypeWhenEncodingIsNull() { + MimeType type = MimeTypes.create("*/*", null); + + assertThat(MimeTypes.getCharset(type, null), nullValue()); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateRejectsMalformedMimeType() { + MimeTypes.create("not-a-mime-type", null); + } + + @Test + public void testCreateIncludesOriginalMimeTypeInParseExceptionMessage() { Review Comment: This is a duplicate of `testCreateRejectsMalformedMimeType`. Use `assertThrows` and you can validate the message. ########## gateway-util-common/src/test/java/org/apache/knox/gateway/util/MimeTypesTest.java: ########## @@ -0,0 +1,271 @@ +/* + * 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.knox.gateway.util; + +import jakarta.activation.MimeType; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MimeTypesTest { + + @Test + public void testCreateReturnsNullForNullBaseType() { + assertThat(MimeTypes.create(null, StandardCharsets.UTF_8.name()), nullValue()); + } + + @Test + public void testCreateAddsEncodingWhenCharsetIsMissing() { + MimeType type = MimeTypes.create("application/json", StandardCharsets.UTF_8.name()); + + assertThat(type.getBaseType(), is("application/json")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreateAddsCustomEncodingWhenCharsetIsMissing() { + MimeType type = MimeTypes.create("text/plain", "ISO-8859-1"); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateSupportsWildcardMimeType() { + MimeType type = MimeTypes.create("text/*", StandardCharsets.UTF_8.name()); + + assertThat(type.getBaseType(), is("text/*")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreatePreservesExistingCharset() { + MimeType type = MimeTypes.create("application/json; charset=ISO-8859-1", StandardCharsets.UTF_8.name()); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateReadsQuotedCharsetValue() { + MimeType type = MimeTypes.create("text/plain; charset=\"ISO-8859-1\"", null); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreatePreservesOtherMimeTypeParameters() { + MimeType type = MimeTypes.create("application/json; version=1", StandardCharsets.UTF_8.name()); + + assertThat(type.getParameter("version"), is("1")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreatePreservesOtherParametersWhenCharsetAlreadyExists() { + MimeType type = MimeTypes.create( + "application/json; version=1; charset=ISO-8859-1", StandardCharsets.UTF_8.name()); + + assertThat(type.getParameter("version"), is("1")); + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateRecognizesCharsetParameterRegardlessOfParameterNameCase() { + MimeType type = MimeTypes.create("text/plain; CHARSET=ISO-8859-1", StandardCharsets.UTF_8.name()); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateLeavesCharsetUnsetWhenEncodingIsNull() { + MimeType type = MimeTypes.create("application/json", null); + + assertThat(MimeTypes.getCharset(type, null), nullValue()); + } + + @Test + public void testCreateLeavesCharsetUnsetForWildcardTypeWhenEncodingIsNull() { + MimeType type = MimeTypes.create("*/*", null); + + assertThat(MimeTypes.getCharset(type, null), nullValue()); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateRejectsMalformedMimeType() { + MimeTypes.create("not-a-mime-type", null); + } + + @Test + public void testCreateIncludesOriginalMimeTypeInParseExceptionMessage() { + try { + MimeTypes.create("not-a-mime-type", null); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), is("not-a-mime-type")); + return; + } + throw new AssertionError("Expected an IllegalArgumentException"); + } + + @Test + public void testCreateRejectsEmptyMimeType() { Review Comment: Use `expected` or `assertThrows` please. ########## gateway-util-common/src/test/java/org/apache/knox/gateway/util/MimeTypesTest.java: ########## @@ -0,0 +1,271 @@ +/* + * 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.knox.gateway.util; + +import jakarta.activation.MimeType; +import org.junit.Test; + +import java.nio.charset.StandardCharsets; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +public class MimeTypesTest { + + @Test + public void testCreateReturnsNullForNullBaseType() { + assertThat(MimeTypes.create(null, StandardCharsets.UTF_8.name()), nullValue()); + } + + @Test + public void testCreateAddsEncodingWhenCharsetIsMissing() { + MimeType type = MimeTypes.create("application/json", StandardCharsets.UTF_8.name()); + + assertThat(type.getBaseType(), is("application/json")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreateAddsCustomEncodingWhenCharsetIsMissing() { + MimeType type = MimeTypes.create("text/plain", "ISO-8859-1"); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateSupportsWildcardMimeType() { + MimeType type = MimeTypes.create("text/*", StandardCharsets.UTF_8.name()); + + assertThat(type.getBaseType(), is("text/*")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreatePreservesExistingCharset() { + MimeType type = MimeTypes.create("application/json; charset=ISO-8859-1", StandardCharsets.UTF_8.name()); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateReadsQuotedCharsetValue() { + MimeType type = MimeTypes.create("text/plain; charset=\"ISO-8859-1\"", null); + + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreatePreservesOtherMimeTypeParameters() { + MimeType type = MimeTypes.create("application/json; version=1", StandardCharsets.UTF_8.name()); + + assertThat(type.getParameter("version"), is("1")); + assertThat(MimeTypes.getCharset(type, null), is(StandardCharsets.UTF_8.name())); + } + + @Test + public void testCreatePreservesOtherParametersWhenCharsetAlreadyExists() { + MimeType type = MimeTypes.create( + "application/json; version=1; charset=ISO-8859-1", StandardCharsets.UTF_8.name()); + + assertThat(type.getParameter("version"), is("1")); + assertThat(MimeTypes.getCharset(type, null), is("ISO-8859-1")); + } + + @Test + public void testCreateRecognizesCharsetParameterRegardlessOfParameterNameCase() { Review Comment: Where exactly is the case logic here? -- 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]
