OSDN Git Service

SAX対応
[mikutoga/Pmd2XML.git] / src / test / java / testdata / CnvAssert.java
1 /*
2  */
3
4 package testdata;
5
6 import java.io.BufferedInputStream;
7 import java.io.BufferedOutputStream;
8 import java.io.File;
9 import java.io.FileInputStream;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import jp.sfjp.mikutoga.pmd2xml.ModelFileType;
15 import jp.sfjp.mikutoga.pmd2xml.Pmd2XmlConv;
16
17 import static org.junit.Assert.*;
18
19 /**
20  *
21  */
22 public class CnvAssert {
23
24     private CnvAssert(){
25     }
26
27     /**
28      * テスト出力用テンポラリファイルの生成。
29      * テスト終了時(VM終了時)に消える。
30      * @return テンポラリファイル
31      * @throws IOException エラー
32      */
33     public static File openTempFile() throws IOException{
34         File file = File.createTempFile("pmd2xml", null);
35         file.deleteOnExit();
36         return file;
37     }
38
39     /**
40      * XMLリソースをPMDに変換した結果がPMDリソースに等しいと表明する。
41      * @param klass リソース元クラス
42      * @param xmlResource XMLリソース名
43      * @param expPmdResource PMDリソース名
44      * @throws Exception エラー
45      */
46     public static void assertXml2Pmd(
47             Class<?> klass,
48             String xmlResource,
49             String expPmdResource )
50             throws Exception{
51         InputStream xmlis =
52                 klass.getResourceAsStream(xmlResource);
53         assertNotNull(xmlis);
54         xmlis = new BufferedInputStream(xmlis);
55
56         File destFile = openTempFile();
57         OutputStream destOut;
58         destOut = new FileOutputStream(destFile);
59         destOut = new BufferedOutputStream(destOut);
60 //        destOut = new DebugOutputStream(destOut);
61
62         Pmd2XmlConv converter = new Pmd2XmlConv();
63         converter.setInType(ModelFileType.XML_AUTO);
64         converter.setOutType(ModelFileType.PMD);
65         converter.setNewline("\n");
66
67         converter.convert(xmlis, destOut);
68
69         xmlis.close();
70         destOut.close();
71
72         assertSameFile(klass, expPmdResource, destFile);
73
74         return;
75     }
76
77     /**
78      * PMDリソースをXMLに変換した結果がXMLリソースに等しいと表明する。
79      * @param klass リソース元クラス
80      * @param pmdResource PMDリソース名
81      * @param expXmlResource XMLリソース名
82      * @throws Exception エラー
83      */
84     public static void assertPmd2Xml(
85             Class<?> klass,
86             String pmdResource,
87             String expXmlResource )
88             throws Exception{
89         InputStream pmdis =
90                 klass.getResourceAsStream(pmdResource);
91         assertNotNull(pmdis);
92         pmdis = new BufferedInputStream(pmdis);
93
94         File destFile = openTempFile();
95         OutputStream destOut;
96         destOut = new FileOutputStream(destFile);
97         destOut = new BufferedOutputStream(destOut);
98
99         Pmd2XmlConv converter = new Pmd2XmlConv();
100         converter.setInType(ModelFileType.PMD);
101         converter.setOutType(ModelFileType.XML_101009);
102         converter.setNewline("\n");
103         converter.setGenerator(null);
104
105         converter.convert(pmdis, destOut);
106
107         pmdis.close();
108         destOut.close();
109
110         assertSameFile(klass, expXmlResource, destFile);
111
112         return;
113     }
114
115     /**
116      * PMDリソースをXMLに変換した結果がXMLリソースに等しいと表明する。
117      * @param klass リソース元クラス
118      * @param pmdResource PMDリソース名
119      * @param expXmlResource XMLリソース名
120      * @throws Exception エラー
121      */
122     public static void assertPmd2Xml13(
123             Class<?> klass,
124             String pmdResource,
125             String expXmlResource )
126             throws Exception{
127         InputStream pmdis =
128                 klass.getResourceAsStream(pmdResource);
129         assertNotNull(pmdis);
130         pmdis = new BufferedInputStream(pmdis);
131
132         File destFile = openTempFile();
133         OutputStream destOut;
134         destOut = new FileOutputStream(destFile);
135         destOut = new BufferedOutputStream(destOut);
136
137         Pmd2XmlConv converter = new Pmd2XmlConv();
138         converter.setInType(ModelFileType.PMD);
139         converter.setOutType(ModelFileType.XML_130128);
140         converter.setNewline("\n");
141         converter.setGenerator(null);
142
143         converter.convert(pmdis, destOut);
144
145         pmdis.close();
146         destOut.close();
147
148         assertSameFile(klass, expXmlResource, destFile);
149
150         return;
151     }
152
153     /**
154      * リソースとファイルの内容が等しいと表明する。
155      * @param klass リソース元クラス
156      * @param resourceName リソース名
157      * @param resFile ファイル
158      * @throws IOException 入力エラー
159      */
160     public static void assertSameFile(
161             Class<?> klass,
162             String resourceName,
163             File resFile )
164             throws IOException{
165         InputStream expis =
166                 klass.getResourceAsStream(resourceName);
167         assertNotNull(expis);
168
169         InputStream resIn = new FileInputStream(resFile);
170
171         try{
172             assertSameStream(expis, resIn);
173         }finally{
174             expis.close();
175             resIn.close();
176         }
177
178         return;
179     }
180
181     /**
182      * 2つの入力ストリーム内容が等しいと表明する。
183      * @param expIn 期待する入力ストリーム
184      * @param resIn 結果入力ストリーム
185      * @throws IOException 入力エラー
186      */
187     public static void assertSameStream(InputStream expIn, InputStream resIn)
188             throws IOException{
189         InputStream expis = new BufferedInputStream(expIn);
190         InputStream resis = new BufferedInputStream(resIn);
191
192         int offset = 0;
193         for(;;){
194             int expCh = expis.read();
195             int resCh = resis.read();
196
197             try{
198                 assertEquals(expCh, resCh);
199             }catch(AssertionError e){
200                 System.err.println("offset=" + offset);
201                 throw e;
202             }
203             offset++;
204
205             if(expCh < 0) break;
206         }
207
208         return;
209     }
210
211 }