OSDN Git Service

1458c9bcb29582d8f8593ce9a5448bd9f3abcfa0
[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     private 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(Class<?> klass,
47                                        String xmlResource,
48                                        String expPmdResource )
49             throws Exception{
50         InputStream xmlis =
51                 klass.getResourceAsStream(xmlResource);
52         assertNotNull(xmlis);
53         xmlis = new BufferedInputStream(xmlis);
54
55         File destFile = openTempFile();
56         OutputStream destOut;
57         destOut = new FileOutputStream(destFile);
58         destOut = new BufferedOutputStream(destOut);
59 //        destOut = new DebugOutputStream(destOut);
60
61         Pmd2XmlConv converter = new Pmd2XmlConv();
62         converter.setInType(ModelFileType.XML_AUTO);
63         converter.setOutType(ModelFileType.PMD);
64         converter.setNewline("\n");
65
66         converter.convert(xmlis, destOut);
67
68         xmlis.close();
69         destOut.close();
70
71         assertSameFile(klass, expPmdResource, destFile);
72
73         return;
74     }
75
76     /**
77      * PMDリソースをXMLに変換した結果がXMLリソースに等しいと表明する。
78      * @param klass リソース元クラス
79      * @param pmdResource PMDリソース名
80      * @param expXmlResource XMLリソース名
81      * @throws Exception エラー
82      */
83     public static void assertPmd2Xml(Class<?> klass,
84                                        String pmdResource,
85                                        String expXmlResource )
86             throws Exception{
87         assertPmd2Xml(klass, pmdResource, expXmlResource,
88                       ModelFileType.XML_101009 );
89         return;
90     }
91
92     /**
93      * PMDリソースをXMLに変換した結果がXMLリソースに等しいと表明する。
94      * @param klass リソース元クラス
95      * @param pmdResource PMDリソース名
96      * @param expXmlResource XMLリソース名
97      * @throws Exception エラー
98      */
99     public static void assertPmd2Xml13(Class<?> klass,
100                                          String pmdResource,
101                                          String expXmlResource )
102             throws Exception{
103         assertPmd2Xml(klass, pmdResource, expXmlResource,
104                       ModelFileType.XML_130128 );
105         return;
106     }
107
108     /**
109      * PMDリソースをXMLに変換した結果がXMLリソースに等しいと表明する。
110      * @param klass リソース元クラス
111      * @param pmdResource PMDリソース名
112      * @param expXmlResource XMLリソース名
113      * @param type XML種別
114      * @throws Exception エラー
115      */
116     private static void assertPmd2Xml(Class<?> klass,
117                                        String pmdResource,
118                                        String expXmlResource,
119                                        ModelFileType type )
120             throws Exception{
121         InputStream pmdis =
122                 klass.getResourceAsStream(pmdResource);
123         assertNotNull(pmdis);
124         pmdis = new BufferedInputStream(pmdis);
125
126         File destFile = openTempFile();
127         OutputStream destOut;
128         destOut = new FileOutputStream(destFile);
129         destOut = new BufferedOutputStream(destOut);
130
131         Pmd2XmlConv converter = new Pmd2XmlConv();
132         converter.setInType(ModelFileType.PMD);
133         converter.setOutType(type);
134         converter.setNewline("\n");
135         converter.setGenerator(null);
136
137         converter.convert(pmdis, destOut);
138
139         pmdis.close();
140         destOut.close();
141
142         assertSameFile(klass, expXmlResource, destFile);
143
144         return;
145     }
146
147     /**
148      * リソースとファイルの内容が等しいと表明する。
149      * @param klass リソース元クラス
150      * @param resourceName リソース名
151      * @param resFile ファイル
152      * @throws IOException 入力エラー
153      */
154     public static void assertSameFile(Class<?> klass, String resourceName,
155                                         File resFile )
156             throws IOException{
157         InputStream expis =
158                 klass.getResourceAsStream(resourceName);
159         assertNotNull(expis);
160
161         InputStream resIn = new FileInputStream(resFile);
162
163         try{
164             assertSameStream(expis, resIn);
165         }finally{
166             expis.close();
167             resIn.close();
168         }
169
170         return;
171     }
172
173     /**
174      * 2つの入力ストリーム内容が等しいと表明する。
175      * @param expIn 期待する入力ストリーム
176      * @param resIn 結果入力ストリーム
177      * @throws IOException 入力エラー
178      */
179     public static void assertSameStream(InputStream expIn, InputStream resIn)
180             throws IOException{
181         InputStream expis = new BufferedInputStream(expIn);
182         InputStream resis = new BufferedInputStream(resIn);
183
184         int offset = 0;
185         for(;;){
186             int expCh = expis.read();
187             int resCh = resis.read();
188
189             try{
190                 assertEquals(expCh, resCh);
191             }catch(AssertionError e){
192                 System.err.println("unmatch stream:offset=" + offset);
193                 throw e;
194             }
195             offset++;
196
197             if(expCh < 0) break;
198             if(resCh < 0) break;
199         }
200
201         return;
202     }
203
204 }