private void setExpectations(boolean noCache, Integer refresh) { EasyMock.expect(jsUri.isJsload()).andReturn(true); EasyMock.expect(jsUri.getOnload()).andReturn(ONLOAD_FUNCTION); jsUri.setJsload(false); jsUri.setNohint(true); EasyMock.expect(jsUriManager.makeExternJsUri(jsUri)).andReturn(uri); EasyMock.expect(jsUri.isNoCache()).andReturn(noCache); if (!noCache) { EasyMock.expect(jsUri.getRefresh()).andReturn(refresh); } }
private void setExpectations(boolean noCache, Integer refresh) { EasyMock.expect(jsUri.isJsload()).andReturn(true); EasyMock.expect(jsUri.getOnload()).andReturn(ONLOAD_FUNCTION); jsUri.setJsload(false); jsUri.setNohint(true); EasyMock.expect(jsUriManager.makeExternJsUri(jsUri)).andReturn(uri); EasyMock.expect(jsUri.isNoCache()).andReturn(noCache); if (!noCache) { EasyMock.expect(jsUri.getRefresh()).andReturn(refresh); } }
private JsUri mockJsUri(String container, RenderingContext context, boolean debug, boolean jsload, boolean nocache, String onload, int refresh, UriStatus status, String... libs) { JsUri result = mock(JsUri.class); expect(result.getContainer()).andReturn(container).anyTimes(); expect(result.getContext()).andReturn(context).anyTimes(); expect(result.getOnload()).andReturn(onload).anyTimes(); expect(result.getRefresh()).andReturn(refresh).anyTimes(); expect(result.isDebug()).andReturn(debug).anyTimes(); expect(result.isNoCache()).andReturn(nocache).anyTimes(); expect(result.isJsload()).andReturn(jsload).anyTimes(); expect(result.getStatus()).andReturn(status).anyTimes(); if (libs != null) { expect(result.getLibs()).andReturn(Lists.newArrayList(libs)).anyTimes(); } return result; }
@Test public void newJsUriWithConfiguredGadgetContext() throws Exception { Uri uri = newTestUriBuilder(RenderingContext.CONFIGURED_GADGET, JsCompileMode.CONCAT_COMPILE_EXPORT_ALL).toUri(); JsUriManager.JsUri jsUri = new JsUriManager.JsUri(STATUS, uri, LIBS, HAVE); assertEquals(RenderingContext.CONFIGURED_GADGET, jsUri.getContext()); assertEquals(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL, jsUri.getCompileMode()); assertEquals(CONTAINER_VALUE, jsUri.getContainer()); assertTrue(jsUri.isJsload()); assertTrue(jsUri.isNoCache()); assertTrue(jsUri.isNohint()); assertEquals(ONLOAD_VALUE, jsUri.getOnload()); assertEquals(LIBS, Lists.newArrayList(jsUri.getLibs())); assertEquals(HAVE, Lists.newArrayList(jsUri.getLoadedLibs())); assertEquals(uri, jsUri.getOrigUri()); }
@Test public void newJsUriWithEmptyOriginalUri() throws Exception { JsUriManager.JsUri jsUri = new JsUriManager.JsUri(STATUS, null, Collections.<String>emptyList(), null); // Null URI. assertEquals(RenderingContext.GADGET, jsUri.getContext()); assertEquals(ContainerConfig.DEFAULT_CONTAINER, jsUri.getContainer()); assertEquals(JsCompileMode.COMPILE_CONCAT, jsUri.getCompileMode()); assertFalse(jsUri.isJsload()); assertFalse(jsUri.isNoCache()); assertFalse(jsUri.isNohint()); assertNull(jsUri.getOnload()); assertTrue(jsUri.getLibs().isEmpty()); assertTrue(jsUri.getLoadedLibs().isEmpty()); assertNull(jsUri.getOrigUri()); }
@Test public void newJsUriWithEmptyOriginalUri() throws Exception { JsUriManager.JsUri jsUri = new JsUriManager.JsUri(STATUS, null, Collections.<String>emptyList(), null); // Null URI. assertEquals(RenderingContext.GADGET, jsUri.getContext()); assertEquals(ContainerConfig.DEFAULT_CONTAINER, jsUri.getContainer()); assertEquals(JsCompileMode.COMPILE_CONCAT, jsUri.getCompileMode()); assertFalse(jsUri.isJsload()); assertFalse(jsUri.isNoCache()); assertFalse(jsUri.isNohint()); assertNull(jsUri.getOnload()); assertTrue(jsUri.getLibs().isEmpty()); assertTrue(jsUri.getLoadedLibs().isEmpty()); assertNull(jsUri.getOrigUri()); }
@Test public void newJsUriWithConfiguredGadgetContext() throws Exception { Uri uri = newTestUriBuilder(RenderingContext.CONFIGURED_GADGET, JsCompileMode.CONCAT_COMPILE_EXPORT_ALL).toUri(); JsUriManager.JsUri jsUri = new JsUriManager.JsUri(STATUS, uri, LIBS, HAVE); assertEquals(RenderingContext.CONFIGURED_GADGET, jsUri.getContext()); assertEquals(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL, jsUri.getCompileMode()); assertEquals(CONTAINER_VALUE, jsUri.getContainer()); assertTrue(jsUri.isJsload()); assertTrue(jsUri.isNoCache()); assertTrue(jsUri.isNohint()); assertEquals(ONLOAD_VALUE, jsUri.getOnload()); assertEquals(LIBS, Lists.newArrayList(jsUri.getLibs())); assertEquals(HAVE, Lists.newArrayList(jsUri.getLoadedLibs())); assertEquals(uri, jsUri.getOrigUri()); }
@Test public void testFailsWhenNoOnloadIsSpecified() throws Exception { EasyMock.expect(jsUri.isJsload()).andReturn(true); EasyMock.expect(jsUri.getOnload()).andReturn(null); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(JsLoadProcessor.JSLOAD_ONLOAD_ERROR, e.getMessage()); } control.verify(); }
@Test public void newJsUriWithOriginalUri() throws Exception { Uri uri = newTestUriBuilder(RenderingContext.CONTAINER, JsCompileMode.CONCAT_COMPILE_EXPORT_ALL).toUri(); JsUriManager.JsUri jsUri = new JsUriManager.JsUri(STATUS, uri, LIBS, HAVE); assertEquals(RenderingContext.CONTAINER, jsUri.getContext()); assertEquals(JsCompileMode.CONCAT_COMPILE_EXPORT_ALL, jsUri.getCompileMode()); assertEquals(CONTAINER_VALUE, jsUri.getContainer()); assertTrue(jsUri.isJsload()); assertTrue(jsUri.isNoCache()); assertTrue(jsUri.isNohint()); assertEquals(ONLOAD_VALUE, jsUri.getOnload()); assertEquals(LIBS, Lists.newArrayList(jsUri.getLibs())); assertEquals(HAVE, Lists.newArrayList(jsUri.getLoadedLibs())); assertEquals(uri, jsUri.getOrigUri()); }
@Test public void testFailsWithInvalidFunctionName() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn("!!%%!!%%"); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(AddOnloadFunctionProcessor.ONLOAD_FUNCTION_NAME_ERROR, e.getMessage()); } control.verify(); }
@Test public void testFailsWhenNoOnloadIsSpecified() throws Exception { EasyMock.expect(jsUri.isJsload()).andReturn(true); EasyMock.expect(jsUri.getOnload()).andReturn(null); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(JsLoadProcessor.JSLOAD_ONLOAD_ERROR, e.getMessage()); } control.verify(); }
@Test public void testWithHint() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(false); control.replay(); assertTrue(processor.process(request, response)); assertEquals("", response.build().toJsString()); control.verify(); } }
@Test public void testFailsWithInvalidFunctionName() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn("!!%%!!%%"); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(AddOnloadFunctionProcessor.ONLOAD_FUNCTION_NAME_ERROR, e.getMessage()); } control.verify(); }
@Test public void testWithoutHint() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(true); control.replay(); assertTrue(processor.process(request, response)); assertEquals(AddOnloadFunctionProcessor.JSL_CALLBACK_JS, response.build().toJsString()); control.verify(); }
@Test public void testSkipsWhenNoOnloadAndWithHintSpecified() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(false); response = control.createMock(JsResponseBuilder.class); control.replay(); assertTrue(processor.process(request, response)); control.verify(); }
@Test public void testWithHint() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(false); control.replay(); assertTrue(processor.process(request, response)); assertEquals("", response.build().toJsString()); control.verify(); } }
@Test public void testGeneratesCallbackCode() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(ONLOAD_FUNCTION); control.replay(); assertTrue(processor.process(request, response)); assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); String expectedBody = String.format(AddOnloadFunctionProcessor.ONLOAD_JS_TPL, ONLOAD_FUNCTION); assertEquals(expectedBody, response.build().toJsString()); control.verify(); }
@Test public void testGeneratesCallbackCode() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(ONLOAD_FUNCTION); control.replay(); assertTrue(processor.process(request, response)); assertEquals(HttpServletResponse.SC_OK, response.getStatusCode()); String expectedBody = String.format(AddOnloadFunctionProcessor.ONLOAD_JS_TPL, ONLOAD_FUNCTION); assertEquals(expectedBody, response.build().toJsString()); control.verify(); }
@Test public void testSkipsWhenNoOnloadAndWithHintSpecified() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(false); response = control.createMock(JsResponseBuilder.class); control.replay(); assertTrue(processor.process(request, response)); control.verify(); }
@Test public void testWithoutHint() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn(null); EasyMock.expect(jsUri.isNohint()).andReturn(true); control.replay(); assertTrue(processor.process(request, response)); assertEquals(AddOnloadFunctionProcessor.JSL_CALLBACK_JS, response.build().toJsString()); control.verify(); }