1
1
package jp .kusumotolab .kgenprog .project .factory ;
2
2
3
+ import java .io .IOException ;
3
4
import java .io .InputStream ;
4
5
import java .nio .file .Files ;
5
6
import java .nio .file .Path ;
6
7
import java .nio .file .StandardCopyOption ;
7
- import java .util .Arrays ;
8
+ import java .util .Collections ;
8
9
import java .util .EnumMap ;
9
10
import java .util .List ;
11
+ import java .util .MissingResourceException ;
10
12
import jp .kusumotolab .kgenprog .CUILauncher ;
11
13
import jp .kusumotolab .kgenprog .project .ClassPath ;
12
14
@@ -25,26 +27,26 @@ public enum JUnitVersion {
25
27
new EnumMap <>(JUnitVersion .class );
26
28
27
29
static {
30
+ setupResourceJar (JUNIT3_DIR , JUNIT3_JUNIT , JUnitVersion .JUNIT3 );
31
+ setupResourceJar (JUNIT4_DIR , JUNIT4_JUNIT , JUnitVersion .JUNIT4 );
32
+ }
33
+
34
+ private static void setupResourceJar (final String dir , final String jar ,
35
+ final JUnitVersion version ) {
36
+ final String jarPath = dir + jar ;
37
+ final ClassLoader classLoader = CUILauncher .class .getClassLoader ();
38
+ final InputStream is = classLoader .getResourceAsStream (jarPath );
39
+ if (null == is ) {
40
+ throw new MissingResourceException ("Missing runtime junit library: " + jarPath , jarPath , "" );
41
+ }
28
42
43
+ final Path tempPath = getTempDirectory ().resolve (jar );
29
44
try {
30
- final ClassLoader classLoader = CUILauncher .class .getClassLoader ();
31
- final InputStream junit3JInputStream =
32
- classLoader .getResourceAsStream (JUNIT3_DIR + JUNIT3_JUNIT );
33
- final InputStream junit4JInputStream =
34
- classLoader .getResourceAsStream (JUNIT4_DIR + JUNIT4_JUNIT );
35
-
36
- final Path systemTempPath = getTempDirectory ();
37
- final Path junit3JPath = systemTempPath .resolve (JUNIT3_JUNIT );
38
- final Path junit4JPath = systemTempPath .resolve (JUNIT4_JUNIT );
39
-
40
- Files .copy (junit3JInputStream , junit3JPath , StandardCopyOption .REPLACE_EXISTING );
41
- Files .copy (junit4JInputStream , junit4JPath , StandardCopyOption .REPLACE_EXISTING );
42
-
43
- libraries .put (JUnitVersion .JUNIT3 , Arrays .asList (new ClassPath (junit3JPath )));
44
- libraries .put (JUnitVersion .JUNIT4 , Arrays .asList (new ClassPath (junit4JPath )));
45
- } catch (Exception e ) {
46
- e .printStackTrace ();
45
+ Files .copy (is , tempPath , StandardCopyOption .REPLACE_EXISTING );
46
+ } catch (final IOException e ) {
47
+ throw new RuntimeException (e );
47
48
}
49
+ libraries .put (version , Collections .singletonList (new ClassPath (tempPath )));
48
50
}
49
51
50
52
// TODO 一時dirの責務をひとまずこのクラスに任せたが,巨大になるなら別クラスに切った方がよさそう.
@@ -55,8 +57,8 @@ public static Path getTempDirectory() {
55
57
if (null == tempDir ) {
56
58
tempDir = Files .createTempDirectory ("kgp-" );
57
59
}
58
- } catch (Exception e ) {
59
- e . printStackTrace ( );
60
+ } catch (final IOException e ) {
61
+ throw new RuntimeException ( e );
60
62
}
61
63
return tempDir ;
62
64
}
0 commit comments