Search in sources :

Example 1 with StAXPipelineComponent

use of org.apereo.portal.rendering.StAXPipelineComponent in project uPortal by Jasig.

the class CachingStAXPipelineComponentTest method testCacheMiss.

@Test
public void testCacheMiss() {
    final MockHttpServletRequest mockReq = new MockHttpServletRequest();
    final MockHttpServletResponse mockRes = new MockHttpServletResponse();
    final CacheKey cacheKey = CacheKey.build("testCacheKey");
    final List<XMLEvent> eventBuffer = Collections.emptyList();
    final PipelineEventReader<XMLEventReader, XMLEvent> eventReader = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(new XMLEventBufferReader(eventBuffer.listIterator()));
    final Ehcache cache = createMock(Ehcache.class);
    final StAXPipelineComponent targetComponent = createMock(StAXPipelineComponent.class);
    final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
    expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
    expect(targetComponent.getCacheKey(mockReq, mockRes)).andReturn(cacheKey);
    expect(cache.get(cacheKey)).andReturn(null);
    expect(targetComponent.getEventReader(mockReq, mockRes)).andReturn(eventReader);
    cache.put((Element) notNull());
    expectLastCall();
    replay(cache, targetComponent, elementsProvider);
    final CachingStAXPipelineComponent cachingComponent = new CachingStAXPipelineComponent();
    cachingComponent.setCache(cache);
    cachingComponent.setWrappedComponent(targetComponent);
    cachingComponent.setResourcesElementsProvider(elementsProvider);
    final PipelineEventReader<XMLEventReader, XMLEvent> actualEventReader = cachingComponent.getEventReader(mockReq, mockRes);
    Assert.assertNotNull(actualEventReader);
    Assert.assertNotNull(actualEventReader.getEventReader());
    Assert.assertFalse(actualEventReader.getEventReader().hasNext());
    verify(cache, targetComponent, elementsProvider);
}
Also used : StAXPipelineComponent(org.apereo.portal.rendering.StAXPipelineComponent) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) PipelineEventReaderImpl(org.apereo.portal.rendering.PipelineEventReaderImpl) XMLEventBufferReader(org.apereo.portal.xml.stream.XMLEventBufferReader) XMLEvent(javax.xml.stream.events.XMLEvent) XMLEventReader(javax.xml.stream.XMLEventReader) Ehcache(net.sf.ehcache.Ehcache) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) CacheKey(org.apereo.portal.utils.cache.CacheKey) Test(org.junit.Test)

Example 2 with StAXPipelineComponent

use of org.apereo.portal.rendering.StAXPipelineComponent in project uPortal by Jasig.

the class CachingStAXPipelineComponentTest method testCacheHit.

@Test
public void testCacheHit() {
    final MockHttpServletRequest mockReq = new MockHttpServletRequest();
    final MockHttpServletResponse mockRes = new MockHttpServletResponse();
    final CacheKey cacheKey = CacheKey.build("testCacheKey");
    final CachedEventReader<XMLEvent> eventReader = new CachedEventReader<XMLEvent>(Collections.EMPTY_LIST, Collections.EMPTY_MAP);
    final Element cacheElement = new Element(cacheKey, eventReader);
    final Ehcache cache = createMock(Ehcache.class);
    final StAXPipelineComponent targetComponent = createMock(StAXPipelineComponent.class);
    final ResourcesElementsProvider elementsProvider = createMock(ResourcesElementsProvider.class);
    expect(elementsProvider.getDefaultIncludedType()).andReturn(Included.AGGREGATED);
    expect(targetComponent.getCacheKey(mockReq, mockRes)).andReturn(cacheKey);
    expect(cache.get(cacheKey)).andReturn(cacheElement);
    replay(cache, targetComponent, elementsProvider);
    final CachingStAXPipelineComponent cachingComponent = new CachingStAXPipelineComponent();
    cachingComponent.setCache(cache);
    cachingComponent.setWrappedComponent(targetComponent);
    cachingComponent.setResourcesElementsProvider(elementsProvider);
    final PipelineEventReader<XMLEventReader, XMLEvent> actualEventReader = cachingComponent.getEventReader(mockReq, mockRes);
    Assert.assertNotNull(actualEventReader);
    Assert.assertNotNull(actualEventReader.getEventReader());
    Assert.assertFalse(actualEventReader.getEventReader().hasNext());
    verify(cache, targetComponent, elementsProvider);
}
Also used : StAXPipelineComponent(org.apereo.portal.rendering.StAXPipelineComponent) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Element(net.sf.ehcache.Element) ResourcesElementsProvider(org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider) XMLEvent(javax.xml.stream.events.XMLEvent) Ehcache(net.sf.ehcache.Ehcache) XMLEventReader(javax.xml.stream.XMLEventReader) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) CacheKey(org.apereo.portal.utils.cache.CacheKey) Test(org.junit.Test)

