001/*
002 * Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch). Licensed under the Apache
003 * License, Version 2.0 (the "License"); you may not use this file except in compliance with the
004 * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005 * Unless required by applicable law or agreed to in writing, software distributed under the License
006 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
007 * or implied. See the License for the specific language governing permissions and limitations under
008 * the License. Contributors: Anatole Tresch - initial version.
009 */
010package org.javamoney.tck.tests;
011
012import org.javamoney.tck.TCKTestSetup;
013import org.javamoney.tck.tests.internal.TestAmount;
014import org.jboss.test.audit.annotations.SpecAssertion;
015import org.jboss.test.audit.annotations.SpecVersion;
016import org.testng.annotations.Test;
017
018import javax.money.Monetary;
019import javax.money.MonetaryAmount;
020import javax.money.MonetaryAmountFactory;
021import javax.money.MonetaryOperator;
022import java.util.Collection;
023
024import static org.testng.Assert.assertEquals;
025import static org.testng.Assert.assertNotNull;
026
027/**
028 * Tests the functional extension points.
029 */
030@SpecVersion(spec = "JSR 354", version = "1.0.0")
031public class FunctionalExtensionPointsTest {
032
033    // *************************** A. Monetary Operator Implementation Requirements ***************
034
035    /**
036     * The return type of apply must be the same type as the
037     * parameter
038     * (amount.getClass() == result.getClass()).
039     */
040    @SpecAssertion(section = "4.2.4", id = "424-A1")
041    @Test(description = "4.2.4 Ensures the result of all operators under test is of the same class as the input.")
042    public void testOperatorReturnTypeEqualsParameter() {
043        Collection<MonetaryOperator> operators = TCKTestSetup.getTestConfiguration().getMonetaryOperators4Test();
044        assertNotNull(operators,
045                "No operators (null) to test returned from TestConfiguration.getMonetaryOperators4Test().");
046        for (Class type : Monetary.getAmountTypes()) {
047            if (type.equals(TestAmount.class)) {
048                continue;
049            }
050            MonetaryAmountFactory<?> f = Monetary.getAmountFactory(type);
051            f.setCurrency("CHF");
052            f.setNumber(200.10);
053            MonetaryAmount m = f.create();
054            for (MonetaryOperator op : operators) {
055                MonetaryAmount m2 = m.with(op);
056                assertEquals(m2.getClass(), m.getClass(),
057                        "Operator returned type with different type, which is not allowed: " +
058                                op.getClass().getName());
059            }
060        }
061    }
062
063
064}