Search in sources :

Example 21 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project mobile-center-sdk-android by Microsoft.

the class AppCenterFutureTest method isDoneWithInterruption.

@Test
@PrepareForTest(DefaultAppCenterFuture.class)
public void isDoneWithInterruption() throws Exception {
    CountDownLatch latch = mock(CountDownLatch.class);
    whenNew(CountDownLatch.class).withAnyArguments().thenReturn(latch);
    when(latch.await(anyLong(), any(TimeUnit.class))).thenThrow(new InterruptedException()).thenReturn(true);
    final DefaultAppCenterFuture<Boolean> future = new DefaultAppCenterFuture<>();
    final AtomicReference<Boolean> result = new AtomicReference<>();
    Thread thread = new Thread() {

        @Override
        public void run() {
            result.set(future.isDone());
        }
    };
    thread.start();
    thread.join();
    assertEquals(true, result.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 22 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project leopard by tanhaichao.

the class LoepardMockRunnerDelegateImpl method autoMockStatic.

/**
 * 自动mock静态类.
 */
protected void autoMockStatic() {
    Description description = getDescription();
    PrepareForTest prepareForTest = description.getAnnotation(PrepareForTest.class);
    // System.out.println("prepareForTest:" + prepareForTest);
    if (prepareForTest == null) {
        return;
    }
    Class<?>[] classes = prepareForTest.value();
    mockStatic(classes);
}
Also used : Description(org.junit.runner.Description) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 23 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project pentaho-kettle by pentaho.

the class StepWithMappingMetaTest method activateParamsTest.

@Test
@PrepareForTest(StepWithMappingMeta.class)
public void activateParamsTest() throws Exception {
    String childParam = "childParam";
    String childValue = "childValue";
    String paramOverwrite = "paramOverwrite";
    String parentValue = "parentValue";
    VariableSpace parent = new Variables();
    parent.setVariable(paramOverwrite, parentValue);
    TransMeta childVariableSpace = new TransMeta();
    childVariableSpace.setParameterValue(childParam, childValue);
    String[] parameters = childVariableSpace.listParameters();
    StepWithMappingMeta.activateParams(childVariableSpace, childVariableSpace, parent, parameters, new String[] { childParam, paramOverwrite }, new String[] { childValue, childValue });
    Assert.assertEquals(childValue, childVariableSpace.getVariable(childParam));
    Assert.assertEquals(parentValue, childVariableSpace.getVariable(paramOverwrite));
}
Also used : Variables(org.pentaho.di.core.variables.Variables) VariableSpace(org.pentaho.di.core.variables.VariableSpace) Mockito.anyString(org.mockito.Mockito.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 24 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project pentaho-kettle by pentaho.

the class AllocateServerSocketServletTest method testAllocateServerSocketServletEncodesParametersForHmtlResponse.

@Test
@PrepareForTest({ Encode.class })
public void testAllocateServerSocketServletEncodesParametersForHmtlResponse() throws ServletException, IOException {
    HttpServletRequest mockRequest = mock(HttpServletRequest.class);
    HttpServletResponse mockResponse = mock(HttpServletResponse.class);
    SocketPortAllocation mockSocketPortAllocation = mock(SocketPortAllocation.class);
    PowerMockito.spy(Encode.class);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ServletOutputStream servletOutputStream = new ServletOutputStream() {

        @Override
        public void write(int b) throws IOException {
            byteArrayOutputStream.write(b);
        }
    };
    when(mockRequest.getContextPath()).thenReturn(AllocateServerSocketServlet.CONTEXT_PATH);
    when(mockRequest.getParameter(anyString())).thenReturn(ServletTestUtils.BAD_STRING_TO_TEST);
    when(mockResponse.getOutputStream()).thenReturn(servletOutputStream);
    when(mockTransformationMap.allocateServerSocketPort(anyInt(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyString())).thenReturn(mockSocketPortAllocation);
    allocateServerSocketServlet.doGet(mockRequest, mockResponse);
    String response = byteArrayOutputStream.toString();
    // Pull out dynamic part of body, remove hardcoded html
    String dynamicBody = ServletTestUtils.getInsideOfTag("BODY", response).replaceAll("<p>", "").replaceAll("<br>", "").replaceAll("<H1>.+</H1>", "").replaceAll("--> port", "");
    assertFalse(ServletTestUtils.hasBadText(dynamicBody));
    PowerMockito.verifyStatic(atLeastOnce());
    Encode.forHtml(anyString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 25 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project pentaho-kettle by pentaho.

the class CleanupTransServletTest method testCleanupTransServletEscapesHtmlWhenTransNotFound.

@Test
@PrepareForTest({ Encode.class })
public void testCleanupTransServletEscapesHtmlWhenTransNotFound() throws ServletException, IOException {
    HttpServletRequest mockHttpServletRequest = mock(HttpServletRequest.class);
    HttpServletResponse mockHttpServletResponse = mock(HttpServletResponse.class);
    StringWriter out = new StringWriter();
    PrintWriter printWriter = new PrintWriter(out);
    PowerMockito.spy(Encode.class);
    when(mockHttpServletRequest.getContextPath()).thenReturn(CleanupTransServlet.CONTEXT_PATH);
    when(mockHttpServletRequest.getParameter(anyString())).thenReturn(ServletTestUtils.BAD_STRING_TO_TEST);
    when(mockHttpServletResponse.getWriter()).thenReturn(printWriter);
    cleanupTransServlet.doGet(mockHttpServletRequest, mockHttpServletResponse);
    assertFalse(ServletTestUtils.hasBadText(ServletTestUtils.getInsideOfTag("H1", out.toString())));
    PowerMockito.verifyStatic(atLeastOnce());
    Encode.forHtml(anyString());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringWriter(java.io.StringWriter) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)188 Test (org.junit.Test)186 HttpServletRequest (javax.servlet.http.HttpServletRequest)30 HttpServletResponse (javax.servlet.http.HttpServletResponse)30 StringWriter (java.io.StringWriter)28 PrintWriter (java.io.PrintWriter)27 File (java.io.File)21 ArrayList (java.util.ArrayList)15 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)14 Method (java.lang.reflect.Method)13 Config (com.twitter.heron.spi.common.Config)12 DialogInterface (android.content.DialogInterface)11 Intent (android.content.Intent)11 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)11 Job (hudson.model.Job)11 Point (org.pentaho.di.core.gui.Point)10 Date (java.util.Date)9 HashMap (java.util.HashMap)9 Matchers.anyString (org.mockito.Matchers.anyString)9 InvocationOnMock (org.mockito.invocation.InvocationOnMock)9