OSDN Git Service

130128版スキーマ対応
[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
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(
84             Class<?> klass,
85             String pmdResource,
86             String expXmlResource )
87             throws Exception{
88         InputStream pmdis =
89                 klass.getResourceAsStream(pmdResource);
90         assertNotNull(pmdis);
91         pmdis = new BufferedInputStream(pmdis);
92
93         File destFile = openTempFile();
94         OutputStream destOut;
95         destOut = new FileOutputStream(destFile);
96         destOut = new BufferedOutputStream(destOut);
97
98         Pmd2XmlConv converter = new Pmd2XmlConv();
99         converter.setInType(ModelFileType.PMD);
100         converter.setOutType(ModelFileType.XML_101009);
101         converter.setNewline("\n");
102         converter.setGenerator(null);
103
104         converter.convert(pmdis, destOut);
105
106         pmdis.close();
107         destOut.close();
108
109         assertSameFile(klass, expXmlResource, destFile);
110
111         return;
112     }
113
114     /**
115      * PMDリソースをXMLに変換した結果がXMLリソースに等しいと表明する。
116      * @param klass リソース元クラス
117      * @param pmdResource PMDリソース名
118      * @param expXmlResource XMLリソース名
119      * @throws Exception エラー
120      */
121     public static void assertPmd2Xml13(
122             Class<?> klass,
123             String pmdResource,
124             String expXmlResource )
125             throws Exception{
126         InputStream pmdis =
127                 klass.getResourceAsStream(pmdResource);
128         assertNotNull(pmdis);
129         pmdis = new BufferedInputStream(pmdis);
130
131         File destFile = openTempFile();
132         OutputStream destOut;
133         destOut = new FileOutputStream(destFile);
134         destOut = new BufferedOutputStream(destOut);
135
136         Pmd2XmlConv converter = new Pmd2XmlConv();
137         converter.setInType(ModelFileType.PMD);
138         converter.setOutType(ModelFileType.XML_130128);
139         converter.setNewline("\n");
140         converter.setGenerator(null);
141
142         converter.convert(pmdis, destOut);
143
144         pmdis.close();
145         destOut.close();
146
147         assertSameFile(klass, expXmlResource, destFile);
148
149         return;
150     }
151
152     /**
153      * リソースとファイルの内容が等しいと表明する。
154      * @param klass リソース元クラス
155      * @param resourceName リソース名
156      * @param resFile ファイル
157      * @throws IOException 入力エラー
158      */
159     public static void assertSameFile(
160             Class<?> klass,
161             String resourceName,
162             File resFile )
163             throws IOException{
164         InputStream expis =
165                 klass.getResourceAsStream(resourceName);
166         assertNotNull(expis);
167
168         InputStream resIn = new FileInputStream(resFile);
169
170         try{
171             assertSameStream(expis, resIn);
172         }finally{
173             expis.close();
174             resIn.close();
175         }
176
177         return;
178     }
179
180     /**
181      * 2つの入力ストリーム内容が等しいと表明する。
182      * @param expIn 期待する入力ストリーム
183      * @param resIn 結果入力ストリーム
184      * @throws IOException 入力エラー
185      */
186     public static void assertSameStream(InputStream expIn, InputStream resIn)
187             throws IOException{
188         InputStream expis = new BufferedInputStream(expIn);
189         InputStream resis = new BufferedInputStream(resIn);
190
191
192         for(;;){
193             int expCh = expis.read();
194             int resCh = resis.read();
195
196             assertEquals(expCh, resCh);
197
198             if(expCh < 0) break;
199         }
200
201         return;
202     }
203
204 }