1 package org.codehaus.mojo.keytool.requests;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import org.codehaus.plexus.util.FileUtils;
20 import org.codehaus.plexus.util.IOUtil;
21 import org.junit.Assert;
22
23 import java.io.File;
24 import java.io.FileOutputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.OutputStream;
28 import java.net.URL;
29
30
31
32
33
34
35
36 public class ResourceFixtures
37 {
38
39 protected final File workingDirectory;
40
41 public ResourceFixtures( File workingDirectory )
42 {
43 this.workingDirectory = workingDirectory;
44 }
45
46 public File simpleDestKeyStoreFile( String name )
47 {
48 File file = new File( workingDirectory, name );
49
50 Assert.assertFalse( file.exists() );
51
52 return file;
53 }
54
55 public File simpleDestKeyStoreFile()
56 {
57 return simpleDestKeyStoreFile( "destkeystore" );
58 }
59
60 public File simpleKeyStore( String name, boolean copy )
61 throws IOException
62 {
63 URL keyStoreURL = getKeyStoreURL( "simple" );
64 File keyStore = new File( workingDirectory, name );
65 if ( copy )
66 {
67 copyURLToFile( keyStoreURL, keyStore );
68 }
69 return keyStore;
70 }
71
72 public File simpleKeyStore()
73 throws IOException
74 {
75 File keyStore = simpleKeyStore( true );
76 return keyStore;
77 }
78
79 public File simpleKeyStore( boolean copy )
80 throws IOException
81 {
82 File keyStore = simpleKeyStore( "keystore.jks", copy );
83 FileUtils.forceMkdir( keyStore.getParentFile() );
84 return keyStore;
85 }
86
87 public File simpleKeyStore( String name )
88 throws IOException
89 {
90 File keyStore = simpleKeyStore( name, true );
91 Assert.assertTrue( keyStore.exists() );
92 return keyStore;
93 }
94
95 public File simpleCertificateRequest()
96 throws IOException
97 {
98 URL certificateRequestURL = getCertificateRequestURL( "simple" );
99 File inFile = new File( workingDirectory, "inFile" );
100 Assert.assertFalse( inFile.exists() );
101 copyURLToFile( certificateRequestURL, inFile );
102 Assert.assertTrue( inFile.exists() );
103 return inFile;
104 }
105
106 public File simpleCertificate()
107 throws IOException
108 {
109 URL certificateURL = getCertificateURL( "simple" );
110 File inFile = new File( workingDirectory, "inFile" );
111 Assert.assertFalse( inFile.exists() );
112 copyURLToFile( certificateURL, inFile );
113 Assert.assertTrue( inFile.exists() );
114 return inFile;
115 }
116
117 public File outputFile()
118 {
119 File outputFile = new File( workingDirectory, "outputFile" );
120 Assert.assertFalse( outputFile.exists() );
121 return outputFile;
122 }
123
124 protected URL getKeyStoreURL( String prefix )
125 {
126 return getClass().getResource( "/" + prefix + "-keystore" );
127 }
128
129 protected URL getCertificateRequestURL( String prefix )
130 {
131 return getClass().getResource( "/" + prefix + "-certificate-request" );
132 }
133
134 protected URL getCertificateURL( String prefix )
135 {
136 return getClass().getResource( "/" + prefix + "-certificate" );
137 }
138
139 protected void copyURLToFile( URL url, File dest )
140 throws IOException
141 {
142 FileUtils.mkdir( dest.getParentFile().getAbsolutePath() );
143 InputStream inputStream = url.openStream();
144 try
145 {
146 OutputStream outputStream = new FileOutputStream( dest );
147 try
148 {
149 IOUtil.copy( inputStream, outputStream );
150 }
151 finally
152 {
153 IOUtil.close( outputStream );
154 }
155 }
156 finally
157 {
158 IOUtil.close( inputStream );
159 }
160 }
161 }