Search in sources :

Example 21 with IMocksControl

use of org.easymock.IMocksControl in project guice by google.

the class ContextPathTest method testSimple.

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();
    guiceFilter.doFilter(req, res, testFilterChain);
    assertFalse(testFilterChain.isTriggered());
    assertFalse(fooServlet.isTriggered());
    assertTrue(barServlet.isTriggered());
    testControl.verify();
}
Also used : IMocksControl(org.easymock.IMocksControl) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 22 with IMocksControl

use of org.easymock.IMocksControl in project cobar by alibaba.

the class JdbcTest method test.

public void test() {
    IMocksControl control = EasyMock.createControl();
    DBUtility mockDBUtility = control.createMock(DBUtility.class);
    Connection mockConnection = control.createMock(Connection.class);
    Statement mockStatement = control.createMock(Statement.class);
    ResultSet mockResultSet = control.createMock(ResultSet.class);
    try {
        mockDBUtility.getConnection();
        EasyMock.expectLastCall().andStubReturn(mockConnection);
        mockConnection.createStatement();
        EasyMock.expectLastCall().andStubReturn(mockStatement);
        mockStatement.executeQuery(SQLEquals.sqlEquals("SELECT * FROM sales_order_table"));
        EasyMock.expectLastCall().andStubReturn(mockResultSet);
        mockResultSet.next();
        EasyMock.expectLastCall().andReturn(true).times(3);
        EasyMock.expectLastCall().andReturn(false).times(1);
        mockResultSet.getString(1);
        EasyMock.expectLastCall().andReturn("DEMO_ORDER_001").times(1);
        EasyMock.expectLastCall().andReturn("DEMO_ORDER_002").times(1);
        EasyMock.expectLastCall().andReturn("DEMO_ORDER_003").times(1);
        mockResultSet.getString(2);
        EasyMock.expectLastCall().andReturn("Asia Pacific").times(1);
        EasyMock.expectLastCall().andReturn("Europe").times(1);
        EasyMock.expectLastCall().andReturn("America").times(1);
        mockResultSet.getDouble(3);
        EasyMock.expectLastCall().andReturn(350.0).times(1);
        EasyMock.expectLastCall().andReturn(1350.0).times(1);
        EasyMock.expectLastCall().andReturn(5350.0).times(1);
        control.replay();
        Connection conn = mockDBUtility.getConnection();
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery("select * from sales_order_table");
        int i = 0;
        String[] priceLevels = { "Level_A", "Level_C", "Level_E" };
        while (rs.next()) {
            SalesOrder order = new SalesOrderImpl();
            order.loadDataFromDB(rs);
            assertEquals(order.getPriceLevel(), priceLevels[i]);
            i++;
        }
        control.verify();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : IMocksControl(org.easymock.IMocksControl) Statement(java.sql.Statement) DBUtility(com.alibaba.cobar.manager.jdbcmock.domain.DBUtility) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SalesOrderImpl(com.alibaba.cobar.manager.jdbcmock.domain.SalesOrderImpl) SalesOrder(com.alibaba.cobar.manager.jdbcmock.domain.SalesOrder)

Example 23 with IMocksControl

use of org.easymock.IMocksControl in project titan by thinkaurelius.

the class IDPoolTest method testAllocationTimeoutAndRecovery.

@Test
public void testAllocationTimeoutAndRecovery() throws BackendException {
    IMocksControl ctrl = EasyMock.createStrictControl();
    final int partition = 42;
    final int idNamespace = 777;
    final Duration timeout = Duration.ofSeconds(1L);
    final IDAuthority mockAuthority = ctrl.createMock(IDAuthority.class);
    // Sleep for two seconds, then throw a backendexception
    // this whole delegate could be deleted if we abstracted StandardIDPool's internal executor and stopwatches
    expect(mockAuthority.getIDBlock(partition, idNamespace, timeout)).andDelegateTo(new IDAuthority() {

        @Override
        public IDBlock getIDBlock(int partition, int idNamespace, Duration timeout) throws BackendException {
            try {
                Thread.sleep(2000L);
            } catch (InterruptedException e) {
                fail();
            }
            throw new TemporaryBackendException("slow backend");
        }

        @Override
        public List<KeyRange> getLocalIDPartition() throws BackendException {
            throw new IllegalArgumentException();
        }

        @Override
        public void setIDBlockSizer(IDBlockSizer sizer) {
            throw new IllegalArgumentException();
        }

        @Override
        public void close() throws BackendException {
            throw new IllegalArgumentException();
        }

        @Override
        public String getUniqueID() {
            throw new IllegalArgumentException();
        }

        @Override
        public boolean supportsInterruption() {
            return true;
        }
    });
    expect(mockAuthority.getIDBlock(partition, idNamespace, timeout)).andReturn(new IDBlock() {

        @Override
        public long numIds() {
            return 2;
        }

        @Override
        public long getId(long index) {
            return 200;
        }
    });
    expect(mockAuthority.supportsInterruption()).andStubReturn(true);
    ctrl.replay();
    StandardIDPool pool = new StandardIDPool(mockAuthority, partition, idNamespace, Integer.MAX_VALUE, timeout, 0.1);
    try {
        pool.nextID();
        fail();
    } catch (TitanException e) {
    }
    long nextID = pool.nextID();
    assertEquals(200, nextID);
    ctrl.verify();
}
Also used : IDBlockSizer(com.thinkaurelius.titan.graphdb.database.idassigner.IDBlockSizer) Duration(java.time.Duration) BackendException(com.thinkaurelius.titan.diskstorage.BackendException) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) IMocksControl(org.easymock.IMocksControl) StandardIDPool(com.thinkaurelius.titan.graphdb.database.idassigner.StandardIDPool) TemporaryBackendException(com.thinkaurelius.titan.diskstorage.TemporaryBackendException) IDAuthority(com.thinkaurelius.titan.diskstorage.IDAuthority) IDBlock(com.thinkaurelius.titan.diskstorage.IDBlock) TitanException(com.thinkaurelius.titan.core.TitanException) List(java.util.List) Test(org.junit.Test)

Example 24 with IMocksControl

use of org.easymock.IMocksControl in project powermock by powermock.

the class PowerMock method doMock.

@SuppressWarnings("unchecked")
private static <T> T doMock(Class<T> type, boolean isStatic, MockStrategy mockStrategy, ConstructorArgs constructorArgs, Method... methods) {
    if (type == null) {
        throw new IllegalArgumentException("The class to mock cannot be null");
    }
    /*
         * Clear the EasyMock state after the test method is executed.
         */
    MockRepository.addAfterMethodRunner(new Runnable() {

        @Override
        public void run() {
            LastControl.reportLastControl(null);
        }
    });
    IMocksControl control = mockStrategy.createMockControl(type);
    MockRepository.addAfterMethodRunner(new EasyMockStateCleaner());
    T mock;
    if (type.isInterface()) {
        mock = control.createMock(type);
    } else if (type.getName().startsWith("java.") && Modifier.isFinal(type.getModifiers())) {
        Class<?> replicaType = createReplicaType(type, isStatic, constructorArgs);
        final Object replica = doCreateMock(replicaType, constructorArgs, control, methods);
        control = mockStrategy.createMockControl(replicaType);
        MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);
        final Set<Method> methodsToMock = toSet(methods);
        if (isStatic) {
            MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));
            MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);
            return null;
        } else {
            final T newInstance;
            if (constructorArgs == null) {
                newInstance = Whitebox.newInstance(type);
                DefaultFieldValueGenerator.fillWithDefaultValues(newInstance);
            } else {
                try {
                    newInstance = (T) constructorArgs.getConstructor().newInstance(constructorArgs.getInitArgs());
                } catch (Exception e) {
                    throw new RuntimeException("Internal error", e);
                }
            }
            MockRepository.putInstanceMethodInvocationControl(newInstance, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica));
            if (!(newInstance instanceof InvocationSubstitute<?>)) {
                MockRepository.addObjectsToAutomaticallyReplayAndVerify(newInstance);
            }
            return newInstance;
        }
    } else {
        mock = doCreateMock(type, constructorArgs, control, methods);
    }
    MockInvocationHandler h = new MockInvocationHandler((MocksControl) control);
    final Set<Method> methodsToMock = toSet(methods);
    if (isStatic) {
        MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<T>(h, methodsToMock, mock));
        MockRepository.addObjectsToAutomaticallyReplayAndVerify(type);
    } else {
        MockRepository.putInstanceMethodInvocationControl(mock, new EasyMockMethodInvocationControl<T>(h, methodsToMock));
        if (!(mock instanceof InvocationSubstitute<?>)) {
            MockRepository.addObjectsToAutomaticallyReplayAndVerify(mock);
        }
    }
    ClassLoader classLoader = mock.getClass().getClassLoader();
    if (classLoader instanceof MockClassLoader) {
        ((MockClassLoader) classLoader).cache(mock.getClass());
    }
    return mock;
}
Also used : IMocksControl(org.easymock.IMocksControl) MocksControl(org.easymock.internal.MocksControl) MockClassLoader(org.powermock.core.classloader.MockClassLoader) EasyMockMethodInvocationControl(org.powermock.api.easymock.internal.invocationcontrol.EasyMockMethodInvocationControl) IMocksControl(org.easymock.IMocksControl) MockInvocationHandler(org.easymock.internal.MockInvocationHandler) InvocationSubstitute(org.powermock.core.spi.support.InvocationSubstitute) MockClassLoader(org.powermock.core.classloader.MockClassLoader)

