001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package acegifier;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020:
021: import junit.framework.TestCase;
022:
023: import org.dom4j.Document;
024: import org.dom4j.io.OutputFormat;
025: import org.dom4j.io.XMLWriter;
026:
027: /**
028: * Tests the WebXmlConverter by applying it to a sample web.xml file.
029: *
030: * @author Luke Taylor
031: * @version $Id: WebXmlConverterTests.java 1784 2007-02-24 21:00:24Z luke_t $
032: */
033: public class WebXmlConverterTests extends TestCase {
034:
035: private static final String XML_TRANSFORMER = "javax.xml.transform.TransformerFactory";
036:
037: public void testFileConversion() throws Exception {
038: /*
039:
040: THIS TEST HAS BEEN DISABLED AS IT BREAKS THE BUILD (see SEC-181 for details)
041:
042: WebXmlConverter converter;
043: try {
044: converter = new WebXmlConverter();
045: } catch (Exception e) {
046: // TODO: Something went wrong, set transforer manually and retry...
047: System.out.println("**** WARNING: NEEDING TO FALLBACK TO A MANUAL SYSTEM PROPERTY ****");
048: System.setProperty(XML_TRANSFORMER, "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
049: System.out.println(XML_TRANSFORMER + ": " + System.getProperty(XML_TRANSFORMER));
050: converter = new WebXmlConverter();
051: }
052:
053: Resource r = new ClassPathResource("test-web.xml");
054: converter.setInput(r.getInputStream());
055: converter.doConversion();
056:
057: DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
058: XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(bf);
059:
060: beanReader.loadBeanDefinitions(
061: new InMemoryResource(converter.getAcegiBeans().asXML().getBytes()));
062: assertNotNull(bf.getBean("filterChainProxy"));
063:
064: ProviderManager pm = (ProviderManager) bf.getBean("authenticationManager");
065: assertNotNull(pm);
066: assertEquals(3, pm.getProviders().size());
067:
068: DaoAuthenticationProvider dap =
069: (DaoAuthenticationProvider) bf.getBean("daoAuthenticationProvider");
070: assertNotNull(dap);
071:
072: InMemoryDaoImpl dao = (InMemoryDaoImpl) dap.getUserDetailsService();
073: UserDetails user = dao.loadUserByUsername("superuser");
074: assertEquals("password",user.getPassword());
075: assertEquals(2, user.getAuthorities().length);
076: assertNotNull(bf.getBean("anonymousProcessingFilter"));
077: assertNotNull(bf.getBean("anonymousAuthenticationProvider"));
078: assertNotNull(bf.getBean("httpSessionContextIntegrationFilter"));
079: assertNotNull(bf.getBean("rememberMeProcessingFilter"));
080: assertNotNull(bf.getBean("rememberMeAuthenticationProvider"));
081:
082: ExceptionTranslationFilter etf =
083: (ExceptionTranslationFilter) bf.getBean("exceptionTranslationFilter");
084: assertNotNull(etf);
085: assertNotNull(etf.getAuthenticationEntryPoint());
086: System.out.println(prettyPrint(converter.getNewWebXml()));
087: System.out.println(prettyPrint(converter.getAcegiBeans()));
088: */
089: }
090:
091: private String prettyPrint(Document document) throws IOException {
092: ByteArrayOutputStream output = new ByteArrayOutputStream();
093: OutputFormat format = OutputFormat.createPrettyPrint();
094: format.setNewlines(true);
095: format.setTrimText(false);
096: XMLWriter writer = new XMLWriter(output, format);
097: writer.write(document);
098: writer.flush();
099: writer.close();
100: return output.toString();
101: }
102: }
|