From: gmungoc Date: Fri, 15 May 2015 09:28:12 +0000 (+0100) Subject: JAL-1270 main methods converted to JUnit X-Git-Tag: Release_2_10_0~675 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=d07fe6a0891b57a9d707e356f7769395ce94b0da;p=jalview.git JAL-1270 main methods converted to JUnit --- diff --git a/src/com/stevesoft/pat/RegexWriter.java b/src/com/stevesoft/pat/RegexWriter.java index a8c3e50..61bcdf6 100755 --- a/src/com/stevesoft/pat/RegexWriter.java +++ b/src/com/stevesoft/pat/RegexWriter.java @@ -7,9 +7,10 @@ // package com.stevesoft.pat; -import java.io.*; +import java.io.IOException; +import java.io.Writer; -import com.stevesoft.pat.wrap.*; +import com.stevesoft.pat.wrap.WriterWrap; /** * A basic extension of FilterWriter that uses Transformer to make replacements @@ -77,6 +78,7 @@ public class RegexWriter extends Writer * * @deprecated */ + @Deprecated public char getEOLchar() { return EOLchar; @@ -87,6 +89,7 @@ public class RegexWriter extends Writer * * @deprecated */ + @Deprecated public void setEOLchar(char c) { EOLchar = c; @@ -99,6 +102,7 @@ public class RegexWriter extends Writer * * @deprecated */ + @Deprecated public int getMaxLines() { return max_lines; @@ -109,6 +113,7 @@ public class RegexWriter extends Writer * * @deprecated */ + @Deprecated public void setMaxLines(int ml) { max_lines = ml; @@ -226,51 +231,4 @@ public class RegexWriter extends Writer { bufferSize = i; } - - static void test(String re, String inp, int n) throws Exception - { - StringWriter sw = new StringWriter(); - Regex rex = Regex.perlCode(re); - String res1 = rex.replaceAll(inp); - RegexWriter rw = new RegexWriter(rex, sw); - for (int i = 0; i < inp.length(); i++) - { - rw.write(inp.charAt(i)); - } - rw.close(); - String res2 = sw.toString(); - if (!res1.equals(res2)) - { - System.out.println("nmax=" + n); - System.out.println("re=" + re); - System.out.println("inp=" + inp); - System.out.println("res1=" + res1); - System.out.println("res2=" + res2); - System.exit(255); - } - } - - public static void main(String[] args) throws Exception - { - for (int n = 1; n <= 1; n++) - { - test("s/x/y/", "-----x123456789", n); - test("s/x/y/", "x123456789", n); - test("s/x/y/", "-----x", n); - test("s/x.*?x/y/", ".xx..x..x...x...x....x....x", n); - test("s/x.*x/[$&]/", "--x........x--xx", n); - test("s/x.*x/[$&]/", "--x........x------", n); - test("s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb", n); - test("s/.$/a/", "123", n); - test("s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb", n); - test("s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb", n); - test("s/$/a/", "bbb", n); - test("s/^/a/", "bbb", n); - test("s/^/a/", "", n); - test("s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", n); - test("s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", n); - test("s/x/$&/", "xxx", n); - } - System.out.println("Success!!!"); - } } diff --git a/src/jalview/analysis/ParseProperties.java b/src/jalview/analysis/ParseProperties.java index 2810ec9..38d8371 100644 --- a/src/jalview/analysis/ParseProperties.java +++ b/src/jalview/analysis/ParseProperties.java @@ -155,29 +155,4 @@ public class ParseProperties } return count; } - - public static void main(String argv[]) - { - SequenceI[] seqs = new SequenceI[] - { new Sequence("sq1", "THISISAPLACEHOLDER"), - new Sequence("sq2", "THISISAPLACEHOLDER"), - new Sequence("sq3", "THISISAPLACEHOLDER"), - new Sequence("sq4", "THISISAPLACEHOLDER") }; - seqs[0].setDescription("1 mydescription1"); - seqs[1].setDescription("mydescription2"); - seqs[2].setDescription("2. 0.1 mydescription3"); - seqs[3].setDescription("3 0.01 mydescription4"); - // seqs[4].setDescription("5 mydescription5"); - Alignment al = new Alignment(seqs); - ParseProperties pp = new ParseProperties(al); - String regex = ".*([-0-9.+]+)"; - System.out.println("Matched " - + pp.getScoresFromDescription("my Score", - "my Score Description", regex, true) + " for " + regex); - regex = ".*([-0-9.+]+).+([-0-9.+]+).*"; - System.out.println("Matched " - + pp.getScoresFromDescription("my Score", - "my Score Description", regex, true) + " for " + regex); - System.out.println("Finished."); - } } diff --git a/test/com/stevesoft/pat/RegexWriterTest.java b/test/com/stevesoft/pat/RegexWriterTest.java new file mode 100644 index 0000000..d91502e --- /dev/null +++ b/test/com/stevesoft/pat/RegexWriterTest.java @@ -0,0 +1,73 @@ +package com.stevesoft.pat; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.io.StringWriter; + +import org.junit.Test; + +/** + * Test class refactored from RegexWriter main method + */ +public class RegexWriterTest +{ + + /** + * Asserts that the result of performing 'replaceAll' on the input using the + * regular expression is the same as the output of a RegexWriter, constructed + * from the regular expression, writing out each character of the input + * string. + * + * @param re + * a regular expression + * @param inp + * an input string + * @throws Exception + */ + void test(String re, String inp) throws IOException + { + StringWriter sw = new StringWriter(); + Regex rex = Regex.perlCode(re); + String res1 = rex.replaceAll(inp); + RegexWriter rw = new RegexWriter(rex, sw); + for (int i = 0; i < inp.length(); i++) + { + rw.write(inp.charAt(i)); + } + rw.close(); + String res2 = sw.toString(); + if (!res1.equals(res2)) + { + System.out.println("re=" + re); + System.out.println("inp=" + inp); + System.out.println("res1=" + res1); + System.out.println("res2=" + res2); + assertEquals(res1, res2); + } + } + + @Test + public void testWrite() throws IOException + { + for (int n = 1; n <= 1; n++) + { + test("s/x/y/", "-----x123456789"); + test("s/x/y/", "x123456789"); + test("s/x/y/", "-----x"); + test("s/x.*?x/y/", ".xx..x..x...x...x....x....x"); + test("s/x.*x/[$&]/", "--x........x--xx"); + test("s/x.*x/[$&]/", "--x........x------"); + test("s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb"); + test("s/.$/a/", "123"); + test("s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb"); + test("s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb"); + test("s/$/a/", "bbb"); + test("s/^/a/", "bbb"); + test("s/^/a/", ""); + test("s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); + test("s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); + test("s/x/$&/", "xxx"); + } + } +} diff --git a/test/jalview/analysis/ParsePropertiesTest.java b/test/jalview/analysis/ParsePropertiesTest.java new file mode 100644 index 0000000..a679a46 --- /dev/null +++ b/test/jalview/analysis/ParsePropertiesTest.java @@ -0,0 +1,141 @@ +package jalview.analysis; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Before; +import org.junit.Test; + +import jalview.datamodel.Alignment; +import jalview.datamodel.AlignmentAnnotation; +import jalview.datamodel.Sequence; +import jalview.datamodel.SequenceI; + +public class ParsePropertiesTest +{ + + private Alignment al; + + private ParseProperties pp; + + /** + * Construct an alignment with 4 sequences with varying description format + */ + @Before + public void setUp() + { + SequenceI[] seqs = new SequenceI[] + { new Sequence("sq1", "THISISAPLACEHOLDER"), + new Sequence("sq2", "THISISAPLACEHOLDER"), + new Sequence("sq3", "THISISAPLACEHOLDER"), + new Sequence("sq4", "THISISAPLACEHOLDER") }; + seqs[0].setDescription("1 mydescription1"); + seqs[1].setDescription("mydescription2"); + seqs[2].setDescription("2. 0.1 mydescription+3"); + seqs[3].setDescription("3 0.01 mydescription4"); + al = new Alignment(seqs); + + pp = new ParseProperties(al); + + } + + /** + * Test with a description pattern that matches any string ending in one or + * more 'number characters' (0-9+.), i.e. greedily matches any trailing + * numeric part of the string + */ + @Test + public void testGetScoresFromDescription() + { + // TODO - test the regex actually used by Jalview? + // \\W*([-+eE0-9.]+) + // see AlignFrame.extractScores_actionPerformed + String regex = ".*([-0-9.+]+)"; + final int count = pp.getScoresFromDescription("my Score", + "my Score Description", regex, true); + System.out.println("Matched " + count + " for " + regex); + assertEquals(4, count); + + /* + * Verify values 1/2/3/4 have been parsed from sequence descriptions + */ + AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation(); + assertEquals(1, anns.length); + assertEquals(1d, anns[0].getScore(), 0.001d); + assertEquals("my Score Description", anns[0].description); + assertEquals("my Score", anns[0].label); + anns = al.getSequenceAt(1).getAnnotation(); + assertEquals(1, anns.length); + assertEquals(2d, anns[0].getScore(), 0.001d); + assertEquals("my Score Description", anns[0].description); + assertEquals("my Score", anns[0].label); + anns = al.getSequenceAt(2).getAnnotation(); + assertEquals(1, anns.length); + assertEquals(3d, anns[0].getScore(), 0.001d); + anns = al.getSequenceAt(3).getAnnotation(); + assertEquals(1, anns.length); + assertEquals(4d, anns[0].getScore(), 0.001d); + } + + /** + * Test with a description pattern that matches any string (or none), followed + * by a 'number character' (0-9+.), followed by at least one separator + * character, followed by at least one 'number character', then any trailing + * characters. + */ + @Test + public void testGetScoresFromDescription_twoScores() + { + String regex = ".*([-0-9.+]+).+([-0-9.+]+).*"; + final int count = pp.getScoresFromDescription("my Score", + "my Score Description", regex, true); + System.out.println("Matched " + count + " for " + regex); + assertEquals(3, count); + + /* + * Seq1 has two score values parsed out + */ + AlignmentAnnotation[] anns = al.getSequenceAt(0).getAnnotation(); + assertEquals(2, anns.length); + assertEquals(1d, anns[0].getScore(), 0.001d); + assertEquals("my Score Description", anns[0].description); + assertEquals("my Score", anns[0].label); + assertEquals(1d, anns[1].getScore(), 0.001d); + assertEquals("my Score Description (column 1)", anns[1].description); + assertEquals("my Score_1", anns[1].label); + + /* + * Seq2 has no score parsed out (is this right?) + */ + assertNull(al.getSequenceAt(1).getAnnotation()); + + /* + * Seq3 has two score values parsed out + */ + // TODO parsed values (1.0 and 3.0) look wrong v description + // would expect 2.0 and 0.1 + // undesired 'greedy' behaviour of regex? + anns = al.getSequenceAt(2).getAnnotation(); + assertEquals(2, anns.length); + assertEquals(1d, anns[0].getScore(), 0.001d); + assertEquals("my Score Description", anns[0].description); + assertEquals("my Score", anns[0].label); + assertEquals(3d, anns[1].getScore(), 0.001d); + assertEquals("my Score Description (column 1)", anns[1].description); + assertEquals("my Score_1", anns[1].label); + + /* + * Seq3 has two score values parsed out + */ + // TODO parsed values (1.0 and 4.0) look wrong v description + // would expect 3 and 0.01 + anns = al.getSequenceAt(3).getAnnotation(); + assertEquals(2, anns.length); + assertEquals(1d, anns[0].getScore(), 0.001d); + assertEquals("my Score Description", anns[0].description); + assertEquals("my Score", anns[0].label); + assertEquals(4d, anns[1].getScore(), 0.001d); + assertEquals("my Score Description (column 1)", anns[1].description); + assertEquals("my Score_1", anns[1].label); + } +}