@Test
  public void testContinueOnSomeDbDirectoriesMissing() throws Exception {
    File targetDir1 = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    File targetDir2 = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());

    try {
      assertTrue(targetDir1.mkdirs());
      assertTrue(targetDir2.mkdirs());

      if (!targetDir1.setWritable(false, false)) {
        System.err.println(
            "Cannot execute 'testContinueOnSomeDbDirectoriesMissing' because cannot mark directory non-writable");
        return;
      }

      RocksDBStateBackend rocksDbBackend = new RocksDBStateBackend(TEMP_URI);
      rocksDbBackend.setDbStoragePaths(targetDir1.getAbsolutePath(), targetDir2.getAbsolutePath());

      try {
        rocksDbBackend.initializeForJob(getMockEnvironment(), "foobar", IntSerializer.INSTANCE);
      } catch (Exception e) {
        e.printStackTrace();
        fail("Backend initialization failed even though some paths were available");
      }
    } finally {
      //noinspection ResultOfMethodCallIgnored
      targetDir1.setWritable(true, false);
      FileUtils.deleteDirectory(targetDir1);
      FileUtils.deleteDirectory(targetDir2);
    }
  }
Example #2
0
    @Test
    public void testProcessZuulFilter() {

      ZuulRunner runner = new ZuulRunner();
      runner = spy(runner);
      RequestContext context = spy(RequestContext.getCurrentContext());

      try {
        FilterProcessor.setProcessor(processor);
        RequestContext.testSetCurrentContext(context);
        when(servletResponse.getWriter()).thenReturn(writer);

        runner.init(servletRequest, servletResponse);
        verify(runner, times(1)).init(servletRequest, servletResponse);
        assertTrue(
            RequestContext.getCurrentContext().getRequest() instanceof HttpServletRequestWrapper);
        assertEquals(RequestContext.getCurrentContext().getResponse(), servletResponse);

        runner.preRoute();
        verify(processor, times(1)).preRoute();

        runner.postRoute();
        verify(processor, times(1)).postRoute();
        //                verify(context, times(1)).unset();

        runner.route();
        verify(processor, times(1)).route();
        RequestContext.testSetCurrentContext(null);

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  @Test
  public void testFailWhenNoLocalStorageDir() throws Exception {
    File targetDir = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
    try {
      assertTrue(targetDir.mkdirs());

      if (!targetDir.setWritable(false, false)) {
        System.err.println(
            "Cannot execute 'testFailWhenNoLocalStorageDir' because cannot mark directory non-writable");
        return;
      }

      RocksDBStateBackend rocksDbBackend = new RocksDBStateBackend(TEMP_URI);
      rocksDbBackend.setDbStoragePath(targetDir.getAbsolutePath());

      try {
        rocksDbBackend.initializeForJob(getMockEnvironment(), "foobar", IntSerializer.INSTANCE);
      } catch (Exception e) {
        assertTrue(e.getMessage().contains("No local storage directories available"));
        assertTrue(e.getMessage().contains(targetDir.getAbsolutePath()));
      }
    } finally {
      //noinspection ResultOfMethodCallIgnored
      targetDir.setWritable(true, false);
      FileUtils.deleteDirectory(targetDir);
    }
  }
  @Test
  public void testKeyValueStateInWindowFunction() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>(50);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      StatefulFunction.globalCounts.clear();

      // tumbling window that triggers every 20 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              new StatefulFunction(),
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              50,
              50);

      op.setup(mockTask, createTaskConfig(identitySelector, IntSerializer.INSTANCE), out);
      op.open();

      synchronized (lock) {
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
      }
      out.waitForNElements(2, 60000);

      synchronized (lock) {
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
        op.processElement(new StreamRecord<Integer>(2));
      }
      out.waitForNElements(8, 60000);

      List<Integer> result = out.getElements();
      assertEquals(8, result.size());

      Collections.sort(result);
      assertEquals(Arrays.asList(1, 1, 1, 1, 2, 2, 2, 2), result);

      assertEquals(4, StatefulFunction.globalCounts.get(1).intValue());
      assertEquals(4, StatefulFunction.globalCounts.get(2).intValue());

      synchronized (lock) {
        op.close();
      }
      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
Example #5
0
 public static void load() {
   try {
     load(false);
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
Example #6
0
 @Test
 public void shouldWireUpAtExitHook() {
   factory.registerExitHook();
   try {
     Runtime.getRuntime().addShutdownHook(factory.exitHook());
   } catch (Exception e) {
     assertThat(e.getMessage(), is("Hook previously registered"));
   }
 }
Example #7
0
 @Theory
 public void testParseDate(TestData data) {
   try {
     assertThat(testee.parseDate(data.getFile()), is(equalTo(data.getExpectedDate())));
   } catch (Exception ex) {
     ex.printStackTrace(System.err);
     throw ex;
   }
 }
 @Test
 public void testHandlerDispatching_ThrowingException() throws Throwable {
   try {
     testSubject.handle(GenericCommandMessage.asCommandMessage(new HashSet()), mockUnitOfWork);
     fail("Expected exception");
   } catch (Exception ex) {
     assertEquals(Exception.class, ex.getClass());
   }
 }
 @Test(expected = MongoException.class)
 public void createConnectionException2Test() {
   try {
     when(mongo.getDB(anyString())).thenThrow(MongoException.class);
   } catch (Exception ex) {
     fail(ex.getLocalizedMessage());
   }
   MongoDAOFactory.createConnection();
 }
 @Test
 public void testDoesNotBombWhenDirectoryDoesNotExist() throws Exception {
   IPlatformImporter importer = mock(IPlatformImporter.class);
   ArchiveLoader loader = new ArchiveLoader(importer);
   try {
     loader.loadAll(new File("/fake/path/that/does/not/exist"), ArchiveLoader.ZIPS_FILTER);
   } catch (Exception e) {
     Assert.fail("Expected no exception but got " + e.getMessage());
   }
 }
  @SuppressWarnings("ThrowableResultOfMethodCallIgnored")
  private void checkException(
      final ArgumentCaptor<Exception> exceptionCaptor,
      final Class<? extends Exception> expectedException,
      final String expectedMessage) {
    final Exception exception = exceptionCaptor.getValue();

    assertEquals(expectedException, exception.getClass());
    assertEquals(expectedMessage, exception.getMessage());
  }
Example #12
0
 @Test
 public void testMostRecentFile() {
   try {
     assertThat(
         testee.getMostRecent(filesInTestDir), is(equalTo(DataItems.DIR_FILE_2030.getFile())));
   } catch (Exception ex) {
     ex.printStackTrace(System.err);
     throw ex;
   }
 }
 @Test
 public void getResourceDAOTest() {
   mongoResourceDAO = mock(MongoResourceDAO.class);
   try {
     PowerMockito.whenNew(MongoResourceDAO.class).withNoArguments().thenReturn(mongoResourceDAO);
   } catch (Exception ex) {
     fail(ex.getLocalizedMessage());
   }
   toTest = new MongoDAOFactory();
   toTest.getResourceDAO();
 }
Example #14
0
 @Test(expected = Exception.class)
 public void runningCommandWithNonExistentAddressResultsInError() throws Exception {
   String[] args = {"-o", "-a", "user:pwd", "-c", "user:pwd:/FitNesse.NonExistentTestCase?test"};
   Arguments arguments = new Arguments(args);
   try {
     Integer exitCode = new FitNesseMain().launchFitNesse(arguments);
   } catch (Exception e) {
     assertEquals("error loading page: 404", e.getMessage());
     throw e;
   }
 }
  @Test
  public void testTumblingWindow() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final int windowSize = 50;
      final CollectingOutput<Integer> out = new CollectingOutput<>(windowSize);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // tumbling window that triggers every 20 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              windowSize,
              windowSize);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      final int numElements = 1000;

      for (int i = 0; i < numElements; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      synchronized (lock) {
        op.close();
      }
      op.dispose();

      // get and verify the result
      List<Integer> result = out.getElements();
      assertEquals(numElements, result.size());

      Collections.sort(result);
      for (int i = 0; i < numElements; i++) {
        assertEquals(i, result.get(i).intValue());
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  @Test
  public void testInvalidParameters() {
    try {
      assertInvalidParameter(-1L, -1L);
      assertInvalidParameter(10000L, -1L);
      assertInvalidParameter(-1L, 1000L);
      assertInvalidParameter(1000L, 2000L);

      // actual internal slide is too low here:
      assertInvalidParameter(1000L, 999L);
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }
  @Test
  public void testSlidingWindowSingleElements() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();

    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>(50);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // tumbling window that triggers every 20 milliseconds
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              150,
              50);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      synchronized (lock) {
        op.processElement(new StreamRecord<Integer>(1));
        op.processElement(new StreamRecord<Integer>(2));
      }

      // each element should end up in the output three times
      // wait until the elements have arrived 6 times in the output
      out.waitForNElements(6, 120000);

      List<Integer> result = out.getElements();
      assertEquals(6, result.size());

      Collections.sort(result);
      assertEquals(Arrays.asList(1, 1, 1, 2, 2, 2), result);

      synchronized (lock) {
        op.close();
      }
      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  @Test
  public void testUpdateStudent_passnull() {

    // Given
    StudentDirectoryServiceImpl studentDirectoryService = new StudentDirectoryServiceImpl();

    try {
      // When
      studentDirectoryService.updateStudent(null);
      fail("An exception should have been thrown for a null value passed as a parameter.");
    } catch (Exception e) {
      // Then
      assertTrue(e instanceof NullPointerException);
      assertEquals("Unable to update a student. The student cannot be null.", e.getMessage());
    }
  }
  @Test
  public void test_wait_until_with_failure() {
    evaluator = mock(Evaluator.class);

    when(evaluator.existComponent(id)).thenReturn(true);
    when(evaluator.isVisible(any(Component.class))).thenReturn(false);
    when(evaluator.isEnabled(any(Component.class))).thenReturn(true);

    Component component = new Component(evaluator, id);

    try {
      waitUntil(component, is(visible()));
      fail();
    } catch (Exception e) {
      assertThat(e.getMessage(), is("Unable to reach the condition in 5 SECONDS"));
    }
  }
  @Test
  public void testEmitTrailingDataOnClose() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>();
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // the operator has a window time that is so long that it will not fire in this test
      final long oneYear = 365L * 24 * 60 * 60 * 1000;
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              oneYear,
              oneYear);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      List<Integer> data = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
      for (Integer i : data) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
      }

      synchronized (lock) {
        op.close();
      }
      op.dispose();

      // get and verify the result
      List<Integer> result = out.getElements();
      Collections.sort(result);
      assertEquals(data, result);
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
Example #21
0
 @Test
 public void shouldThrowExceptionWhenAgentWithNoCookieTriesToUpdateStatus() throws Exception {
   AgentRuntimeInfo runtimeInfo =
       new AgentRuntimeInfo(
           agentIdentifier, AgentRuntimeStatus.Idle, currentWorkingDirectory(), null, null);
   try {
     agentService.updateRuntimeInfo(runtimeInfo);
     fail("should throw exception when no cookie is set");
   } catch (Exception e) {
     assertThat(e, instanceOf(AgentNoCookieSetException.class));
     assertThat(
         e.getMessage(),
         is(format("Agent [%s] has no cookie set", runtimeInfo.agentInfoDebugString())));
     assertThat(
         Arrays.asList(logFixture.getMessages()),
         hasItem(format("Agent [%s] has no cookie set", runtimeInfo.agentInfoDebugString())));
   }
 }
 private void assertInvalidParameter(long windowSize, long windowSlide) {
   try {
     new AccumulatingProcessingTimeWindowOperator<String, String, String>(
         mockFunction,
         mockKeySelector,
         StringSerializer.INSTANCE,
         StringSerializer.INSTANCE,
         windowSize,
         windowSlide);
     fail("This should fail with an IllegalArgumentException");
   } catch (IllegalArgumentException e) {
     // expected
   } catch (Exception e) {
     fail(
         "Wrong exception. Expected IllegalArgumentException but found "
             + e.getClass().getSimpleName());
   }
 }
Example #23
0
 @Test
 public void shouldThrowExceptionWhenADuplicateAgentTriesToUpdateStatus() throws Exception {
   AgentRuntimeInfo runtimeInfo =
       new AgentRuntimeInfo(
           agentIdentifier, AgentRuntimeStatus.Idle, currentWorkingDirectory(), null, null);
   runtimeInfo.setCookie("invalid_cookie");
   AgentInstance original =
       AgentInstance.createFromLiveAgent(
           new AgentRuntimeInfo(
               agentIdentifier, AgentRuntimeStatus.Idle, currentWorkingDirectory(), null, null),
           new SystemEnvironment());
   try {
     when(agentService.findAgentAndRefreshStatus(runtimeInfo.getUUId())).thenReturn(original);
     agentService.updateRuntimeInfo(runtimeInfo);
     fail("should throw exception when cookie mismatched");
   } catch (Exception e) {
     assertThat(
         e.getMessage(),
         is(format("Agent [%s] has invalid cookie", runtimeInfo.agentInfoDebugString())));
     assertThat(
         Arrays.asList(logFixture.getMessages()),
         hasItem(
             format(
                 "Found agent [%s] with duplicate uuid. Please check the agent installation.",
                 runtimeInfo.agentInfoDebugString())));
     verify(serverHealthService)
         .update(
             ServerHealthState.warning(
                 format(
                     "[%s] has duplicate unique identifier which conflicts with [%s]",
                     runtimeInfo.agentInfoForDisplay(), original.agentInfoForDisplay()),
                 "Please check the agent installation. Click <a href='http://www.go.cd/documentation/user/current/faq/agent_guid_issue.html' target='_blank'>here</a> for more info.",
                 HealthStateType.duplicateAgent(
                     HealthStateScope.forAgent(runtimeInfo.getCookie())),
                 Timeout.THIRTY_SECONDS));
   }
   verify(agentInstances).findAgentAndRefreshStatus(runtimeInfo.getUUId());
   verifyNoMoreInteractions(agentInstances);
 }
  public boolean tearDown() {
    /*
    List<MultiverseWorld> worlds = new ArrayList<MultiverseWorld>(core.getMVWorldManager()
            .getMVWorlds());
    for (MultiverseWorld world : worlds) {
        core.getMVWorldManager().deleteWorld(world.getName());
    }
    */

    Server maybeNullServer = getServer();
    PluginManager maybeNullPluginManager = maybeNullServer.getPluginManager();
    Plugin plugin = maybeNullPluginManager.getPlugin("Multiverse-Inventories");
    // Plugin plugin = getServer().getPluginManager().getPlugin("Multiverse-Inventories");
    MultiverseInventories inventories = (MultiverseInventories) plugin;
    inventories.onDisable();

    MockWorldFactory.clearWorlds();

    plugin = getServer().getPluginManager().getPlugin("Multiverse-Core");
    MultiverseCore core = (MultiverseCore) plugin;
    core.onDisable();

    try {
      Field serverField = Bukkit.class.getDeclaredField("server");
      serverField.setAccessible(true);
      serverField.set(Class.forName("org.bukkit.Bukkit"), null);
    } catch (Exception e) {
      Util.log(
          Level.SEVERE,
          "Error while trying to unregister the server from Bukkit. Has Bukkit changed?");
      e.printStackTrace();
      Assert.fail(e.getMessage());
      return false;
    }

    return true;
  }
  @Test
  public void testReadDataFromEavesdrop() {

    try {
      String exampleString = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">";
      exampleString += "<html>";
      exampleString += "</html>";

      URLConnection connection = mock(URLConnection.class); // Create mock dependency: mock()

      InputStream i = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));

      when(connection.getInputStream()).thenReturn(i); // Setting up the expectations

      String result = englishEditor.readDataFromEavesdrop(connection);

      assertEquals(exampleString, result);

      verify(connection).getInputStream();

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Example #26
0
    @Test
    public void testUnsubscribeAfterTake() {
      Subscription s = mock(Subscription.class);
      TestObservable w = new TestObservable(s, "one", "two", "three");

      @SuppressWarnings("unchecked")
      Observer<String> aObserver = mock(Observer.class);
      Observable<String> take = Observable.create(take(w, 1));
      take.subscribe(aObserver);

      // wait for the Observable to complete
      try {
        w.t.join();
      } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
      }

      System.out.println("TestObservable thread finished");
      verify(aObserver, times(1)).onNext("one");
      verify(aObserver, never()).onNext("two");
      verify(aObserver, never()).onNext("three");
      verify(s, times(1)).unsubscribe();
    }
  @Test
  public void testPropagateExceptionsFromClose() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final CollectingOutput<Integer> out = new CollectingOutput<>();
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      WindowFunction<Integer, Integer, Integer, TimeWindow> failingFunction =
          new FailingFunction(100);

      // the operator has a window time that is so long that it will not fire in this test
      final long hundredYears = 100L * 365 * 24 * 60 * 60 * 1000;
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              failingFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              hundredYears,
              hundredYears);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      for (int i = 0; i < 150; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
      }

      try {
        synchronized (lock) {
          op.close();
        }
        fail("This should fail with an exception");
      } catch (Exception e) {
        assertTrue(
            e.getMessage().contains("Artificial Test Exception")
                || (e.getCause() != null
                    && e.getCause().getMessage().contains("Artificial Test Exception")));
      }

      op.dispose();
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
  @Test
  public void checkpointRestoreWithPendingWindowSliding() {
    final ScheduledExecutorService timerService = Executors.newSingleThreadScheduledExecutor();
    try {
      final int factor = 4;
      final int windowSlide = 50;
      final int windowSize = factor * windowSlide;

      final CollectingOutput<Integer> out = new CollectingOutput<>(windowSlide);
      final Object lock = new Object();
      final StreamTask<?, ?> mockTask = createMockTaskWithTimer(timerService, lock);

      // sliding window (200 msecs) every 50 msecs
      AccumulatingProcessingTimeWindowOperator<Integer, Integer, Integer> op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              windowSize,
              windowSlide);

      op.setup(mockTask, new StreamConfig(new Configuration()), out);
      op.open();

      // inject some elements
      final int numElements = 1000;
      final int numElementsFirst = 700;

      for (int i = 0; i < numElementsFirst; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      // draw a snapshot
      StreamTaskState state;
      List<Integer> resultAtSnapshot;
      synchronized (lock) {
        int beforeSnapShot = out.getElements().size();
        state = op.snapshotOperatorState(1L, System.currentTimeMillis());
        resultAtSnapshot = new ArrayList<>(out.getElements());
        int afterSnapShot = out.getElements().size();
        assertEquals(
            "operator performed computation during snapshot", beforeSnapShot, afterSnapShot);
      }

      assertTrue(resultAtSnapshot.size() <= factor * numElementsFirst);

      // inject the remaining elements - these should not influence the snapshot
      for (int i = numElementsFirst; i < numElements; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      op.dispose();

      // re-create the operator and restore the state
      final CollectingOutput<Integer> out2 = new CollectingOutput<>(windowSlide);
      op =
          new AccumulatingProcessingTimeWindowOperator<>(
              validatingIdentityFunction,
              identitySelector,
              IntSerializer.INSTANCE,
              IntSerializer.INSTANCE,
              windowSize,
              windowSlide);

      op.setup(mockTask, new StreamConfig(new Configuration()), out2);
      op.restoreState(state, 1);
      op.open();

      // inject again the remaining elements
      for (int i = numElementsFirst; i < numElements; i++) {
        synchronized (lock) {
          op.processElement(new StreamRecord<Integer>(i));
        }
        Thread.sleep(1);
      }

      // for a deterministic result, we need to wait until all pending triggers
      // have fired and emitted their results
      long deadline = System.currentTimeMillis() + 120000;
      do {
        Thread.sleep(20);
      } while (resultAtSnapshot.size() + out2.getElements().size() < factor * numElements
          && System.currentTimeMillis() < deadline);

      synchronized (lock) {
        op.close();
      }
      op.dispose();

      // get and verify the result
      List<Integer> finalResult = new ArrayList<>(resultAtSnapshot);
      finalResult.addAll(out2.getElements());
      assertEquals(factor * numElements, finalResult.size());

      Collections.sort(finalResult);
      for (int i = 0; i < factor * numElements; i++) {
        assertEquals(i / factor, finalResult.get(i).intValue());
      }
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    } finally {
      timerService.shutdown();
    }
  }
 private void testGroupUserIDsGroupsException(Exception exception)
     throws SQLException, InstantiationException, IllegalAccessException, MalformedURLException {
   when(sql.sqlQuery(anyString())).thenThrow(exception);
   assertEquals(0, webGroupDao.getGroupUserIDs(group1).size());
   verify(log).severe(SingleWebGroupDao.EXCEPTION_MESSAGE_GET_USERIDS + exception.getMessage());
 }
  public boolean setUp() {
    try {
      FileUtils.deleteFolder(invDirectory);
      FileUtils.deleteFolder(serverDirectory);
      invDirectory.mkdirs();
      Assert.assertTrue(invDirectory.exists());

      MockGateway.MOCK_STANDARD_METHODS = false;

      plugin = PowerMockito.spy(new MultiverseInventories());
      core = PowerMockito.spy(new MultiverseCore());

      // Let's let all MV files go to bin/test
      doReturn(invDirectory).when(plugin).getDataFolder();
      // Let's let all MV files go to bin/test
      doReturn(coreDirectory).when(core).getDataFolder();

      // Return a fake PDF file.
      PluginDescriptionFile pdf =
          PowerMockito.spy(
              new PluginDescriptionFile(
                  "Multiverse-Inventories",
                  "2.4-test",
                  "com.onarandombox.multiverseinventories.MultiverseInventories"));
      when(pdf.getAuthors()).thenReturn(new ArrayList<String>());
      doReturn(pdf).when(plugin).getDescription();
      doReturn(core).when(plugin).getCore();
      doReturn(true).when(plugin).isEnabled();
      PluginDescriptionFile pdfCore =
          PowerMockito.spy(
              new PluginDescriptionFile(
                  "Multiverse-Core", "2.2-Test", "com.onarandombox.MultiverseCore.MultiverseCore"));
      when(pdfCore.getAuthors()).thenReturn(new ArrayList<String>());
      doReturn(pdfCore).when(core).getDescription();
      doReturn(true).when(core).isEnabled();
      plugin.setServerFolder(serverDirectory);

      // Add Core to the list of loaded plugins
      JavaPlugin[] plugins = new JavaPlugin[] {plugin, core};

      // Mock the Plugin Manager
      PluginManager mockPluginManager = PowerMockito.mock(PluginManager.class);
      when(mockPluginManager.getPlugins()).thenReturn(plugins);
      when(mockPluginManager.getPlugin("Multiverse-Inventories")).thenReturn(plugin);
      when(mockPluginManager.getPlugin("Multiverse-Core")).thenReturn(core);
      when(mockPluginManager.getPermission(anyString())).thenReturn(null);

      // Make some fake folders to fool the fake MV into thinking these worlds exist
      File worldNormalFile = new File(plugin.getServerFolder(), "world");
      Util.log("Creating world-folder: " + worldNormalFile.getAbsolutePath());
      worldNormalFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world", Environment.NORMAL, WorldType.NORMAL);
      File worldNetherFile = new File(plugin.getServerFolder(), "world_nether");
      Util.log("Creating world-folder: " + worldNetherFile.getAbsolutePath());
      worldNetherFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world_nether", Environment.NETHER, WorldType.NORMAL);
      File worldSkylandsFile = new File(plugin.getServerFolder(), "world_the_end");
      Util.log("Creating world-folder: " + worldSkylandsFile.getAbsolutePath());
      worldSkylandsFile.mkdirs();
      MockWorldFactory.makeNewMockWorld("world_the_end", Environment.THE_END, WorldType.NORMAL);
      File world2File = new File(plugin.getServerFolder(), "world2");
      Util.log("Creating world-folder: " + world2File.getAbsolutePath());
      world2File.mkdirs();
      MockWorldFactory.makeNewMockWorld("world2", Environment.NORMAL, WorldType.NORMAL);

      // Initialize the Mock server.
      mockServer = mock(Server.class);
      when(mockServer.getName()).thenReturn("TestBukkit");
      Logger.getLogger("Minecraft").setParent(Util.logger);
      when(mockServer.getLogger()).thenReturn(Util.logger);
      when(mockServer.getWorldContainer()).thenReturn(worldsDirectory);
      when(plugin.getServer()).thenReturn(mockServer);
      when(core.getServer()).thenReturn(mockServer);
      when(mockServer.getPluginManager()).thenReturn(mockPluginManager);
      Answer<Player> playerAnswer =
          new Answer<Player>() {
            public Player answer(InvocationOnMock invocation) throws Throwable {
              String arg;
              try {
                arg = (String) invocation.getArguments()[0];
              } catch (Exception e) {
                return null;
              }
              Player player = players.get(arg);
              if (player == null) {
                player = new MockPlayer(arg, mockServer);
                players.put(arg, player);
              }
              return player;
            }
          };
      when(mockServer.getPlayer(anyString())).thenAnswer(playerAnswer);
      when(mockServer.getOfflinePlayer(anyString())).thenAnswer(playerAnswer);
      when(mockServer.getOfflinePlayers())
          .thenAnswer(
              new Answer<OfflinePlayer[]>() {
                public OfflinePlayer[] answer(InvocationOnMock invocation) throws Throwable {
                  return players.values().toArray(new Player[players.values().size()]);
                }
              });
      when(mockServer.getOnlinePlayers())
          .thenAnswer(
              new Answer<Player[]>() {
                public Player[] answer(InvocationOnMock invocation) throws Throwable {
                  return players.values().toArray(new Player[players.values().size()]);
                }
              });

      // Give the server some worlds
      when(mockServer.getWorld(anyString()))
          .thenAnswer(
              new Answer<World>() {
                public World answer(InvocationOnMock invocation) throws Throwable {
                  String arg;
                  try {
                    arg = (String) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  return MockWorldFactory.getWorld(arg);
                }
              });

      when(mockServer.getWorld(any(UUID.class)))
          .thenAnswer(
              new Answer<World>() {
                @Override
                public World answer(InvocationOnMock invocation) throws Throwable {
                  UUID arg;
                  try {
                    arg = (UUID) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  return MockWorldFactory.getWorld(arg);
                }
              });

      when(mockServer.getWorlds())
          .thenAnswer(
              new Answer<List<World>>() {
                public List<World> answer(InvocationOnMock invocation) throws Throwable {
                  return MockWorldFactory.getWorlds();
                }
              });

      when(mockServer.createWorld(Matchers.isA(WorldCreator.class)))
          .thenAnswer(
              new Answer<World>() {
                public World answer(InvocationOnMock invocation) throws Throwable {
                  WorldCreator arg;
                  try {
                    arg = (WorldCreator) invocation.getArguments()[0];
                  } catch (Exception e) {
                    return null;
                  }
                  // Add special case for creating null worlds.
                  // Not sure I like doing it this way, but this is a special case
                  if (arg.name().equalsIgnoreCase("nullworld")) {
                    return MockWorldFactory.makeNewNullMockWorld(
                        arg.name(), arg.environment(), arg.type());
                  }
                  return MockWorldFactory.makeNewMockWorld(
                      arg.name(), arg.environment(), arg.type());
                }
              });

      when(mockServer.unloadWorld(anyString(), anyBoolean())).thenReturn(true);

      // add mock scheduler
      BukkitScheduler mockScheduler = mock(BukkitScheduler.class);
      when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class), anyLong()))
          .thenAnswer(
              new Answer<Integer>() {
                public Integer answer(InvocationOnMock invocation) throws Throwable {
                  Runnable arg;
                  try {
                    arg = (Runnable) invocation.getArguments()[1];
                  } catch (Exception e) {
                    return null;
                  }
                  arg.run();
                  return null;
                }
              });
      when(mockScheduler.scheduleSyncDelayedTask(any(Plugin.class), any(Runnable.class)))
          .thenAnswer(
              new Answer<Integer>() {
                public Integer answer(InvocationOnMock invocation) throws Throwable {
                  Runnable arg;
                  try {
                    arg = (Runnable) invocation.getArguments()[1];
                  } catch (Exception e) {
                    return null;
                  }
                  arg.run();
                  return null;
                }
              });
      when(mockServer.getScheduler()).thenReturn(mockScheduler);

      // Set InventoriesListener
      InventoriesListener il = PowerMockito.spy(new InventoriesListener(plugin));
      Field inventoriesListenerField =
          MultiverseInventories.class.getDeclaredField("inventoriesListener");
      inventoriesListenerField.setAccessible(true);
      inventoriesListenerField.set(plugin, il);

      // Set Core
      Field coreField = MultiverseInventories.class.getDeclaredField("core");
      coreField.setAccessible(true);
      coreField.set(plugin, core);

      // Set server
      Field serverfield = JavaPlugin.class.getDeclaredField("server");
      serverfield.setAccessible(true);
      serverfield.set(plugin, mockServer);

      // Set worldManager
      WorldManager wm = PowerMockito.spy(new WorldManager(core));
      Field worldmanagerfield = MultiverseCore.class.getDeclaredField("worldManager");
      worldmanagerfield.setAccessible(true);
      worldmanagerfield.set(core, wm);

      // Set playerListener
      MVPlayerListener pl = PowerMockito.spy(new MVPlayerListener(core));
      Field playerlistenerfield = MultiverseCore.class.getDeclaredField("playerListener");
      playerlistenerfield.setAccessible(true);
      playerlistenerfield.set(core, pl);

      // Set entityListener
      MVEntityListener el = PowerMockito.spy(new MVEntityListener(core));
      Field entitylistenerfield = MultiverseCore.class.getDeclaredField("entityListener");
      entitylistenerfield.setAccessible(true);
      entitylistenerfield.set(core, el);

      // Set weatherListener
      MVWeatherListener wl = PowerMockito.spy(new MVWeatherListener(core));
      Field weatherlistenerfield = MultiverseCore.class.getDeclaredField("weatherListener");
      weatherlistenerfield.setAccessible(true);
      weatherlistenerfield.set(core, wl);

      // Init our command sender
      final Logger commandSenderLogger = Logger.getLogger("CommandSender");
      commandSenderLogger.setParent(Util.logger);
      commandSender = mock(CommandSender.class);
      doAnswer(
              new Answer<Void>() {
                public Void answer(InvocationOnMock invocation) throws Throwable {
                  commandSenderLogger.info(
                      ChatColor.stripColor((String) invocation.getArguments()[0]));
                  return null;
                }
              })
          .when(commandSender)
          .sendMessage(anyString());
      when(commandSender.getServer()).thenReturn(mockServer);
      when(commandSender.getName()).thenReturn("MockCommandSender");
      when(commandSender.isPermissionSet(anyString())).thenReturn(true);
      when(commandSender.isPermissionSet(Matchers.isA(Permission.class))).thenReturn(true);
      when(commandSender.hasPermission(anyString())).thenReturn(true);
      when(commandSender.hasPermission(Matchers.isA(Permission.class))).thenReturn(true);
      when(commandSender.addAttachment(plugin)).thenReturn(null);
      when(commandSender.isOp()).thenReturn(true);

      Bukkit.setServer(mockServer);

      // Load Multiverse Core
      core.onLoad();
      plugin.onLoad();

      // Enable it.
      core.onEnable();
      plugin.onEnable();

      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }