Java tutorial
/** * Licensed to Apereo under one or more contributor license * agreements. See the NOTICE file distributed with this work * for additional information regarding copyright ownership. * Apereo licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a * copy of the License at the following location: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apereo.portal.security.xslt; import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.replay; import static org.easymock.EasyMock.verify; import static org.junit.Assert.assertTrue; import java.io.InputStream; import java.io.StringWriter; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.LogFactory; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; import org.apereo.portal.xml.ResourceLoaderURIResolver; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.ClassRelativeResourceLoader; import org.springframework.util.xml.SimpleTransformErrorListener; /** * @author Eric Dalquist * @version $Revision$ */ public class XalanAuthorizationHelperTest { protected final Logger logger = LoggerFactory.getLogger(getClass()); private TransformerFactory tFactory; private StreamSource xmlSource; @Before public void setup() throws Exception { final ResourceLoaderURIResolver resolver = new ResourceLoaderURIResolver( new ClassRelativeResourceLoader(getClass())); this.tFactory = TransformerFactory.newInstance(); this.tFactory.setURIResolver(resolver); this.tFactory.setErrorListener(new SimpleTransformErrorListener(LogFactory.getLog(getClass()))); this.xmlSource = new StreamSource(this.getClass().getResourceAsStream("test.xml")); } @Test public void testIsUserDeepMemberOf() throws Exception { final IXalanAuthorizationHelper helperBean = createMock(IXalanAuthorizationHelper.class); expect(helperBean.canRender("testUser", "portlet-admin")).andReturn(true); replay(helperBean); XalanAuthorizationHelper helper = new XalanAuthorizationHelper(); helper.setAuthorizationHelper(helperBean); // set up configuration in the transformer impl final StringWriter resultWriter = new StringWriter(); final InputStream xslStream = this.getClass().getResourceAsStream("authorizationTest.xsl"); final StreamSource xslSource = new StreamSource(xslStream); final Transformer transformer = tFactory.newTransformer(xslSource); transformer.setParameter("USER_ID", "testUser"); transformer.transform(xmlSource, new StreamResult(resultWriter)); final String result = resultWriter.getBuffer().toString(); logger.debug(result); final String expected = IOUtils.toString(this.getClass().getResourceAsStream("authorizationResult.xml")); XMLUnit.setIgnoreWhitespace(true); Diff d = new Diff(expected, result); assertTrue("Transformation result differs from what's expected" + d, d.similar()); verify(helperBean); } }