2020
2121import java .io .File ;
2222import java .io .IOException ;
23+ import java .security .MessageDigest ;
2324import java .util .Date ;
2425import java .util .HashMap ;
2526import java .util .Map ;
@@ -171,7 +172,7 @@ public void testLastModify() throws IOException {
171172 fr .set (key , data );
172173 byte [] data2 = fr .get (key );
173174
174- File recoderFile = new File (folder , UrlSafeBase64 . encodeToString (key ));
175+ File recoderFile = new File (folder , hash (key ));
175176
176177 long m1 = recoderFile .lastModified ();
177178
@@ -183,10 +184,12 @@ public void testLastModify() throws IOException {
183184 assertEquals (3 , data2 .length );
184185 assertEquals ('8' , data2 [1 ]);
185186
187+ // 让记录文件过期,两天
186188 recoderFile .setLastModified (new Date ().getTime () - 1000 * 3600 * 48 - 2300 );
187189
188190 long m2 = recoderFile .lastModified ();
189191
192+ // 过期后,记录数据作废
190193 byte [] data3 = fr .get (key );
191194
192195 assertNull (data3 );
@@ -201,4 +204,21 @@ public void testLastModify() throws IOException {
201204 long m4 = recoderFile .lastModified ();
202205 assertTrue (m4 > m1 );
203206 }
207+
208+ // copy from FileRecorder.
209+ private static String hash (String base ) {
210+ try {
211+ MessageDigest digest = MessageDigest .getInstance ("SHA-1" );
212+ byte [] hash = digest .digest (base .getBytes ());
213+ StringBuffer hexString = new StringBuffer ();
214+
215+ for (int i = 0 ; i < hash .length ; i ++) {
216+ hexString .append (Integer .toString ((hash [i ] & 0xff ) + 0x100 , 16 ).substring (1 ));
217+ }
218+ return hexString .toString ();
219+ } catch (Exception ex ) {
220+ ex .printStackTrace ();
221+ }
222+ return null ;
223+ }
204224}
0 commit comments