1 16 17 package org.springframework.util; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.StringReader ; 23 import java.io.StringWriter ; 24 import java.util.Properties ; 25 26 import junit.framework.TestCase; 27 28 32 public class PropertiesPersisterTests extends TestCase { 33 34 public void testPropertiesPersister() throws IOException { 35 String propString = "code1=message1\ncode2:message2"; 36 Properties props = loadProperties(propString, false); 37 String propCopy = storeProperties(props, null, false); 38 loadProperties(propCopy, false); 39 } 40 41 public void testPropertiesPersisterWithWhitespace() throws IOException { 42 String propString = " code1\t= \tmessage1\n code2 \t :\t mess\\\n \t age2"; 43 Properties props = loadProperties(propString, false); 44 String propCopy = storeProperties(props, null, false); 45 loadProperties(propCopy, false); 46 } 47 48 public void testPropertiesPersisterWithHeader() throws IOException { 49 String propString = "code1=message1\ncode2:message2"; 50 Properties props = loadProperties(propString, false); 51 String propCopy = storeProperties(props, "myHeader", false); 52 loadProperties(propCopy, false); 53 } 54 55 public void testPropertiesPersisterWithEmptyValue() throws IOException { 56 String propString = "code1=message1\ncode2:message2\ncode3="; 57 Properties props = loadProperties(propString, false); 58 String propCopy = storeProperties(props, null, false); 59 loadProperties(propCopy, false); 60 } 61 62 public void testPropertiesPersisterWithReader() throws IOException { 63 String propString = "code1=message1\ncode2:message2"; 64 Properties props = loadProperties(propString, true); 65 String propCopy = storeProperties(props, null, true); 66 loadProperties(propCopy, false); 67 } 68 69 public void testPropertiesPersisterWithReaderAndWhitespace() throws IOException { 70 String propString = " code1\t= \tmessage1\n code2 \t :\t mess\\\n \t age2"; 71 Properties props = loadProperties(propString, true); 72 String propCopy = storeProperties(props, null, true); 73 loadProperties(propCopy, false); 74 } 75 76 public void testPropertiesPersisterWithReaderAndHeader() throws IOException { 77 String propString = "code1\t=\tmessage1\n code2 \t : \t message2"; 78 Properties props = loadProperties(propString, true); 79 String propCopy = storeProperties(props, "myHeader", true); 80 loadProperties(propCopy, false); 81 } 82 83 public void testPropertiesPersisterWithReaderAndEmptyValue() throws IOException { 84 String propString = "code1=message1\ncode2:message2\ncode3="; 85 Properties props = loadProperties(propString, true); 86 String propCopy = storeProperties(props, null, true); 87 loadProperties(propCopy, false); 88 } 89 90 private Properties loadProperties(String propString, boolean useReader) throws IOException { 91 DefaultPropertiesPersister persister = new DefaultPropertiesPersister(); 92 Properties props = new Properties (); 93 if (useReader) { 94 persister.load(props, new StringReader (propString)); 95 } 96 else { 97 persister.load(props, new ByteArrayInputStream (propString.getBytes())); 98 } 99 assertEquals("message1", props.getProperty("code1")); 100 assertEquals("message2", props.getProperty("code2")); 101 return props; 102 } 103 104 private String storeProperties(Properties props, String header, boolean useWriter) throws IOException { 105 DefaultPropertiesPersister persister = new DefaultPropertiesPersister(); 106 String propCopy = null; 107 if (useWriter) { 108 StringWriter propWriter = new StringWriter (); 109 persister.store(props, propWriter, header); 110 propCopy = propWriter.toString(); 111 } 112 else { 113 ByteArrayOutputStream propOut = new ByteArrayOutputStream (); 114 persister.store(props, propOut, header); 115 propCopy = new String (propOut.toByteArray()); 116 } 117 if (header != null) { 118 assertTrue(propCopy.indexOf(header) != -1); 119 } 120 assertTrue(propCopy.indexOf("\ncode1=message1") != -1); 121 assertTrue(propCopy.indexOf("\ncode2=message2") != -1); 122 return propCopy; 123 } 124 125 } 126 | Popular Tags |