Search in sources :

Example 86 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.

the class NetworkACLServiceImplTest method updateNetworkACLTestParametersWithNullValues.

@Test
@PrepareForTest(CallContext.class)
public void updateNetworkACLTestParametersWithNullValues() {
    Mockito.when(updateNetworkACLListCmdMock.getName()).thenReturn(null);
    Mockito.when(updateNetworkACLListCmdMock.getDescription()).thenReturn(null);
    Mockito.when(updateNetworkACLListCmdMock.getCustomId()).thenReturn(null);
    Mockito.when(updateNetworkACLListCmdMock.getId()).thenReturn(networkAclListId);
    Mockito.when(updateNetworkACLListCmdMock.getDisplay()).thenReturn(null);
    networkAclServiceImpl.updateNetworkACL(updateNetworkACLListCmdMock);
    InOrder inOrder = Mockito.inOrder(networkAclDaoMock, entityManagerMock, accountManagerMock, networkACLVOMock);
    inOrder.verify(networkAclDaoMock).findById(networkAclListId);
    inOrder.verify(entityManagerMock).findById(eq(Vpc.class), Mockito.anyLong());
    inOrder.verify(accountManagerMock).checkAccess(any(Account.class), isNull(), eq(true), nullable(Vpc.class));
    Mockito.verify(networkACLVOMock, Mockito.times(0)).setName(null);
    inOrder.verify(networkACLVOMock, Mockito.times(0)).setDescription(null);
    inOrder.verify(networkACLVOMock, Mockito.times(0)).setUuid(null);
    inOrder.verify(networkACLVOMock, Mockito.times(0)).setDisplay(false);
    inOrder.verify(networkAclDaoMock).update(networkAclListId, networkACLVOMock);
    inOrder.verify(networkAclDaoMock).findById(networkAclListId);
}
Also used : Account(com.cloud.user.Account) InOrder(org.mockito.InOrder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 87 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.

the class NfsSecondaryStorageResourceTest method testSwiftWriteMetadataFile.

@Test
@PrepareForTest(NfsSecondaryStorageResource.class)
public void testSwiftWriteMetadataFile() throws Exception {
    String filename = "testfile";
    try {
        String expected = "uniquename=test\nfilename=" + filename + "\nsize=100\nvirtualsize=1000";
        StringWriter stringWriter = new StringWriter();
        BufferedWriter bufferWriter = new BufferedWriter(stringWriter);
        PowerMockito.whenNew(BufferedWriter.class).withArguments(any(FileWriter.class)).thenReturn(bufferWriter);
        resource.swiftWriteMetadataFile(filename, "test", filename, 100, 1000);
        Assert.assertEquals(expected, stringWriter.toString());
    } finally {
        File remnance = new File(filename);
        remnance.delete();
    }
}
Also used : StringWriter(java.io.StringWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 88 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.

the class UriUtilsParametrizedTest method validateUrl.

@Test
@PrepareForTest({ UriUtils.class })
public void validateUrl() throws Exception {
    InetAddress inetAddressMock = Mockito.mock(InetAddress.class);
    PowerMockito.mockStatic(InetAddress.class);
    PowerMockito.when(InetAddress.getByName(Mockito.anyString())).thenReturn(inetAddressMock);
    if (expectSuccess) {
        UriUtils.validateUrl(format, url);
    } else {
        assertThrows(() -> UriUtils.validateUrl(format, url), IllegalArgumentException.class);
    }
    PowerMockito.verifyStatic(InetAddress.class);
    InetAddress.getByName(Mockito.anyString());
    Mockito.verify(inetAddressMock).isAnyLocalAddress();
    Mockito.verify(inetAddressMock).isLinkLocalAddress();
    Mockito.verify(inetAddressMock).isLoopbackAddress();
    Mockito.verify(inetAddressMock).isMulticastAddress();
}
Also used : InetAddress(java.net.InetAddress) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 89 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project canal by alibaba.

the class UtilTest method parseDate2InputNotNullOutputNull.

// Test written by Diffblue Cover.
@PrepareForTest(StringUtils.class)
@Test
public void parseDate2InputNotNullOutputNull() throws Exception {
    // Setup mocks
    PowerMockito.mockStatic(StringUtils.class);
    // Arrange
    final String datetimeStr = "1a 2b 3c";
    final Method isEmptyMethod = DTUMemberMatcher.method(StringUtils.class, "isEmpty", String.class);
    PowerMockito.doReturn(true).when(StringUtils.class, isEmptyMethod).withArguments(or(isA(String.class), isNull(String.class)));
    // Act
    final Date actual = Util.parseDate2(datetimeStr);
    // Assert result
    Assert.assertNull(actual);
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) Method(java.lang.reflect.Method) Date(java.util.Date) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 90 with PrepareForTest

use of org.powermock.core.classloader.annotations.PrepareForTest in project alluxio by Alluxio.

the class OSSOutputStreamTest method testConstructor.

/**
 * Tests to ensure IOException is thrown if {@link FileOutputStream}() throws an IOException.
 */
@Test
@PrepareForTest(OSSOutputStream.class)
public void testConstructor() throws Exception {
    PowerMockito.whenNew(File.class).withArguments(Mockito.anyString()).thenReturn(mFile);
    String errorMessage = "protocol doesn't support output";
    PowerMockito.whenNew(FileOutputStream.class).withArguments(mFile).thenThrow(new IOException(errorMessage));
    mThrown.expect(IOException.class);
    mThrown.expectMessage(errorMessage);
    new OSSOutputStream("testBucketName", "testKey", mOssClient, sConf.getList(PropertyKey.TMP_DIRS, ",")).close();
}
Also used : IOException(java.io.IOException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)196 Test (org.junit.Test)194 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)24 ArrayList (java.util.ArrayList)16 LogChannelInterface (org.pentaho.di.core.logging.LogChannelInterface)14 Method (java.lang.reflect.Method)13 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)12 Config (com.twitter.heron.spi.common.Config)12 Matchers.anyString (org.mockito.Matchers.anyString)12 DialogInterface (android.content.DialogInterface)11 Intent (android.content.Intent)11 SchedulerStateManagerAdaptor (com.twitter.heron.spi.statemgr.SchedulerStateManagerAdaptor)11 Job (hudson.model.Job)11 IOException (java.io.IOException)11 Date (java.util.Date)10 HashMap (java.util.HashMap)10