Java Code Examples for org.powermock.api.easymock.PowerMock#suppressMethod()

The following examples show how to use org.powermock.api.easymock.PowerMock#suppressMethod() . 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
Source File: TestWBFreeMarkerModuleDirective.java    From cms with Apache License 2.0 5 votes vote down vote up
@Test
public void test_execute_catch_exception()
{
	Environment envMock = PowerMock.createMock(Environment.class);
	TemplateModel[] loopVars = null;
	TemplateDirectiveBody directiveBodyMock = null;
	
	Map params = new HashMap();
	String name = "testXYZ";
	StringModel nameModel = new StringModel(name, new DefaultObjectWrapper() );
	params.put("name", nameModel);

	try
	{
		
		WPBPageModule pageModuleMock = PowerMock.createMock(WPBPageModule.class);		
		WPBPageModulesCache pageModuleCacheMock = PowerMock.createMock(WPBPageModulesCache.class);
		EasyMock.expect(pageModuleCacheMock.getByExternalKey(name)).andThrow(new WPBIOException(""));
		
		EasyMock.expect(cacheInstancesMock.getPageModuleCache()).andReturn(pageModuleCacheMock);

		EasyMock.replay(cacheInstancesMock, templateEngineMock, envMock, pageModuleMock, pageModuleCacheMock);
		
		FreeMarkerModuleDirective templateDirective = new FreeMarkerModuleDirective();
		Whitebox.setInternalState(templateDirective, "templateEngine",templateEngineMock);
		Whitebox.setInternalState(templateDirective, "cacheInstances",cacheInstancesMock);
		PowerMock.suppressMethod(FreeMarkerModuleDirective.class, "copyParams");
		templateDirective.execute(envMock, params, loopVars, directiveBodyMock);
		
		assertTrue(false);		
	} catch (Exception e)
	{
		assertTrue(e instanceof TemplateModelException);
	}
}
 
Example 2
Source File: TestWBFreeMarkerModuleDirective.java    From cms with Apache License 2.0 5 votes vote down vote up
@Test
public void test_execute_noPageModule()
{
	Environment envMock = PowerMock.createMock(Environment.class);
	TemplateModel[] loopVars = null;
	TemplateDirectiveBody directiveBodyMock = null;
	
	Map params = new HashMap();
	String name = "testXYZ";
	StringModel nameModel = new StringModel(name, new DefaultObjectWrapper() );
	params.put("name", nameModel);

	try
	{

		WPBPageModule pageModuleMock = PowerMock.createMock(WPBPageModule.class);		
		WPBPageModulesCache pageModuleCacheMock = PowerMock.createMock(WPBPageModulesCache.class);
		EasyMock.expect(pageModuleCacheMock.getByExternalKey(name)).andReturn(null);
		
		EasyMock.expect(cacheInstancesMock.getPageModuleCache()).andReturn(pageModuleCacheMock);

		EasyMock.replay(cacheInstancesMock, templateEngineMock, envMock, pageModuleMock, pageModuleCacheMock);
		
		FreeMarkerModuleDirective templateDirective = new FreeMarkerModuleDirective();
		Whitebox.setInternalState(templateDirective, "templateEngine",templateEngineMock);
		Whitebox.setInternalState(templateDirective, "cacheInstances",cacheInstancesMock);
		PowerMock.suppressMethod(FreeMarkerModuleDirective.class, "copyParams");
		templateDirective.execute(envMock, params, loopVars, directiveBodyMock);
	
		assertTrue(false);
		
	} catch (Exception e)
	{
		assertTrue(e instanceof TemplateModelException);
	}
}
 
Example 3
Source File: TestWBFreeMarkerModuleDirective.java    From cms with Apache License 2.0 5 votes vote down vote up
@Test
public void test_execute_noDirectiveName()
{
	Environment envMock = PowerMock.createMock(Environment.class);
	TemplateModel[] loopVars = null;
	TemplateDirectiveBody directiveBodyMock = null;
	Map params = new HashMap();
		
	FreeMarkerModuleDirective templateDirective = new FreeMarkerModuleDirective();
	try
	{

		Whitebox.setInternalState(templateDirective, "templateEngine",templateEngineMock);
		Whitebox.setInternalState(templateDirective, "cacheInstances",cacheInstancesMock);
		PowerMock.suppressMethod(FreeMarkerModuleDirective.class, "copyParams");

		EasyMock.replay(cacheInstancesMock, templateEngineMock, envMock);

		templateDirective.execute(envMock, params, loopVars, directiveBodyMock);

		assertTrue (false);
		
	} catch (Exception e)
	{
		assertTrue(e instanceof TemplateModelException);
	}
}
 
