|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.commons.beanutils2.sql; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | + |
| 23 | +import java.lang.reflect.InvocationHandler; |
| 24 | +import java.lang.reflect.Proxy; |
| 25 | +import java.sql.ResultSet; |
| 26 | +import java.sql.ResultSetMetaData; |
| 27 | +import java.sql.Types; |
| 28 | +import java.util.Locale; |
| 29 | + |
| 30 | +import org.apache.commons.beanutils2.DynaProperty; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + * Tests that column names are folded to lower case independently of the default {@link Locale}. |
| 35 | + */ |
| 36 | +class JdbcDynaClassLocaleTest { |
| 37 | + |
| 38 | + private static ResultSet singleColumnResultSet(final String columnName) { |
| 39 | + final InvocationHandler metaData = (proxy, method, args) -> { |
| 40 | + switch (method.getName()) { |
| 41 | + case "getColumnCount": |
| 42 | + return Integer.valueOf(1); |
| 43 | + case "getColumnLabel": |
| 44 | + case "getColumnName": |
| 45 | + return columnName; |
| 46 | + case "getColumnType": |
| 47 | + return Integer.valueOf(Types.VARCHAR); |
| 48 | + case "getColumnClassName": |
| 49 | + return String.class.getName(); |
| 50 | + default: |
| 51 | + throw new UnsupportedOperationException(method.getName()); |
| 52 | + } |
| 53 | + }; |
| 54 | + final ResultSetMetaData metaDataProxy = (ResultSetMetaData) Proxy.newProxyInstance(ResultSetMetaData.class.getClassLoader(), |
| 55 | + new Class[] { ResultSetMetaData.class }, metaData); |
| 56 | + final InvocationHandler resultSet = (proxy, method, args) -> { |
| 57 | + if ("getMetaData".equals(method.getName())) { |
| 58 | + return metaDataProxy; |
| 59 | + } |
| 60 | + throw new UnsupportedOperationException(method.getName()); |
| 61 | + }; |
| 62 | + return (ResultSet) Proxy.newProxyInstance(ResultSet.class.getClassLoader(), new Class[] { ResultSet.class }, resultSet); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void testColumnNameLowerCasedIndependentOfLocale() throws Exception { |
| 67 | + final Locale save = Locale.getDefault(); |
| 68 | + try { |
| 69 | + // Under the Turkish locale "ID".toLowerCase() is "ıd" (dotless i), not "id". |
| 70 | + Locale.setDefault(new Locale("tr", "TR")); |
| 71 | + final ResultSetDynaClass dynaClass = new ResultSetDynaClass(singleColumnResultSet("ID")); |
| 72 | + final DynaProperty property = dynaClass.getDynaProperty("id"); |
| 73 | + assertNotNull(property, "column 'ID' must map to property 'id'"); |
| 74 | + assertEquals("id", property.getName()); |
| 75 | + } finally { |
| 76 | + Locale.setDefault(save); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments