org.easymock.classextension.EasyMock Java Examples

The following examples show how to use org.easymock.classextension.EasyMock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceTest.java    From travelguide with Apache License 2.0 6 votes vote down vote up
/** Tests Service.callMethod(). */
public void testCallMethod() throws Exception {
  FooRequest fooRequest = FooRequest.newBuilder().build();
  BarRequest barRequest = BarRequest.newBuilder().build();
  MockCallback<Message> fooCallback = new MockCallback<Message>();
  MockCallback<Message> barCallback = new MockCallback<Message>();
  TestService mockService = control.createMock(TestService.class);

  mockService.foo(EasyMock.same(mockController), EasyMock.same(fooRequest),
                  this.<FooResponse>wrapsCallback(fooCallback));
  mockService.bar(EasyMock.same(mockController), EasyMock.same(barRequest),
                  this.<BarResponse>wrapsCallback(barCallback));
  control.replay();

  mockService.callMethod(fooDescriptor, mockController,
                         fooRequest, fooCallback);
  mockService.callMethod(barDescriptor, mockController,
                         barRequest, barCallback);
  control.verify();
}
 
Example #2
Source File: ServiceTest.java    From travelguide with Apache License 2.0 6 votes vote down vote up
public void testNewReflectiveBlockingService() throws ServiceException {
  ServiceWithNoOuter.BlockingInterface impl =
      control.createMock(ServiceWithNoOuter.BlockingInterface.class);
  RpcController controller = control.createMock(RpcController.class);
  BlockingService service =
      ServiceWithNoOuter.newReflectiveBlockingService(impl);

  MethodDescriptor fooMethod =
      ServiceWithNoOuter.getDescriptor().findMethodByName("Foo");
  MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance();

  TestAllTypes expectedResponse = TestAllTypes.getDefaultInstance();
  EasyMock.expect(impl.foo(EasyMock.same(controller), EasyMock.same(request)))
      .andReturn(expectedResponse);

  control.replay();

  Message response =
      service.callBlockingMethod(fooMethod, controller, request);
  assertEquals(expectedResponse, response);

  control.verify();
}
 
Example #3
Source File: FileBasedProctorStoreTest.java    From proctor with Apache License 2.0 6 votes vote down vote up
@Test
public void updateTestDefinitionTestDoesNotExist() throws Exception {
    final TestDefinition definition = new TestDefinition();
    final Map<String, String> metadata = Maps.newHashMap();

    final Capture<FileBasedProctorStore.ProctorUpdater> capturedArgument = new Capture<>();
    coreMock.doInWorkingDirectory(
            eq("fooUser"),
            eq("fooPassw0rd"),
            eq("fooComment"),
            eq("r0"),
            capture(capturedArgument)
    );
    EasyMock.expectLastCall();
    replay(coreMock);

    try {
        store.updateTestDefinition("fooUser", "fooPassw0rd", "fooAuthor", "r0",
                definition, metadata, "fooComment");
        fail("Expected Exception");
    } catch (final StoreException.TestUpdateException tue) {
        assertEquals("Attempting to update non-existent test r0", tue.getCause().getMessage());
    }
}
 
Example #4
Source File: AutoMarkdownTest.java    From dal with Apache License 2.0 5 votes vote down vote up
public DalConnection mockDalConnection(){
	DalConnection conn = EasyMock.createMock(DalConnection.class);
	DbMeta meta = EasyMock.createMock(DbMeta.class);
	EasyMock.expect(meta.getDataBaseKeyName()).andReturn(dbName).times(1);
	EasyMock.expect(meta.getDatabaseCategory()).andReturn(DatabaseCategory.MySql).times(1);
	
	EasyMock.expect(conn.getMeta()).andReturn(meta).times(3);
	
	EasyMock.replay(meta, conn);
	return conn;
}
 
