Skip to content

Commit cb3f59c

Browse files
committed
Adapted ServiceLoader impl.
Make dep on latest snapshot (release 1.0 will come soon). Wrote tests for FutureValue, PresentValue formulas.
1 parent 21a81ae commit cb3f59c

File tree

5 files changed

+168
-76
lines changed

5 files changed

+168
-76
lines changed

calc/src/test/java/org/javamoney/calc/common/FutureValueTest.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,58 +15,65 @@
1515
*/
1616
package org.javamoney.calc.common;
1717

18-
import static org.junit.Assert.assertEquals;
18+
import org.javamoney.moneta.Money;
19+
import org.junit.Test;
1920

21+
import javax.money.MonetaryOperator;
22+
import javax.money.MonetaryRoundings;
23+
import javax.money.RoundingQueryBuilder;
2024
import java.math.BigDecimal;
2125
import java.math.RoundingMode;
22-
import javax.money.*;
2326

24-
import org.javamoney.moneta.Money;
25-
import org.junit.Ignore;
26-
import org.junit.Test;
27+
import static org.junit.Assert.assertEquals;
2728

28-
public class FutureValueTest{
29+
/**
30+
* Tests for {@link org.javamoney.calc.common.FutureValue} formula.
31+
*/
32+
public class FutureValueTest {
2933

3034
/**
3135
* Method: of(Rate rate, int periods)
3236
*/
3337
@Test
34-
public void testOf() throws Exception {
35-
//TODO: Test goes here...
38+
public void testOfAndApply() throws Exception {
39+
Money money = Money.of(100, "CHF");
40+
MonetaryOperator rounding = MonetaryRoundings.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
41+
.build());
42+
assertEquals(Money.of(BigDecimal.valueOf(105.00), "CHF"), money.with(FutureValue.of(Rate.of(0.05), 1)).with(rounding));
43+
assertEquals(Money.of(BigDecimal.valueOf(110.25), "CHF"), money.with(FutureValue.of(Rate.of(0.05), 2)).with(rounding));
44+
assertEquals(Money.of(BigDecimal.valueOf(210.49), "CHF"), money.with(FutureValue.of(Rate.of(0.07), 11)).with(rounding));
3645
}
3746

3847
/**
3948
* Method: calculate(MonetaryAmount amount, Rate rate, int periods)
4049
*/
4150
@Test
4251
public void testCalculate() throws Exception {
43-
//TODO: Test goes here...
44-
}
45-
46-
/**
47-
* Method: apply(MonetaryAmount amount)
48-
*/
49-
@Test
50-
public void testApply() throws Exception {
51-
//TODO: Test goes here...
52+
Money money = Money.of(100, "CHF");
53+
MonetaryOperator rounding = MonetaryRoundings.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
54+
.build());
55+
assertEquals(Money.of(BigDecimal.valueOf(105.00), "CHF"), FutureValue.calculate(money, Rate.of(0.05), 1).with(rounding));
56+
assertEquals(Money.of(BigDecimal.valueOf(110.25), "CHF"), FutureValue.calculate(money, Rate.of(0.05), 2).with(rounding));
57+
assertEquals(Money.of(BigDecimal.valueOf(210.49), "CHF"), FutureValue.calculate(money, Rate.of(0.07), 11).with(rounding));
5258
}
5359

5460
/**
5561
* Method: toString()
5662
*/
5763
@Test
5864
public void testToString() throws Exception {
59-
//TODO: Test goes here...
65+
assertEquals("FutureValue{rate=Rate[0.05], periods=1}", FutureValue.of(Rate.of(0.05), 1).toString());
66+
assertEquals("FutureValue{rate=Rate[0.05], periods=2}", FutureValue.of(Rate.of(0.05), 2).toString());
67+
assertEquals("FutureValue{rate=Rate[0.07], periods=11}", FutureValue.of(Rate.of(0.07), 11).toString());
6068
}
6169

6270
@Test
63-
public void test(){
71+
public void testFormula() {
6472
Money money = Money.of(100, "CHF");
65-
MonetaryOperator rounding = MonetaryRoundings.getRounding(RoundingQueryBuilder.of().setScale(2)
66-
.setRoundingName("r1")
73+
MonetaryOperator rounding = MonetaryRoundings.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
6774
.build());
68-
assertEquals(Money.of(BigDecimal.valueOf(95.24), "CHF"), FutureValue.calculate(money, Rate.of(0.05), 1).with(rounding));
69-
assertEquals(Money.of(BigDecimal.valueOf(90.7), "CHF"), FutureValue.calculate(money, Rate.of(0.05), 2).with(rounding));
70-
assertEquals(Money.of(BigDecimal.valueOf(86.38), "CHF"), FutureValue.calculate(money, Rate.of(0.05), 3).with(rounding));
75+
assertEquals(Money.of(BigDecimal.valueOf(105.00), "CHF"), FutureValue.calculate(money, Rate.of(0.05), 1).with(rounding));
76+
assertEquals(Money.of(BigDecimal.valueOf(110.25), "CHF"), FutureValue.calculate(money, Rate.of(0.05), 2).with(rounding));
77+
assertEquals(Money.of(BigDecimal.valueOf(210.49), "CHF"), FutureValue.calculate(money, Rate.of(0.07), 11).with(rounding));
7178
}
7279
}

calc/src/test/java/org/javamoney/calc/common/PresentValueTest.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,45 @@
1818
import org.javamoney.moneta.Money;
1919
import org.junit.Test;
2020

21+
/**
22+
* Tests for the {@link org.javamoney.calc.common.PresentValue} formula calculator.
23+
*/
2124
public class PresentValueTest{
2225

2326
/**
2427
* Method: of(Rate rate, int periods)
2528
*/
2629
@Test
27-
public void testOf() throws Exception {
28-
//TODO: Test goes here...
30+
public void testOfAndApply() throws Exception {
31+
Money money = Money.of(100, "CHF");
32+
MonetaryOperator rounding = MonetaryRoundings.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
33+
.build());
34+
assertEquals(Money.of(BigDecimal.valueOf(95.24), "CHF"), money.with(PresentValue.of(Rate.of(0.05), 1)).with(rounding));
35+
assertEquals(Money.of(BigDecimal.valueOf(90.70), "CHF"), money.with(PresentValue.of(Rate.of(0.05), 2)).with(rounding));
36+
assertEquals(Money.of(BigDecimal.valueOf(47.51), "CHF"), money.with(PresentValue.of(Rate.of(0.07), 11)).with(rounding));
2937
}
3038

3139
/**
3240
* Method: calculate(MonetaryAmount amount, Rate rate, int periods)
3341
*/
3442
@Test
3543
public void testCalculate() throws Exception {
36-
//TODO: Test goes here...
37-
}
38-
39-
/**
40-
* Method: apply(MonetaryAmount amount)
41-
*/
42-
@Test
43-
public void testApply() throws Exception {
44-
//TODO: Test goes here...
44+
Money money = Money.of(100, "CHF");
45+
MonetaryOperator rounding = MonetaryRoundings.getRounding(RoundingQueryBuilder.of().setScale(2).set(RoundingMode.HALF_EVEN)
46+
.build());
47+
assertEquals(Money.of(BigDecimal.valueOf(95.24), "CHF"), PresentValue.calculate(money, Rate.of(0.05), 1).with(rounding));
48+
assertEquals(Money.of(BigDecimal.valueOf(90.70), "CHF"), PresentValue.calculate(money, Rate.of(0.05), 2).with(rounding));
49+
assertEquals(Money.of(BigDecimal.valueOf(47.51), "CHF"), PresentValue.calculate(money, Rate.of(0.07), 11).with(rounding));
4550
}
4651

4752
/**
4853
* Method: toString()
4954
*/
5055
@Test
5156
public void testToString() throws Exception {
52-
//TODO: Test goes here...
57+
assertEquals("PresentValue{rate=Rate[0.05], periods=1}", PresentValue.of(Rate.of(0.05), 1).toString());
58+
assertEquals("PresentValue{rate=Rate[0.05], periods=2}", PresentValue.of(Rate.of(0.05), 2).toString());
59+
assertEquals("PresentValue{rate=Rate[0.07], periods=11}", PresentValue.of(Rate.of(0.07), 11).toString());
5360
}
5461

55-
56-
57-
@Test
58-
public void test(){
59-
Money money = Money.of(100, "CHF");
60-
MonetaryRounding rounding = MonetaryRoundings
61-
.getRounding(RoundingQueryBuilder.of().setScale(2).setRoundingName("r1").build());
62-
assertEquals(Money.of(BigDecimal.valueOf(95.24), "CHF"), PresentValue.calculate(money, Rate.of(0.05), 1).with(rounding));
63-
assertEquals(Money.of(new BigDecimal("90.70"), "CHF"), PresentValue.calculate(money, Rate.of(0.05), 2).with(rounding));
64-
assertEquals(Money.of(BigDecimal.valueOf(86.38), "CHF"), PresentValue.calculate(money, Rate.of(0.05), 3).with(rounding));
65-
}
6662
}

integration/javamoney-cdi/src/main/java/org/javamoney/cdi/bootstrap/CDISEServiceProvider.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,51 @@
1+
/*
2+
* Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*
16+
*
17+
* Contributors: Anatole Tresch - initial version.
18+
*/
119
package org.javamoney.cdi.bootstrap;
220

321
import java.util.*;
4-
import java.util.concurrent.ConcurrentHashMap;
522

623
import javax.enterprise.inject.Instance;
7-
import javax.money.spi.DefaultServiceProvider;
824
import javax.money.spi.ServiceProvider;
925

26+
import org.javamoney.moneta.internal.PriorityAwareServiceProvider;
1027
import org.javamoney.moneta.spi.ServicePriority;
11-
import org.slf4j.LoggerFactory;
1228

1329
/**
1430
* Overriding ServiceProvider that actually tries to satisfy component requests from CDI,
1531
* where possible. Additionally ServiceLoader based service are loaded and are returned
1632
* ONLY, when not the same service is loaded as well in CDI.
1733
*/
18-
@ServicePriority(ServicePriority.NORM_PRIORITY + 1)
1934
public class CDISEServiceProvider implements ServiceProvider {
2035
/**
2136
* Default provider, using ServiceLoader.
2237
*/
23-
private ServiceProvider defaultServiceProvider = new DefaultServiceProvider();
38+
private ServiceProvider defaultServiceProvider = new PriorityAwareServiceProvider();
39+
40+
/**
41+
* Get the components priority, which returns 100.
42+
*
43+
* @return 100.
44+
*/
45+
@Override
46+
public int getPriority() {
47+
return 100;
48+
}
2449

2550
@Override
2651
public <T> List<T> getServices(Class<T> serviceType) {
@@ -36,28 +61,7 @@ public <T> List<T> getServices(Class<T> serviceType) {
3661
instances.add(t);
3762
}
3863
}
39-
return instances;
40-
}
41-
42-
@Override
43-
public <T> List<T> getServices(Class<T> serviceType,
44-
List<T> defaultList) {
45-
Instance<T> found = CDIAccessor
46-
.getInstances(serviceType);
47-
Set<String> types = new HashSet<>();
48-
List<T> instances = new ArrayList<T>();
49-
for (T t : found) {
50-
instances.add(t);
51-
types.add(t.getClass().getName());
52-
}
53-
for (T t : defaultServiceProvider.getServices(serviceType)) {
54-
if (!types.contains(t.getClass().getName())){
55-
instances.add(t);
56-
}
57-
}
58-
if (instances.isEmpty()) {
59-
return defaultList;
60-
}
64+
instances.sort(PriorityAwareServiceProvider::compareServices);
6165
return instances;
6266
}
6367

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch).
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*
16+
*
17+
* Contributors: Anatole Tresch - initial version.
18+
*/
19+
package org.javamoney.cdi.bootstrap;
20+
21+
import javax.money.spi.ServiceProvider;
22+
import java.util.ArrayList;
23+
import java.util.Collections;
24+
import java.util.List;
25+
import java.util.ServiceLoader;
26+
import java.util.concurrent.ConcurrentHashMap;
27+
import java.util.logging.Level;
28+
import java.util.logging.Logger;
29+
30+
/**
31+
* Default SE ServiceLoader based ServiceProvider implementation.
32+
*/
33+
public class DefaultServiceProvider implements ServiceProvider {
34+
/**
35+
* List of services loaded, per class.
36+
*/
37+
private final ConcurrentHashMap<Class, List<Object>> servicesLoaded = new ConcurrentHashMap<>();
38+
39+
/**
40+
* Loads and registers services.
41+
*
42+
* @param serviceType The service type.
43+
* @param <T> the concrete type.
44+
* @param defaultList the list of items returned, if no services were found.
45+
* @return the items found, never {@code null}.
46+
*/
47+
@Override
48+
public <T> List<T> getServices(final Class<T> serviceType, final List<T> defaultList) {
49+
@SuppressWarnings("unchecked")
50+
List<T> found = (List<T>) servicesLoaded.get(serviceType);
51+
if (found != null) {
52+
return found;
53+
}
54+
55+
return loadServices(serviceType, defaultList);
56+
}
57+
58+
/**
59+
* Loads and registers services.
60+
*
61+
* @param serviceType The service type.
62+
* @param <T> the concrete type.
63+
* @param defaultList the list of items returned, if no services were found.
64+
* @return the items found, never {@code null}.
65+
*/
66+
private <T> List<T> loadServices(final Class<T> serviceType, final List<T> defaultList) {
67+
try {
68+
List<T> services = new ArrayList<>();
69+
for (T t : ServiceLoader.load(serviceType)) {
70+
services.add(t);
71+
}
72+
if (services.isEmpty()) {
73+
services.addAll(defaultList);
74+
}
75+
@SuppressWarnings("unchecked")
76+
final List<T> previousServices = (List<T>) servicesLoaded.putIfAbsent(serviceType, (List<Object>) services);
77+
return Collections.unmodifiableList(previousServices != null ? previousServices : services);
78+
} catch (Exception e) {
79+
Logger.getLogger(DefaultServiceProvider.class.getName()).log(Level.WARNING,
80+
"Error loading services of type " + serviceType, e);
81+
return defaultList;
82+
}
83+
}
84+
85+
}

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<parent>
5-
<version>1.0-RC1</version>
5+
<version>1.0-RC2</version>
66
<groupId>org.javamoney</groupId>
77
<artifactId>javamoney-parent</artifactId>
88
</parent>
@@ -12,7 +12,7 @@
1212
<packaging>pom</packaging>
1313
<version>0.6-SNAPSHOT</version>
1414
<properties>
15-
<jsr.version>1.0-RC1</jsr.version>
15+
<jsr.version>1.0-SNAPSHOT</jsr.version>
1616
<jdkVersion>1.8</jdkVersion>
1717
<maven.compile.targetLevel>${jdkVersion}</maven.compile.targetLevel>
1818
<maven.compile.sourceLevel>${jdkVersion}</maven.compile.sourceLevel>

0 commit comments

Comments
 (0)