1 /* Copyright (C) 2001-2003  Jmol Project
2  * Copyright (C) 2003-2007  The Chemistry Development Kit (CDK) project
3  *
4  * Contact: cdk-devel@lists.sourceforge.net
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2.1 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20 package org.openscience.cdk.io;
21 
22 import java.io.BufferedReader;
23 import java.io.InputStreamReader;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.openscience.cdk.CDKTestCase;
31 import org.openscience.cdk.io.formats.IChemFormat;
32 import org.openscience.cdk.io.formats.IResourceFormat;
33 import org.openscience.cdk.tools.ILoggingTool;
34 import org.openscience.cdk.tools.LoggingToolFactory;
35 
36 /**
37  * Tests whether all Reader and Writer classes can be instantiated.
38  *
39  * @cdk.module test-io
40  *
41  * @author Egon Willighagen <egonw@sci.kun.nl>
42  */
43 public class ChemObjectIOInstantionTests extends CDKTestCase {
44 
45     private final static String      IO_FORMATS_LIST = "io-formats.set";
46 
47     private static ILoggingTool      logger          = LoggingToolFactory
48                                                              .createLoggingTool(ChemObjectIOInstantionTests.class);
49 
50     private static List<IChemFormat> formats         = null;
51 
loadFormats()52     private void loadFormats() {
53         if (formats == null) {
54             formats = new ArrayList<IChemFormat>();
55             try {
56                 logger.debug("Starting loading Formats...");
57                 BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader()
58                         .getResourceAsStream(IO_FORMATS_LIST)));
59                 int formatCount = 0;
60                 while (reader.ready()) {
61                     // load them one by one
62                     String formatName = reader.readLine();
63                     formatCount++;
64                     try {
65                         IResourceFormat format = (IResourceFormat) this.getClass().getClassLoader()
66                                 .loadClass(formatName).newInstance();
67                         if (format instanceof IChemFormat) {
68                             formats.add((IChemFormat) format);
69                             logger.info("Loaded IChemFormat: " + format.getClass().getName());
70                         }
71                     } catch (ClassNotFoundException exception) {
72                         logger.error("Could not find this IResourceFormat: ", formatName);
73                         logger.debug(exception);
74                     } catch (InstantiationException | IllegalAccessException exception) {
75                         logger.error("Could not load this IResourceFormat: ", formatName);
76                         logger.debug(exception);
77                     }
78                 }
79                 logger.info("Number of loaded formats used in detection: ", formatCount);
80             } catch (Exception exception) {
81                 logger.error("Could not load this io format list: ", IO_FORMATS_LIST);
82                 logger.debug(exception);
83             }
84         }
85     }
86 
87     @Test
testInstantion()88     public void testInstantion() {
89         loadFormats();
90 
91         IChemFormat format = null;
92         Iterator<IChemFormat> formatIter = formats.iterator();
93         while (formatIter.hasNext()) {
94             format = (IChemFormat) formatIter.next();
95             if (format.getReaderClassName() != null) {
96                 tryToInstantiate(format.getReaderClassName());
97             }
98             if (format.getWriterClassName() != null) {
99                 tryToInstantiate(format.getWriterClassName());
100             }
101         }
102     }
103 
tryToInstantiate(String className)104     private void tryToInstantiate(String className) {
105         try {
106             // make a new instance of this class
107             Object instance = this.getClass().getClassLoader().loadClass(className).newInstance();
108             Assert.assertNotNull(instance);
109             Assert.assertEquals(className, instance.getClass().getName());
110         } catch (ClassNotFoundException exception) {
111             logger.debug("Could not find this class: " + className);
112             // but that's not error, it can mean that it is a Jmol based IO class, and no Jmol is in the classpath
113         } catch (InstantiationException | IllegalAccessException exception) {
114             logger.debug(exception);
115             Assert.fail("Could not instantiate this class: " + className);
116         }
117     }
118 
119 }
120