Java Code Examples for org.easymock.IMocksControl

The following examples show how to use org.easymock.IMocksControl. These examples are extracted from open source projects. 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
public void setUp() throws Exception
{
    super.setUp();
    migrationRunnerStrategy = new OrderedMigrationRunnerStrategy();
    allMigrationTasks = new ArrayList<MigrationTask>();
    IMocksControl mockControl = createControl();
    currentPatchInfoStore = mockControl.createMock(PatchInfoStore.class);
    allMigrationTasks.add(new TestRollbackableTask1());
    allMigrationTasks.add(new TestRollbackableTask2());
    allMigrationTasks.add(new TestRollbackableTask3());
    allMigrationTasks.add(new TestRollbackableTask4());
    allMigrationTasks.add(new TestRollbackableTask5());
    expect(currentPatchInfoStore.getPatchLevel()).andReturn(12);
    mockControl.replay();

}
 
Example 2
Source Project: dagger-servlet   Source File: ContextPathTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() throws Exception {
    IMocksControl testControl = createControl();
    TestFilterChain testFilterChain = new TestFilterChain();
    HttpServletRequest req = testControl.createMock(HttpServletRequest.class);
    HttpServletResponse res = testControl.createMock(HttpServletResponse.class);

    expect(req.getMethod()).andReturn("GET").anyTimes();
    expect(req.getRequestURI()).andReturn("/bar/foo").anyTimes();
    expect(req.getServletPath()).andReturn("/bar/foo").anyTimes();
    expect(req.getContextPath()).andReturn("").anyTimes();

    testControl.replay();

    daggerFilter.doFilter(req, res, testFilterChain);

    assertFalse(testFilterChain.isTriggered());
    assertFalse(fooServlet.isTriggered());
    assertTrue(barServlet.isTriggered());

    testControl.verify();
}
 
Example 3
Source Project: todo-apps   Source File: CloudantStoreTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGet() throws Exception {
  IMocksControl control = createControl();
  Response resp = control.createMock(Response.class);
  expect(resp.getStatus()).andReturn(200).times(3);
  Capture<Class<CloudantToDo>> classCapture = new Capture<Class<CloudantToDo>>();
  expect(resp.readEntity(capture(classCapture))).andReturn(ctd1);
  replay(resp);
  WebTarget wt = createMockWebTarget();
  Invocation.Builder builder = createBuilder();
  expect(builder.get()).andReturn(resp).times(3);
  replay(builder);
  expect(wt.path(eq("bluemix-todo"))).andReturn(wt).times(2);
  expect(wt.path(eq("todos"))).andReturn(wt);
  expect(wt.path(eq("_design"))).andReturn(wt).times(1);
  expect(wt.path(eq("123"))).andReturn(wt);
  expect(wt.request(eq("application/json"))).andReturn(builder).anyTimes();
  replay(wt);
  CloudantStore store = new CloudantStore(wt);
  assertEquals(ctd1.getToDo(), store.get("123"));
  assertEquals(CloudantToDo.class, classCapture.getValue());
  verify(resp);
  verify(wt);
  verify(builder);
}
 
Example 4
Source Project: todo-apps   Source File: CloudantStoreTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCount() throws Exception {
  IMocksControl control = createControl();
  Response resp = control.createMock(Response.class);
  expect(resp.getStatus()).andReturn(200).times(3);
  Capture<Class<CloudantCount>> classCapture = new Capture<Class<CloudantCount>>();
  expect(resp.readEntity(capture(classCapture))).andReturn(count);
  replay(resp);
  WebTarget wt = createMockWebTarget();
  Invocation.Builder builder = createBuilder();
  expect(builder.get()).andReturn(resp).times(3);
  replay(builder);
  expect(wt.path(eq("bluemix-todo"))).andReturn(wt).times(2);
  expect(wt.path(eq("todos"))).andReturn(wt).times(2);
  expect(wt.path(eq("_design"))).andReturn(wt).times(2);
  expect(wt.path(eq("_view"))).andReturn(wt);
  expect(wt.path(eq("allTodos"))).andReturn(wt);
  expect(wt.request(eq("application/json"))).andReturn(builder).anyTimes();
  replay(wt);
  CloudantStore store = new CloudantStore(wt);
  assertEquals(123, store.count());
  assertEquals(CloudantCount.class, classCapture.getValue());
  verify(resp);
  verify(wt);
  verify(builder);
}
 
