@Test
  public void testUpdate3() throws SchedulerException {
    MockScheduler mockScheduler =
        ReflectionTestUtil.getFieldValue(_quartzSchedulerEngine, "_memoryScheduler");

    String jobName = _TEST_JOB_NAME_PREFIX + "memory";

    mockScheduler.addJob(jobName, _MEMORY_TEST_GROUP_NAME, StorageType.MEMORY, null);

    SchedulerResponse schedulerResponse =
        _quartzSchedulerEngine.getScheduledJob(
            jobName, _MEMORY_TEST_GROUP_NAME, StorageType.MEMORY);

    Assert.assertNull(schedulerResponse.getTrigger());

    Trigger trigger =
        _quartzTriggerFactory.createTrigger(
            jobName, _MEMORY_TEST_GROUP_NAME, new Date(), null, _DEFAULT_INTERVAL, TimeUnit.SECOND);

    _quartzSchedulerEngine.update(trigger, StorageType.MEMORY);

    schedulerResponse =
        _quartzSchedulerEngine.getScheduledJob(
            _TEST_JOB_NAME_0, _MEMORY_TEST_GROUP_NAME, StorageType.MEMORY);

    Assert.assertNotNull(schedulerResponse.getTrigger());
  }
  @Test
  public void testMigratedPortalKeys() throws Exception {
    String[][] originalMigratedPortalKeys =
        ReflectionTestUtil.getFieldValue(VerifyProperties.class, "_MIGRATED_PORTAL_KEYS");

    String migratedPortalKey = getFirstPortalPropertyKey();

    ReflectionTestUtil.setFieldValue(
        VerifyProperties.class,
        "_MIGRATED_PORTAL_KEYS",
        new String[][] {new String[] {migratedPortalKey, migratedPortalKey}});

    try (CaptureAppender captureAppender =
        Log4JLoggerTestUtil.configureLog4JLogger(VerifyProperties.class.getName(), Level.ERROR)) {

      doVerify();

      List<LoggingEvent> loggingEvents = captureAppender.getLoggingEvents();

      Assert.assertEquals(1, loggingEvents.size());

      LoggingEvent loggingEvent = loggingEvents.get(0);

      Assert.assertEquals(
          "Portal property \""
              + migratedPortalKey
              + "\" was migrated to the system property \""
              + migratedPortalKey
              + "\"",
          loggingEvent.getMessage());
    } finally {
      ReflectionTestUtil.setFieldValue(
          VerifyProperties.class, "_MIGRATED_PORTAL_KEYS", originalMigratedPortalKeys);
    }
  }
  @Test
  public void testInitJobState() throws Exception {
    List<SchedulerResponse> schedulerResponses =
        _quartzSchedulerEngine.getScheduledJobs(_PERSISTED_TEST_GROUP_NAME, StorageType.PERSISTED);

    Assert.assertEquals(_DEFAULT_JOB_NUMBER, schedulerResponses.size());

    MockScheduler mockScheduler =
        ReflectionTestUtil.getFieldValue(_quartzSchedulerEngine, "_persistedScheduler");

    mockScheduler.addJob(
        _TEST_JOB_NAME_PREFIX + "persisted",
        _PERSISTED_TEST_GROUP_NAME,
        StorageType.PERSISTED,
        null);

    schedulerResponses =
        _quartzSchedulerEngine.getScheduledJobs(_PERSISTED_TEST_GROUP_NAME, StorageType.PERSISTED);

    Assert.assertEquals(_DEFAULT_JOB_NUMBER + 1, schedulerResponses.size());

    _quartzSchedulerEngine.initJobState();

    schedulerResponses =
        _quartzSchedulerEngine.getScheduledJobs(_PERSISTED_TEST_GROUP_NAME, StorageType.PERSISTED);

    Assert.assertEquals(_DEFAULT_JOB_NUMBER, schedulerResponses.size());
  }
  @Test
  public void testObsoletePortalKeys() throws Exception {
    String[] originalObsoletePortalKeys =
        ReflectionTestUtil.getFieldValue(VerifyProperties.class, "_OBSOLETE_PORTAL_KEYS");

    String obsoletePortalKey = getFirstPortalPropertyKey();

    ReflectionTestUtil.setFieldValue(
        VerifyProperties.class, "_OBSOLETE_PORTAL_KEYS", new String[] {obsoletePortalKey});

    try (CaptureAppender captureAppender =
        Log4JLoggerTestUtil.configureLog4JLogger(VerifyProperties.class.getName(), Level.ERROR)) {

      doVerify();

      List<LoggingEvent> loggingEvents = captureAppender.getLoggingEvents();

      Assert.assertEquals(1, loggingEvents.size());

      LoggingEvent loggingEvent = loggingEvents.get(0);

      Assert.assertEquals(
          "Portal property \"" + obsoletePortalKey + "\" is obsolete", loggingEvent.getMessage());
    } finally {
      ReflectionTestUtil.setFieldValue(
          VerifyProperties.class, "_OBSOLETE_PORTAL_KEYS", originalObsoletePortalKeys);
    }
  }
  @Test
  public void testRenamedSystemKeys() throws Exception {
    String[][] originalRenamedSystemKeys =
        ReflectionTestUtil.getFieldValue(VerifyProperties.class, "_RENAMED_SYSTEM_KEYS");

    String renamedSystemKey = getFirstSystemPropertyKey();

    ReflectionTestUtil.setFieldValue(
        VerifyProperties.class,
        "_RENAMED_SYSTEM_KEYS",
        new String[][] {new String[] {renamedSystemKey, renamedSystemKey}});

    try (CaptureAppender captureAppender =
        Log4JLoggerTestUtil.configureLog4JLogger(VerifyProperties.class.getName(), Level.ERROR)) {

      doVerify();

      List<LoggingEvent> loggingEvents = captureAppender.getLoggingEvents();

      Assert.assertEquals(1, loggingEvents.size());

      LoggingEvent loggingEvent = loggingEvents.get(0);

      Assert.assertEquals(
          "System property \""
              + renamedSystemKey
              + "\" was renamed to \""
              + renamedSystemKey
              + "\"",
          loggingEvent.getMessage());
    } finally {
      ReflectionTestUtil.setFieldValue(
          VerifyProperties.class, "_RENAMED_SYSTEM_KEYS", originalRenamedSystemKeys);
    }
  }