Example #5
Source File: ServiceTest.java    From travelguide with Apache License 2.0 5 votes vote down vote up
/** Tests generated stubs. */
public void testStub() throws Exception {
  FooRequest fooRequest = FooRequest.newBuilder().build();
  BarRequest barRequest = BarRequest.newBuilder().build();
  MockCallback<FooResponse> fooCallback = new MockCallback<FooResponse>();
  MockCallback<BarResponse> barCallback = new MockCallback<BarResponse>();
  RpcChannel mockChannel = control.createMock(RpcChannel.class);
  TestService stub = TestService.newStub(mockChannel);

  mockChannel.callMethod(
    EasyMock.same(fooDescriptor),
    EasyMock.same(mockController),
    EasyMock.same(fooRequest),
    EasyMock.same(FooResponse.getDefaultInstance()),
    this.<Message>wrapsCallback(fooCallback));
  mockChannel.callMethod(
    EasyMock.same(barDescriptor),
    EasyMock.same(mockController),
    EasyMock.same(barRequest),
    EasyMock.same(BarResponse.getDefaultInstance()),
    this.<Message>wrapsCallback(barCallback));
  control.replay();

  stub.foo(mockController, fooRequest, fooCallback);
  stub.bar(mockController, barRequest, barCallback);
  control.verify();
}
 
Example #6
Source File: ServiceTest.java    From travelguide with Apache License 2.0 5 votes vote down vote up
/** Tests generated blocking stubs. */
public void testBlockingStub() throws Exception {
  FooRequest fooRequest = FooRequest.newBuilder().build();
  BarRequest barRequest = BarRequest.newBuilder().build();
  BlockingRpcChannel mockChannel =
      control.createMock(BlockingRpcChannel.class);
  TestService.BlockingInterface stub =
      TestService.newBlockingStub(mockChannel);

  FooResponse fooResponse = FooResponse.newBuilder().build();
  BarResponse barResponse = BarResponse.newBuilder().build();

  EasyMock.expect(mockChannel.callBlockingMethod(
    EasyMock.same(fooDescriptor),
    EasyMock.same(mockController),
    EasyMock.same(fooRequest),
    EasyMock.same(FooResponse.getDefaultInstance()))).andReturn(fooResponse);
  EasyMock.expect(mockChannel.callBlockingMethod(
    EasyMock.same(barDescriptor),
    EasyMock.same(mockController),
    EasyMock.same(barRequest),
    EasyMock.same(BarResponse.getDefaultInstance()))).andReturn(barResponse);
  control.replay();

  assertSame(fooResponse, stub.foo(mockController, fooRequest));
  assertSame(barResponse, stub.bar(mockController, barRequest));
  control.verify();
}
 
Example #7
Source File: ServiceTest.java    From travelguide with Apache License 2.0 5 votes vote down vote up
public void testNewReflectiveService() {
  ServiceWithNoOuter.Interface impl =
      control.createMock(ServiceWithNoOuter.Interface.class);
  RpcController controller = control.createMock(RpcController.class);
  Service service = ServiceWithNoOuter.newReflectiveService(impl);

  MethodDescriptor fooMethod =
      ServiceWithNoOuter.getDescriptor().findMethodByName("Foo");
  MessageWithNoOuter request = MessageWithNoOuter.getDefaultInstance();
  RpcCallback<Message> callback = new RpcCallback<Message>() {
    public void run(Message parameter) {
      // No reason this should be run.
      fail();
    }
  };
  RpcCallback<TestAllTypes> specializedCallback =
      RpcUtil.specializeCallback(callback);

  impl.foo(EasyMock.same(controller), EasyMock.same(request),
      EasyMock.same(specializedCallback));
  EasyMock.expectLastCall();

  control.replay();

  service.callMethod(fooMethod, controller, request, callback);

  control.verify();
}
 
