package se.jiderhamn.classloader.leak.prevention.cleanup;

import java.security.NoSuchAlgorithmException;
import java.security.Provider;

/**
 * Test that the leak caused by {@link javax.crypto.JceSecurity} is cleared.
 * Thanks to Paul Kiman for the report.
 * @author Mattias Jiderhamn
 */
public class JceSecurityCleanUpTest extends ClassLoaderPreMortemCleanUpTestBase<JceSecurityCleanUp> {
  @Override
  protected void triggerLeak() throws Exception {
    try {
      // Alternative classes: KeyAgreement, KeyGenerator, ExemptionMechanism
      MyProvider myProvider = new MyProvider("foo", 1.0, "bar");
      javax.crypto.Mac.getInstance("baz", myProvider);
    }
    catch (SecurityException e) { // CS:IGNORE
    }
    catch (NoSuchAlgorithmException e) {
      // Custom providers seem to work different in Java 11, this still triggers the leak, even if we get this exception
    }
  }
  
  /** Custom {@link Provider}, to be put in {@link javax.crypto.JceSecurity} caches */
  public static class MyProvider extends Provider {
    public MyProvider(String name, double version, String info) {
      super(name, version, info);
    }

    @Override
    public synchronized Service getService(String type, String algorithm) {
      return new Service(this, "type", "algorithm", "className", null, null);
    }
  }
}
@@ -695,192 +695,112 @@
 ion 
-e) %7B // CS:IGNORE%0A    %7D%0A    catch (NoSuchAlgorithmException e) %7B%0A      // Custom providers seem to work different in Java 11, this still triggers the leak, even if we get this exceptio
+%7C NoSuchAlgorithmException e) %7B // CS:IGNORE%0A      // Leak is triggered despite an exception being throw
 n%0A  

Move exceptions in one catch expression