Esempio n. 1
0
  public static void rollbackForTestClass() {
    SavePoint savePoint = TestRun.getSavePointForTestClass();

    if (savePoint != null) {
      savePoint.rollback();
      TestRun.setSavePointForTestClass(null);
    }
  }
Esempio n. 2
0
 public SavePoint() {
   MockFixture mockFixture = TestRun.mockFixture();
   previousTransformedClasses = mockFixture.getTransformedClasses();
   previousRedefinedClasses = mockFixture.getRedefinedClasses();
   previousCaptureTransformerCount = mockFixture.getCaptureTransformerCount();
   previousMockedClasses = mockFixture.getMockedClasses();
   previousMockClasses = TestRun.getMockClasses().new SavePoint();
 }
Esempio n. 3
0
  public synchronized void rollback() {
    if (rollbackActions != null) {
      for (Runnable action : rollbackActions) {
        action.run();
      }
    }

    MockFixture mockFixture = TestRun.mockFixture();
    mockFixture.restoreTransformedClasses(previousTransformedClasses);
    mockFixture.restoreRedefinedClasses(previousRedefinedClasses);
    TestRun.getMockClasses().getRegularMocks().removeInstances(previousMockInstancesCount);
  }
Esempio n. 4
0
  private boolean isMatchingInstance(
      @Nonnull Object invokedInstance, @Nonnull Expectation expectation) {
    ExpectedInvocation invocation = expectation.invocation;
    assert invocation.instance != null;

    if (isEquivalentInstance(invocation.instance, invokedInstance)) {
      return true;
    }

    if (TestRun.getExecutingTest().isInjectableMock(invokedInstance)) {
      return false;
    }

    if (dynamicMockInstancesToMatch != null) {
      if (containsReference(dynamicMockInstancesToMatch, invokedInstance)) {
        return false;
      }

      Class<?> invokedClass = invocation.instance.getClass();

      for (Object dynamicMock : dynamicMockInstancesToMatch) {
        if (dynamicMock.getClass() == invokedClass) {
          return false;
        }
      }
    }

    return !invocation.matchInstance && expectation.recordPhase != null;
  }
Esempio n. 5
0
 boolean isEquivalentInstance(
     @Nonnull Object invocationInstance, @Nonnull Object invokedInstance) {
   return invocationInstance == invokedInstance
       || invocationInstance == replacementMap.get(invokedInstance)
       || invocationInstance == instanceMap.get(invokedInstance)
       || invokedInstance == instanceMap.get(invocationInstance)
       || TestRun.getExecutingTest()
           .isInvokedInstanceEquivalentToCapturedInstance(invocationInstance, invokedInstance);
 }
Esempio n. 6
0
  private boolean isSameMockedClass(@Nullable Object mock1, @Nullable Object mock2) {
    if (mock1 == mock2) {
      return true;
    }

    if (mock1 != null && mock2 != null) {
      Class<?> mockedClass1 = mock1.getClass();
      Class<?> mockedClass2 = mock2.getClass();

      if (mockedClass1 == mockedClass2
          || TestRun.getExecutingTest()
              .isInvokedInstanceEquivalentToCapturedInstance(mock1, mock2)) {
        return true;
      }

      return TestRun.mockFixture().areCapturedClasses(mockedClass1, mockedClass2);
    }

    return false;
  }
Esempio n. 7
0
  public synchronized void rollback() {
    RECORD_OR_REPLAY_LOCK.lock();

    try {
      MockFixture mockFixture = TestRun.mockFixture();
      mockFixture.removeCaptureTransformers(previousCaptureTransformerCount);
      mockFixture.restoreTransformedClasses(previousTransformedClasses);
      mockFixture.restoreRedefinedClasses(previousRedefinedClasses);
      mockFixture.removeMockedClasses(previousMockedClasses);
      previousMockClasses.rollback();
    } finally {
      RECORD_OR_REPLAY_LOCK.unlock();
    }
  }
Esempio n. 8
0
  /**
   * Excutes the selected test run.
   *
   * @param testRunName name of the test run to execute
   */
  private void executeTestRun(String testRunName) {

    log.log(Level.INFO, "Executing Test Run '" + testRunName + "'");

    // get the selected test run
    TestRun testRun = config.getTestRun(testRunName);

    // check if the application under test is available
    BaseTestCase.testAUTHTTPConnection(testRun.getBaseURL());

    // check if all configured servers for this test run are responding
    BaseTestCase.testSeleniumHTTPConnection(config, testRun.getTestRunServerIterator());

    // set the Application-under-Test parameters in the JUnit Base Class
    BaseTestCase.setBaseURL(testRun.getBaseURL());

    String msg = "Application under Test:\n" + "BaseURL=" + testRun.getBaseURL();

    System.out.println(msg);
    log.log(Level.INFO, msg);

    // go through all servers defined in the test run and execute the tests
    Iterator<TestRun.TestRunServer> selServerIt = testRun.getTestRunServerIterator();
    while (selServerIt.hasNext()) {

      // get the next server entry from the test run
      TestRun.TestRunServer selServerConfig = selServerIt.next();
      // set the speed for this server/browser combination
      BaseTestCase.setSpeed(selServerConfig.getSpeed());

      // get the test server settings
      Map<String, String> testServer = config.getTestServers().get(selServerConfig.getName());

      // set the host,port and browser used for the next tests
      BaseTestCase.isExternallySetup = true;
      BaseTestCase.setSeleniumServerHost(testServer.get("host"));
      BaseTestCase.setSeleniumServerPort(testServer.get("port"));

      msg = "Platform is: " + System.getProperty("os.name");
      System.out.println(msg);
      log.log(Level.INFO, msg);

      Map<String, Platform> platforms = config.getPlatforms();
      for (Platform platformToCheck : platforms.values()) {
        if (platformToCheck.isValid()) {
          executeForPlatform(selServerConfig, testServer, platformToCheck);
        }
      }
    }
  }