Example #8
Source File: TestStandardTestChooser.java    From proctor with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultAllocationWithNonEmptyRule_fallback() {
    final String testName = "test";

    final ConsumableTestDefinition testDefinition = new ConsumableTestDefinition();
    testDefinition.setConstants(Collections.<String, Object>emptyMap());
    testDefinition.setTestType(TestType.ANONYMOUS_USER);
    testDefinition.setSalt(testName);

    testDefinition.setBuckets(INACTIVE_CONTROL_TEST_BUCKETS);

    final List<Allocation> allocations = Lists.newArrayList();
    allocations.add(new Allocation("${country == 'US'}", RANGES_100_0, "#B1"));
    allocations.add(new Allocation("${country == 'GB'}", RANGES_100_0, "#C1"));
    testDefinition.setAllocations(allocations);

    final RuleEvaluator ruleEvaluator = newRuleEvaluator(false);
    final TestRangeSelector selector = new TestRangeSelector(
        ruleEvaluator,
        testName,
        testDefinition
    );

    final TestChooser.Result chooseResult = new StandardTestChooser(selector)
        .choose("identifier", Collections.<String, Object>emptyMap());

    assertNotNull(chooseResult);
    assertNull("Expected no bucket to be found", chooseResult.getTestBucket());
    assertNull("Expected no allocation to be found", chooseResult.getAllocation());

    EasyMock.verify(ruleEvaluator);
}
 
Example #9
Source File: TestStandardTestChooser.java    From proctor with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultAllocationWithNonEmptyRule_match() {
    final String testName = "test";

    final ConsumableTestDefinition testDefinition = new ConsumableTestDefinition();
    testDefinition.setConstants(Collections.<String, Object>emptyMap());
    testDefinition.setRule("${lang == 'en'}");

    testDefinition.setTestType(TestType.ANONYMOUS_USER);
    testDefinition.setSalt(testName);

    testDefinition.setBuckets(INACTIVE_CONTROL_TEST_BUCKETS);

    final List<Allocation> allocations = Lists.newArrayList();
    allocations.add(new Allocation("${country == 'GB'}", RANGES_100_0, "#B1"));
    testDefinition.setAllocations(allocations);

    final RuleEvaluator ruleEvaluator = newRuleEvaluator(true);
    final TestRangeSelector selector = new TestRangeSelector(
        ruleEvaluator,
        testName,
        testDefinition
    );

    final TestChooser.Result chooseResult = new StandardTestChooser(selector)
        .choose("identifier", Collections.<String, Object>emptyMap());

    assertEquals("Test bucket with value 1 expected", 1, chooseResult.getTestBucket().getValue());
    assertEquals("Test allocation with id #B1 expected", "#B1", chooseResult.getAllocation().getId());

    EasyMock.verify(ruleEvaluator);
}
 
Example #10
Source File: TestStandardTestChooser.java    From proctor with Apache License 2.0 5 votes vote down vote up
private RuleEvaluator newRuleEvaluator(final boolean result) {
    final RuleEvaluator ruleEvaluator = EasyMock.createMock(RuleEvaluator.class);
    EasyMock.expect(ruleEvaluator.evaluateBooleanRule(
        EasyMock.<String>anyObject(),
        EasyMock.<Map<String,Object>>anyObject()
    ))
        .andReturn(result)
        .anyTimes();
    EasyMock.replay(ruleEvaluator);
    return ruleEvaluator;
}
 
Example #11
Source File: FileBasedProctorStoreTest.java    From proctor with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    gitClient = EasyMock.createMock(FileBasedProctorStore.RcsClient.class);
    expect(gitClient.getRevisionControlType()).andReturn("git").anyTimes();
    gitClient.add(EasyMock.<File>anyObject());
    EasyMock.expectLastCall().anyTimes();
    replay(gitClient);
    coreMock = EasyMock.createMock(FileBasedPersisterCore.class);
    final DelegatingCore coreMockWrapper = new DelegatingCore(coreMock, temporaryFolder.getRoot(), gitClient);
    store = new TestFileBasedProctorStore(coreMockWrapper);
}
 
