OSDN Git Service

cfc9a5fdc4aef557667103c81bad4af5d0ea81be
[mikutoga/Vmd2XML.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.vmd2xml.MotionFileType;
15 import jp.sfjp.mikutoga.vmd2xml.Vmd2XmlConv;
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("vmd2xml", null);
35         file.deleteOnExit();
36         return file;
37     }
38
39     /**
40      * XMLリソースをVMDに変換した結果がVMDリソースに等しいと表明する。
41      * @param klass リソース元クラス
42      * @param xmlResource XMLリソース名
43      * @param expVmdResource VMDリソース名
44      * @throws Exception エラー
45      */
46     public static void assertXml2Vmd(
47             Class<?> klass,
48             String xmlResource,
49             String expVmdResource )
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         Vmd2XmlConv converter = new Vmd2XmlConv();
62         converter.setInType(MotionFileType.XML_110820);
63         converter.setOutType(MotionFileType.VMD);
64         converter.setNewline("\n");
65
66         converter.convert(xmlis, destOut);
67
68         xmlis.close();
69         destOut.close();
70
71         assertSameFile(klass, expVmdResource, 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 assertVmd2Xml(
84             Class<?> klass,
85             String pmdResource,
86             String expXmlResource )
87             throws Exception{
88         Vmd2XmlConv converter = new Vmd2XmlConv();
89         converter.setInType(MotionFileType.VMD);
90         converter.setOutType(MotionFileType.XML_110820);
91         converter.setNewline("\n");
92         converter.setGenerator(null);
93
94         assertVmd2Xml(klass, pmdResource, expXmlResource, converter);
95
96         return;
97     }
98
99     /**
100      * VMDリソースをXMLに変換した結果がXMLリソースに等しいと表明する。
101      * @param klass リソース元クラス
102      * @param vmdResource PMDリソース名
103      * @param expXmlResource XMLリソース名
104      * @param converter コンバータ
105      * @throws Exception エラー
106      */
107     public static void assertVmd2Xml(
108             Class<?> klass,
109             String vmdResource,
110             String expXmlResource,
111             Vmd2XmlConv converter )
112             throws Exception{
113         InputStream vmdis =
114                 klass.getResourceAsStream(vmdResource);
115         assertNotNull(vmdis);
116         vmdis = new BufferedInputStream(vmdis);
117
118         File destFile = openTempFile();
119         OutputStream destOut;
120         destOut = new FileOutputStream(destFile);
121         destOut = new BufferedOutputStream(destOut);
122
123         converter.convert(vmdis, destOut);
124
125         vmdis.close();
126         destOut.close();
127
128         assertSameFile(klass, expXmlResource, destFile);
129
130         return;
131     }
132
133     /**
134      * リソースとファイルの内容が等しいと表明する。
135      * @param klass リソース元クラス
136      * @param resourceName リソース名
137      * @param resFile ファイル
138      * @throws IOException 入力エラー
139      */
140     public static void assertSameFile(
141             Class<?> klass,
142             String resourceName,
143             File resFile )
144             throws IOException{
145         InputStream expis =
146                 klass.getResourceAsStream(resourceName);
147         assertNotNull(expis);
148
149         InputStream resIn = new FileInputStream(resFile);
150
151         try{
152             assertSameStream(expis, resIn);
153         }finally{
154             expis.close();
155             resIn.close();
156         }
157
158         return;
159     }
160
161     /**
162      * 2つの入力ストリーム内容が等しいと表明する。
163      * @param expIn 期待する入力ストリーム
164      * @param resIn 結果入力ストリーム
165      * @throws IOException 入力エラー
166      */
167     public static void assertSameStream(InputStream expIn, InputStream resIn)
168             throws IOException{
169         InputStream expis = new BufferedInputStream(expIn);
170         InputStream resis = new BufferedInputStream(resIn);
171
172
173         for(;;){
174             int expCh = expis.read();
175             int resCh = resis.read();
176
177             assertEquals(expCh, resCh);
178
179             if(expCh < 0) break;
180         }
181
182         return;
183     }
184
185 }