OpearFile implements Opear { static Result<String> getHashCacheKey(final @NotNull String path) { try (FileInputStream is = new FileInputStream(path)) { MessageDigest digest = MessageDigest.getInstance("SHA-256"); final byte[] buf = new byte[1024]; int nread; while ((nread = is.read(buf)) != -1) { digest.update(buf, 0, nread); } final byte[] mdbytes = digest.digest(); return Result.success(Base64.getUrlEncoder().withoutPadding().encodeToString(mdbytes)); } catch (Exception e) { return Result.failure(e); } } OpearFile(final File cacheDir, final OpearMetadata metadata); @Override URL getDefaultPlan(); @Override Result<URL> getSpecificPlan(final @NotNull String planName); @Override ClassLoader getPlanClassLoader(final @NotNull ClassLoader parent); static Result<OpearFile> fromDirectory(final @NotNull File directory); static Result<OpearFile> fromJar(final @NotNull JarFile jarFile, final @NotNull File cacheBaseDir); static final Attributes.Name NAME_BUNDLE_SYMBOLICNAME; static final Attributes.Name NAME_BUNDLE_VERSION; static final Attributes.Name NAME_CLASS_PATH; static final Attributes.Name NAME_OAKPAL_VERSION; static final Attributes.Name NAME_OAKPAL_PLAN; }
@Test public void testGetHashCacheKey() throws Exception { Result<String> cacheKeyDeletedResult = OpearFile.getHashCacheKey("/no/such/path"); assertTrue("cacheKey is failure", cacheKeyDeletedResult.isFailure()); assertTrue("cacheKey failure is FileNotFoundException", cacheKeyDeletedResult.findCause(FileNotFoundException.class).isPresent()); buildDeepTestJar(); Result<String> cacheKeyResult = OpearFile.getHashCacheKey(deepTestTarget.getPath()); assertTrue("cacheKey is success", cacheKeyResult.isSuccess()); String cacheKey = cacheKeyResult.getOrDefault(""); assertEquals("cacheKey should be 43 characters long: " + cacheKey, 43, cacheKey.length()); final String pattern = "^[0-9A-Za-z_-]*$"; assertTrue(String.format("cacheKey %s matches regex %s", cacheKey, pattern), cacheKey.matches(pattern)); }