Esempio n. 9
0
  boolean isToBeMatchedOnInstance(@Nullable Object mock, @Nonnull String mockNameAndDesc) {
    if (mock == null || mockNameAndDesc.charAt(0) == '<') {
      return false;
    }

    if (dynamicMockInstancesToMatch != null
        && containsReference(dynamicMockInstancesToMatch, mock)) {
      return true;
    }

    if (mockedTypesToMatchOnInstances != null) {
      Class<?> mockedClass = GeneratedClasses.getMockedClass(mock);

      if (containsReference(mockedTypesToMatchOnInstances, mockedClass)) {
        return true;
      }
    } else if (TestRun.getExecutingTest().isInjectableMock(mock)) {
      return true;
    }

    return false;
  }
Esempio n. 10
0
 public static void registerNewActiveSavePoint() {
   TestRun.setSavePointForTestClass(new SavePoint());
 }
Esempio n. 11
0
 public SavePoint() {
   MockFixture mockFixture = TestRun.mockFixture();
   previousTransformedClasses = mockFixture.getTransformedClasses();
   previousRedefinedClasses = mockFixture.getRedefinedClasses();
   previousMockInstancesCount = TestRun.getMockClasses().getRegularMocks().getInstanceCount();
 }
Esempio n. 12
0
 void registerRead() {
   int testId = TestRun.getTestId();
   testIdsToAssignments.put(testId, null);
   readCount++;
 }
Esempio n. 13
0
 void registerAssignment() {
   int testId = TestRun.getTestId();
   testIdsToAssignments.put(testId, Boolean.TRUE);
   writeCount++;
 }
Esempio n. 14
0
 private void discardStateForCorrespondingMockClassIfAny(@Nonnull Class<?> redefinedClass) {
   String mockClassesInternalNames = realClassesToMockClasses.remove(redefinedClass);
   TestRun.getMockStates().removeClassState(redefinedClass, mockClassesInternalNames);
 }
Esempio n. 15
0
 /**
  * Discards any mocks set up for the specified classes that are currently in effect, for all test
  * scopes: the current test method (if any), the current test (which starts with the first
  * "before" method and continues until the last "after" method), the current test class (which
  * includes all code from the first "before class" method to the last "after class" method), and
  * the current test suite.
  *
  * <p>Notice that if one of the given real classes has a mock class applied at the level of the
  * test class, calling this method would negate the application of that mock class. JMockit will
  * automatically restore classes mocked by a test at the end of its execution, as well as all
  * classes mocked for the test class as a whole (through a "before class" method or an
  * {@code @UsingMocksAndStubs} annotation) before the first test in the next test class is
  * executed.
  *
  * @param realClasses one or more real classes from production code, which may have mocked methods
  */
 public static void tearDownMocks(Class<?>... realClasses) {
   Set<Class<?>> classesToRestore = new HashSet<Class<?>>();
   Collections.addAll(classesToRestore, realClasses);
   TestRun.mockFixture().restoreAndRemoveRedefinedClasses(classesToRestore);
 }
Esempio n. 16
0
 /**
  * Discards any mocks currently in effect, for all test scopes: the current test method (if any),
  * the current test (which starts with the first "before" method and continues until the last
  * "after" method), the current test class (which includes all code from the first "before class"
  * method to the last "after class" method), and the current test suite.
  *
  * <p>Notice that a call to this method will tear down <em>all</em> mock classes that were applied
  * through use of the Mockups API that are still in effect, as well as any mock classes or stubs
  * applied to the current test class through {@code @UsingMocksAndStubs}. In other words, it would
  * effectively prevent mocks to be set up at the test class and test suite levels. So, use it only
  * if necessary and if it won't discard mock classes that should remain in effect. Consider using
  * {@link #tearDownMocks(Class...)} instead, which lets you restrict the set of real classes to be
  * restored.
  *
  * <p>JMockit will automatically restore classes mocked by a test at the end of its execution, as
  * well as all classes mocked for the test class as a whole (through a "before class" method or an
  * {@code @UsingMocksAndStubs} annotation) before the first test in the next test class is
  * executed.
  *
  * @see <a
  *     href="http://code.google.com/p/jmockit/source/browse/trunk/main/test/mockit/MockAnnotationsTest.java#450">
  *     Example</a>
  */
 public static void tearDownMocks() {
   TestRun.mockFixture().restoreAndRemoveRedefinedClasses(null);
   TestRun.getMockClasses().getRegularMocks().discardInstances();
 }