From 04da31d37c4ecb3f1337f455d773cadaa1c846c0 Mon Sep 17 00:00:00 2001 From: Jochen Staerk Date: Mon, 22 Jul 2019 20:00:01 +0200 Subject: [PATCH] closes #23 --- History.md | 1 + .../ZUGFeRD/ZUGFeRD2PullProvider.java | 26 +++----- .../org/mustangproject/ZUGFeRD/BaseTest.java | 65 +++++++++++++++++++ 3 files changed, 76 insertions(+), 16 deletions(-) create mode 100644 src/test/java/org/mustangproject/ZUGFeRD/BaseTest.java diff --git a/History.md b/History.md index 9f3087f8..dc797fd2 100644 --- a/History.md +++ b/History.md @@ -2,6 +2,7 @@ - in ram:ExchangedDocument: ram:name entfernen - updated javadoc - fixed #104 nullpointerex when specifying no parameter +- closed #23 1.7.2 ===== diff --git a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRD2PullProvider.java b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRD2PullProvider.java index e2f7f09d..12b17673 100644 --- a/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRD2PullProvider.java +++ b/src/main/java/org/mustangproject/ZUGFeRD/ZUGFeRD2PullProvider.java @@ -77,7 +77,7 @@ public class ZUGFeRD2PullProvider implements IXMLProvider, IProfileProvider { public void setTest() { } - private String nDigitFormat(BigDecimal value, int scale) { + public static String nDigitFormat(BigDecimal value, int scale) { /* * I needed 123,45, locale independent.I tried * NumberFormat.getCurrencyInstance().format( 12345.6789 ); but that is locale @@ -92,16 +92,6 @@ public class ZUGFeRD2PullProvider implements IXMLProvider, IProfileProvider { * http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html in the * end I decided to calculate myself and take eur+sparator+cents * - * This function will cut off, i.e. floor() subcent values Tests: - * System.err.println(utils.currencyFormat(new BigDecimal(0), - * ".")+"\n"+utils.currencyFormat(new BigDecimal("-1.10"), - * ",")+"\n"+utils.currencyFormat(new BigDecimal("-1.1"), - * ",")+"\n"+utils.currencyFormat(new BigDecimal("-1.01"), - * ",")+"\n"+utils.currencyFormat(new BigDecimal("20000123.3489"), - * ",")+"\n"+utils.currencyFormat(new BigDecimal("20000123.3419"), - * ",")+"\n"+utils.currencyFormat(new BigDecimal("12"), ",")); - * - * results 0.00 -1,10 -1,10 -1,01 20000123,34 20000123,34 12,00 */ value = value.setScale(scale, BigDecimal.ROUND_HALF_UP); // first, round so that e.g. // 1.189999999999999946709294817992486059665679931640625 @@ -111,25 +101,29 @@ public class ZUGFeRD2PullProvider implements IXMLProvider, IProfileProvider { DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(); otherSymbols.setDecimalSeparator('.'); - DecimalFormat dec = new DecimalFormat("0." + new String(repeat), otherSymbols); + String baseFormat="0"; + if (scale>0) { + baseFormat+="."; + } + DecimalFormat dec = new DecimalFormat(baseFormat + new String(repeat), otherSymbols); return dec.format(value); } private String vatFormat(BigDecimal value) { - return nDigitFormat(value, 2); + return ZUGFeRD2PullProvider.nDigitFormat(value, 2); } private String currencyFormat(BigDecimal value) { - return nDigitFormat(value, 2); + return ZUGFeRD2PullProvider.nDigitFormat(value, 2); } private String priceFormat(BigDecimal value) { - return nDigitFormat(value, 4); + return ZUGFeRD2PullProvider.nDigitFormat(value, 4); } private String quantityFormat(BigDecimal value) { - return nDigitFormat(value, 4); + return ZUGFeRD2PullProvider.nDigitFormat(value, 4); } @Override diff --git a/src/test/java/org/mustangproject/ZUGFeRD/BaseTest.java b/src/test/java/org/mustangproject/ZUGFeRD/BaseTest.java new file mode 100644 index 00000000..8d3152b6 --- /dev/null +++ b/src/test/java/org/mustangproject/ZUGFeRD/BaseTest.java @@ -0,0 +1,65 @@ +/** ********************************************************************** + * + * Copyright 2019 Jochen Staerk + * + * Use is subject to license terms. + * + * Licensed 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.mustangproject.ZUGFeRD; + +import junit.framework.Test; +import junit.framework.TestSuite; +import junit.framework.TestCase; + +import org.junit.FixMethodOrder; +import org.junit.runners.MethodSorters; + +import java.io.IOException; +import java.io.InputStream; +import java.math.BigDecimal; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +public class BaseTest extends TestCase { + /** + * Create the test case + * + * @param testName name of the test case + */ + public BaseTest(String testName) { + super(testName); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() { + return new TestSuite(BaseTest.class); + } + + public void testCorrectDigits() { + assertEquals("0.00", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal(0),2)); + assertEquals("-1.10", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal("-1.10"),2)); + assertEquals("-1.10", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal("-1.1"),2)); + assertEquals("-1.01", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal("-1.01"),2)); + assertEquals("20000123.35", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal("20000123.3489"),2)); + assertEquals("20000123.34", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal("20000123.3419"),2)); + assertEquals("12.00", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal("12"),2)); + assertEquals("12", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal("12"),0)); + assertEquals("20000123.342", ZUGFeRD2PullProvider.nDigitFormat(new BigDecimal("20000123.3419"),3)); + } + +}