1212import org .junit .Test ;
1313
1414import java .io .ByteArrayInputStream ;
15+ import java .io .FileInputStream ;
1516import java .io .InputStream ;
1617import java .util .Map ;
1718import java .util .Random ;
2627public class CommonUtilsTest {
2728 @ Test
2829 public void testReplaceMacro () throws Exception {
30+ testReplaceMacro ("abcd" , "abcd" , null );
2931 testReplaceMacro ("" , "" , ImmutableMap .<String , String >of ());
3032 testReplaceMacro ("$a" , "$a" , ImmutableMap .<String , String >of ());
3133 testReplaceMacro ("" , "" , ImmutableMap .of ("a" , "b" ));
@@ -36,10 +38,30 @@ public void testReplaceMacro() throws Exception {
3638
3739 private void testReplaceMacro (String expected , String original , Map <String , String > variables ) throws Exception {
3840 ByteArrayInputStream in = new ByteArrayInputStream (original .getBytes (Constants .DEFAULT_CHARSET ));
39- InputStream result = CommonUtils .replaceMacro (in , new VariableResolver .ByMap <>(variables ));
41+ InputStream result = CommonUtils .replaceMacro (in , variables == null ? null : new VariableResolver .ByMap <>(variables ));
4042 assertEquals (expected , IOUtils .toString (result , Constants .DEFAULT_CHARSET ));
4143 }
4244
45+ @ Test
46+ public void testFilestream () throws Exception {
47+ InputStream in = CommonUtilsTest .class .getResourceAsStream ("CommonUtilsTest.data" );
48+ try {
49+ assertEquals ("${name}" , IOUtils .toString (CommonUtils .replaceMacro (in , null )));
50+ } finally {
51+ if (in != null ) {
52+ in .close ();
53+ }
54+ }
55+ in = CommonUtils .class .getResourceAsStream ("CommonUtilsTest.data" );
56+ try {
57+ assertEquals ("Common" , IOUtils .toString (CommonUtils .replaceMacro (in , new VariableResolver .ByMap <String >(ImmutableMap .of ("name" , "Common" )))));
58+ } finally {
59+ if (in != null ) {
60+ in .close ();
61+ }
62+ }
63+ }
64+
4365 @ Test
4466 public void testRandomString () {
4567 assertEquals (16 , CommonUtils .randomString ().length ());
0 commit comments