1 package com.aragost.javahg.internals;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 import java.nio.charset.Charset;
6 import java.util.Map;
7
8 import org.junit.Assert;
9 import org.junit.Test;
10
11 import com.google.common.collect.Maps;
12
13 public class PatternReplacingOutputStreamTest {
14
15 private static final Charset UTF8 = Charset.forName("UTF-8");
16 private static final Map<String, byte[]> REPLACEMENTS = Maps.newHashMap();
17 static {
18 REPLACEMENTS.put("one", "1".getBytes());
19 REPLACEMENTS.put("two", "2".getBytes());
20 REPLACEMENTS.put("three", "3".getBytes());
21 }
22
23 @Test
24 public void test() throws IOException {
25 assertFilter("", "");
26 assertFilter("abc", "abc");
27 assertFilter("1 2 3", "%{one} %{two} %{three}");
28 assertFilter("A%{B}{C", "A%%{B}{C");
29 assertFilter("1%{%{two=null}}", "%{one}%{%{two}}%{three");
30 }
31
32 private void assertFilter(String expected, String input) throws IOException {
33 String filtered = filter(input);
34 Assert.assertEquals(expected, filtered);
35 }
36
37 private String filter(String s) throws IOException {
38 byte[] bytes = s.getBytes("UTF-8");
39 ByteArrayOutputStream baos = new ByteArrayOutputStream();
40 PatternReplacingOutputStream out = new PatternReplacingOutputStream(baos, REPLACEMENTS);
41 out.write(bytes);
42 out.flush();
43 return Utils.decodeBytes(baos.toByteArray(), UTF8.newDecoder());
44 }
45 }