예제 #6
0
  private static MPI _getMPIImpl() {
    MPI mpiImpl = ReflectionTestUtil.getFieldValue(MPIHelperUtil.class, "_mpiImpl");

    Assert.assertNotNull(mpiImpl);

    return mpiImpl;
  }
  @Test
  public void testAutoRemoveFileInputStream() throws Exception {
    File tempFile = new File("tempFile");

    Assert.assertTrue(tempFile.createNewFile());

    AutoDeleteFileInputStream autoRemoveFileInputStream = new AutoDeleteFileInputStream(tempFile);

    final AtomicInteger checkDeleteCount = new AtomicInteger();

    SwappableSecurityManager swappableSecurityManager =
        new SwappableSecurityManager() {

          @Override
          public void checkDelete(String file) {
            if (file.contains("tempFile")) {
              checkDeleteCount.getAndIncrement();
            }
          }
        };

    try (SwappableSecurityManager autoCloseSwappableSecurityManager = swappableSecurityManager) {

      autoCloseSwappableSecurityManager.install();

      autoRemoveFileInputStream.close();
    }

    Assert.assertFalse(tempFile.exists());
    Assert.assertEquals(1, checkDeleteCount.get());

    checkDeleteCount.set(0);

    Assert.assertTrue(tempFile.createNewFile());

    autoRemoveFileInputStream = new AutoDeleteFileInputStream(tempFile);

    Assert.assertTrue(tempFile.delete());

    try (SwappableSecurityManager autoCloseSwappableSecurityManager = swappableSecurityManager) {

      autoCloseSwappableSecurityManager.install();

      autoRemoveFileInputStream.close();
    }

    Assert.assertFalse(tempFile.exists());
    Assert.assertEquals(2, checkDeleteCount.get());

    Set<String> files =
        ReflectionTestUtil.getFieldValue(Class.forName("java.io.DeleteOnExitHook"), "files");

    Assert.assertTrue(files.contains(tempFile.getPath()));
  }
  protected SocketImpl swapSocketImpl(Socket socket, SocketImpl socketImpl) {
    SocketImpl oldSocketImpl = ReflectionTestUtil.getFieldValue(socket, "impl");

    if (socketImpl == null) {
      Socket unbindSocket = new Socket();

      socketImpl = ReflectionTestUtil.getFieldValue(unbindSocket, "impl");

      ReflectionTestUtil.setFieldValue(
          socketImpl,
          "cmdsock",
          new Socket() {

            @Override
            public synchronized void close() throws IOException {
              throw new IOException();
            }
          });
    }

    ReflectionTestUtil.setFieldValue(socket, "impl", socketImpl);

    return oldSocketImpl;
  }
  @Before
  public void setUp() throws Exception {
    _assetCategoryPersistence = AssetCategoryUtil.getPersistence();

    _sessionFactoryInvocationHandler =
        new SessionFactoryInvocationHandler(
            ReflectionTestUtil.getFieldValue(_assetCategoryPersistence, "_sessionFactory"));

    ReflectionTestUtil.setFieldValue(
        _assetCategoryPersistence,
        "_sessionFactory",
        ProxyUtil.newProxyInstance(
            SessionFactory.class.getClassLoader(),
            new Class<?>[] {SessionFactory.class},
            _sessionFactoryInvocationHandler));

    _assetCategoryPersistence.setRebuildTreeEnabled(false);

    _nestedSetsTreeManager =
        new PersistenceNestedSetsTreeManager<AssetCategory>(
            (BasePersistenceImpl<?>) _assetCategoryPersistence,
            "AssetCategory",
            "AssetCategory",
            AssetCategoryImpl.class,
            "categoryId",
            "groupId",
            "leftCategoryId",
            "rightCategoryId");

    _group = GroupTestUtil.addGroup();

    _assetVocabulary = AssetTestUtil.addVocabulary(_group.getGroupId());

    _assetCategories = new AssetCategory[9];

    for (int i = 0; i < 9; i++) {
      _assetCategories[i] =
          AssetTestUtil.addCategory(_group.getGroupId(), _assetVocabulary.getVocabularyId());
    }

    PropsValues.SPRING_HIBERNATE_SESSION_DELEGATED = false;
  }
  @Test
  public void testTransferResponse() throws Exception {

    // Exception

    MockRegistrationReference mockRegistrationReference =
        new MockRegistrationReference(new MockIntraband());

    HttpClientSPIAgent httpClientSPIAgent =
        new HttpClientSPIAgent(_spiConfiguration, mockRegistrationReference);

    MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_REQUEST, new SPIAgentRequest(_mockHttpServletRequest));

    RecordSPIAgentResponse recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    Exception exception = new Exception();

    httpClientSPIAgent.transferResponse(
        mockHttpServletRequest,
        new BufferCacheServletResponse(new MockHttpServletResponse()),
        exception);

    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertSame(exception, recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertNull(recordSPIAgentResponse._request);
    Assert.assertNull(recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);

    // Response

    SPIAgentRequest spiAgentRequest = new SPIAgentRequest(_mockHttpServletRequest);

    File tempFile = File.createTempFile(HttpClientSPIAgentTest.class.getName(), null);

    Assert.assertTrue(tempFile.exists());

    spiAgentRequest.requestBodyFile = tempFile;

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_REQUEST, spiAgentRequest);

    recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    BufferCacheServletResponse bufferCacheServletResponse =
        new BufferCacheServletResponse(new MockHttpServletResponse());

    httpClientSPIAgent.transferResponse(mockHttpServletRequest, bufferCacheServletResponse, null);

    Assert.assertFalse(tempFile.exists());
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_REQUEST));
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_RESPONSE));
    Assert.assertNull(recordSPIAgentResponse.exception);
    Assert.assertNull(mockHttpServletRequest.getAttribute(WebKeys.SPI_AGENT_ORIGINAL_RESPONSE));
    Assert.assertEquals(8, mockHttpServletResponse.getContentLength());
    Assert.assertSame(mockHttpServletRequest, recordSPIAgentResponse._request);
    Assert.assertSame(
        bufferCacheServletResponse, recordSPIAgentResponse._bufferCacheServletResponse);
    Assert.assertSame(mockRegistrationReference, recordSPIAgentResponse._registrationReference);
    Assert.assertSame(
        mockHttpServletResponse.getOutputStream(), recordSPIAgentResponse._outputStream);

    // Undeletable request body file

    spiAgentRequest = new SPIAgentRequest(_mockHttpServletRequest);

    tempFile = File.createTempFile(HttpClientSPIAgentTest.class.getName(), null);

    Assert.assertTrue(tempFile.exists());

    spiAgentRequest.requestBodyFile = tempFile;

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_REQUEST, spiAgentRequest);

    recordSPIAgentResponse = new RecordSPIAgentResponse();

    mockHttpServletRequest.setAttribute(WebKeys.SPI_AGENT_RESPONSE, recordSPIAgentResponse);

    mockHttpServletResponse = new MockHttpServletResponse();

    mockHttpServletRequest.setAttribute(
        WebKeys.SPI_AGENT_ORIGINAL_RESPONSE, mockHttpServletResponse);

    bufferCacheServletResponse = new BufferCacheServletResponse(new MockHttpServletResponse());

    Assert.assertTrue(tempFile.delete());

    httpClientSPIAgent.transferResponse(mockHttpServletRequest, bufferCacheServletResponse, null);

    Set<String> files =
        ReflectionTestUtil.getFieldValue(Class.forName("java.io.DeleteOnExitHook"), "files");

    Assert.assertTrue(files.contains(tempFile.getPath()));
  }
