1 package net.sf.tomp.djunit.stream; 2 3 import net.sf.tomp.djunit.Provider; 4 import net.sf.tomp.djunit.TestData; 5 import net.sf.tomp.djunit.simple.RecordImpl; 6 import net.sf.tomp.djunit.simple.RecordMetadataImpl; 7 import net.sf.tomp.djunit.simple.TestDataCache; 8 import net.sf.tomp.djunit.simple.TestDataImpl; 9 10 import java.io.BufferedReader; 11 import java.io.EOFException; 12 import java.io.IOException; 13 import java.io.InputStream; 14 import java.io.InputStreamReader; 15 16 import java.util.StringTokenizer; 17 18 public abstract class StreamProvider implements Provider 19 { 20 protected static final String STRUCTURE = "!tomp.junit.testdata.Record.structure"; 21 protected static final String TESTDATA = ".testdata"; 22 private TestDataCache cache = new TestDataCache(); 23 24 public TestData getTestData(String testClassName) 25 throws IOException 26 { 27 return getCachedTestData(testClassName + TESTDATA); 28 } 29 30 protected TestData getCachedTestData(String testDataFileName) 31 throws IOException 32 { 33 TestData data = cache.getTestData(testDataFileName); 34 35 if (data == null) 36 { 37 data = doGetTestData(testDataFileName); 38 cache.putTestData(testDataFileName, data); 39 } 40 41 return data; 42 } 43 44 protected abstract InputStream getInputStream(String isName) 45 throws IOException; 46 47 protected TestData doGetTestData(String resourceName) 48 throws IOException 49 { 50 BufferedReader in = new BufferedReader(new InputStreamReader( 51 getInputStream(resourceName))); 52 53 String line = in.readLine(); 54 55 if (line == null) 56 { 57 throw new EOFException("TestData file empty for resource name: " 58 + resourceName); 59 } 60 61 RecordMetadataImpl rm = new RecordMetadataImpl(); 62 63 if (line.startsWith(STRUCTURE)) 64 { 65 line = line.substring(STRUCTURE.length()).trim(); 66 67 StringTokenizer st = new StringTokenizer(line); 68 69 while (st.hasMoreTokens()) 70 { 71 rm.addField(st.nextToken(), String.class); 72 } 73 } 74 75 TestDataImpl data = new TestDataImpl(); 76 77 line = in.readLine(); 78 79 while ((line != null) && (line.trim().length() > 0)) 80 { 81 RecordImpl r = new RecordImpl(rm); 82 StringTokenizer st = new StringTokenizer(line.trim()); 83 int i = 0; 84 85 while (st.hasMoreTokens()) 86 { 87 r.addField(rm.fieldNameAt(i++), st.nextToken()); 88 } 89 90 data.addRecord(r); 91 line = in.readLine(); 92 } 93 94 return data; 95 } 96 }