Example #12
Source File: FileBasedProctorStoreTest.java    From proctor with Apache License 2.0 5 votes vote down vote up
@Test
public void updateTestDefinitionTestNoChange() throws Exception {
    final Map<String, String> metadata = Maps.newHashMap();

    final File definitionFolder = store.getTestDefinitionDirectoryForTest("r0", temporaryFolder.getRoot());
    assertTrue(definitionFolder.mkdirs());
    final TestDefinition definition = new TestDefinition();
    OBJECT_MAPPER.writeValue(new File(definitionFolder, FileBasedProctorStore.TEST_DEFINITION_FILENAME), definition);
    OBJECT_MAPPER.writeValue(new File(definitionFolder, FileBasedProctorStore.TEST_METADATA_FILENAME), metadata);

    final Capture<FileBasedProctorStore.ProctorUpdater> capturedArgument = new Capture<>();
    coreMock.doInWorkingDirectory(
            eq("fooUser"),
            eq("fooPassw0rd"),
            eq("fooComment"),
            eq("r0"),
            capture(capturedArgument)
    );
    EasyMock.expectLastCall();
    replay(coreMock);

    try {
        store.updateTestDefinition("fooUser", "fooPassw0rd", "fooAuthor", "r0",
                definition, metadata, "fooComment");
        fail("Expected Exception");
    } catch (final StoreException.TestUpdateException tue) {
        assertEquals("Attempting to save test definition without changes for test r0", tue.getCause().getMessage());
    }
}
 
Example #13
Source File: ServiceTest.java    From travelguide with Apache License 2.0 4 votes vote down vote up
@Override
protected void setUp() throws Exception {
  super.setUp();
  control = EasyMock.createStrictControl();
  mockController = control.createMock(RpcController.class);
}
 
Example #14
Source File: ServiceTest.java    From travelguide with Apache License 2.0 4 votes vote down vote up
/**
 * wrapsCallback() is an EasyMock argument predicate.  wrapsCallback(c)
 * matches a callback if calling that callback causes c to be called.
 * In other words, c wraps the given callback.
 */
private <Type extends Message> RpcCallback<Type> wrapsCallback(
    MockCallback<?> callback) {
  EasyMock.reportMatcher(new WrapsCallback(callback));
  return null;
}
 
Example #15
Source File: TestStandardTestChooser.java    From proctor with Apache License 2.0 4 votes vote down vote up
@Test
public void testExceptionsDealtWith() {
    final String testName = "test";

    final ConsumableTestDefinition testDefinition = new ConsumableTestDefinition();
    testDefinition.setConstants(Collections.<String, Object>emptyMap());
    testDefinition.setRule("${lang == 'en'}");

    testDefinition.setTestType(TestType.ANONYMOUS_USER);

    // most tests just set the salt to be the same as the test name
    testDefinition.setSalt(testName);
    testDefinition.setBuckets(Collections.<TestBucket>emptyList());

    final RuleEvaluator ruleEvaluator = EasyMock.createMock(RuleEvaluator.class);
    EasyMock.expect(ruleEvaluator.evaluateBooleanRule(
            EasyMock.<String>anyObject(),
            EasyMock.<Map<String,Object>>anyObject()
    ))
            // throw an unexpected type of runtime exception
            .andThrow(new RuntimeException() {})
            // Must be evaluated, or this was not a valid test
            .once();
    EasyMock.replay(ruleEvaluator);

    final TestRangeSelector selector = new TestRangeSelector(
            ruleEvaluator,
            testName,
            testDefinition
    );

    // Ensure no exceptions thrown.
    final TestChooser.Result chooseResult = new StandardTestChooser(selector)
            .choose("identifier", Collections.<String, Object>emptyMap());

    assertNotNull(chooseResult);
    assertNull( "Expected no bucket to be found ", chooseResult.getTestBucket());
    assertNull( "Expected no allocation to be found ", chooseResult.getAllocation());

    EasyMock.verify(ruleEvaluator);
}