Example 5
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void sqlArgLongPositional() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);
  ResultSet rs = control.createMock(ResultSet.class);

  expect(c.prepareStatement("select a from b where c=?")).andReturn(ps);
  ps.setObject(eq(1), eq(new Long(1)));
  expect(ps.executeQuery()).andReturn(rs);
  expect(rs.next()).andReturn(false);
  rs.close();
  ps.close();

  control.replay();

  assertNull(new DatabaseImpl(c, options).toSelect("select a from b where c=?").argLong(1L).queryLongOrNull());

  control.verify();
}
 
Example 6
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void sqlArgLongNull() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);
  ResultSet rs = control.createMock(ResultSet.class);

  expect(c.prepareStatement("select a from b where c=?")).andReturn(ps);
  ps.setNull(eq(1), eq(Types.NUMERIC));
  expect(ps.executeQuery()).andReturn(rs);
  expect(rs.next()).andReturn(false);
  rs.close();
  ps.close();

  control.replay();

  assertNull(new DatabaseImpl(c, options).toSelect("select a from b where c=?").argLong(null).queryLongOrNull());

  control.verify();
}
 
Example 7
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void settingTimeout() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);
  ResultSet rs = control.createMock(ResultSet.class);

  expect(c.prepareStatement("select a from b")).andReturn(ps);
  ps.setQueryTimeout(21);
  expect(ps.executeQuery()).andReturn(rs);
  expect(rs.next()).andReturn(false);
  rs.close();
  ps.close();

  control.replay();

  assertNull(new DatabaseImpl(c, options).toSelect("select a from b").withTimeoutSeconds(21).queryLongOrNull());

  control.verify();
}
 
Example 8
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void settingMaxRows() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);
  ResultSet rs = control.createMock(ResultSet.class);

  expect(c.prepareStatement("select a from b")).andReturn(ps);
  ps.setMaxRows(15);
  expect(ps.executeQuery()).andReturn(rs);
  expect(rs.next()).andReturn(false);
  rs.close();
  ps.close();

  control.replay();

  assertNull(new DatabaseImpl(c, options).toSelect("select a from b").withMaxRows(15).queryLongOrNull());

  control.verify();
}
 
Example 9
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void sqlArgLongNamed() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);
  ResultSet rs = control.createMock(ResultSet.class);

  expect(c.prepareStatement("select ':a' from b where c=?")).andReturn(ps);
  ps.setObject(eq(1), eq(new Long(1)));
  expect(ps.executeQuery()).andReturn(rs);
  expect(rs.next()).andReturn(false);
  rs.close();
  ps.close();

  control.replay();

  assertNull(new DatabaseImpl(c, options).toSelect("select '::a' from b where c=:c").argLong("c", 1L).queryLongOrNull());

  control.verify();
}
 
Example 10
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void missingPositionalParameter() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);

  control.replay();

  try {
    Long value = new DatabaseImpl(c, new OptionsDefault(Flavor.postgresql) {
      int errors = 0;

      @Override
      public String generateErrorCode() {
        errors++;
        return Integer.toString(errors);
      }
    }).toSelect("select a from b where c=?").queryLongOrNull();
    fail("Should have thrown an exception, but returned " + value);
  } catch (DatabaseException e) {
    assertEquals("Error executing SQL (errorCode=1)", e.getMessage());
  }

  control.verify();
}
 
