Skip to content

Commit dc0d2a1

Browse files
johnmayegonw
authored andcommitted
Mock the ChemObjectIO test cases
1 parent 0a99998 commit dc0d2a1

File tree

5 files changed

+72
-78
lines changed

5 files changed

+72
-78
lines changed

base/test/src/test/java/org/openscience/cdk/io/ChemObjectIOTest.java

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,19 @@
2424

2525
import org.junit.Assert;
2626
import org.junit.Test;
27+
import org.mockito.Mockito;
2728
import org.openscience.cdk.CDKTestCase;
29+
import org.openscience.cdk.interfaces.IAtomContainer;
30+
import org.openscience.cdk.interfaces.IChemFile;
31+
import org.openscience.cdk.interfaces.IChemModel;
32+
import org.openscience.cdk.interfaces.IChemObject;
33+
import org.openscience.cdk.interfaces.IReaction;
2834
import org.openscience.cdk.io.formats.IResourceFormat;
2935
import org.openscience.cdk.io.listener.IChemObjectIOListener;
3036
import org.openscience.cdk.io.setting.IOSetting;
37+
import org.openscience.cdk.isomorphism.matchers.IRGroupQuery;
38+
39+
import static org.mockito.Mockito.mock;
3140

3241
/**
3342
* TestCase for CDK IO classes.
@@ -53,54 +62,29 @@ public void testGetFormat() {
5362
Assert.assertNotNull("The IChemObjectIO.getFormat method returned null.", format);
5463
}
5564

56-
/*
57-
private static IChemObject[] acceptableNNChemObjects = {new ChemFile(), new ChemModel(), new AtomContainer(),
58-
new Reaction() };
59-
60-
@Test
61-
public void testAcceptsAtLeastOneNonotifyObject() {
62-
boolean oneAccepted = false;
63-
for (IChemObject object : acceptableNNChemObjects) {
64-
if (chemObjectIO.accepts(object.getClass())) {
65-
oneAccepted = true;
66-
}
67-
}
68-
Assert.assertTrue(
69-
"At least one of the following IChemObect's should be accepted: IChemFile, IChemModel, IAtomContainer, IReaction",
70-
oneAccepted);
71-
}
72-
73-
private static IChemObject[] acceptableDebugChemObjects = {new DebugChemFile(), new DebugChemModel(),
74-
new DebugAtomContainer(), new DebugReaction() };
75-
76-
@Test
77-
public void testAcceptsAtLeastOneDebugObject() {
78-
boolean oneAccepted = false;
79-
for (IChemObject object : acceptableDebugChemObjects) {
80-
if (chemObjectIO.accepts(object.getClass())) {
81-
oneAccepted = true;
82-
}
83-
}
84-
Assert.assertTrue(
85-
"At least one of the following IChemObect's should be accepted: IChemFile, IChemModel, IAtomContainer, IReaction",
86-
oneAccepted);
87-
}
88-
89-
// static objects, shared between tests - difficult to locate bugs.
90-
@Deprecated
91-
protected static IChemObject[] acceptableChemObjects = {new ChemFile(), new ChemModel(), new AtomContainer(),
92-
new Reaction(), new RGroupQuery(DefaultChemObjectBuilder.getInstance())};
65+
// FIXME add IRgroupQuery.class
66+
protected static Class<?>[] acceptableChemObjectClasses = {
67+
IChemFile.class,
68+
IChemModel.class,
69+
IAtomContainer.class,
70+
IReaction.class,
71+
IRGroupQuery.class};
9372

9473
protected static IChemObject[] acceptableChemObjects() {
95-
return new IChemObject[]{new ChemFile(), new ChemModel(), new AtomContainer(), new Reaction(),
96-
new RGroupQuery(DefaultChemObjectBuilder.getInstance())};
74+
return new IChemObject[]{
75+
mock(IChemFile.class),
76+
mock(IChemModel.class),
77+
mock(IAtomContainer.class),
78+
mock(IReaction.class),
79+
mock(IRGroupQuery.class)
80+
};
9781
}
9882

9983
@Test
10084
public void testAcceptsAtLeastOneChemObject() {
10185
boolean oneAccepted = false;
102-
for (IChemObject object : acceptableChemObjects) {
103-
if (chemObjectIO.accepts(object.getClass())) {
86+
for (IChemObject obj : acceptableChemObjects()) {
87+
if (chemObjectIO.accepts(obj.getClass())) {
10488
oneAccepted = true;
10589
}
10690
}
@@ -109,25 +93,20 @@ public void testAcceptsAtLeastOneChemObject() {
10993
oneAccepted);
11094
}
11195

112-
@SuppressWarnings("rawtypes")
113-
protected static Class[] acceptableChemObjectClasses = {IChemFile.class, IChemModel.class, IAtomContainer.class,
114-
IReaction.class, IRGroupQuery.class };
115-
116-
// @cdk.bug 3553780
117-
@SuppressWarnings("unchecked")
96+
/** @cdk.bug 3553780 */
11897
@Test
98+
@SuppressWarnings("unchecked")
11999
public void testAcceptsAtLeastOneChemObjectClass() {
120100
boolean oneAccepted = false;
121-
for (Class<? extends IChemObject> clazz : acceptableChemObjectClasses) {
122-
if (chemObjectIO.accepts(clazz)) {
101+
for (Class<?> cls : acceptableChemObjectClasses) {
102+
if (chemObjectIO.accepts((Class<? extends IChemObject>)cls)) {
123103
oneAccepted = true;
124104
}
125105
}
126106
Assert.assertTrue(
127107
"At least one of the following IChemObect's should be accepted: IChemFile, IChemModel, IAtomContainer, IReaction, IRGroupQuery",
128108
oneAccepted);
129109
}
130-
*/
131110

132111
@Test
133112
public void testClose() throws Exception {

base/test/src/test/java/org/openscience/cdk/io/ChemObjectWriterTest.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import org.junit.Assert;
2525
import org.junit.Test;
26+
import org.openscience.cdk.exception.CDKException;
27+
import org.openscience.cdk.interfaces.IChemObject;
2628

2729
import java.io.ByteArrayOutputStream;
2830
import java.io.StringWriter;
@@ -41,34 +43,32 @@ public static void setChemObjectWriter(IChemObjectWriter aChemObjectWriter) {
4143
ChemObjectWriterTest.chemObjectIO = aChemObjectWriter;
4244
}
4345

44-
// private static IChemObject[] allChemObjectsTypes = {new ChemFile(), new ChemModel(), new Reaction(),
45-
// new AtomContainerSet(), new AtomContainer()};
46-
//
47-
// /**
48-
// * Unit tests that iterates over all common objects that can be
49-
// * serialized and tests that if it is marked as accepted with
50-
// * <code>accepts</code>, that it can actually be written too.
51-
// */
52-
// @Test
53-
// public void testAcceptsWriteConsistency() throws CDKException {
54-
// Assert.assertNotNull("The IChemObjectWriter is not set.", chemObjectIO);
55-
// for (IChemObject object : allChemObjectsTypes) {
56-
// if (chemObjectIO.accepts(object.getClass())) {
57-
// StringWriter writer = new StringWriter();
58-
// chemObjectIO.setWriter(writer);
59-
// try {
60-
// chemObjectIO.write(object);
61-
// } catch (CDKException exception) {
62-
// if (exception.getMessage().contains("Only supported")) {
63-
// Assert.fail("IChemObject of type " + object.getClass().getName() + " is marked as "
64-
// + "accepted, but failed to be written.");
65-
// } else {
66-
// throw exception;
67-
// }
68-
// }
69-
// }
70-
// }
71-
// }
46+
47+
/**
48+
* Unit tests that iterates over all common objects that can be
49+
* serialized and tests that if it is marked as accepted with
50+
* <code>accepts</code>, that it can actually be written too.
51+
*/
52+
@Test
53+
public void testAcceptsWriteConsistency() throws CDKException {
54+
Assert.assertNotNull("The IChemObjectWriter is not set.", chemObjectIO);
55+
for (IChemObject object : acceptableChemObjects()) {
56+
if (chemObjectIO.accepts(object.getClass())) {
57+
StringWriter writer = new StringWriter();
58+
chemObjectIO.setWriter(writer);
59+
try {
60+
chemObjectIO.write(object);
61+
} catch (CDKException exception) {
62+
if (exception.getMessage().contains("Only supported")) {
63+
Assert.fail("IChemObject of type " + object.getClass().getName() + " is marked as "
64+
+ "accepted, but failed to be written.");
65+
} else {
66+
throw exception;
67+
}
68+
}
69+
}
70+
}
71+
}
7272

7373
@Test
7474
public void testSetWriter_Writer() throws Exception {

misc/test-extra/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@
3535
<artifactId>hamcrest</artifactId>
3636
<scope>test</scope>
3737
</dependency>
38+
<dependency>
39+
<groupId>org.mockito</groupId>
40+
<artifactId>mockito-core</artifactId>
41+
<scope>test</scope>
42+
</dependency>
3843
<dependency>
3944
<groupId>org.apache.logging.log4j</groupId>
4045
<artifactId>log4j-core</artifactId>

storage/iordf/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@
5151
<artifactId>hamcrest</artifactId>
5252
<scope>test</scope>
5353
</dependency>
54+
<dependency>
55+
<groupId>org.mockito</groupId>
56+
<artifactId>mockito-core</artifactId>
57+
<scope>test</scope>
58+
</dependency>
5459
<dependency>
5560
<groupId>org.apache.logging.log4j</groupId>
5661
<artifactId>log4j-1.2-api</artifactId>

storage/pdb/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<artifactId>junit</artifactId>
2424
<scope>test</scope>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.mockito</groupId>
28+
<artifactId>mockito-core</artifactId>
29+
<scope>test</scope>
30+
</dependency>
2631
<dependency>
2732
<groupId>${project.groupId}</groupId>
2833
<artifactId>cdk-interfaces</artifactId>

0 commit comments

Comments
 (0)