예제 #11
0
  @AdviseWith(adviceClasses = {PropsUtilAdvice.class})
  @Test
  public void testSPIRegistration() {
    try (CaptureHandler captureHandler =
        JDKLoggerTestUtil.configureJDKLogger(MPIHelperUtil.class.getName(), Level.WARNING)) {

      // Mismatch MPI, with log

      List<LogRecord> logRecords = captureHandler.getLogRecords();

      MockSPI mockSPI1 = new MockSPI();

      mockSPI1.mpi = new MockMPI();

      Assert.assertFalse(MPIHelperUtil.registerSPI(mockSPI1));

      LogRecord logRecord = logRecords.get(0);

      Assert.assertEquals(
          "Not registering SPI "
              + mockSPI1
              + " with foreign MPI "
              + mockSPI1.mpi
              + " versus "
              + MPIHelperUtil.getMPI(),
          logRecord.getMessage());

      // Mismatch MPI, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      Assert.assertFalse(MPIHelperUtil.registerSPI(mockSPI1));
      Assert.assertTrue(logRecords.isEmpty());

      // Null SPI provider name

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiProviderName = null;

      try {
        MPIHelperUtil.registerSPI(mockSPI1);

        Assert.fail();
      } catch (NullPointerException npe) {
      }

      // No such SPI provider, with log

      logRecords = captureHandler.resetLogLevel(Level.WARNING);

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiProviderName = "name1";

      Assert.assertFalse(MPIHelperUtil.registerSPI(mockSPI1));
      Assert.assertEquals(1, logRecords.size());

      logRecord = logRecords.get(0);

      Assert.assertEquals(
          "Not registering SPI "
              + mockSPI1
              + " with unknown SPI provider "
              + mockSPI1.spiProviderName,
          logRecord.getMessage());

      // No such SPI provider, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiProviderName = "name1";

      Assert.assertFalse(MPIHelperUtil.registerSPI(mockSPI1));
      Assert.assertTrue(logRecords.isEmpty());

      // Successful register, with log

      String name = "name1";

      MockSPIProvider mockSPIProvider = new MockSPIProvider(name);

      Assert.assertTrue(MPIHelperUtil.registerSPIProvider(mockSPIProvider));

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiConfiguration =
          new SPIConfiguration(
              "testId1", "", 8081, "", new String[0], new String[] {"servletContextName1"}, null);
      mockSPI1.spiProviderName = name;

      logRecords = captureHandler.resetLogLevel(Level.INFO);

      Assert.assertTrue(MPIHelperUtil.registerSPI(mockSPI1));
      Assert.assertEquals(1, logRecords.size());

      logRecord = logRecords.get(0);

      Assert.assertEquals("Registered SPI " + mockSPI1, logRecord.getMessage());

      // Successful register, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      MessagingConfigurator messagingConfigurator =
          new AbstractMessagingConfigurator() {

            @Override
            public void connect() {}

            @Override
            public void disconnect() {}

            @Override
            protected MessageBus getMessageBus() {
              return null;
            }

            @Override
            protected ClassLoader getOperatingClassloader() {
              return null;
            }
          };

      MessagingConfiguratorRegistry.registerMessagingConfigurator(
          "servletContextName2", messagingConfigurator);

      MockSPI mockSPI2 = new MockSPI();

      mockSPI2.mpi = MPIHelperUtil.getMPI();
      mockSPI2.spiConfiguration =
          new SPIConfiguration(
              "testId2", "", 8082, "", new String[0], new String[] {"servletContextName2"}, null);
      mockSPI2.spiProviderName = name;

      Assert.assertTrue(MPIHelperUtil.registerSPI(mockSPI2));
      Assert.assertTrue(logRecords.isEmpty());

      // Duplicate register, with log

      logRecords = captureHandler.resetLogLevel(Level.WARNING);

      Assert.assertFalse(MPIHelperUtil.registerSPI(mockSPI1));
      Assert.assertEquals(1, logRecords.size());

      logRecord = logRecords.get(0);

      Assert.assertEquals(
          "Not registering SPI " + mockSPI1 + " because it duplicates " + mockSPI1,
          logRecord.getMessage());

      // Duplicate register, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      Assert.assertFalse(MPIHelperUtil.registerSPI(mockSPI2));
      Assert.assertTrue(logRecords.isEmpty());

      // Bad SPI impl

      mockSPI1 = new MockSPI();

      mockSPI1.failOnGetConfiguration = true;
      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiProviderName = name;

      try {
        MPIHelperUtil.registerSPI(mockSPI1);

        Assert.fail();
      } catch (RuntimeException re) {
        Throwable throwable = re.getCause();

        Assert.assertSame(RemoteException.class, throwable.getClass());
      }

      // Get SPI, no such SPI provider

      Assert.assertNull(MPIHelperUtil.getSPI("name2", "testId1"));

      // Get SPI, exists

      Assert.assertNotNull(MPIHelperUtil.getSPI(name, "testId1"));

      // Get SPI, does not exist

      Assert.assertNull(MPIHelperUtil.getSPI(name, "testId3"));

      // Get SPIs

      logRecords = captureHandler.resetLogLevel(Level.SEVERE);

      mockSPI2.failOnIsAlive = true;

      List<SPI> spis = MPIHelperUtil.getSPIs();

      Assert.assertEquals(1, spis.size());

      mockSPI1 = (MockSPI) spis.get(0);

      Assert.assertEquals(name, mockSPI1.spiProviderName);
      Assert.assertEquals(1, logRecords.size());

      logRecord = logRecords.get(0);

      Throwable throwable = logRecord.getThrown();

      Assert.assertSame(RemoteException.class, throwable.getClass());

      // Get SPIs by SPI provider, exists

      mockSPI2 = new MockSPI();

      mockSPI2.mpi = MPIHelperUtil.getMPI();
      mockSPI2.spiConfiguration =
          new SPIConfiguration("testId2", "", 8082, "", new String[0], new String[0], null);
      mockSPI2.spiProviderName = name;

      Assert.assertTrue(MPIHelperUtil.registerSPI(mockSPI2));

      mockSPI2.failOnIsAlive = true;

      spis = MPIHelperUtil.getSPIs(name);

      Assert.assertEquals(1, spis.size());

      mockSPI1 = (MockSPI) spis.get(0);

      Assert.assertEquals(name, mockSPI1.spiProviderName);

      // Get SPIs by SPI provider, does not exist

      spis = MPIHelperUtil.getSPIs("name2");

      Assert.assertTrue(spis.isEmpty());

      // Unregister thread local shortcut, with log

      mockSPI1 = new MockSPI();

      mockSPI1.spiConfiguration =
          new SPIConfiguration(null, null, 0, null, null, new String[0], null);

      ThreadLocal<SPI> unregisteringSPIThreadLocal =
          ReflectionTestUtil.getFieldValue(MPIHelperUtil.class, "_unregisteringSPIThreadLocal");

      unregisteringSPIThreadLocal.set(mockSPI1);

      try {
        Assert.assertTrue(MPIHelperUtil.unregisterSPI(mockSPI1));
      } finally {
        unregisteringSPIThreadLocal.remove();
      }

      // Unregister MPI mismatch, with log

      logRecords = captureHandler.resetLogLevel(Level.WARNING);

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = new MockMPI();

      Assert.assertFalse(MPIHelperUtil.unregisterSPI(mockSPI1));
      Assert.assertEquals(1, logRecords.size());

      logRecord = logRecords.get(0);

      Assert.assertEquals(
          "Not unregistering SPI "
              + mockSPI1
              + " with foreign MPI "
              + mockSPI1.mpi
              + " versus "
              + MPIHelperUtil.getMPI(),
          logRecord.getMessage());

      // Unregister MPI mismatch, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      Assert.assertFalse(MPIHelperUtil.unregisterSPI(mockSPI1));
      Assert.assertTrue(logRecords.isEmpty());

      // Unregister no such SPI provider, with log

      logRecords = captureHandler.resetLogLevel(Level.WARNING);

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiProviderName = "name2";

      Assert.assertFalse(MPIHelperUtil.unregisterSPI(mockSPI1));
      Assert.assertEquals(1, logRecords.size());

      logRecord = logRecords.get(0);

      Assert.assertEquals(
          "Not unregistering SPI "
              + mockSPI1
              + " with unknown SPI provider "
              + mockSPI1.spiProviderName,
          logRecord.getMessage());

      // Unregister no such SPI provider, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiProviderName = "name2";

      Assert.assertFalse(MPIHelperUtil.unregisterSPI(mockSPI1));
      Assert.assertTrue(logRecords.isEmpty());

      // Unregister no such SPI, with log

      logRecords = captureHandler.resetLogLevel(Level.WARNING);

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiConfiguration =
          new SPIConfiguration("testId3", "", 8083, "", new String[0], new String[0], null);
      mockSPI1.spiProviderName = name;

      Assert.assertFalse(MPIHelperUtil.unregisterSPI(mockSPI1));
      Assert.assertEquals(1, logRecords.size());

      logRecord = logRecords.get(0);

      Assert.assertEquals("Not unregistering unregistered SPI " + mockSPI1, logRecord.getMessage());

      // Unregister no such SPI, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      mockSPI1 = new MockSPI();

      mockSPI1.mpi = MPIHelperUtil.getMPI();
      mockSPI1.spiConfiguration =
          new SPIConfiguration("testId3", "", 8083, "", new String[0], new String[0], null);
      mockSPI1.spiProviderName = name;

      Assert.assertFalse(MPIHelperUtil.unregisterSPI(mockSPI1));
      Assert.assertTrue(logRecords.isEmpty());

      // Unregister success, with log

      mockSPI1 = (MockSPI) MPIHelperUtil.getSPI(name, "testId1");

      logRecords = captureHandler.resetLogLevel(Level.INFO);

      Assert.assertTrue(MPIHelperUtil.unregisterSPI(mockSPI1));
      Assert.assertEquals(1, logRecords.size());

      logRecord = logRecords.get(0);

      Assert.assertEquals("Unregistered SPI " + mockSPI1, logRecord.getMessage());

      // Unregister success, without log

      Assert.assertTrue(MPIHelperUtil.registerSPI(mockSPI1));

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      Assert.assertTrue(MPIHelperUtil.unregisterSPI(mockSPI1));
      Assert.assertTrue(logRecords.isEmpty());

      // Unregister fail on getting configuration

      mockSPI1.failOnGetConfiguration = true;

      try {
        MPIHelperUtil.unregisterSPI(mockSPI1);

        Assert.fail();
      } catch (RuntimeException re) {
        throwable = re.getCause();

        Assert.assertSame(RemoteException.class, throwable.getClass());
      }
    }
  }
