1 5 package com.nightlabs.ipanema.test.util; 6 7 import java.io.IOException ; 8 import java.io.InputStream ; 9 import java.util.Hashtable ; 10 import java.util.Iterator ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 14 import javax.naming.InitialContext ; 15 import javax.naming.NamingException ; 16 import javax.security.auth.login.LoginException ; 17 18 import com.nightlabs.j2ee.InitialContextProvider; 19 20 import junit.framework.TestCase; 21 22 26 public class IpanemaTestCase extends TestCase implements InitialContextProvider 27 { 28 protected IpanemaLoginContext loginContext; 29 protected Properties initialContextProperties; 30 protected InitialContext initialContext; 31 32 public IpanemaTestCase(String name) 33 { 34 super(name); 35 loadProperties(); 36 IpanemaSecurityConfiguration.declareConfiguration(); 37 init(); 38 } 39 40 private void init() 41 { 42 try 43 { 44 loginContext = new IpanemaLoginContext("ipanema", new LoginCallbackHandler(this)); 45 loginContext.setCredentials( 46 System.getProperty("com.nightlabs.ipanema.test.userid"), 47 System.getProperty("com.nightlabs.ipanema.test.organisationid"), 48 System.getProperty("com.nightlabs.ipanema.test.password") 49 ); 50 } 51 catch (LoginException e) 52 { 53 fail("Error creating login context"); 54 } 55 } 56 57 public Hashtable getInitialContextProperties() 58 throws LoginException 59 { 60 if (initialContextProperties == null) 61 { 62 initialContextProperties = new Properties (); 63 initialContextProperties.put(InitialContext.INITIAL_CONTEXT_FACTORY, System.getProperty("com.nightlabs.ipanema.test.initialcontextfactory")); 64 initialContextProperties.put(InitialContext.PROVIDER_URL, System.getProperty("com.nightlabs.ipanema.test.serverurl")); 65 initialContextProperties.put(InitialContext.SECURITY_PRINCIPAL, loginContext.getUsername()); 66 initialContextProperties.put(InitialContext.SECURITY_CREDENTIALS, loginContext.getPassword()); 67 initialContextProperties.put(InitialContext.SECURITY_PROTOCOL, "ipanema"); 68 } 69 return initialContextProperties; 70 } 71 72 73 74 public InitialContext getInitialContext() 75 throws LoginException , NamingException 76 { 77 if (initialContext == null) 78 initialContext = new InitialContext (getInitialContextProperties()); 79 return initialContext; 80 } 81 82 public IpanemaLoginContext getLoginContext() { 83 return loginContext; 84 } 85 86 protected void loadProperties() 87 { 88 if((System.getProperty("com.nightlabs.ipanema.test.initialcontextfactory") != null) && 90 (System.getProperty("com.nightlabs.ipanema.test.serverurl") != null) && 91 (System.getProperty("com.nightlabs.ipanema.test.userid") != null) && 92 (System.getProperty("com.nightlabs.ipanema.test.organisationid") != null) && 93 (System.getProperty("com.nightlabs.ipanema.test.password") != null)) 94 return; 95 96 Properties props = new Properties (); 97 InputStream is = null; 98 String conf = System.getProperty("com.nightlabs.ipanema.test.config"); 99 if(conf == null) 100 fail("com.nightlabs.ipanema.test.config not defined"); 101 is = IpanemaTestCase.class.getResourceAsStream(conf); 102 if(is == null) 103 fail("could not open properties: " + conf); 104 try 105 { 106 props.load(is); 107 for( Iterator it = props.entrySet().iterator(); it.hasNext(); ) 108 { 109 Map.Entry entry = (Map.Entry ) it.next(); 110 System.setProperty((String )entry.getKey(),(String )entry.getValue()); 111 } 112 } 113 catch (IOException e) 114 { 115 e.printStackTrace(); 116 } 117 } 118 } 119 | Popular Tags |