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.MonetaryAmount;
019import javax.money.MonetaryAmountFactory;
020import javax.money.MonetaryAmounts;
021import javax.money.MonetaryOperator;
022import java.util.Collection;
023
024import static javax.money.MonetaryAmounts.getAmountFactory;
025import static org.testng.Assert.assertEquals;
026import static org.testng.Assert.assertNotNull;
027
028/**
029 * Created by Anatole on 10.03.14.
030 */
031@SpecVersion(spec = "JSR 354", version = "1.0.0")
032public class FunctionalExtensionPointsTest{
033
034    // *************************** A. Monetary Operator Implementation Requirements ***************
035
036    /**
037     * The return type of apply must be the same type as the
038     * parameter
039     * (amount.getClass() == result.getClass()).
040     */
041    @SpecAssertion(section = "4.2.4", id = "424-A1")
042    @Test(description = "4.2.4 Ensures the result of all operators under test is of the same class as the input.")
043    public void testOperatorReturnTypeEqualsParameter(){
044        Collection<MonetaryOperator> operators = TCKTestSetup.getTestConfiguration().getMonetaryOperators4Test();
045        assertNotNull(operators,
046                      "No operators (null) to test returned from TestConfiguration.getMonetaryOperators4Test().");
047        for(Class type : MonetaryAmounts.getAmountTypes()){
048            if(type.equals(TestAmount.class)){
049                continue;
050            }
051            MonetaryAmountFactory<?> f = getAmountFactory(type);
052            f.setCurrency("CHF");
053            f.setNumber(200.10);
054            MonetaryAmount m = f.create();
055            for(MonetaryOperator op : operators){
056                MonetaryAmount m2 = m.with(op);
057                assertEquals(m2.getClass(), m.getClass(),
058                             "Operator returned type with different type, which is not allowed: " +
059                                     op.getClass().getName());
060            }
061        }
062    }
063
064
065}