content: package io.agrest.jaxrs3;
  
  import io.agrest.runtime.AgRuntime;
  import org.junit.jupiter.api.Test;
  
  import jakarta.ws.rs.core.Feature;
  import jakarta.ws.rs.core.FeatureContext;
  import java.util.HashSet;
  import java.util.Set;
  import java.util.function.Consumer;
  
  import static org.junit.jupiter.api.Assertions.*;
  import static org.mockito.ArgumentMatchers.any;
  import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.when;
  
  public class AgJaxrsFeature_FeatureProviderTest {
  
      @Test
      public void feature() {
          inRuntime(
                  AgJaxrsFeature.builder(AgRuntime.build()).feature(new LocalTestFeature()),
                  this::assertLocalTestFeatureActive);
      }
  
      @Test
      public void featureProvider() {
          inRuntime(
                  AgJaxrsFeature.builder(AgRuntime.build()).feature(new LocalTestFeatureProvider()),
                  this::assertLocalTestFeatureActive);
      }
  
      @Test
      public void autoLoadFeaturesDefault() {
          inRuntime(
                  AgJaxrsFeature.builder(AgRuntime.build()),
                  this::assertTestFeatureActive);
      }
  
      @Test
      public void doNotAutoLoadFeatures() {
          inRuntime(
                  AgJaxrsFeature.builder(AgRuntime.build()).doNotAutoLoadFeatures(),
                  this::assertTestFeatureNotActive);
      }
  
      private void assertTestFeatureActive(AgJaxrsFeature feature) {
          Set<Object> registered = extractRegisteredInJaxRS(feature);
          assertTrue(registered.contains(TestFeatureProvider.RegisteredByFeature.class));
      }
  
      private void assertTestFeatureNotActive(AgJaxrsFeature feature) {
          Set<Object> registered = extractRegisteredInJaxRS(feature);
          assertFalse(registered.contains(TestFeatureProvider.RegisteredByFeature.class), "Auto-loading was on");
      }
  
      private void assertLocalTestFeatureActive(AgJaxrsFeature feature) {
          Set<Object> registered = extractRegisteredInJaxRS(feature);
          assertTrue(registered.contains(LocalRegisteredByFeature.class), "Auto-loading was off");
      }
  
      private Set<Object> extractRegisteredInJaxRS(AgJaxrsFeature feature) {
          Set<Object> registered = new HashSet<>();
          FeatureContext fc = mock(FeatureContext.class);
          when(fc.register(any(Class.class))).then(i -> {
              registered.add(i.getArguments()[0]);
              return fc;
          });
  
          feature.configure(fc);
          return registered;
      }
  
      private void inRuntime(AgJaxrsFeatureBuilder builder, Consumer<AgJaxrsFeature> test) {
          AgJaxrsFeature feature = builder.build();
          try {
              test.accept(feature);
          } finally {
              feature.getRuntime().shutdown();
          }
      }
  
      static class LocalTestFeatureProvider implements AgFeatureProvider {
  
          @Override
          public Feature feature(AgRuntime runtime) {
              assertNotNull(runtime);
              return new LocalTestFeature();
          }
      }
  
      static class LocalTestFeature implements Feature {
  
          @Override
          public boolean configure(FeatureContext context) {
              context.register(LocalRegisteredByFeature.class);
              return false;
          }
      }
  
      static class LocalRegisteredByFeature {
  
      }
  }
  