예제 #12
0
  @AdviseWith(adviceClasses = {PropsUtilAdvice.class})
  @Test
  public void testSPIProviderRegistration() throws RemoteException {

    // Register SPI provider, null name

    MockSPIProvider mockSPIProvider1 = new MockSPIProvider(null);

    try {
      MPIHelperUtil.registerSPIProvider(mockSPIProvider1);

      Assert.fail();
    } catch (NullPointerException npe) {
    }

    try (CaptureHandler captureHandler =
        JDKLoggerTestUtil.configureJDKLogger(MPIHelperUtil.class.getName(), Level.INFO)) {

      // Register SPI provider, with log

      List<LogRecord> logRecords = captureHandler.getLogRecords();

      String name1 = "spiProvider1";

      mockSPIProvider1 = new MockSPIProvider(name1);

      Assert.assertTrue(MPIHelperUtil.registerSPIProvider(mockSPIProvider1));
      Assert.assertEquals(1, logRecords.size());

      LogRecord logRecord1 = logRecords.get(0);

      Assert.assertEquals("Registered SPI provider " + mockSPIProvider1, logRecord1.getMessage());

      // Register SPI provider, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      String name2 = "spiProvider2";

      MockSPIProvider mockSPIProvider2 = new MockSPIProvider(name2);

      Assert.assertTrue(MPIHelperUtil.registerSPIProvider(mockSPIProvider2));
      Assert.assertTrue(logRecords.isEmpty());

      // Register SPI provider, duplicate name, with log

      logRecords = captureHandler.resetLogLevel(Level.WARNING);

      MockSPIProvider mockSPIProvider3 = new MockSPIProvider(name1);

      Assert.assertFalse(MPIHelperUtil.registerSPIProvider(mockSPIProvider3));
      Assert.assertEquals(1, logRecords.size());

      logRecord1 = logRecords.get(0);

      Assert.assertEquals(
          "Not registering SPI provider "
              + mockSPIProvider3
              + " because it duplicates "
              + mockSPIProvider1,
          logRecord1.getMessage());

      // Register SPI provider, duplicate name, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      mockSPIProvider3 = new MockSPIProvider(name1);

      Assert.assertFalse(MPIHelperUtil.registerSPIProvider(mockSPIProvider3));
      Assert.assertTrue(logRecords.isEmpty());

      // Get SPI provider

      String name3 = "spiProvider3";

      Assert.assertSame(mockSPIProvider1, MPIHelperUtil.getSPIProvider(name1));
      Assert.assertSame(mockSPIProvider2, MPIHelperUtil.getSPIProvider(name2));
      Assert.assertNull(MPIHelperUtil.getSPIProvider(name3));

      List<SPIProvider> spiProviders = MPIHelperUtil.getSPIProviders();

      Assert.assertEquals(2, spiProviders.size());
      Assert.assertTrue(spiProviders.contains(mockSPIProvider1));
      Assert.assertTrue(spiProviders.contains(mockSPIProvider2));

      // Unregister SPI provider, null name

      mockSPIProvider3 = new MockSPIProvider(null);

      try {
        MPIHelperUtil.unregisterSPIProvider(mockSPIProvider3);

        Assert.fail();
      } catch (NullPointerException npe) {
      }

      // Unregister SPI provider, nonexistent name, with log

      logRecords = captureHandler.resetLogLevel(Level.WARNING);

      mockSPIProvider3 = new MockSPIProvider(name3);

      Assert.assertFalse(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider3));
      Assert.assertEquals(1, logRecords.size());

      logRecord1 = logRecords.get(0);

      Assert.assertEquals(
          "Not unregistering unregistered SPI provider " + mockSPIProvider3,
          logRecord1.getMessage());

      // Unregister SPI provider, nonexistent name, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      mockSPIProvider3 = new MockSPIProvider(name3);

      Assert.assertFalse(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider3));
      Assert.assertTrue(logRecords.isEmpty());

      // Unregister SPI provider, mismatch instance, with log

      logRecords = captureHandler.resetLogLevel(Level.INFO);

      Assert.assertFalse(MPIHelperUtil.unregisterSPIProvider(new MockSPIProvider(name2)));
      Assert.assertEquals(1, logRecords.size());

      logRecord1 = logRecords.get(0);

      Assert.assertEquals(
          "Not unregistering unregistered SPI provider " + name2, logRecord1.getMessage());

      // Unregister SPI provider, mismatch instance, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      Assert.assertFalse(MPIHelperUtil.unregisterSPIProvider(new MockSPIProvider(name2)));
      Assert.assertTrue(logRecords.isEmpty());

      // Unregister SPI provider, concurrent remove failure, with log

      logRecords = captureHandler.resetLogLevel(Level.INFO);

      ConcurrentMap<String, Object> oldSPIProviderContainers =
          ReflectionTestUtil.getFieldValue(MPIHelperUtil.class, "_spiProviderContainers");

      try {
        ReflectionTestUtil.setFieldValue(
            MPIHelperUtil.class,
            "_spiProviderContainers",
            new ConcurrentHashMap<String, Object>(oldSPIProviderContainers) {

              @Override
              public boolean remove(Object key, Object value) {
                return false;
              }
            });

        Assert.assertFalse(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider2));
      } finally {
        ReflectionTestUtil.setFieldValue(
            MPIHelperUtil.class, "_spiProviderContainers", oldSPIProviderContainers);
      }

      Assert.assertEquals(1, logRecords.size());

      logRecord1 = logRecords.get(0);

      Assert.assertEquals(
          "Not unregistering unregistered SPI provider " + name2, logRecord1.getMessage());

      // Unregister SPI provider, concurrent remove failure, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      oldSPIProviderContainers =
          ReflectionTestUtil.getFieldValue(MPIHelperUtil.class, "_spiProviderContainers");

      try {
        ReflectionTestUtil.setFieldValue(
            MPIHelperUtil.class,
            "_spiProviderContainers",
            new ConcurrentHashMap<String, Object>(oldSPIProviderContainers) {

              @Override
              public boolean remove(Object key, Object value) {
                return false;
              }
            });

        Assert.assertFalse(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider2));
      } finally {
        ReflectionTestUtil.setFieldValue(
            MPIHelperUtil.class, "_spiProviderContainers", oldSPIProviderContainers);
      }

      Assert.assertTrue(logRecords.isEmpty());

      // Unregister SPI provider, with no SPI, with log

      logRecords = captureHandler.resetLogLevel(Level.INFO);

      Assert.assertTrue(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider2));
      Assert.assertEquals(1, logRecords.size());

      logRecord1 = logRecords.get(0);

      Assert.assertEquals("Unregistered SPI provider " + mockSPIProvider2, logRecord1.getMessage());

      // Unregister SPI provider, with no SPI, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      Assert.assertTrue(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider1));
      Assert.assertTrue(logRecords.isEmpty());

      // Unregister SPI provider, with SPI, fail on stop, with log

      logRecords = captureHandler.resetLogLevel(Level.SEVERE);

      mockSPIProvider1 = new MockSPIProvider(name1);

      Assert.assertTrue(MPIHelperUtil.registerSPIProvider(mockSPIProvider1));

      mockSPIProvider2 = new MockSPIProvider(name2);

      Assert.assertTrue(MPIHelperUtil.registerSPIProvider(mockSPIProvider2));

      MockSPI mockSPI1 = new MockSPI();

      mockSPI1.failOnStop = true;
      mockSPI1.spiProviderName = name1;

      MPIHelperUtilTestUtil.directResigterSPI("spi1", mockSPI1);

      MockSPI mockSPI2 = new MockSPI();

      mockSPI2.failOnStop = true;
      mockSPI2.spiProviderName = name2;

      MPIHelperUtilTestUtil.directResigterSPI("spi2", mockSPI2);

      Assert.assertTrue(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider1));
      Assert.assertFalse(mockSPI1.destroyed);
      Assert.assertFalse(mockSPI1.stopped);
      Assert.assertFalse(mockSPI2.destroyed);
      Assert.assertFalse(mockSPI2.stopped);
      Assert.assertEquals(1, logRecords.size());

      logRecord1 = logRecords.get(0);

      Assert.assertEquals(
          "Unable to unregister SPI "
              + mockSPI1
              + " while unregistering SPI provider "
              + mockSPIProvider1,
          logRecord1.getMessage());

      Throwable throwable = logRecord1.getThrown();

      Assert.assertSame(RemoteException.class, throwable.getClass());

      // Unregister SPI provider, with SPI, fail on destroy, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      mockSPI1.failOnDestroy = true;
      mockSPI1.failOnStop = false;

      mockSPI2.failOnDestroy = true;
      mockSPI2.failOnStop = false;

      Assert.assertTrue(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider2));
      Assert.assertFalse(mockSPI1.destroyed);
      Assert.assertFalse(mockSPI1.stopped);
      Assert.assertFalse(mockSPI2.destroyed);
      Assert.assertTrue(mockSPI2.stopped);
      Assert.assertTrue(logRecords.isEmpty());

      // Unregister SPI provider, with SPI, fail on catch, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      mockSPIProvider1 = new MockSPIProvider(name1);

      Assert.assertTrue(MPIHelperUtil.registerSPIProvider(mockSPIProvider1));

      final RuntimeException runtimeException = new RuntimeException();

      mockSPI1 =
          new MockSPI() {

            @Override
            public String toString() {
              throw runtimeException;
            }
          };

      mockSPI1.failOnDestroy = true;
      mockSPI1.failOnStop = false;
      mockSPI1.spiProviderName = name1;

      MPIHelperUtilTestUtil.directResigterSPI(name1, mockSPI1);

      try {
        MPIHelperUtil.unregisterSPIProvider(mockSPIProvider1);

        Assert.fail();
      } catch (RuntimeException re) {
        Assert.assertSame(runtimeException, re);
      }

      Assert.assertTrue(logRecords.isEmpty());

      // Unregister SPI provider, with SPI, success, with log

      mockSPIProvider1 = new MockSPIProvider(name1);

      Assert.assertTrue(MPIHelperUtil.registerSPIProvider(mockSPIProvider1));

      mockSPIProvider2 = new MockSPIProvider(name2);

      Assert.assertTrue(MPIHelperUtil.registerSPIProvider(mockSPIProvider2));

      mockSPI1 = new MockSPI();

      mockSPI1.failOnDestroy = false;
      mockSPI1.spiProviderName = name1;

      MPIHelperUtilTestUtil.directResigterSPI("spi1", mockSPI1);

      mockSPI2 = new MockSPI();

      mockSPI2.failOnDestroy = false;
      mockSPI2.spiProviderName = name2;

      MPIHelperUtilTestUtil.directResigterSPI("spi2", mockSPI2);

      logRecords = captureHandler.resetLogLevel(Level.INFO);

      Assert.assertTrue(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider1));
      Assert.assertTrue(mockSPI1.destroyed);
      Assert.assertTrue(mockSPI1.stopped);
      Assert.assertFalse(mockSPI2.destroyed);
      Assert.assertFalse(mockSPI2.stopped);
      Assert.assertEquals(2, logRecords.size());

      logRecord1 = logRecords.get(0);

      Assert.assertEquals(
          "Unregistered SPI " + mockSPI1 + " while unregistering SPI provider " + mockSPIProvider1,
          logRecord1.getMessage());

      LogRecord logRecord2 = logRecords.get(1);

      Assert.assertEquals("Unregistered SPI provider " + mockSPIProvider1, logRecord2.getMessage());

      // Unregister SPI provider, with SPI, success, without log

      logRecords = captureHandler.resetLogLevel(Level.OFF);

      Assert.assertTrue(MPIHelperUtil.unregisterSPIProvider(mockSPIProvider2));
      Assert.assertTrue(mockSPI1.destroyed);
      Assert.assertTrue(mockSPI1.stopped);
      Assert.assertTrue(mockSPI2.destroyed);
      Assert.assertTrue(mockSPI2.stopped);
      Assert.assertTrue(logRecords.isEmpty());
    }
  }