Example 11
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void missingNamedParameter2() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);

  control.replay();

  try {
    Long value = new DatabaseImpl(c, options).toSelect("select a from b where c=:x and d=:y")
        .argString("x", "hi").queryLongOrNull();
    fail("Should have thrown an exception but returned " + value);
  } catch (DatabaseException e) {
    assertEquals("Error executing SQL (errorCode=1)", e.getMessage());
  }

  control.verify();
}
 
Example 12
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void extraNamedParameter() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);

  control.replay();

  try {
    Long value = new DatabaseImpl(c, options).toSelect("select a from b where c=:x")
        .argString("x", "hi").argString("y", "bye").queryLongOrNull();
    fail("Should have thrown an exception but returned " + value);
  } catch (DatabaseException e) {
    assertEquals("Error executing SQL (errorCode=1)", e.getMessage());
  }

  control.verify();
}
 
Example 13
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void mixedParameterTypes() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);
  ResultSet rs = control.createMock(ResultSet.class);

  expect(c.prepareStatement("select a from b where c=? and d=?")).andReturn(ps);
  ps.setObject(eq(1), eq("bye"));
  ps.setNull(eq(2), eq(Types.TIMESTAMP));
  expect(ps.executeQuery()).andReturn(rs);
  expect(rs.next()).andReturn(false);
  rs.close();
  ps.close();

  control.replay();

  assertNull(new DatabaseImpl(c, options).toSelect("select a from b where c=:x and d=?")
      .argString(":x", "bye").argDate(null).queryLongOrNull());

  control.verify();
}
 
Example 14
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void mixedParameterTypesReversed() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);
  ResultSet rs = control.createMock(ResultSet.class);

  expect(c.prepareStatement("select a from b where c=? and d=?")).andReturn(ps);
  ps.setObject(eq(1), eq("bye"));
  ps.setNull(eq(2), eq(Types.TIMESTAMP));
  expect(ps.executeQuery()).andReturn(rs);
  expect(rs.next()).andReturn(false);
  rs.close();
  ps.close();

  control.replay();

  // Reverse order of args should be the same
  assertNull(new DatabaseImpl(c, options).toSelect("select a from b where c=:x and d=?")
      .argDate(null).argString(":x", "bye").queryLongOrNull());

  control.verify();
}
 
Example 15
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void wrongNumberOfInserts() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);

  expect(c.prepareStatement("insert into x (y) values (1)")).andReturn(ps);
  expect(ps.executeUpdate()).andReturn(2);
  ps.close();

  control.replay();

  try {
    new DatabaseImpl(c, options).toInsert("insert into x (y) values (1)").insert(1);
    fail("Should have thrown an exception");
  } catch (DatabaseException e) {
    assertThat(e.getMessage(), containsString("The number of affected rows was 2, but 1 were expected."));
  }

  control.verify();
}
 
Example 16
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void cancelQuery() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);
  PreparedStatement ps = control.createMock(PreparedStatement.class);

  expect(c.prepareStatement("select a from b")).andReturn(ps);
  expect(ps.executeQuery()).andThrow(new SQLException("Cancelled", "cancel", 1013));
  ps.close();

  control.replay();

  try {
    Long value = new DatabaseImpl(c, options).toSelect("select a from b").queryLongOrNull();
    fail("Should have thrown an exception but returned " + value);
  } catch (DatabaseException e) {
    assertEquals("Timeout of -1 seconds exceeded or user cancelled", e.getMessage());
  }

  control.verify();
}
 
Example 17
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactionCommitSuccess() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);

  c.commit();

  control.replay();

  new DatabaseImpl(c, new OptionsDefault(Flavor.postgresql) {
    @Override
    public boolean allowTransactionControl() {
      return true;
    }
  }).commitNow();

  control.verify();
}
 
