Skip to content

Commit

Permalink
Improved: Lint unit tests
Browse files Browse the repository at this point in the history
This makes no behavioral changes, only coding style fixes.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1869037 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mthl committed Oct 27, 2019
1 parent d5e0963 commit 59efa18
Show file tree
Hide file tree
Showing 27 changed files with 368 additions and 316 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ checkstyle {
// ‘checkstyle’ tool present in the framework and in the official
// plugins.
tasks.checkstyleMain.maxErrors = 37830
tasks.checkstyleTest.maxErrors = 176
// Currently there are a lot of errors so we need to temporarily
// hide them to avoid polluting the terminal output.
showViolations = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import org.apache.ofbiz.base.conversion.Converter;
import org.apache.ofbiz.base.conversion.DateTimeConverters;
import org.junit.Test;

import com.ibm.icu.util.Calendar;

public class DateTimeTests {

private static <S, T> void assertConversion(String label, Converter<S, T> converter, S source, T target) throws Exception {
private static <S, T> void assertConversion(String label, Converter<S, T> converter, S source, T target)
throws Exception {
assertTrue(label + " can convert", converter.canConvert(source.getClass(), target.getClass()));
assertEquals(label + " converted", target, converter.convert(source));
}
Expand All @@ -48,7 +47,8 @@ public void testDateTimeConverters() throws Exception {
java.sql.Timestamp timestamp = new java.sql.Timestamp(longTime);
// Source class = java.util.Date
assertConversion("DateToLong", new DateTimeConverters.DateToLong(), utilDate, longTime);
assertConversion("DateToSqlDate", new DateTimeConverters.DateToSqlDate(), utilDate, new java.sql.Date(longTime));
assertConversion("DateToSqlDate", new DateTimeConverters.DateToSqlDate(), utilDate,
new java.sql.Date(longTime));
assertConversion("DateToString", new DateTimeConverters.DateToString(), utilDate, utilDate.toString());
assertConversion("DateToTimestamp", new DateTimeConverters.DateToTimestamp(), utilDate, timestamp);
// Source class = java.sql.Date
Expand All @@ -60,13 +60,16 @@ public void testDateTimeConverters() throws Exception {
assertConversion("TimestampToLong", new DateTimeConverters.DateToLong(), timestamp, longTime);
assertConversion("TimestampToDate", new DateTimeConverters.TimestampToDate(), timestamp, utilDate);
assertConversion("TimestampToSqlDate", new DateTimeConverters.TimestampToSqlDate(), timestamp, sqlDate);
assertConversion("TimestampToString", new DateTimeConverters.TimestampToString(), timestamp, timestamp.toString());
assertConversion("TimestampToString", new DateTimeConverters.TimestampToString(), timestamp,
timestamp.toString());
// Source class = java.lang.Long
assertConversion("LongToDate", new DateTimeConverters.NumberToDate(), longTime, utilDate);
assertConversion("LongToSqlDate", new DateTimeConverters.NumberToSqlDate(), longTime, sqlDate);
assertConversion("LongToSqlDate", new DateTimeConverters.NumberToSqlDate(), currentTime, sqlDate); //Test conversion to start of day
//Test conversion to start of day
assertConversion("LongToSqlDate", new DateTimeConverters.NumberToSqlDate(), currentTime, sqlDate);
assertConversion("LongToTimestamp", new DateTimeConverters.NumberToTimestamp(), longTime, timestamp);
// Source class = java.lang.String
assertConversion("StringToTimestamp", new DateTimeConverters.StringToTimestamp(), timestamp.toString(), timestamp);
assertConversion("StringToTimestamp", new DateTimeConverters.StringToTimestamp(), timestamp.toString(),
timestamp);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@
import java.util.List;
import java.util.Map;

import org.apache.ofbiz.base.conversion.Converter;
import org.apache.ofbiz.base.conversion.ConverterLoader;
import org.apache.ofbiz.base.conversion.Converters;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.base.util.UtilMisc;
import org.junit.Test;

public class MiscTests {

public static class ConverterLoaderImpl implements ConverterLoader {
public static final class ConverterLoaderImpl implements ConverterLoader {
@Override
public void loadConverters() {
throw new RuntimeException();
Expand All @@ -54,7 +51,8 @@ public static <S> void assertPassThru(Object wanted, Class<S> sourceClass) throw
assertPassThru(wanted, sourceClass, sourceClass);
}

public static <S> void assertPassThru(Object wanted, Class<S> sourceClass, Class<? super S> targetClass) throws Exception {
public static <S> void assertPassThru(Object wanted, Class<S> sourceClass, Class<? super S> targetClass)
throws Exception {
Converter<S, ? super S> converter = Converters.getConverter(sourceClass, targetClass);
Object result = converter.convert(UtilGenerics.<S>cast(wanted));
assertEquals("pass thru convert", wanted, result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
import java.util.List;
import java.util.Set;

import org.apache.ofbiz.base.conversion.BooleanConverters;
import org.apache.ofbiz.base.conversion.Converter;
import org.apache.ofbiz.base.conversion.ConverterLoader;
import org.apache.ofbiz.base.conversion.Converters;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public TestJSONConverters() {

@Test
public void testJSONToMap() throws Exception {
Converter<JSON, Map<String,String>> converter =
Converter<JSON, Map<String, String>> converter =
UtilGenerics.cast(Converters.getConverter(JSON.class, Map.class));
Map<String,String> map, convertedMap;
Map<String, String> map;
Map<String, String> convertedMap;
map = new HashMap<>();
map.put("field1", "value1");
JSON json = JSON.from(map);
Expand All @@ -53,22 +54,21 @@ public void testJSONToMap() throws Exception {
@Test
public void testJSONToList() throws Exception {
Converter<JSON, List<Object>> converter = UtilGenerics.cast(Converters.getConverter(JSON.class, List.class));
List<Object> list, convertedList;
list = new ArrayList<>();
List<Object> list = new ArrayList<>();
list.add("field1");
list.add("field2");
JSON json = JSON.from(list);
Object obj = converter.convert(json);
convertedList = (obj instanceof List) ? UtilGenerics.cast(obj) : null;
List<Object> convertedList = (obj instanceof List) ? UtilGenerics.cast(obj) : null;
assertEquals("JSON to List", list, convertedList);
}

@Test
public void testMapToJSON() throws Exception {
Converter<Map<String,Object>, JSON> converter =
Converter<Map<String, Object>, JSON> converter =
UtilGenerics.cast(Converters.getConverter(Map.class, JSON.class));
JSON json;
Map<String,Object> map = new LinkedHashMap<>();
Map<String, Object> map = new LinkedHashMap<>();
map.put("field1", "value1");
map.put("field2", new BigDecimal("3.7"));
json = converter.convert(map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.apache.ofbiz.base.lang.ComparableRange;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.junit.Test;

public class ComparableRangeTests {

private static <L extends Comparable<L>, R extends Comparable<R>> void comparableRangeConstructorTest(L left, R right) {
private static <L extends Comparable<L>, R extends Comparable<R>> void comparableRangeConstructorTest(L left,
R right) {
new ComparableRange<>(left, left);
new ComparableRange<>(right, right);
}

private static <T extends Comparable<T>, B extends Comparable<B>> void comparableRangeTest(String label, B bad, T a, T b, T c, T d) {
private static <T extends Comparable<T>, B extends Comparable<B>> void comparableRangeTest(String label, B bad,
T a, T b, T c, T d) {
comparableRangeConstructorTest(bad, a);
assertTrue(label + ":a-isPoint", new ComparableRange<>(a, a).isPoint());
assertTrue(label + ":b-isPoint", new ComparableRange<>(b, b).isPoint());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,43 @@ public void testDebug() {
}

@Test
public void testFormatPrintableCreditCard_1() {
public void testFormatPrintableCreditCard1() {
assertEquals("test 4111111111111111 to ************111",
"************1111",
UtilFormatOut.formatPrintableCreditCard("4111111111111111"));
}

@Test
public void testFormatPrintableCreditCard_2() {
public void testFormatPrintableCreditCard2() {
assertEquals("test 4111 to 4111",
"4111",
UtilFormatOut.formatPrintableCreditCard("4111"));
}

@Test
public void testFormatPrintableCreditCard_3() {
public void testFormatPrintableCreditCard3() {
assertEquals("test null to null",
null,
UtilFormatOut.formatPrintableCreditCard(null));
}

@Test
public void testIsDouble_1() {
public void testIsDouble1() {
assertFalse(UtilValidate.isDouble("10.0", true, true, 2, 2));
}

@Test
public void testIsFloat_1() {
public void testIsFloat1() {
assertFalse(UtilValidate.isFloat("10.0", true, true, 2, 2));
}

@Test
public void testIsDouble_2() {
public void testIsDouble2() {
assertTrue(UtilValidate.isDouble("10.000", true, true, 3, 3));
}

@Test
public void testIsFloat_2() {
public void testIsFloat2() {
assertTrue(UtilValidate.isFloat("10.000", true, true, 3, 3));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* 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.
Expand All @@ -29,12 +29,12 @@
* Assert tests {@link org.apache.ofbiz.base.util.Assert}.
*/
public class AssertTests {
public static final String module = AssertTests.class.getName();
private static final String MODULE = AssertTests.class.getName();

@Test
public void testAssert(){
Object testObject = new Object();
private Object testObject = new Object();

@Test
public void testAssertNullOrEmpty() {
//-----------------------------------------------------------------------
try {
Assert.notNull("foo", testObject);
Expand All @@ -45,10 +45,9 @@ public void testAssert(){
Assert.notNull("foo", null);
fail("notNull - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}


//-----------------------------------------------------------------------
try {
Assert.notNull("foo", testObject, "bar", testObject);
Expand All @@ -59,10 +58,9 @@ public void testAssert(){
Assert.notNull("foo", testObject, "bar", null);
fail("notNull (argument list) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}


//-----------------------------------------------------------------------
try {
Assert.notEmpty("foo", "foo");
Expand All @@ -73,10 +71,9 @@ public void testAssert(){
Assert.notEmpty("foo", "");
fail("notEmpty(String) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}


//-----------------------------------------------------------------------
String[] strArray = {"foo", "bar"};
try {
Expand All @@ -88,17 +85,16 @@ public void testAssert(){
Assert.notEmpty("foo", new String[0]);
fail("notEmpty(Array) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}


//-----------------------------------------------------------------------
List<String> strList = new ArrayList<>();
try {
Assert.notEmpty("foo", strList);
fail("notEmpty(Collection) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}

strList.add("foo");
Expand All @@ -109,12 +105,12 @@ public void testAssert(){
}

//-----------------------------------------------------------------------
Map<String,String> strMap = new HashMap<>();
Map<String, String> strMap = new HashMap<>();
try {
Assert.notEmpty("foo", strMap);
fail("notEmpty(Map) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}

strMap.put("foo", "foo");
Expand All @@ -123,7 +119,12 @@ public void testAssert(){
} catch (Exception e) {
fail("notEmpty(Map) threw an exception - " + e);
}
}

@Test
public void testAssertInstance() {
Map<String, String> strMap = new HashMap<>();
strMap.put("foo", "foo");
//-----------------------------------------------------------------------
try {
Assert.isInstanceOf("foo", strMap, Map.class);
Expand All @@ -134,10 +135,9 @@ public void testAssert(){
Assert.isInstanceOf("foo", strMap, AssertTests.class);
fail("isInstanceOf - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}


//-----------------------------------------------------------------------
try {
Assert.isInstanceOf("foo", strMap, String.class, Map.class);
Expand All @@ -148,10 +148,9 @@ public void testAssert(){
Assert.isInstanceOf("foo", strMap, String.class, AssertTests.class);
fail("isInstanceOf (argument list) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}


//-----------------------------------------------------------------------
try {
Assert.isNotInstanceOf("foo", strMap, String.class);
Expand All @@ -162,10 +161,9 @@ public void testAssert(){
Assert.isNotInstanceOf("foo", strMap, Map.class);
fail("isNotInstanceOf - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}


//-----------------------------------------------------------------------
try {
Assert.isNotInstanceOf("foo", strMap, String.class, AssertTests.class);
Expand All @@ -176,10 +174,13 @@ public void testAssert(){
Assert.isNotInstanceOf("foo", strMap, String.class, Map.class);
fail("isNotInstanceOf (argument list) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}
}


@Test
public void testAssertAssignable() {
String[] strArray = {"foo", "bar"};
//-----------------------------------------------------------------------
try {
Assert.isAssignableTo("foo", strArray, strArray.getClass());
Expand All @@ -190,8 +191,7 @@ public void testAssert(){
Assert.isAssignableTo("foo", strArray, Map[].class);
fail("isNotInstanceOf (argument list) - IllegalArgumentException not thrown");
} catch (IllegalArgumentException e) {
Debug.logError(e, module);
Debug.logError(e, MODULE);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.StringWriter;

import org.apache.ofbiz.base.util.IndentingWriter;
import org.junit.Test;

public class IndentingWriterTests {
Expand Down
Loading

0 comments on commit 59efa18

Please sign in to comment.