Example 25 with IMocksControl

use of org.easymock.IMocksControl in project powermock by powermock.

the class PowerMock method doCreateMock.

private static <T> T doCreateMock(Class<T> type, ConstructorArgs constructorArgs, final IMocksControl control, Method... methods) {
    T mock;
    MocksControl mocksControl = ((MocksControl) control);
    if (constructorArgs == null) {
        if (methods == null) {
            mock = mocksControl.createMock(type);
        } else {
            mock = mocksControl.createMock(null, type, null, methods);
        }
    } else {
        if (methods == null) {
            mock = mocksControl.createMock(null, type, constructorArgs);
        } else {
            mock = mocksControl.createMock(null, type, constructorArgs, methods);
        }
    }
    return mock;
}
Also used : IMocksControl(org.easymock.IMocksControl) MocksControl(org.easymock.internal.MocksControl)

Aggregations

IMocksControl (org.easymock.IMocksControl)57 Test (org.junit.Test)34 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Message (org.apache.cxf.message.Message)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 MessageImpl (org.apache.cxf.message.MessageImpl)9 QName (javax.xml.namespace.QName)7 BindingFactoryManager (org.apache.cxf.binding.BindingFactoryManager)7 Before (org.junit.Before)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Set (java.util.Set)6 Exchange (org.apache.cxf.message.Exchange)6 FakeHttpServletRequest (com.google.gerrit.util.http.testutil.FakeHttpServletRequest)5 FakeHttpServletResponse (com.google.gerrit.util.http.testutil.FakeHttpServletResponse)5 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Binding (javax.xml.ws.Binding)5 Handler (javax.xml.ws.handler.Handler)5 MessageContext (javax.xml.ws.handler.MessageContext)5