Example 18
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactionCommitFail() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);

  c.commit();
  expectLastCall().andThrow(new SQLException("Oops"));

  control.replay();

  try {
    new DatabaseImpl(c, new OptionsDefault(Flavor.postgresql) {
      @Override
      public boolean allowTransactionControl() {
        return true;
      }
    }).commitNow();
    fail("Should have thrown an exception");
  } catch (DatabaseException e) {
    assertEquals("Unable to commit transaction", e.getMessage());
  }

  control.verify();
}
 
Example 19
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactionRollbackSuccess() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);

  c.rollback();

  control.replay();

  new DatabaseImpl(c, new OptionsDefault(Flavor.postgresql) {
    @Override
    public boolean allowTransactionControl() {
      return true;
    }
  }).rollbackNow();

  control.verify();
}
 
Example 20
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactionRollbackFail() throws Exception {
  IMocksControl control = createStrictControl();

  Connection c = control.createMock(Connection.class);

  c.rollback();
  expectLastCall().andThrow(new SQLException("Oops"));

  control.replay();

  try {
    new DatabaseImpl(c, new OptionsDefault(Flavor.postgresql) {
      @Override
      public boolean allowTransactionControl() {
        return true;
      }
    }).rollbackNow();
    fail("Should have thrown an exception");
  } catch (DatabaseException e) {
    assertEquals("Unable to rollback transaction", e.getMessage());
  }

  control.verify();
}
 
Example 21
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactCommitOnlyWithNoError() throws Exception {
  IMocksControl control = createStrictControl();

  final Connection c = control.createMock(Connection.class);

  c.setAutoCommit(false);
  c.commit();
  c.close();

  control.replay();

  new DatabaseProvider(() -> c, new OptionsDefault(Flavor.postgresql)).transact((db, tx) -> {
    tx.setRollbackOnError(false);
    db.get();
  });

  control.verify();
}
 
Example 22
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactCommitOnlyWithError() throws Exception {
  IMocksControl control = createStrictControl();

  final Connection c = control.createMock(Connection.class);

  c.setAutoCommit(false);
  c.commit();
  c.close();

  control.replay();

  try {
    new DatabaseProvider(() -> c, new OptionsDefault(Flavor.postgresql)).transact((db, tx) -> {
      tx.setRollbackOnError(false);
      db.get();
      throw new Error("Oops");
    });
    fail("Should have thrown an exception");
  } catch (Exception e) {
    assertEquals("Error during transaction", e.getMessage());
  }

  control.verify();
}
 
Example 23
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactCommitOnlyOverrideWithError() throws Exception {
  IMocksControl control = createStrictControl();

  final Connection c = control.createMock(Connection.class);

  c.setAutoCommit(false);
  c.rollback();
  c.close();

  control.replay();

  try {
    new DatabaseProvider(() -> c, new OptionsDefault(Flavor.postgresql)).transact((db, tx) -> {
      db.get();
      tx.setRollbackOnError(true);
      throw new DatabaseException("Oops");
    });
    fail("Should have thrown an exception");
  } catch (Exception e) {
    assertEquals("Oops", e.getMessage());
  }

  control.verify();
}
 
Example 24
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactCommitOnlyOverrideWithError2() throws Exception {
  IMocksControl control = createStrictControl();

  final Connection c = control.createMock(Connection.class);

  c.setAutoCommit(false);
  c.rollback();
  c.close();

  control.replay();

  try {
    new DatabaseProvider(() -> c, new OptionsDefault(Flavor.postgresql)).transact((db, tx) -> {
      db.get();
      tx.setRollbackOnError(false);
      tx.setRollbackOnly(true);
      throw new DatabaseException("Oops");
    });
    fail("Should have thrown an exception");
  } catch (Exception e) {
    assertEquals("Oops", e.getMessage());
  }

  control.verify();
}
 
Example 25
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactRollbackOnly() throws Exception {
  IMocksControl control = createStrictControl();

  final Connection c = control.createMock(Connection.class);

  c.setAutoCommit(false);
  c.rollback();
  c.close();

  control.replay();

  new DatabaseProvider(() -> c, new OptionsDefault(Flavor.postgresql)).transact((db, tx) -> {
    db.get();
    tx.setRollbackOnly(true);
  });

  control.verify();
}
 