Example 4
Source File: TestWBFreeMarkerModuleDirective.java    From cms with Apache License 2.0 4 votes vote down vote up
@Test
public void test_execute_plainhtml()
{
	Environment envMock = PowerMock.createMock(Environment.class);
	TemplateModel[] loopVars = null;
	TemplateDirectiveBody directiveBodyMock = null;
	
	Map params = new HashMap();
	String key = "testXYZ";
	StringModel keyModel = new StringModel(key, new DefaultObjectWrapper() );
	params.put("externalKey", keyModel);

	String htmlSource = "<b>this is bold text</b> and this is not bold";
	try
	{

		
		WPBPageModule pageModuleMock = PowerMock.createMock(WPBPageModule.class);		
		WPBPageModulesCache pageModuleCacheMock = PowerMock.createMock(WPBPageModulesCache.class);
		EasyMock.expect(pageModuleCacheMock.getByExternalKey(key)).andReturn(pageModuleMock);
		EasyMock.expect(pageModuleMock.getIsTemplateSource()).andReturn(0);
		EasyMock.expect(pageModuleMock.getHtmlSource()).andReturn(htmlSource);
		EasyMock.expect(cacheInstancesMock.getPageModuleCache()).andReturn(pageModuleCacheMock);
		
		StringWriter outWriter = new StringWriter();
		EasyMock.expect(envMock.getOut()).andReturn(outWriter);
		
		EasyMock.replay(cacheInstancesMock, templateEngineMock, envMock, pageModuleMock, pageModuleCacheMock);
		
		FreeMarkerModuleDirective templateDirective = new FreeMarkerModuleDirective();
		
		PowerMock.suppressMethod(FreeMarkerModuleDirective.class, "copyParams");
		
		Whitebox.setInternalState(templateDirective, "cacheInstances", cacheInstancesMock);
		
		templateDirective.execute(envMock, params, loopVars, directiveBodyMock);
		
		EasyMock.verify(cacheInstancesMock, templateEngineMock, envMock, pageModuleMock, pageModuleCacheMock);
		
		assertTrue( outWriter.getBuffer().toString().equals(htmlSource));
		
	} catch (Exception e)
	{
		assertTrue(false);
	}
}
 
Example 5
Source File: TestWBFreeMarkerModuleDirective.java    From cms with Apache License 2.0 4 votes vote down vote up
@Test
public void test_execute_templatehtml()
{
	Environment envMock = PowerMock.createMock(Environment.class);
	TemplateModel[] loopVars = null;
	TemplateDirectiveBody directiveBodyMock = null;
	
	Map params = new HashMap();
	String key = "test123";
	StringModel keyModel = new StringModel(key, new DefaultObjectWrapper() );
	params.put("externalKey", keyModel);
	String name = "name123";
	String htmlSource = "<b>this is bold ${x}</b> and this is not bold ${x}";
	try
	{
	
		WPBPageModule pageModuleMock = PowerMock.createMock(WPBPageModule.class);
		EasyMock.expect(pageModuleMock.getExternalKey()).andReturn(name);
		WPBPageModulesCache pageModuleCacheMock = PowerMock.createMock(WPBPageModulesCache.class);
		EasyMock.expect(pageModuleCacheMock.getByExternalKey(key)).andReturn(pageModuleMock);
		EasyMock.expect(pageModuleMock.getIsTemplateSource()).andReturn(1);

		EasyMock.expect(cacheInstancesMock.getPageModuleCache()).andReturn(pageModuleCacheMock);

		StringWriter outWriter = new StringWriter();
		EasyMock.expect(envMock.getOut()).andReturn(outWriter);
	
		Capture<String> captureTemplateName = new Capture<String>();
		Capture<Map> captureRoot = new Capture<Map>();
		Capture<Writer> captureWriter = new Capture<Writer>();
		
		templateEngineMock.process(EasyMock.capture(captureTemplateName), EasyMock.capture(captureRoot), EasyMock.capture(captureWriter));	
		EasyMock.replay(cacheInstancesMock, templateEngineMock, envMock, pageModuleMock, pageModuleCacheMock);
		
		FreeMarkerModuleDirective templateDirective = new FreeMarkerModuleDirective();
		Whitebox.setInternalState(templateDirective, "templateEngine",templateEngineMock);
		Whitebox.setInternalState(templateDirective, "cacheInstances",cacheInstancesMock);
		PowerMock.suppressMethod(FreeMarkerModuleDirective.class, "copyParams");
		
		templateDirective.execute(envMock, params, loopVars, directiveBodyMock);
		
		EasyMock.verify(cacheInstancesMock, templateEngineMock, envMock, pageModuleMock, pageModuleCacheMock);
		assertTrue(captureRoot.getValue() == params);
		assertTrue(captureTemplateName.getValue().equals(WPBTemplateEngine.WEBMODULES_PATH_PREFIX + name));
		assertTrue(captureWriter.getValue() == outWriter);

	} catch (Exception e)
	{
		assertTrue(false);
	}
}