1 33 34 package edu.rice.cs.drjava.config; 35 36 import edu.rice.cs.drjava.DrJavaTestCase; 37 38 import java.io.ByteArrayInputStream ; 39 import java.io.ByteArrayOutputStream ; 40 import java.io.IOException ; 41 import java.text.SimpleDateFormat ; 42 import java.text.ParseException ; 43 import java.util.Date ; 44 45 50 public class SavableConfigurationTest extends DrJavaTestCase { 51 54 SimpleDateFormat dateFormat = new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy"); 55 56 ByteArrayOutputStream outputBytes = null; 57 58 public void setUp() throws Exception { 59 super.setUp(); 60 outputBytes = new ByteArrayOutputStream (); 61 } 62 63 66 public void testEmptyConfiguration() throws IOException { 67 SavableConfiguration emptyConfig = new SavableConfiguration(new DefaultOptionMap()); 68 69 emptyConfig.saveConfiguration(outputBytes, "header"); 70 71 String outputString = outputBytes.toString(); 72 String [] lines = outputString.split("\n"); 73 74 assertTrue("Data exists", outputString.length() > 0); 75 assertEquals("Number of lines", 2, lines.length); 76 assertEquals("Starts with \"#header\"", "#header", lines[0]); 77 try { 78 String bareDate = lines[1].substring(1, lines[1].length()); 80 Date readInDate = dateFormat.parse(bareDate); 81 assertTrue("Embedded date less than now", 82 readInDate.compareTo(new Date ()) <= 0); 83 } 84 catch (ParseException pe) { 85 fail("Could not parse second line into a date."); 86 } 87 } 88 89 92 public void testNonEmptyConfiguration() throws IOException { 93 DefaultOptionMap optionsMap = new DefaultOptionMap(); 94 optionsMap.setOption(new BooleanOption("tests_are_good", false), true); 95 optionsMap.setOption(new IntegerOption("meaning_of_life", 0), 42); 96 optionsMap.setOption(new StringOption("yay_strings", "hello?"), "goodbye"); 97 98 SavableConfiguration nonEmptyConfig = new SavableConfiguration(optionsMap); 99 100 nonEmptyConfig.saveConfiguration(outputBytes, "header"); 101 102 String outputString = outputBytes.toString(); 103 String [] lines = outputString.split("\n"); 104 105 assertTrue("Data exists", outputString.length() > 0); 106 assertEquals("Number of lines", 5, lines.length); 107 assertEquals("Starts with \"#header\"", "#header", lines[0]); 108 try { 109 String bareDate = lines[1].substring(1, lines[1].length()); 111 Date readInDate = dateFormat.parse(bareDate); 112 assertTrue("Embedded date less than now", 113 readInDate.compareTo(new Date ()) <= 0); 114 } 115 catch (ParseException pe) { 116 fail("Could not parse second line into a date."); 117 } 118 assertEquals("BooleanOption", "tests_are_good = true", lines[2]); 119 assertEquals("IntegerOption", "meaning_of_life = 42", lines[3]); 120 assertEquals("StringOption", "yay_strings = goodbye", lines[4]); 121 } 122 } | Popular Tags |