Example 26
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactRollbackOnErrorWithError() throws Exception {
  IMocksControl control = createStrictControl();

  final Connection c = control.createMock(Connection.class);

  c.setAutoCommit(false);
  c.rollback();
  c.close();

  control.replay();

  try {
    new DatabaseProvider(() -> c, new OptionsDefault(Flavor.postgresql)).transact(db -> {
      db.get();
      throw new Exception("Oops");
    });
    fail("Should have thrown an exception");
  } catch (Exception e) {
    assertEquals("Error during transaction", e.getMessage());
  }

  control.verify();
}
 
Example 27
Source Project: database   Source File: DatabaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void transactRollbackOnErrorWithNoError() throws Exception {
  IMocksControl control = createStrictControl();

  final Connection c = control.createMock(Connection.class);

  c.setAutoCommit(false);
  c.commit();
  c.close();

  control.replay();

  new DatabaseProvider(() -> c, new OptionsDefault(Flavor.postgresql)).transact((db) -> {
    db.get();
  });

  control.verify();
}
 
Example 28
Source Project: cxf   Source File: BareInInterceptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    bus = BusFactory.newInstance().createBus();

    BindingFactoryManager bfm = bus.getExtension(BindingFactoryManager.class);

    IMocksControl control = createNiceControl();
    BindingFactory bf = control.createMock(BindingFactory.class);
    Binding binding = control.createMock(Binding.class);
    expect(bf.createBinding(null)).andStubReturn(binding);
    expect(binding.getInFaultInterceptors())
        .andStubReturn(new ArrayList<Interceptor<? extends Message>>());
    expect(binding.getOutFaultInterceptors())
        .andStubReturn(new ArrayList<Interceptor<? extends Message>>());

    bfm.registerBindingFactory("http://schemas.xmlsoap.org/wsdl/soap/", bf);

}
 
Example 29
Source Project: cxf   Source File: DocLiteralInInterceptorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    bus = BusFactory.newInstance().createBus();

    BindingFactoryManager bfm = bus.getExtension(BindingFactoryManager.class);

    IMocksControl control = createNiceControl();
    BindingFactory bf = control.createMock(BindingFactory.class);
    Binding binding = control.createMock(Binding.class);
    expect(bf.createBinding(null)).andStubReturn(binding);
    expect(binding.getInFaultInterceptors())
        .andStubReturn(new ArrayList<Interceptor<? extends Message>>());
    expect(binding.getOutFaultInterceptors())
        .andStubReturn(new ArrayList<Interceptor<? extends Message>>());

    bfm.registerBindingFactory("http://schemas.xmlsoap.org/wsdl/soap/", bf);
}
 
Example 30
Source Project: cxf   Source File: CXFBusImplTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConstructionWithExtensions() throws BusException {

    IMocksControl control;
    BindingFactoryManager bindingFactoryManager;
    InstrumentationManager instrumentationManager;
    PhaseManager phaseManager;

    control = EasyMock.createNiceControl();

    Map<Class<?>, Object> extensions = new HashMap<>();
    bindingFactoryManager = control.createMock(BindingFactoryManager.class);
    instrumentationManager = control.createMock(InstrumentationManager.class);
    phaseManager = control.createMock(PhaseManager.class);

    extensions.put(BindingFactoryManager.class, bindingFactoryManager);
    extensions.put(InstrumentationManager.class, instrumentationManager);
    extensions.put(PhaseManager.class, phaseManager);

    Bus bus = new ExtensionManagerBus(extensions);

    assertSame(bindingFactoryManager, bus.getExtension(BindingFactoryManager.class));
    assertSame(instrumentationManager, bus.getExtension(InstrumentationManager.class));
    assertSame(phaseManager, bus.getExtension(PhaseManager.class));

}