1 46 47 package groovy.lang; 48 49 import java.util.List ; 50 51 import org.codehaus.groovy.runtime.InvokerHelper; 52 53 import groovy.util.GroovyTestCase; 54 55 61 public class SequenceTest extends GroovyTestCase { 62 63 public void testConstruction() { 64 Sequence sequence = new Sequence(String .class); 65 sequence.add("James"); 66 sequence.add("Bob"); 67 68 assertEquals("Size", 2, sequence.size()); 69 assertEquals("Element", "James", sequence.get(0)); 70 assertEquals("Element", "Bob", sequence.get(1)); 71 72 List answer = (List ) InvokerHelper.invokeMethod(sequence, "startsWith", new Object [] { "Ja" }); 74 assertArrayEquals(new Object [] { Boolean.TRUE, Boolean.FALSE }, answer.toArray()); 75 76 answer = (List ) InvokerHelper.invokeMethod(sequence, "length", null); 77 assertArrayEquals(new Object [] { new Integer (5), new Integer (3)}, answer.toArray()); 78 } 79 80 public void testAddingWrongTypeFails() { 81 try { 82 Sequence sequence = new Sequence(String .class); 83 sequence.add(new Integer (5)); 84 85 fail("Should have thrown exception"); 86 } 87 catch (IllegalArgumentException e) { 88 System.out.println("Caught: " + e); 89 } 90 } 91 92 public void testAddingNullFails() { 93 try { 94 Sequence sequence = new Sequence(String .class); 95 sequence.add(null); 96 97 fail("Should have thrown exception"); 98 } 99 catch (NullPointerException e) { 100 System.out.println("Caught: " + e); 101 } 102 } 103 104 } 105 | Popular Tags |