001package com.randomnoun.maven.plugin.yamlCombine;
002
003import static java.nio.charset.StandardCharsets.ISO_8859_1;
004import static java.nio.charset.StandardCharsets.UTF_8;
005
006import java.io.File;
007import java.io.FileOutputStream;
008import java.io.IOException;
009import java.io.OutputStreamWriter;
010import java.io.PrintWriter;
011import java.io.StringWriter;
012import java.nio.charset.Charset;
013import java.nio.file.Files;
014import java.nio.file.Path;
015import java.util.Arrays;
016import java.util.List;
017
018import org.apache.maven.plugin.logging.Log;
019import org.apache.maven.plugin.logging.SystemStreamLog;
020
021import junit.framework.Assert;
022import junit.framework.TestCase;
023
024public class TestCombine extends  TestCase {
025
026    private Charset inputFileSetCharset = UTF_8;
027
028    public void setUp() { }
029        
030        // tests that $xrefs are resolved, and that $refs aren't
031        public void test1() throws IOException {
032                Log log = new SystemStreamLog();
033
034                YamlCombiner sc = new YamlCombiner();
035        sc.setRelativeDir(new File("src/test/resources/t1"));
036        sc.setFiles(new String[] { "input.yaml" });
037        sc.setLog(log);
038        
039                StringWriter w = new StringWriter();
040        sc.combine(w, inputFileSetCharset);
041
042        Path expectedPath = new File("src/test/resources/t1/expected-output.yaml").toPath();
043        List<String> expectedLines = Files.readAllLines(expectedPath);
044        
045        List<String> lines = Arrays.asList(w.toString().split("\n"));
046        
047        Assert.assertEquals(expectedLines, lines);
048        }
049        
050        // slightly different xref syntax ( # toggles /-escaping ) 
051        // ( same output as t1 )
052        public void test2() throws IOException {
053                Log log = new SystemStreamLog();
054
055                YamlCombiner sc = new YamlCombiner();
056        sc.setRelativeDir(new File("src/test/resources/t2"));
057        sc.setFiles(new String[] { "input.yaml" });
058        sc.setLog(log);
059        
060                StringWriter w = new StringWriter();
061        sc.combine(w, inputFileSetCharset);
062
063        Path expectedPath = new File("src/test/resources/t2/expected-output.yaml").toPath();
064        List<String> expectedLines = Files.readAllLines(expectedPath);
065        
066        List<String> lines = Arrays.asList(w.toString().split("\n"));
067
068        Assert.assertEquals(expectedLines, lines);
069        }
070        
071        // override keys in xref object
072        public void test3() throws IOException {
073                Log log = new SystemStreamLog();
074
075                YamlCombiner sc = new YamlCombiner();
076        sc.setRelativeDir(new File("src/test/resources/t3"));
077        sc.setFiles(new String[] { "input.yaml" });
078        // sc.setVerbose(true);
079        sc.setLog(log);
080
081        StringWriter w = new StringWriter();
082        sc.combine(w, inputFileSetCharset);
083
084        Path expectedPath = new File("src/test/resources/t3/expected-output.yaml").toPath();
085        List<String> expectedLines = Files.readAllLines(expectedPath);
086        
087        List<String> lines = Arrays.asList(w.toString().split("\n"));
088        
089        Assert.assertEquals(expectedLines, lines);
090        }
091        
092        // combine inputs from multiple input yamls
093        public void test4() throws IOException {
094                Log log = new SystemStreamLog();
095
096                YamlCombiner sc = new YamlCombiner();
097        sc.setRelativeDir(new File("src/test/resources/t4"));
098        sc.setFiles(new String[] { "input.yaml", "input-2.yaml" });
099        // sc.setVerbose(true);
100        sc.setLog(log);
101
102        StringWriter w = new StringWriter();
103        sc.combine(w, inputFileSetCharset);
104        System.out.println(w.toString());
105
106        Path expectedPath = new File("src/test/resources/t4/expected-output.yaml").toPath();
107        List<String> expectedLines = Files.readAllLines(expectedPath);
108        
109        List<String> lines = Arrays.asList(w.toString().split("\n"));
110
111        Assert.assertEquals(expectedLines, lines);
112        }
113
114    // parse top level $xref to copy whole yaml
115    public void test5() throws IOException {
116        Log log = new SystemStreamLog();
117
118        YamlCombiner sc = new YamlCombiner();
119        sc.setRelativeDir(new File("src/test/resources/t5"));
120        sc.setFiles(new String[] { "input.yaml" });
121        sc.setLog(log);
122
123        StringWriter w = new StringWriter();
124        sc.combine(w, inputFileSetCharset);
125
126        Path expectedPath = new File("src/test/resources/t5/expected-output.yaml").toPath();
127        List<String> expectedLines = Files.readAllLines(expectedPath);
128
129        List<String> lines = Arrays.asList(w.toString().split("\n"));
130
131        Assert.assertEquals(expectedLines, lines);
132    }
133
134    public void test6() throws IOException {
135        Log log = new SystemStreamLog();
136
137        YamlCombiner sc = new YamlCombiner();
138        sc.setRelativeDir(new File("src/test/resources/t6"));
139        sc.setFiles(new String[] { "input.yaml", "input-2.yaml"  });
140        sc.setLog(log);
141        FileOutputStream fos = new FileOutputStream("target/t6.yaml");
142        PrintWriter w = new PrintWriter(new OutputStreamWriter(fos, UTF_8));
143        sc.combine(w, inputFileSetCharset);
144
145        Path expectedPath = new File("src/test/resources/t6/expected-output.yaml").toPath();
146
147        List<String> expectedLines = Files.readAllLines(expectedPath,UTF_8);
148
149        Path actualPath = new File("target/t6.yaml").toPath();
150        List<String> lines = Files.readAllLines(actualPath);
151        for (int i = 0; i < expectedLines.size(); i++) {
152            Assert.assertEquals(expectedLines.get(i),lines.get(i));
153        }
154        Assert.assertEquals(expectedLines, lines);
155    }
156
157    public void test7() throws IOException {
158        Log log = new SystemStreamLog();
159
160        YamlCombiner sc = new YamlCombiner();
161        sc.setRelativeDir(new File("src/test/resources/t7"));
162        sc.setFiles(new String[] { "input.yaml", "input-2.yaml"  });
163        sc.setLog(log);
164        FileOutputStream fos = new FileOutputStream("target/t7.yaml");
165        PrintWriter w = new PrintWriter(new OutputStreamWriter(fos, ISO_8859_1));
166        sc.combine(w, ISO_8859_1);
167
168        Path expectedPath = new File("src/test/resources/t7/expected-output.yaml").toPath();
169
170        List<String> expectedLines = Files.readAllLines(expectedPath,ISO_8859_1);
171
172        Path actualPath = new File("target/t7.yaml").toPath();
173        List<String> lines = Files.readAllLines(actualPath,ISO_8859_1);
174        for (int i = 0; i < expectedLines.size(); i++) {
175            Assert.assertEquals(expectedLines.get(i),lines.get(i));
176        }
177        Assert.assertEquals(expectedLines, lines);
178    }
179
180    public void test8() throws IOException {
181        Log log = new SystemStreamLog();
182
183        YamlCombiner sc = new YamlCombiner();
184        sc.setRelativeDir(new File("src/test/resources/t8"));
185        sc.setFiles(new String[] { "input.yaml", "input-2.yaml"  });
186        sc.setLog(log);
187        FileOutputStream fos = new FileOutputStream("target/t8.yaml");
188        PrintWriter w = new PrintWriter(new OutputStreamWriter(fos, UTF_8));
189        sc.combine(w, ISO_8859_1);
190
191        Path expectedPath = new File("src/test/resources/t8/expected-output.yaml").toPath();
192
193        List<String> expectedLines = Files.readAllLines(expectedPath,UTF_8);
194
195        Path actualPath = new File("target/t8.yaml").toPath();
196        List<String> lines = Files.readAllLines(actualPath,UTF_8);
197        for (int i = 0; i < expectedLines.size(); i++) {
198            Assert.assertEquals(expectedLines.get(i),lines.get(i));
199        }
200        Assert.assertEquals(expectedLines, lines);
201    }
202        
203}