Example 3 with StAXPipelineComponent

use of org.apereo.portal.rendering.StAXPipelineComponent in project uPortal by Jasig.

the class XSLTComponentTest method testXSLTComponent.

@Test
public void testXSLTComponent() throws Exception {
    final MockHttpServletRequest mockReq = new MockHttpServletRequest();
    final MockHttpServletResponse mockRes = new MockHttpServletResponse();
    final XMLEventReader xmlEventReader = this.getXmlEventReader("juser.xml");
    final PipelineEventReaderImpl<XMLEventReader, XMLEvent> cacheableEventReader = new PipelineEventReaderImpl<XMLEventReader, XMLEvent>(xmlEventReader);
    final Transformer transformer = this.getTransformer("columns.xsl");
    final StAXPipelineComponent targetComponent = EasyMock.createMock(StAXPipelineComponent.class);
    final TransformerSource transformerSource = EasyMock.createMock(TransformerSource.class);
    EasyMock.expect(targetComponent.getEventReader(mockReq, mockRes)).andReturn(cacheableEventReader);
    EasyMock.expect(transformerSource.getTransformer(mockReq, mockRes)).andReturn(transformer);
    EasyMock.replay(targetComponent, transformerSource);
    final XSLTComponent xsltComponent = new XSLTComponent();
    xsltComponent.setWrappedComponent(targetComponent);
    xsltComponent.setTransformerSource(transformerSource);
    final PipelineEventReader<XMLEventReader, XMLEvent> eventReader = xsltComponent.getEventReader(mockReq, mockRes);
    Assert.assertNotNull(eventReader);
    final String output = this.serializeXMLEventReader(eventReader.getEventReader());
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setFeature("http://xml.org/sax/features/namespaces", false);
    dbf.setFeature("http://xml.org/sax/features/validation", false);
    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    final DocumentBuilder db = dbf.newDocumentBuilder();
    XMLUnit.setIgnoreWhitespace(true);
    try {
        final Document expected = db.parse(this.getClass().getResourceAsStream("/org/apereo/portal/rendering/xslt/expected.xml"), "/org/apereo/portal/rendering/xslt/expected.xml");
        final Document actual = db.parse(new InputSource(new StringReader(output)));
        Diff d = new Diff(expected, actual);
        assertTrue("Upgraded data doesn't match expected data: " + d, d.similar());
    } catch (Exception e) {
        throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", output, e);
    } catch (Error e) {
        throw new XmlTestException("Failed to assert similar between XSLT output and expected XML", output, e);
    }
    EasyMock.verify(targetComponent, transformerSource);
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) StAXPipelineComponent(org.apereo.portal.rendering.StAXPipelineComponent) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Diff(org.custommonkey.xmlunit.Diff) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Document(org.w3c.dom.Document) XmlTestException(org.apereo.portal.io.xml.XmlTestException) XMLStreamException(javax.xml.stream.XMLStreamException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) PipelineEventReaderImpl(org.apereo.portal.rendering.PipelineEventReaderImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder) XMLEvent(javax.xml.stream.events.XMLEvent) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XmlTestException(org.apereo.portal.io.xml.XmlTestException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

XMLEventReader (javax.xml.stream.XMLEventReader)3 XMLEvent (javax.xml.stream.events.XMLEvent)3 StAXPipelineComponent (org.apereo.portal.rendering.StAXPipelineComponent)3 Test (org.junit.Test)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 Ehcache (net.sf.ehcache.Ehcache)2 PipelineEventReaderImpl (org.apereo.portal.rendering.PipelineEventReaderImpl)2 CacheKey (org.apereo.portal.utils.cache.CacheKey)2 ResourcesElementsProvider (org.jasig.resourceserver.utils.aggr.ResourcesElementsProvider)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 Transformer (javax.xml.transform.Transformer)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 Element (net.sf.ehcache.Element)1 XmlTestException (org.apereo.portal.io.xml.XmlTestException)1