Skip to content

Commit cf5a992

Browse files
committed
Commited fix and test from cm-rudolph for JAVAMONEY-78.
1 parent 3fccc2c commit cf5a992

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/main/java/org/javamoney/moneta/internal/DefaultMonetaryAmountsSingletonSpi.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ private static int getServicePriority(Object service){
101101
* @return the comparison result.
102102
*/
103103
public static <T> int comparePriority(T service1, T service2){
104-
return getServicePriority(service2) - getServicePriority(service1);
104+
int servicePriority1 = getServicePriority(service1);
105+
int servicePriority2 = getServicePriority(service2);
106+
return servicePriority1 == servicePriority2 ? 0 : servicePriority1 < servicePriority2 ? 1 : -1;
105107
}
106108

107109
/**
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
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+
package org.javamoney.moneta.internal;
17+
18+
import org.javamoney.moneta.spi.ServicePriority;
19+
import org.testng.annotations.Test;
20+
21+
import static org.testng.Assert.assertTrue;
22+
23+
/**
24+
* Tests for DefaultMonetaryAmountsSingletonSpi.
25+
*
26+
* @author cm-rudolph
27+
*/
28+
public class DefaultMonetaryAmountsSingletonSpiTest{
29+
30+
/**
31+
* Test method for
32+
* {@link org.javamoney.moneta.internal.DefaultMonetaryAmountsSingletonSpi#comparePriority(Object, Object)}
33+
* .
34+
*/
35+
@Test
36+
public void testComparePriority(){
37+
Object service1 = new Service1();
38+
Object service2 = new Service2();
39+
assertTrue(DefaultMonetaryAmountsSingletonSpi.comparePriority(service1, service2) > 0);
40+
assertTrue(DefaultMonetaryAmountsSingletonSpi.comparePriority(service1, service1) == 0);
41+
assertTrue(DefaultMonetaryAmountsSingletonSpi.comparePriority(service2, service1) < 0);
42+
assertTrue(DefaultMonetaryAmountsSingletonSpi.comparePriority(null, service2) > 0);
43+
}
44+
45+
/**
46+
* Test class used, with prio 1.
47+
*/
48+
@ServicePriority(1)
49+
private static class Service1{}
50+
51+
/**
52+
* Test class used with prio 2.
53+
*/
54+
@ServicePriority(2)
55+
private static class Service2{}
56+
}

0 commit comments

Comments
 (0)