use of org.powermock.core.classloader.annotations.PrepareForTest in project hudson-2.x by hudson.
the class FunctionsTest method testIsAuthorTrue.
@Test
@PrepareForTest(User.class)
public void testIsAuthorTrue() throws Exception {
mockStatic(User.class);
User user = createMock(User.class);
expect(user.getId()).andReturn(USER);
expect(User.current()).andReturn(user);
Job job = createMock(FreeStyleProject.class);
expect(job.getCreatedBy()).andReturn(USER).times(2);
replay(User.class, user, job);
boolean result = Functions.isAuthor(job);
verify(User.class, user, job);
assertTrue(result);
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.
the class ConfigDriveBuilderTest method generateAndRetrieveIsoAsBase64IsoTestGenIsoFailure.
@SuppressWarnings("unchecked")
@Test(expected = CloudRuntimeException.class)
@PrepareForTest({ Script.class, ConfigDriveBuilder.class })
public void generateAndRetrieveIsoAsBase64IsoTestGenIsoFailure() throws Exception {
PowerMockito.mockStatic(Script.class, ConfigDriveBuilder.class);
Script scriptMock = Mockito.mock(Script.class);
PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(scriptMock);
Mockito.doReturn("scriptMessage").when(scriptMock).execute();
Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();
Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso("isoFileName", "driveLabel", "tempDirName");
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.
the class ConfigDriveBuilderTest method buildConfigDriveTest.
@Test
@SuppressWarnings("unchecked")
@PrepareForTest({ ConfigDriveBuilder.class })
public void buildConfigDriveTest() throws Exception {
PowerMockito.mockStatic(ConfigDriveBuilder.class);
Method writeVendorAndNetworkEmptyJsonFileMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("writeVendorAndNetworkEmptyJsonFile")).iterator().next();
PowerMockito.doNothing().when(ConfigDriveBuilder.class, writeVendorAndNetworkEmptyJsonFileMethod).withArguments(Mockito.any(File.class));
Method writeVmMetadataMethod = getWriteVmMetadataMethod();
PowerMockito.doNothing().when(ConfigDriveBuilder.class, writeVmMetadataMethod).withArguments(Mockito.anyListOf(String[].class), Mockito.anyString(), Mockito.any(File.class));
Method linkUserDataMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("linkUserData")).iterator().next();
PowerMockito.doNothing().when(ConfigDriveBuilder.class, linkUserDataMethod).withArguments(Mockito.anyString());
Method generateAndRetrieveIsoAsBase64IsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
PowerMockito.doReturn("mockIsoDataBase64").when(ConfigDriveBuilder.class, generateAndRetrieveIsoAsBase64IsoMethod).withArguments(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
// force execution of real method
PowerMockito.when(ConfigDriveBuilder.class, new ArrayList<>(), "teste", "C:").thenCallRealMethod();
String returnedIsoData = ConfigDriveBuilder.buildConfigDrive(new ArrayList<>(), "teste", "C:");
Assert.assertEquals("mockIsoDataBase64", returnedIsoData);
PowerMockito.verifyStatic(ConfigDriveBuilder.class);
ConfigDriveBuilder.writeVendorAndNetworkEmptyJsonFile(Mockito.any(File.class));
ConfigDriveBuilder.writeVmMetadata(Mockito.anyListOf(String[].class), Mockito.anyString(), Mockito.any(File.class));
ConfigDriveBuilder.linkUserData(Mockito.anyString());
ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.
the class ConfigDriveBuilderTest method generateAndRetrieveIsoAsBase64IsoTest.
@Test
@SuppressWarnings("unchecked")
@PrepareForTest({ File.class, Script.class, ConfigDriveBuilder.class })
public void generateAndRetrieveIsoAsBase64IsoTest() throws Exception {
PowerMockito.mockStatic(File.class, Script.class, ConfigDriveBuilder.class);
File fileMock = Mockito.mock(File.class);
PowerMockito.whenNew(File.class).withArguments("tempDirName", "isoFileName").thenReturn(fileMock);
Script scriptMock = Mockito.mock(Script.class);
PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(scriptMock);
Mockito.when(fileMock.getAbsolutePath()).thenReturn("absolutePath");
Mockito.doReturn(StringUtils.EMPTY).when(scriptMock).execute();
Mockito.doReturn(64L * 1024L * 1024L).when(fileMock).length();
Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();
Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso("isoFileName", "driveLabel", "tempDirName");
InOrder inOrder = Mockito.inOrder(scriptMock);
inOrder.verify(scriptMock).add("-o", "absolutePath");
inOrder.verify(scriptMock).add("-ldots");
inOrder.verify(scriptMock).add("-allow-lowercase");
inOrder.verify(scriptMock).add("-allow-multidot");
inOrder.verify(scriptMock).add("-cache-inodes");
inOrder.verify(scriptMock).add("-l");
inOrder.verify(scriptMock).add("-quiet");
inOrder.verify(scriptMock).add("-J");
inOrder.verify(scriptMock).add("-r");
inOrder.verify(scriptMock).add("-V", "driveLabel");
inOrder.verify(scriptMock).add("tempDirName");
inOrder.verify(scriptMock).execute();
PowerMockito.verifyStatic(ConfigDriveBuilder.class);
ConfigDriveBuilder.fileToBase64String(nullable(File.class));
}
use of org.powermock.core.classloader.annotations.PrepareForTest in project cloudstack by apache.
the class ConfigDriveBuilderTest method generateAndRetrieveIsoAsBase64IsoTestIsoTooBig.
@SuppressWarnings("unchecked")
@Test(expected = CloudRuntimeException.class)
@PrepareForTest({ File.class, Script.class, ConfigDriveBuilder.class })
public void generateAndRetrieveIsoAsBase64IsoTestIsoTooBig() throws Exception {
PowerMockito.mockStatic(File.class, Script.class, ConfigDriveBuilder.class);
File fileMock = Mockito.mock(File.class);
PowerMockito.whenNew(File.class).withAnyArguments().thenReturn(fileMock);
Script scriptMock = Mockito.mock(Script.class);
PowerMockito.whenNew(Script.class).withAnyArguments().thenReturn(scriptMock);
Mockito.doReturn(StringUtils.EMPTY).when(scriptMock).execute();
Mockito.doReturn(64L * 1024L * 1024L + 1l).when(fileMock).length();
Method method = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("generateAndRetrieveIsoAsBase64Iso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, method).withArguments(nullable(String.class), nullable(String.class), nullable(String.class)).thenCallRealMethod();
Method getProgramToGenerateIsoMethod = ReflectionUtils.getMethods(ConfigDriveBuilder.class, ReflectionUtils.withName("getProgramToGenerateIso")).iterator().next();
PowerMockito.when(ConfigDriveBuilder.class, getProgramToGenerateIsoMethod).withNoArguments().thenReturn("/usr/bin/genisoimage");
ConfigDriveBuilder.generateAndRetrieveIsoAsBase64Iso("isoFileName", "driveLabel", "tempDirName");
}
Aggregations