    1   package acegifier;
    2
    3   import java.io.ByteArrayOutputStream;
    4   import java.io.IOException;
    5
    6   import javax.xml.transform.TransformerFactoryConfigurationError;
    7
    8   import junit.framework.TestCase;
    9
    10  import org.acegisecurity.intercept.web.FilterSecurityInterceptor;
    11  import org.acegisecurity.intercept.web.SecurityEnforcementFilter;
    12  import org.acegisecurity.providers.ProviderManager;
    13  import org.acegisecurity.providers.dao.DaoAuthenticationProvider;
    14  import org.acegisecurity.userdetails.UserDetails;
    15  import org.acegisecurity.userdetails.memory.InMemoryDaoImpl;
    16  import org.acegisecurity.util.InMemoryResource;
    17  import org.dom4j.Document;
    18  import org.dom4j.io.OutputFormat;
    19  import org.dom4j.io.XMLWriter;
    20  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
    21  import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
    22  import org.springframework.core.io.ClassPathResource;
    23  import org.springframework.core.io.Resource;
    24
    25  /***
    26   * Tests the WebXmlConverter by applying it to a sample web.xml file.
    27   *
    28   * @author Luke Taylor
    29   * @version $Id: WebXmlConverterTests.java,v 1.4 2005/11/30 23:52:04 benalex Exp $
    30   */
    31  public class WebXmlConverterTests extends TestCase {
    32
    33  	private static final String XML_TRANSFORMER = "javax.xml.transform.TransformerFactory";
    34
    35      public void testFileConversion() throws Exception {
    36  		WebXmlConverter converter;
    37  		try {
    38      		converter = new WebXmlConverter();
    39      	} catch (TransformerFactoryConfigurationError e) {
    40      		// TODO: Something went wrong, set transforer manually and retry...
    41      		System.out.println("**** WARNING: NEEDING TO FALLBACK TO A MANUAL SYSTEM PROPERTY ****");
    42          	System.setProperty(XML_TRANSFORMER, "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
    43          	System.out.println(XML_TRANSFORMER + ": " + System.getProperty(XML_TRANSFORMER));
    44      		converter = new WebXmlConverter();
    45      	}
    46
    47          Resource r = new ClassPathResource("test-web.xml");
    48          converter.setInput(r.getInputStream());
    49          converter.doConversion();
    50
    51          DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    52          XmlBeanDefinitionReader beanReader = new XmlBeanDefinitionReader(bf);
    53
    54          beanReader.loadBeanDefinitions(
    55                  new InMemoryResource(converter.getAcegiBeans().asXML().getBytes()));
    56          assertNotNull(bf.getBean("filterChainProxy"));
    57
    58          ProviderManager pm = (ProviderManager) bf.getBean("authenticationManager");
    59          assertNotNull(pm);
    60          assertEquals(3, pm.getProviders().size());
    61
    62          DaoAuthenticationProvider dap =
    63                  (DaoAuthenticationProvider) bf.getBean("daoAuthenticationProvider");
    64          assertNotNull(dap);
    65
    66          InMemoryDaoImpl dao = (InMemoryDaoImpl) dap.getUserDetailsService();
    67          UserDetails user = dao.loadUserByUsername("superuser");
    68          assertEquals("password",user.getPassword());
    69          assertEquals(2, user.getAuthorities().length);
    70          assertNotNull(bf.getBean("anonymousProcessingFilter"));
    71          assertNotNull(bf.getBean("anonymousAuthenticationProvider"));
    72          assertNotNull(bf.getBean("httpSessionContextIntegrationFilter"));
    73          assertNotNull(bf.getBean("rememberMeProcessingFilter"));
    74          assertNotNull(bf.getBean("rememberMeAuthenticationProvider"));
    75
    76          SecurityEnforcementFilter sef =
    77                  (SecurityEnforcementFilter) bf.getBean("securityEnforcementFilter");
    78          assertNotNull(sef);
    79          assertNotNull(sef.getAuthenticationEntryPoint());
    80          FilterSecurityInterceptor fsi = sef.getFilterSecurityInterceptor();
    81          System.out.println(prettyPrint(converter.getNewWebXml()));
    82          System.out.println(prettyPrint(converter.getAcegiBeans()));
    83
    84      }
    85
    86      private String prettyPrint(Document document) throws IOException {
    87          ByteArrayOutputStream output = new ByteArrayOutputStream();
    88          OutputFormat format = OutputFormat.createPrettyPrint();
    89          format.setNewlines(true);
    90          format.setTrimText(false);
    91          XMLWriter writer = new XMLWriter(output, format);
    92          writer.write(document);
    93          writer.flush();
    94          writer.close();
    95          return output.toString();
    96      }
    97  }