Commit a3c43f90 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

common test function extracted to util class

parent f4afd793
Pipeline #47079 failed with stage
in 29 minutes and 25 seconds
......@@ -13,7 +13,7 @@ import jp.sbi.celldesigner.plugin.PluginReaction;
import jp.sbi.celldesigner.plugin.PluginSpecies;
import jp.sbi.celldesigner.plugin.PluginSpeciesAlias;
import lcsb.mapviewer.common.MinervaLoggerAppender;
import lcsb.mapviewer.common.UnitTestFailedWatcher;
import lcsb.mapviewer.common.tests.UnitTestFailedWatcher;
public class CdPluginFunctions {
......
package lcsb.mapviewer.annotation;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
......@@ -37,19 +13,13 @@ import org.junit.Rule;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import lcsb.mapviewer.annotation.cache.PermanentDatabaseLevelCacheInterface;
import lcsb.mapviewer.common.MinervaLoggerAppender;
import lcsb.mapviewer.common.UnitTestFailedWatcher;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.common.tests.TestUtils;
import lcsb.mapviewer.common.tests.UnitTestFailedWatcher;
import lcsb.mapviewer.converter.ConverterParams;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerXmlParser;
import lcsb.mapviewer.model.map.model.Model;
......@@ -59,12 +29,10 @@ import lcsb.mapviewer.persist.dao.cache.CacheTypeDao;
@Transactional
@ContextConfiguration(classes = SpringAnnotationTestConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class AnnotationTestFunctions extends AbstractTransactionalJUnit4SpringContextTests {
public abstract class AnnotationTestFunctions extends TestUtils {
protected static Logger logger = LogManager.getLogger();
private static int BLOCK_SIZE = 65536;
private static Map<String, Model> models = new HashMap<String, Model>();
@Rule
......@@ -81,20 +49,10 @@ public abstract class AnnotationTestFunctions extends AbstractTransactionalJUnit
private MinervaLoggerAppender appender;
private String dapiLogin;
private String dapiPassword;
@Before
public final void _setUp() throws Exception {
MinervaLoggerAppender.unregisterLogEventStorage(appender);
appender = MinervaLoggerAppender.createAppender(false);
dapiLogin = System.getenv("DAPI_TEST_LOGIN");
dapiPassword = System.getenv("DAPI_TEST_PASSWORD");
}
protected boolean isDapiConfigurationAvailable() {
return dapiLogin != null && dapiPassword != null;
}
@After
......@@ -114,64 +72,6 @@ public abstract class AnnotationTestFunctions extends AbstractTransactionalJUnit
return appender.getFatals();
}
protected String readFile(final String file) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
} finally {
reader.close();
}
return stringBuilder.toString();
}
protected Node getNodeFromXmlString(final String text) throws InvalidXmlSchemaException, IOException {
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(text));
return getXmlDocumentFromInputSource(is).getChildNodes().item(0);
}
protected Document getXmlDocumentFromFile(final String fileName) throws InvalidXmlSchemaException, IOException {
File file = new File(fileName);
InputStream inputStream = new FileInputStream(file);
Reader reader = null;
try {
reader = new InputStreamReader(inputStream, "UTF-8");
InputSource is = new InputSource(reader);
Document result = getXmlDocumentFromInputSource(is);
inputStream.close();
return result;
} catch (final UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected Document getXmlDocumentFromInputSource(final InputSource stream) throws InvalidXmlSchemaException, IOException {
DocumentBuilder db;
try {
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (final ParserConfigurationException e) {
throw new InvalidXmlSchemaException("Problem with xml parser");
}
Document doc = null;
try {
doc = db.parse(stream);
} catch (final SAXException e) {
throw new InvalidXmlSchemaException(e);
}
return doc;
}
protected Model getModelForFile(final String fileName, final boolean fromCache) throws Exception {
if (!fromCache) {
logger.debug("File without cache: " + fileName);
......@@ -188,118 +88,4 @@ public abstract class AnnotationTestFunctions extends AbstractTransactionalJUnit
return result;
}
protected String createTmpFileName() {
try {
File f = File.createTempFile("prefix", ".txt");
String filename = f.getName();
f.delete();
return filename;
} catch (final IOException e) {
e.printStackTrace();
return null;
}
}
protected String nodeToString(final Node node) {
return nodeToString(node, false);
}
protected String nodeToString(final Node node, final boolean includeHeadNode) {
if (node == null) {
return null;
}
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node element = list.item(i);
t.transform(new DOMSource(element), new StreamResult(sw));
}
} catch (final TransformerException te) {
logger.debug("nodeToString Transformer Exception");
}
if (includeHeadNode) {
return "<" + node.getNodeName() + ">" + sw.toString() + "</" + node.getNodeName() + ">";
}
return sw.toString();
}
protected boolean equalFiles(final String fileA, final String fileB) throws IOException {
FileInputStream inputStreamA = new FileInputStream(fileA);
FileInputStream inputStreamB = new FileInputStream(fileB);
// vary BLOCK_SIZE to suit yourself.
// it should probably a factor or multiple of the size of a disk
// sector/cluster.
// Note that your max heap size may need to be adjused
// if you have a very big block size or lots of these comparators.
// assume inputStreamA and inputStreamB are streams from your two files.
byte[] streamABlock = new byte[BLOCK_SIZE];
byte[] streamBBlock = new byte[BLOCK_SIZE];
boolean match = true;
int bytesReadA = 0;
int bytesReadB = 0;
do {
bytesReadA = inputStreamA.read(streamABlock);
bytesReadB = inputStreamB.read(streamBBlock);
match = ((bytesReadA == bytesReadB) && Arrays.equals(streamABlock, streamBBlock));
} while (match && (bytesReadA > -1));
inputStreamA.close();
inputStreamB.close();
return match;
}
public File createTempDirectory() throws IOException {
final File temp;
temp = File.createTempFile("temp", Long.toString(System.nanoTime()));
if (!(temp.delete())) {
throw new IOException("Could not delete temp file: " + temp.getAbsolutePath());
}
if (!(temp.mkdir())) {
throw new IOException("Could not create temp directory: " + temp.getAbsolutePath());
}
return (temp);
}
protected String getWebpage(final String accessUrl) throws IOException {
String inputLine;
IOException exception = null;
for (int i = 0; i < 3; i++) {
try {
StringBuilder tmp = new StringBuilder();
URL url = new URL(accessUrl);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("GET");
urlConn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while ((inputLine = in.readLine()) != null) {
tmp.append(inputLine);
}
in.close();
return tmp.toString();
} catch (final IOException e) {
exception = e;
}
}
throw exception;
}
public String getDapiLogin() {
return dapiLogin;
}
public String getDapiPassword() {
return dapiPassword;
}
}
package lcsb.mapviewer.common.tests;
import java.awt.Desktop;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import javax.imageio.ImageIO;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
public class TestUtils {
protected static final String BUILT_IN_TEST_ADMIN_PASSWORD = "admin";
protected static final String BUILT_IN_TEST_ADMIN_LOGIN = "admin";
protected static final String BUILT_IN_PROJECT = "empty";
protected static final String TEST_PROJECT = "test_project";
protected static String TEST_PROJECT_ID = "Some_id";
protected static double EPSILON = Configuration.EPSILON;
private static final Logger logger = LogManager.getLogger();
private static final int BLOCK_SIZE = 65536;
private String dapiLogin;
private String dapiPassword;
private DocumentBuilder db;
public TestUtils() {
try {
dapiLogin = System.getenv("DAPI_TEST_LOGIN");
dapiPassword = System.getenv("DAPI_TEST_PASSWORD");
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new InvalidStateException();
}
}
public final boolean isDapiConfigurationAvailable() {
return dapiLogin != null && dapiPassword != null;
}
public final String getDapiLogin() {
return dapiLogin;
}
public final String getDapiPassword() {
return dapiPassword;
}
public final boolean equalFiles(final String fileA, final String fileB) throws IOException {
FileInputStream inputStreamA = new FileInputStream(fileA);
FileInputStream inputStreamB = new FileInputStream(fileB);
// vary BLOCK_SIZE to suit yourself.
// it should probably a factor or multiple of the size of a disk
// sector/cluster.
// Note that your max heap size may need to be adjused
// if you have a very big block size or lots of these comparators.
// assume inputStreamA and inputStreamB are streams from your two files.
byte[] streamABlock = new byte[BLOCK_SIZE];
byte[] streamBBlock = new byte[BLOCK_SIZE];
boolean match = true;
int bytesReadA = 0;
int bytesReadB = 0;
do {
bytesReadA = inputStreamA.read(streamABlock);
bytesReadB = inputStreamB.read(streamBBlock);
match = ((bytesReadA == bytesReadB) && Arrays.equals(streamABlock, streamBBlock));
} while (match && (bytesReadA > -1));
inputStreamA.close();
inputStreamB.close();
return match;
}
public final String readFile(final String file) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
} finally {
reader.close();
}
return stringBuilder.toString();
}
public final Node getNodeFromXmlString(final String text) throws InvalidXmlSchemaException, IOException {
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(text));
return getXmlDocumentFromInputSource(is).getChildNodes().item(0);
}
public final Document getXmlDocumentFromFile(final String fileName) throws InvalidXmlSchemaException, IOException {
File file = new File(fileName);
InputStream inputStream = new FileInputStream(file);
Reader reader = null;
try {
reader = new InputStreamReader(inputStream, "UTF-8");
InputSource is = new InputSource(reader);
Document result = getXmlDocumentFromInputSource(is);
inputStream.close();
return result;
} catch (final UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
public final Document getXmlDocumentFromInputSource(final InputSource stream) throws InvalidXmlSchemaException, IOException {
DocumentBuilder db;
try {
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (final ParserConfigurationException e) {
throw new InvalidXmlSchemaException("Problem with xml parser");
}
Document doc = null;
try {
doc = db.parse(stream);
} catch (final SAXException e) {
throw new InvalidXmlSchemaException(e);
}
return doc;
}
public final String nodeToString(final Node node) {
return nodeToString(node, false);
}
public final String nodeToString(final Node node, final boolean includeHeadNode) {
if (node == null) {
return null;
}
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
Node element = list.item(i);
t.transform(new DOMSource(element), new StreamResult(sw));
}
} catch (final TransformerException te) {
logger.debug("nodeToString Transformer Exception");
}
if (includeHeadNode) {
return "<" + node.getNodeName() + ">" + sw.toString() + "</" + node.getNodeName() + ">";
}
return sw.toString();
}
public final String getWebpage(final String accessUrl) throws IOException {
String inputLine;
IOException exception = null;
for (int i = 0; i < 3; i++) {
try {
StringBuilder tmp = new StringBuilder();
URL url = new URL(accessUrl);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("GET");
urlConn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while ((inputLine = in.readLine()) != null) {
tmp.append(inputLine);
}
in.close();
return tmp.toString();
} catch (final IOException e) {
exception = e;
}
}
throw exception;
}
public void showImage(final BufferedImage bi) throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(bi, "PNG", outputStream);
byte[] output2 = outputStream.toByteArray();
FileUtils.writeByteArrayToFile(new File("tmp.png"), output2);
Desktop.getDesktop().open(new File("tmp.png"));
}
public Element fileToNode(final String filename) throws SAXException, IOException {
InputStream is = new FileInputStream(new File(filename));
Document doc = null;
doc = db.parse(is);
return doc.getDocumentElement();
}
}
package lcsb.mapviewer.common;
package lcsb.mapviewer.common.tests;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;
......
......@@ -9,6 +9,8 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import lcsb.mapviewer.common.tests.UnitTestFailedWatcher;
public class CommonTestFunctions {
protected static Logger logger = LogManager.getLogger();
......
......@@ -2,49 +2,21 @@ package lcsb.mapviewer.converter.model.celldesigner;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;