OSDN Git Service

7d39f1a714393811d758b3ddbf2138a1d93b1854
[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         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         Vmd2XmlConv converter = new Vmd2XmlConv();
99         converter.setInType(MotionFileType.VMD);
100         converter.setOutType(MotionFileType.XML_110820);
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      * リソースとファイルの内容が等しいと表明する。
116      * @param klass リソース元クラス
117      * @param resourceName リソース名
118      * @param resFile ファイル
119      * @throws IOException 入力エラー
120      */
121     public static void assertSameFile(
122             Class<?> klass,
123             String resourceName,
124             File resFile )
125             throws IOException{
126         InputStream expis =
127                 klass.getResourceAsStream(resourceName);
128         assertNotNull(expis);
129
130         InputStream resIn = new FileInputStream(resFile);
131
132         try{
133             assertSameStream(expis, resIn);
134         }finally{
135             expis.close();
136             resIn.close();
137         }
138
139         return;
140     }
141
142     /**
143      * 2つの入力ストリーム内容が等しいと表明する。
144      * @param expIn 期待する入力ストリーム
145      * @param resIn 結果入力ストリーム
146      * @throws IOException 入力エラー
147      */
148     public static void assertSameStream(InputStream expIn, InputStream resIn)
149             throws IOException{
150         InputStream expis = new BufferedInputStream(expIn);
151         InputStream resis = new BufferedInputStream(resIn);
152
153
154         for(;;){
155             int expCh = expis.read();
156             int resCh = resis.read();
157
158             assertEquals(expCh, resCh);
159
160             if(expCh < 0) break;
161         }
162
163         return;
164     }
165
166 }