Commit 38c9e770 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

instead catching exception inform junit to expect it

parent 9832a312
......@@ -136,7 +136,7 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
bigFileCache.isLocalFileUpToDate(httpUrl + "?some_get_param");
}
@Test
@Test(expected = FileNotFoundException.class)
public void testIsLocalHttpFileUpToDateWhenFileRemovedManually() throws Exception {
bigFileCache.downloadFile(httpUrl, false, new IProgressUpdater() {
@Override
......@@ -146,10 +146,9 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
new File(bigFileCache.getAbsolutePathForFile(httpUrl)).delete();
try {
bigFileCache.isLocalFileUpToDate(httpUrl);
fail("Exception expected");
} catch (FileNotFoundException e) {
} finally {
bigFileCache.removeFile(httpUrl);
}
bigFileCache.removeFile(httpUrl);
}
@Test(expected = FileNotFoundException.class)
......@@ -157,7 +156,7 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
bigFileCache.isLocalFileUpToDate(ftpUrl);
}
@Test
@Test(expected = IOException.class)
public void testRefreshCacheQueryWhenFtpConnectionFails() throws Exception {
try {
......@@ -183,16 +182,13 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
bigFileCache.setFtpClientFactory(factory);
bigFileCache.isLocalFileUpToDate(ftpUrl);
fail("Exception expected");
} catch (IOException e) {
assertTrue(e.getMessage().contains("FTP server refused connection"));
} finally {
bigFileCache.setFtpClientFactory(new FtpClientFactory());
bigFileCache.removeFile(ftpUrl);
}
}
@Test
@Test(expected = IOException.class)
public void testDownloadWhenFtpConnectionFails() throws Exception {
try {
FTPClient mockClient = Mockito.mock(FTPClient.class);
......@@ -208,10 +204,6 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
bigFileCache.setFtpClientFactory(factory);
bigFileCache.downloadFile(ftpUrl, false, null);
fail("Exception expected");
} catch (IOException e) {
assertTrue(e.getMessage().contains("FTP server refused connection"));
} finally {
bigFileCache.setFtpClientFactory(new FtpClientFactory());
bigFileCache.removeFile(ftpUrl);
......@@ -233,7 +225,7 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
}
}
@Test
@Test(expected = FileNotFoundException.class)
public void testDownloadedFileRemovedException() throws Exception {
String url = ftpUrl;
bigFileCache.downloadFile(url, false, new IProgressUpdater() {
......@@ -248,25 +240,36 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
try {
bigFileCache.getAbsolutePathForFile(url);
fail("Exception expected");
} catch (FileNotFoundException e) {
} finally {
bigFileCache.removeFile(url);
assertNull(bigFileCache.getAbsolutePathForFile(url));
assertFalse(f.exists());
}
}
@Test(expected = FileNotFoundException.class)
public void testDownloadedFileRemovedException2() throws Exception {
String url = ftpUrl;
bigFileCache.downloadFile(url, false, new IProgressUpdater() {
@Override
public void setProgress(double progress) {
}
});
String localFile = bigFileCache.getAbsolutePathForFile(url);
File f = new File(localFile);
assertTrue(f.exists());
f.delete();
try {
bigFileCache.isLocalFileUpToDate(url);
fail("Exception expected");
} catch (FileNotFoundException e) {
} finally {
bigFileCache.removeFile(url);
assertNull(bigFileCache.getAbsolutePathForFile(url));
assertFalse(f.exists());
}
bigFileCache.removeFile(url);
assertNull(bigFileCache.getAbsolutePathForFile(url));
assertFalse(f.exists());
}
@Test
@Test(expected = FileNotFoundException.class)
public void testGetPathForFileBeingDownloaded() throws Exception {
String url = ftpUrl;
BigFileCache bigFileCacheUnderTest = new BigFileCache(null, null, null);
......@@ -275,12 +278,7 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
when(mockDao.getByUrl(anyString())).thenReturn(entry);
bigFileCacheUnderTest.setBigFileEntryDao(mockDao);
try {
bigFileCacheUnderTest.getAbsolutePathForFile(url);
fail("Exception expected");
} catch (FileNotFoundException e) {
assertTrue(e.getMessage().contains("File is not complete"));
}
bigFileCacheUnderTest.getAbsolutePathForFile(url);
}
@Test
......@@ -386,14 +384,9 @@ public class BigFileCacheTest extends AnnotationTestFunctions {
bigFileCache.removeFile("unexisting file");
}
@Test
@Test(expected = URISyntaxException.class)
public void testIsLocalFileUpToDateForInvalidURI() throws Exception {
try {
bigFileCache.isLocalFileUpToDate("unexistingFileProtocol://bla");
fail("Exception expected");
} catch (URISyntaxException e) {
assertTrue(e.getMessage().contains("Unknown protocol"));
}
bigFileCache.isLocalFileUpToDate("unexistingFileProtocol://bla");
}
@Test
......
......@@ -48,54 +48,29 @@ public class GeneralCacheTest extends AnnotationTestFunctions {
}
}
@Test
@Test(expected = InvalidArgumentException.class)
public void testGetXmlNodeByQueryForInvalidType() {
try {
cache.getXmlNodeByQuery("str", null);
fail("Exception expected");
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("Cache type cannot be null"));
}
cache.getXmlNodeByQuery("str", null);
}
@Test
@Test(expected = InvalidArgumentException.class)
public void testSetCachedQueryForInvalidType() {
try {
cache.setCachedQuery("str", null, null);
fail("Exception expected");
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("Cache type cannot be null"));
}
cache.setCachedQuery("str", null, null);
}
@Test
@Test(expected = InvalidArgumentException.class)
public void testRemoveByQueryForInvalidType() {
try {
cache.removeByQuery("str", null);
fail("Exception expected");
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("Cache type cannot be null"));
}
cache.removeByQuery("str", null);
}
@Test
@Test(expected = InvalidArgumentException.class)
public void testInvalidateByQueryForInvalidType() {
try {
cache.invalidateByQuery("str", null);
fail("Exception expected");
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("Cache type cannot be null"));
}
cache.invalidateByQuery("str", null);
}
@Test
@Test(expected = InvalidArgumentException.class)
public void testGetStringByQueryForInvalidType() {
try {
cache.getStringByQuery("str", null);
fail("Exception expected");
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("Cache type cannot be null"));
}
cache.getStringByQuery("str", null);
}
@Test
......
......@@ -25,14 +25,9 @@ public class GeneralCacheWithExclusionTest extends AnnotationTestFunctions {
public void tearDown() throws Exception {
}
@Test
@Test(expected = InvalidArgumentException.class)
public void testConstructorWithInvalidParams() {
try {
new GeneralCacheWithExclusion(null, 1);
fail("Exception expected");
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("Cache passed as argument cannot be null"));
}
new GeneralCacheWithExclusion(null, 1);
}
@Test
......
......@@ -67,20 +67,14 @@ public class PermanentDatabaseLevelCacheTest extends AnnotationTestFunctions {
assertEquals("hello23", node.getNodeName());
}
@Test
@Test(expected = CacheException.class)
public void testSetInvalidQuery() {
try {
String query = "blabla";
Object object = new Object();
CacheType type = cacheTypeDao.getByClassName(PubmedParser.class.getCanonicalName());
String query = "blabla";
Object object = new Object();
cache.setCachedQuery(query, type, object);
fail("Exception expected");
CacheType type = cacheTypeDao.getByClassName(PubmedParser.class.getCanonicalName());
} catch (CacheException e) {
assertTrue(e.getMessage().contains("Unknown object type"));
}
cache.setCachedQuery(query, type, object);
}
@Test
......
package lcsb.mapviewer.annotation.cache;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.UnknownHostException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;
import org.mockito.Mockito;
public class WebPageDownloaderTest {
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testConnectionProblems() {
WebPageDownloader downloader = new WebPageDownloader() {
@Override
public HttpURLConnection openConnection(String url) throws IOException {
HttpURLConnection result = Mockito.mock(HttpURLConnection.class);
Mockito.doReturn(HttpURLConnection.HTTP_INTERNAL_ERROR).when(result).getResponseCode();
Mockito.doThrow(new IOException()).when(result).getInputStream();
return result;
}
};
try {
downloader.getFromNetwork("https://www.google.pl/?gws_rd=ssl");
fail("Exceptio expected");
} catch (IOException e) {
}
}
@Test(expected = IOException.class)
public void testConnectionProblems() throws IOException {
WebPageDownloader downloader = new WebPageDownloader() {
@Override
public HttpURLConnection openConnection(String url) throws IOException {
HttpURLConnection result = Mockito.mock(HttpURLConnection.class);
Mockito.doReturn(HttpURLConnection.HTTP_INTERNAL_ERROR).when(result).getResponseCode();
Mockito.doThrow(new IOException()).when(result).getInputStream();
return result;
}
};
downloader.getFromNetwork("https://www.google.pl/?gws_rd=ssl");
}
@Test
public void testConnectionProblems2() {
WebPageDownloader downloader = new WebPageDownloader() {
@Override
public HttpURLConnection openConnection(String url) throws IOException {
HttpURLConnection result = Mockito.mock(HttpURLConnection.class);
Mockito.doReturn(HttpURLConnection.HTTP_OK).when(result).getResponseCode();
Mockito.doThrow(new UnknownHostException()).when(result).getInputStream();
return result;
}
};
try {
downloader.getFromNetwork("https://www.google.pl/?gws_rd=ssl");
fail("Exceptio expected");
} catch (IOException e) {
}
}
@Test
public void testSend1() {
WebPageDownloader downloader = new WebPageDownloader();
try {
String result = downloader.getFromNetwork("https://www.google.com/");
assertNotNull("GET request to Google should return non-null result", result);
} catch (IOException e) {
}
}
@Test
public void testPost1() {
WebPageDownloader downloader = new WebPageDownloader();
try {
String result = downloader.getFromNetwork("https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/", "POST", "P29373");
assertNotNull("POST request to Uniprot should return non-null result", result);
} catch (IOException e) {
}
}
@Test
public void testInvalidHttpRequestType() {
WebPageDownloader downloader = new WebPageDownloader();
try {
downloader.getFromNetwork("https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/", "XXX", "P29373");
fail("Invalid request exception expected");
} catch (IOException e) {
}
}
@Test(expected = IOException.class)
public void testConnectionProblems2() throws IOException {
WebPageDownloader downloader = new WebPageDownloader() {
@Override
public HttpURLConnection openConnection(String url) throws IOException {
HttpURLConnection result = Mockito.mock(HttpURLConnection.class);
Mockito.doReturn(HttpURLConnection.HTTP_OK).when(result).getResponseCode();
Mockito.doThrow(new UnknownHostException()).when(result).getInputStream();
return result;
}
};
downloader.getFromNetwork("https://www.google.pl/?gws_rd=ssl");
}
@Test
public void testSend1() {
WebPageDownloader downloader = new WebPageDownloader();
try {
String result = downloader.getFromNetwork("https://www.google.com/");
assertNotNull("GET request to Google should return non-null result", result);
} catch (IOException e) {
}
}
@Test
public void testPost1() {
WebPageDownloader downloader = new WebPageDownloader();
try {
String result = downloader.getFromNetwork("https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/", "POST",
"P29373");
assertNotNull("POST request to Uniprot should return non-null result", result);
} catch (IOException e) {
}
}
@Test(expected = IOException.class)
public void testInvalidHttpRequestType() throws IOException {
WebPageDownloader downloader = new WebPageDownloader();
downloader.getFromNetwork("https://www.ebi.ac.uk/pdbe/api/mappings/best_structures/", "XXX", "P29373");
}
}
......@@ -36,14 +36,9 @@ public class TargetTest extends AnnotationTestFunctions {
assertEquals(genes, target.getGenes());
}
@Test
@Test(expected = InvalidArgumentException.class)
public void testAddInvalidGene() {
try {
Target target = new Target();
target.addGene(null);
fail("Exception expected");
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("Cannot add null element"));
}
Target target = new Target();
target.addGene(null);
}
}
......@@ -370,7 +370,7 @@ public class ChemicalParserTest extends AnnotationTestFunctions {
}
}
@Test
@Test(expected = ChemicalSearchException.class)
public void testProblemWithPubmeds() throws Exception {
GeneralCacheInterface cache = chemicalParser.getCache();
WebPageDownloader downloader = chemicalParser.getWebPageDownloader();
......@@ -393,11 +393,7 @@ public class ChemicalParserTest extends AnnotationTestFunctions {
when(mockDownloader.getFromNetwork(anyString(), anyString(), nullable(String.class)))
.thenThrow(new IOException());
chemicalParser.setWebPageDownloader(mockDownloader);
try {
chemicalParser.getChemicals(parkinsonDiseaseId, idsList);
fail("Exception expected");
} catch (ChemicalSearchException e) {
}
chemicalParser.getChemicals(parkinsonDiseaseId, idsList);
} finally {
chemicalParser.setCache(cache);
chemicalParser.setWebPageDownloader(downloader);
......
......@@ -226,7 +226,6 @@ public class DrugbankHTMLParserTest extends AnnotationTestFunctions {
@Test(expected = InvalidArgumentException.class)
public void testFindDrugByInvalidTarget() throws Exception {
drugBankHTMLParser.getDrugListByTarget(new MiriamData(MiriamType.WIKIPEDIA, "h2o"));
fail("Exception");
}
@Test
......
package lcsb.mapviewer.annotation.services;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.*;
import lcsb.mapviewer.annotation.AnnotationTestFunctions;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.species.GenericProtein;
public class ImproperAnnotationsTest {
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testConstructor3() {
try {
List<MiriamData> list = new ArrayList<>();
list.add(new MiriamData(MiriamType.CAS, "a"));
ImproperAnnotations ie = new ImproperAnnotations(new GenericProtein("id"), list);
assertNotNull(ie.toString());
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("Only miriam data from elements are accepted"));
}
}
@Test
public void testConstructor4() {
try {
new ImproperAnnotations(new GenericProtein("id"), new ArrayList<>());
fail("Exception expected");
} catch (InvalidArgumentException e) {
assertTrue(e.getMessage().contains("List of improper annotations cannot be null"));
}
}
public class ImproperAnnotationsTest extends AnnotationTestFunctions {
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testConstructor3() {
List<MiriamData> list = new ArrayList<>();
list.add(new MiriamData(MiriamType.CAS, "a"));
ImproperAnnotations ie = new ImproperAnnotations(new GenericProtein("id"), list);
assertNotNull(ie.toString());
}
@Test(expected = InvalidArgumentException.class)
public void testConstructor4() {
new ImproperAnnotations(new GenericProtein("id"), new ArrayList<>());
}
}
......@@ -70,7 +70,7 @@ public class MiRNAParserTest extends AnnotationTestFunctions {
}
}
@Test
@Test(expected = MiRNASearchException.class)
public void testFindWhenProblemWithTaxonomy() throws Exception {