Skip to content

Commit a9c607a

Browse files
committed
Fixed test setup, added ServiceLoader based load mechanism for
implementations to be tested.
1 parent 2f79cb0 commit a9c607a

File tree

4 files changed

+65
-81
lines changed

4 files changed

+65
-81
lines changed
Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
/*
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.
2+
* Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch). Licensed under the Apache
3+
* License, Version 2.0 (the "License"); you may not use this file except in compliance with the
4+
* License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
* Unless required by applicable law or agreed to in writing, software distributed under the License
6+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
7+
* or implied. See the License for the specific language governing permissions and limitations under
8+
* the License. Contributors: Anatole Tresch - initial version.
189
*/
1910
package org.javamoney.tck;
2011

21-
import java.lang.reflect.AccessibleObject;
2212
import java.util.Collection;
13+
import java.util.ServiceLoader;
2314

24-
public interface JSR354TestConfiguration {
15+
import javax.money.CurrencyUnit;
16+
import javax.money.MonetaryAmount;
17+
import javax.money.spi.MonetaryAmountFactoryProviderSpi;
2518

26-
Collection<Class> getExceptionClasses();
19+
/**
20+
* Libraries that implement this JSR and want to be tested with this TCK must implement this
21+
* interface and register it using the {@link ServiceLoader}.
22+
*
23+
* @author Anatole Tresch
24+
*/
25+
public interface JSR354TestConfiguration {
2726

27+
/**
28+
* Return a collection with all {@link MonetaryAmount} classes that are implemented. The list
29+
* must not be empty and should contain <b>every</b> amount class implemented.<br/>
30+
* This enables the TCK to check in addition to the basic implementation compliance, if
31+
* according {@link MonetaryAmountFactoryProviderSpi} are registered/available correctly.
32+
*
33+
* @return a collection with all implemented amount classes, not null.
34+
*/
2835
Collection<Class> getAmountClasses();
2936

37+
/**
38+
* List a collection of {@link CurrencyUnit} implementation.<br/>
39+
* This enables the TCK to check the basic implementation compliance,
40+
* @return
41+
*/
3042
Collection<Class> getCurrencyClasses();
3143

3244
}

src/test/java/org/javamoney/tck/TCKTestSetup.java

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,61 @@
22

33
import java.util.Arrays;
44
import java.util.Collection;
5-
import java.util.Collections;
6-
7-
import org.javamoney.moneta.FastMoney;
8-
import org.javamoney.moneta.Money;
5+
import java.util.ServiceLoader;
96

107
public final class TCKTestSetup {
118

12-
private static JSRTestSetup TEST_SETUP = new JSRTestSetup();
9+
private static JSR354TestConfiguration TEST_CONFIG = loadTestConfiguration();
1310

1411
private TCKTestSetup() {
1512
}
1613

14+
private static JSR354TestConfiguration loadTestConfiguration() {
15+
try {
16+
return ServiceLoader.load(JSR354TestConfiguration.class).iterator()
17+
.next();
18+
} catch (Exception e) {
19+
try {
20+
Class.forName("org.javamoney.moneta.Money");
21+
return new JSRTestSetup();
22+
} catch (Exception e2) {
23+
throw new IllegalStateException("No valid implementation of "
24+
+ JSR354TestConfiguration.class.getName()
25+
+ " is registered with the ServiceLoader.");
26+
}
27+
}
28+
}
29+
1730
public static JSR354TestConfiguration getTestConfiguration() {
18-
// TODO load dynamicylly
19-
return TEST_SETUP;
31+
return TEST_CONFIG;
2032
}
2133

2234
private static final class JSRTestSetup implements JSR354TestConfiguration {
2335

24-
@Override
25-
public Collection<Class> getExceptionClasses() {
26-
return Collections.emptySet();
27-
// return Arrays
28-
// .asList(new Class[] { CurrencyMismatchException.class,
29-
// UnknownCurrencyException.class });
30-
}
31-
3236
@Override
3337
public Collection<Class> getAmountClasses() {
34-
return Arrays
35-
.asList(new Class[] { Money.class, FastMoney.class });
38+
try {
39+
return Arrays
40+
.asList(new Class[] {
41+
Class.forName("org.javamoney.moneta.Money"),
42+
Class.forName("org.javamoney.moneta.FastMoney") });
43+
} catch (ClassNotFoundException e) {
44+
throw new IllegalStateException("No valid implementation of "
45+
+ JSR354TestConfiguration.class.getName()
46+
+ " is registered with the ServiceLoader.");
47+
}
3648
}
3749

3850
@Override
3951
public Collection<Class> getCurrencyClasses() {
4052
try {
4153
return Arrays
42-
.asList(new Class[] { Class.forName("org.javamoney.moneta.impl.JDKCurrencyAdapter") });
54+
.asList(new Class[] { Class
55+
.forName("org.javamoney.moneta.internal.JDKCurrencyAdapter") });
4356
} catch (ClassNotFoundException e) {
44-
return Collections.emptySet();
57+
throw new IllegalStateException("No valid implementation of "
58+
+ JSR354TestConfiguration.class.getName()
59+
+ " is registered with the ServiceLoader.");
4560
}
4661
}
4762

src/test/java/org/javamoney/tck/tests/ExceptionsTest.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/test/java/org/javamoney/tck/tests/TestSetupTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ public void testTestSetup() {
2020
assertNotNull(TCKTestSetup.getTestConfiguration());
2121
}
2222

23-
@SpecAssertion(
24-
section = "0",
25-
id = "EnsurePackageSetup")
26-
@Test
27-
public void testExceptionClassesSetup() {
28-
assertTrue(
29-
"Implementation Packages not registered.",
30-
TCKTestSetup.getTestConfiguration().getExceptionClasses() != null);
31-
assertFalse("Implementation Packages not registered.",
32-
TCKTestSetup.getTestConfiguration().getExceptionClasses()
33-
.isEmpty());
34-
}
3523

3624
@SpecAssertion(
3725
section = "0",

0 commit comments

Comments
 (0)