OSDN Git Service

テストセット整備
[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.sourceforge.mikutoga.pmd.ModelFileType;
15 import jp.sourceforge.mikutoga.pmd2xml.Pmd2XmlConv;
16
17 import static org.junit.Assert.*;
18
19 /**
20  *
21  */
22 public class CnvAssert {
23
24     /**
25      * テスト出力用テンポラリファイルの生成。
26      * テスト終了時(VM終了時)に消える。
27      * @return テンポラリファイル
28      * @throws IOException エラー
29      */
30     public static File openTempFile() throws IOException{
31         File file = File.createTempFile("pmd2xml", null);
32         file.deleteOnExit();
33         return file;
34     }
35
36     /**
37      * XMLリソースをPMDに変換した結果がPMDリソースに等しいと表明する。
38      * @param klass リソース元クラス
39      * @param xmlResource XMLリソース名
40      * @param expPmdResource PMDリソース名
41      * @throws Exception エラー
42      */
43     public static void assertXml2Pmd(
44             Class<?> klass,
45             String xmlResource,
46             String expPmdResource )
47             throws Exception{
48         InputStream xmlis =
49                 klass.getResourceAsStream(xmlResource);
50         assertNotNull(xmlis);
51         xmlis = new BufferedInputStream(xmlis);
52
53         File destFile = openTempFile();
54         OutputStream destOut;
55         destOut = new FileOutputStream(destFile);
56         destOut = new BufferedOutputStream(destOut);
57
58         Pmd2XmlConv converter = new Pmd2XmlConv();
59         converter.setInType(ModelFileType.XML_101009);
60         converter.setOutType(ModelFileType.PMD);
61         converter.setNewline("\n");
62
63         converter.convert(xmlis, destOut);
64
65         xmlis.close();
66         destOut.close();
67
68         assertSameFile(klass, expPmdResource, destFile);
69
70         return;
71     }
72
73     /**
74      * PMDリソースをXMLに変換した結果がXMLリソースに等しいと表明する。
75      * @param klass リソース元クラス
76      * @param pmdResource PMDリソース名
77      * @param expXmlResource XMLリソース名
78      * @throws Exception エラー
79      */
80     public static void assertPmd2Xml(
81             Class<?> klass,
82             String pmdResource,
83             String expXmlResource )
84             throws Exception{
85         InputStream pmdis =
86                 klass.getResourceAsStream(pmdResource);
87         assertNotNull(pmdis);
88         pmdis = new BufferedInputStream(pmdis);
89
90         File destFile = openTempFile();
91         OutputStream destOut;
92         destOut = new FileOutputStream(destFile);
93         destOut = new BufferedOutputStream(destOut);
94
95         Pmd2XmlConv converter = new Pmd2XmlConv();
96         converter.setInType(ModelFileType.PMD);
97         converter.setOutType(ModelFileType.XML_101009);
98         converter.setNewline("\n");
99         converter.setGenerator(null);
100
101         converter.convert(pmdis, destOut);
102
103         pmdis.close();
104         destOut.close();
105
106         assertSameFile(klass, expXmlResource, destFile);
107
108         return;
109     }
110
111     /**
112      * リソースとファイルの内容が等しいと表明する。
113      * @param klass リソース元クラス
114      * @param resourceName リソース名
115      * @param resFile ファイル
116      * @throws IOException 入力エラー
117      */
118     public static void assertSameFile(
119             Class<?> klass,
120             String resourceName,
121             File resFile )
122             throws IOException{
123         InputStream expis =
124                 klass.getResourceAsStream(resourceName);
125         assertNotNull(expis);
126
127         InputStream resIn = new FileInputStream(resFile);
128
129         try{
130             assertSameStream(expis, resIn);
131         }finally{
132             expis.close();
133             resIn.close();
134         }
135
136         return;
137     }
138
139     /**
140      * 2つの入力ストリーム内容が等しいと表明する。
141      * @param expIn 期待する入力ストリーム
142      * @param resIn 結果入力ストリーム
143      * @throws IOException 入力エラー
144      */
145     public static void assertSameStream(InputStream expIn, InputStream resIn)
146             throws IOException{
147         InputStream expis = new BufferedInputStream(expIn);
148         InputStream resis = new BufferedInputStream(resIn);
149
150
151         for(;;){
152             int expCh = expis.read();
153             int resCh = resis.read();
154
155             assertEquals(expCh, resCh);
156
157             if(expCh < 0) break;
158         }
159
160         return;
161     }
162
163 }