Add testing dirs
[proteocache.git] / testsrc / compbio / casscode / msa / LimitTester.java
diff --git a/testsrc/compbio/casscode/msa/LimitTester.java b/testsrc/compbio/casscode/msa/LimitTester.java
new file mode 100644 (file)
index 0000000..8a9e56a
--- /dev/null
@@ -0,0 +1,146 @@
+/* Copyright (c) 2009 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+\r
+package compbio.runner.msa;\r
+\r
+import static org.testng.Assert.assertEquals;\r
+import static org.testng.Assert.assertFalse;\r
+import static org.testng.Assert.assertNotNull;\r
+import static org.testng.Assert.assertTrue;\r
+import static org.testng.Assert.fail;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.util.List;\r
+\r
+import javax.xml.bind.JAXBException;\r
+\r
+import org.testng.annotations.BeforeMethod;\r
+import org.testng.annotations.Test;\r
+\r
+import compbio.data.sequence.FastaSequence;\r
+import compbio.data.sequence.SequenceUtil;\r
+import compbio.engine.conf.RunnerConfigMarshaller;\r
+import compbio.metadata.AllTestSuit;\r
+import compbio.metadata.Limit;\r
+import compbio.metadata.LimitsManager;\r
+import compbio.metadata.PresetManager;\r
+\r
+public class LimitTester {\r
+\r
+       static final String clustalLimitsFile = AllTestSuit.TEST_DATA_PATH\r
+                       + "ClustalLimits.xml";\r
+       static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
+                       + "TO1381.fasta"; //\r
+       static final String input = AllTestSuit.TEST_DATA_PATH\r
+                       + "ClustalPresets.xml";\r
+\r
+       LimitsManager<ClustalW> clustalLimitConfig = null;\r
+       PresetManager<ClustalW> presets = null;\r
+\r
+       @BeforeMethod(groups = { AllTestSuit.test_group_runner })\r
+       public void setup() {\r
+               try {\r
+                       RunnerConfigMarshaller<ClustalW> clustalmarsh = new RunnerConfigMarshaller<ClustalW>(\r
+                                       LimitsManager.class);\r
+                       clustalLimitConfig = clustalmarsh.read(new FileInputStream(\r
+                                       new File(clustalLimitsFile)), LimitsManager.class);\r
+                       assertNotNull(clustalLimitConfig.getLimits());\r
+                       assertEquals(clustalLimitConfig.getLimits().size(), 3);\r
+                       // Load presets\r
+                       RunnerConfigMarshaller<ClustalW> rconfigPresets = new RunnerConfigMarshaller<ClustalW>(\r
+                                       PresetManager.class);\r
+                       File infile = new File(input);\r
+                       assertTrue(infile.exists());\r
+                       presets = rconfigPresets.read(new FileInputStream(infile),\r
+                                       PresetManager.class);\r
+                       assertNotNull(presets);\r
+                       assertFalse(presets.getPresets().isEmpty());\r
+\r
+               } catch (JAXBException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               } catch (FileNotFoundException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getLocalizedMessage());\r
+               }\r
+\r
+       }\r
+\r
+       @Test\r
+       public void testLoadLimits() {\r
+               assertNotNull(clustalLimitConfig);\r
+               List<Limit<ClustalW>> limits = clustalLimitConfig.getLimits();\r
+               assertEquals(limits.size(), 3);\r
+               Limit<ClustalW> limit = limits.get(0);\r
+               assertNotNull(limit);\r
+               assertEquals(limit.getPreset(),\r
+                               "Disable gap weighting (Speed-oriented)");\r
+               assertEquals(limit.getSeqNumber(), 400);\r
+               assertEquals(limit.getAvgSeqLength(), 600);\r
+               assertFalse(limit.isDefault());\r
+\r
+               limit = limits.get(1);\r
+               assertNotNull(limit);\r
+               assertEquals(limit.getPreset(), null);\r
+               assertEquals(limit.getSeqNumber(), 1000);\r
+               assertEquals(limit.getAvgSeqLength(), 400);\r
+               assertTrue(limit.isDefault());\r
+       }\r
+\r
+       @Test\r
+       public void testLimitExceeded() {\r
+\r
+               String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
+                               + "testlimit.fasta";\r
+\r
+               FileInputStream fio;\r
+               try {\r
+                       fio = new FileInputStream(test_input);\r
+                       List<FastaSequence> data = SequenceUtil.readFasta(fio);\r
+                       fio.close();\r
+                       assertNotNull(data);\r
+                       assertEquals(data.size(), 6);\r
+                       assertEquals(Limit.getAvgSequenceLength(data), 20486);\r
+                       Limit small = new Limit(40, 500, "default");\r
+\r
+                       assertTrue(small.isExceeded(data));\r
+\r
+                       Limit large = new Limit(500, 500, "default");\r
+                       assertFalse(large.isExceeded(data));\r
+\r
+                       Limit numSeqOnly = new Limit(6, 0, "default");\r
+                       assertFalse(numSeqOnly.isExceeded(data));\r
+\r
+                       Limit exnumSeqOnly = new Limit(5, 0, "default");\r
+                       assertTrue(exnumSeqOnly.isExceeded(data));\r
+\r
+                       Limit numSeq3 = new Limit(5, 1000000, "default");\r
+                       assertTrue(numSeq3.isExceeded(data));\r
+\r
+               } catch (FileNotFoundException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (IOException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               }\r
+       }\r
+}\r