Skip to content

Commit

Permalink
Merge bf267ad into 269af57
Browse files Browse the repository at this point in the history
  • Loading branch information
santosr09 committed Apr 11, 2020
2 parents 269af57 + bf267ad commit 14fb0e0
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package com.opensymphony.xwork2.conversion.impl;

import org.apache.struts2.StrutsException;
import org.apache.struts2.conversion.TypeConversionException;

import java.lang.reflect.Constructor;
import java.lang.reflect.Member;
Expand Down Expand Up @@ -90,11 +90,11 @@ public Object convertValue(Map<String, Object> context, Object target, Member me
Constructor constructor = toType.getConstructor(new Class[]{long.class});
return constructor.newInstance(new Object[]{Long.valueOf(result.getTime())});
} catch (Exception e) {
throw new StrutsException("Couldn't create class " + toType + " using default (long) constructor", e);
throw new TypeConversionException("Couldn't create class " + toType + " using default (long) constructor", e);
}
}
} catch (ParseException e) {
throw new StrutsException("Could not parse date", e);
throw new TypeConversionException("Could not parse date", e);
}
} else if (Date.class.isAssignableFrom(value.getClass())) {
result = (Date) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package com.opensymphony.xwork2.conversion.impl;

import org.apache.struts2.conversion.TypeConversionException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.StrutsException;

import java.lang.reflect.Member;
import java.math.BigDecimal;
Expand Down Expand Up @@ -51,7 +51,7 @@ public Object convertValue(Map<String, Object> context, Object target, Member me
Object convertedValue = super.convertValue(context, value, toType);

if (!isInRange((Number) convertedValue, stringValue, toType))
throw new StrutsException("Overflow or underflow casting: \"" + stringValue + "\" into class " + convertedValue.getClass().getName());
throw new TypeConversionException("Overflow or underflow casting: \"" + stringValue + "\" into class " + convertedValue.getClass().getName());

return convertedValue;
} else {
Expand All @@ -67,11 +67,11 @@ public Object convertValue(Map<String, Object> context, Object target, Member me
Number number = numFormat.parse(stringValue, parsePos);

if (parsePos.getIndex() != stringValue.length()) {
throw new StrutsException("Unparseable number: \"" + stringValue + "\" at position "
throw new TypeConversionException("Unparseable number: \"" + stringValue + "\" at position "
+ parsePos.getIndex());
} else {
if (!isInRange(number, stringValue, toType))
throw new StrutsException("Overflow or underflow casting: \"" + stringValue + "\" into class " + number.getClass().getName());
throw new TypeConversionException("Overflow or underflow casting: \"" + stringValue + "\" into class " + number.getClass().getName());

value = super.convertValue(context, number, toType);
}
Expand Down Expand Up @@ -103,7 +103,7 @@ protected Object convertToBigDecimal(Map<String, Object> context, String stringV
Number number = format.parse(stringValue, parsePosition);

if (parsePosition.getIndex() != stringValue.length()) {
throw new StrutsException("Unparseable number: \"" + stringValue + "\" at position " + parsePosition.getIndex());
throw new TypeConversionException("Unparseable number: \"" + stringValue + "\" at position " + parsePosition.getIndex());
}

return number;
Expand All @@ -123,11 +123,11 @@ protected Object convertToDouble(Map<String, Object> context, String stringValue
Number number = format.parse(stringValue, parsePosition);

if (parsePosition.getIndex() != stringValue.length()) {
throw new StrutsException("Unparseable number: \"" + stringValue + "\" at position " + parsePosition.getIndex());
throw new TypeConversionException("Unparseable number: \"" + stringValue + "\" at position " + parsePosition.getIndex());
}

if (!isInRange(number, stringValue, Double.class)) {
throw new StrutsException("Overflow or underflow converting: \"" + stringValue + "\" into class " + number.getClass().getName());
throw new TypeConversionException("Overflow or underflow converting: \"" + stringValue + "\" into class " + number.getClass().getName());
}

if (number != null) {
Expand All @@ -151,11 +151,11 @@ protected Object convertToFloat(Map<String, Object> context, String stringValue)
Number number = format.parse(stringValue, parsePosition);

if (parsePosition.getIndex() != stringValue.length()) {
throw new StrutsException("Unparseable number: \"" + stringValue + "\" at position " + parsePosition.getIndex());
throw new TypeConversionException("Unparseable number: \"" + stringValue + "\" at position " + parsePosition.getIndex());
}

if (!isInRange(number, stringValue, Float.class)) {
throw new StrutsException("Overflow or underflow converting: \"" + stringValue + "\" into class " + number.getClass().getName());
throw new TypeConversionException("Overflow or underflow converting: \"" + stringValue + "\" into class " + number.getClass().getName());
}

if (number != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
*/
package com.opensymphony.xwork2.conversion.impl;

import org.apache.struts2.conversion.TypeConversionException;
import com.opensymphony.xwork2.conversion.TypeConverter;
import com.opensymphony.xwork2.inject.Container;
import com.opensymphony.xwork2.inject.Inject;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.StrutsException;

import java.lang.reflect.Member;
import java.util.Calendar;
Expand Down Expand Up @@ -130,7 +130,7 @@ public Object convertValue(Map<String, Object> context, Object o, Member member,
}

if (result == null && value != null && !"".equals(value)) {
throw new StrutsException("Cannot create type " + toType + " from value " + value);
throw new TypeConversionException("Cannot create type " + toType + " from value " + value);
}
}

Expand Down Expand Up @@ -170,7 +170,7 @@ private Class doConvertToClass(Object value) {
try {
clazz = Class.forName((String) value);
} catch (ClassNotFoundException e) {
throw new StrutsException(e.getLocalizedMessage(), e);
throw new TypeConversionException(e.getLocalizedMessage(), e);
}
}
return clazz;
Expand All @@ -179,39 +179,39 @@ private Class doConvertToClass(Object value) {
private Object doConvertToCollection(Map<String, Object> context, Object o, Member member, String prop, Object value, Class toType) {
TypeConverter converter = container.getInstance(CollectionConverter.class);
if (converter == null) {
throw new StrutsException("TypeConverter with name [#0] must be registered first!", StrutsConstants.STRUTS_CONVERTER_COLLECTION);
throw new TypeConversionException("TypeConverter with name [#0] must be registered first! Converter: "+ StrutsConstants.STRUTS_CONVERTER_COLLECTION);
}
return converter.convertValue(context, o, member, prop, value, toType);
}

private Object doConvertToArray(Map<String, Object> context, Object o, Member member, String prop, Object value, Class toType) {
TypeConverter converter = container.getInstance(ArrayConverter.class);
if (converter == null) {
throw new StrutsException("TypeConverter with name [#0] must be registered first!", StrutsConstants.STRUTS_CONVERTER_ARRAY);
throw new TypeConversionException("TypeConverter with name [#0] must be registered first! Converter: "+ StrutsConstants.STRUTS_CONVERTER_ARRAY);
}
return converter.convertValue(context, o, member, prop, value, toType);
}

private Object doConvertToDate(Map<String, Object> context, Object value, Class toType) {
TypeConverter converter = container.getInstance(DateConverter.class);
if (converter == null) {
throw new StrutsException("TypeConverter with name [#0] must be registered first!", StrutsConstants.STRUTS_CONVERTER_DATE);
throw new TypeConversionException("TypeConverter with name [#0] must be registered first! Converter: "+ StrutsConstants.STRUTS_CONVERTER_DATE);
}
return converter.convertValue(context, null, null, null, value, toType);
}

private Object doConvertToNumber(Map<String, Object> context, Object value, Class toType) {
TypeConverter converter = container.getInstance(NumberConverter.class);
if (converter == null) {
throw new StrutsException("TypeConverter with name [#0] must be registered first!", StrutsConstants.STRUTS_CONVERTER_NUMBER);
throw new TypeConversionException("TypeConverter with name [#0] must be registered first! Converter: "+ StrutsConstants.STRUTS_CONVERTER_NUMBER);
}
return converter.convertValue(context, null, null, null, value, toType);
}

private Object doConvertToString(Map<String, Object> context, Object value) {
TypeConverter converter = container.getInstance(StringConverter.class);
if (converter == null) {
throw new StrutsException("TypeConverter with name [#0] must be registered first!", StrutsConstants.STRUTS_CONVERTER_STRING);
throw new TypeConversionException("TypeConverter with name [#0] must be registered first! Converter: "+ StrutsConstants.STRUTS_CONVERTER_STRING);
}
return converter.convertValue(context, null, null, null, value, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package com.opensymphony.xwork2.conversion;
package org.apache.struts2.conversion;

import org.apache.struts2.StrutsException;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* 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 com.opensymphony.xwork2.conversion.impl;

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.conversion.TypeConversionException;
import org.apache.struts2.StrutsInternalTestCase;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class DateConverterTest extends StrutsInternalTestCase {

private String INPUT_TIME_STAMP_STR;
private String INPUT_WHEN_LONG_CONSTRUCTOR_STR;
private Locale mxLocale = new Locale("es_MX", "MX");
private final static String RES_TIME_STAMP_STR = "2020-03-20 00:00:00.0";
private final static String TIME_01_59_10 = "01:59:10 AM";
private final static String DATE_STR = "2020-03-20";
private final static String DATE_CONVERTED = "Fri Mar 20 00:00:00";
private final static String INVALID_DATE = "99/99/2010";
private final static String MESSAGE_PARSE_ERROR = "Could not parse date";
private final static String MESSAGE_DEFAULT_CONSTRUCTOR_ERROR = "Couldn't create class null using default (long) constructor";

public void testSqlTimeType() {
DateConverter converter = new DateConverter();

Map<String, Object> context = new HashMap<>();
context.put(ActionContext.LOCALE, new Locale("es_MX", "MX"));

Object value = converter.convertValue(context, null, null, null, TIME_01_59_10, Time.class);
assertEquals("01:59:10", value.toString());
}

public void testSqlTimestampType() {
DateConverter converter = new DateConverter();
Map<String, Object> context = new HashMap<>();
context.put(ActionContext.LOCALE, mxLocale);

Object value = converter.convertValue(context, null, null, null, INPUT_TIME_STAMP_STR, Timestamp.class);
assertEquals(RES_TIME_STAMP_STR, value.toString());
}

public void testDateType() {
DateConverter converter = new DateConverter();

Map<String, Object> context = new HashMap<>();
context.put(ActionContext.LOCALE, new Locale("es_MX", "MX"));

Object value = converter.convertValue(context, null, null, null, DATE_STR, Date.class);
assertTrue(((Date) value).toString().startsWith(DATE_CONVERTED));
}

public void testTypeConversionExceptionWhenParseError() {
DateConverter converter = new DateConverter();

Map<String, Object> context = new HashMap<>();
context.put(ActionContext.LOCALE, new Locale("es_MX", "MX"));

try {
Object value = converter.convertValue(context, null, null, null, INVALID_DATE, Date.class);
fail("TypeConversionException expected - Conversion error occurred");
} catch (Exception ex) {
assertEquals(TypeConversionException.class, ex.getClass());
assertEquals(MESSAGE_PARSE_ERROR, ex.getMessage());
}
}

public void testTypeConversionExceptionWhenUsingLongConstructor() {
DateConverter converter = new DateConverter();

Map<String, Object> context = new HashMap<>();
context.put(ActionContext.LOCALE, new Locale("es_MX", "MX"));

try {
Object value = converter.convertValue(context, null, null, null, INPUT_WHEN_LONG_CONSTRUCTOR_STR, null);
fail("TypeConversionException expected - Error using default (long) constructor");
} catch (Exception ex) {
assertEquals(TypeConversionException.class, ex.getClass());
assertEquals(MESSAGE_DEFAULT_CONSTRUCTOR_ERROR, ex.getMessage());
}
}

@Override
protected void setUp() {
//Due to JEP 252: Use CLDR Locale Data by Default
DateFormat dFormat = DateFormat.getDateInstance(DateFormat.SHORT, mxLocale);
if(dFormat.format(new Date()).contains("-")){ // Format when Java 9 or greater
INPUT_TIME_STAMP_STR = "2020-03-20 00:00:00.000";
INPUT_WHEN_LONG_CONSTRUCTOR_STR = "2020-03-20";
}else{ // Format when Java 8 or lower
INPUT_TIME_STAMP_STR = "03/20/2020 00:00:00.000";
INPUT_WHEN_LONG_CONSTRUCTOR_STR = "03/31/20";
}
}

}

0 comments on commit 14fb0e0

Please sign in to comment.