Index: TestHarness4LAMS2/src/org/lamsfoundation/testharness/MockUser.java =================================================================== RCS file: /usr/local/cvsroot/TestHarness4LAMS2/src/org/lamsfoundation/testharness/MockUser.java,v diff -u -r1.3 -r1.4 --- TestHarness4LAMS2/src/org/lamsfoundation/testharness/MockUser.java 5 Oct 2006 05:47:43 -0000 1.3 +++ TestHarness4LAMS2/src/org/lamsfoundation/testharness/MockUser.java 11 Jun 2009 05:58:48 -0000 1.4 @@ -33,162 +33,168 @@ import org.apache.log4j.Logger; import org.xml.sax.SAXException; -import com.meterware.httpunit.UploadFileSpec; import com.meterware.httpunit.WebConversation; import com.meterware.httpunit.WebForm; import com.meterware.httpunit.WebResponse; +import com.meterware.httpunit.protocol.UploadFileSpec; /** * @version - * + * *

* View Source *

- * + * * @author Fei Yang */ -public class MockUser{ +public class MockUser { - private static final Logger log = Logger.getLogger(MockUser.class); - - private static final String[] DELAY_MESSAGES = {" is deserting ", " is napping ", " is pondering ", " is in a daze ", " will have a cup of coffee for ", " is away for toilet "}; - - private static final String LOGIN_PAGE_FLAG = "j_security_check"; - private static final String INDEX_PAGE_FLAG = "images/loading.gif"; - - private static final String USERNAME = "j_username"; - private static final String PASSWORD = "j_password"; - - private static String indexPage; + private static final Logger log = Logger.getLogger(MockUser.class); - protected AbstractTest test; - protected String userId; - protected String username; - protected String password; - protected WebConversation wc; + private static final String[] DELAY_MESSAGES = { " is deserting ", " is napping ", " is pondering ", + " is in a daze ", " will have a cup of coffee for ", " is away for toilet " }; - public MockUser(AbstractTest test, String username, String password, String userId) { - this.test = test; - this.username = username; - this.password = password; - this.userId = userId; - } + private static final String LOGIN_PAGE_FLAG = "j_security_check"; + private static final String INDEX_PAGE_FLAG = "images/loading.gif"; - public static final void setIndexPage(String indexPageURL){ - indexPage = indexPageURL; - } + private static final String USERNAME = "j_username"; + private static final String PASSWORD = "j_password"; - /** - * Login to the system. - * - * @exception TestHarnessException:failure - */ - public final void login() - { - try{ - wc = new WebConversation(); - WebResponse resp = (WebResponse)new Call(wc,test,username+" fetch index page",indexPage).execute(); - if(!checkPageContains(resp,LOGIN_PAGE_FLAG)){ - log.debug(resp.getText()); - throw new TestHarnessException(username +" didn't get login page when hitting LAMS the first time!"); - } - Map params = new HashMap(); - params.put(USERNAME,username); - params.put(PASSWORD,HashUtil.sha1(password)); - resp = (WebResponse)new Call(wc, test,"User login",fillForm(resp,0,params)).execute(); - if(!checkPageContains(resp,INDEX_PAGE_FLAG)){ - log.debug(resp.getText()); - throw new TestHarnessException(username+" failed to login with password "+password); - } - }catch(IOException e){ - throw new RuntimeException(e); - }catch(SAXException e){ - throw new RuntimeException(e); - }catch(NoSuchAlgorithmException e){ - throw new RuntimeException(e); - } + private static String indexPage; + + protected AbstractTest test; + protected String userId; + protected String username; + protected String password; + protected WebConversation wc; + + public MockUser(AbstractTest test, String username, String password, String userId) { + this.test = test; + this.username = username; + this.password = password; + this.userId = userId; + } + + public static final void setIndexPage(String indexPageURL) { + MockUser.indexPage = indexPageURL; + } + + /** + * Login to the system. + * + * @exception TestHarnessException:failure + */ + public final void login() { + try { + wc = new WebConversation(); + WebResponse resp = (WebResponse) new Call(wc, test, username + " fetch index page", MockUser.indexPage) + .execute(); + if (!checkPageContains(resp, MockUser.LOGIN_PAGE_FLAG)) { + MockUser.log.debug(resp.getText()); + throw new TestHarnessException(username + " didn't get login page when hitting LAMS the first time!"); + } + Map params = new HashMap(); + params.put(MockUser.USERNAME, username); + params.put(MockUser.PASSWORD, HashUtil.sha1(password)); + resp = (WebResponse) new Call(wc, test, "User login", fillForm(resp, 0, params)).execute(); + if (!checkPageContains(resp, MockUser.INDEX_PAGE_FLAG)) { + MockUser.log.debug(resp.getText()); + throw new TestHarnessException(username + " failed to login with password " + password); + } + } catch (IOException e) { + throw new RuntimeException(e); + } catch (SAXException e) { + throw new RuntimeException(e); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException(e); } + } - protected final WebForm fillForm(WebResponse resp, int formIndex, Map params) throws SAXException, IOException{ - WebForm[] forms = resp.getForms(); - if((forms==null)||(forms.length<=formIndex)){ - log.debug(resp.getText()); - throw new TestHarnessException(username + " cannot find the form whose index is "+formIndex+" in the page"); + protected final WebForm fillForm(WebResponse resp, int formIndex, Map params) throws SAXException, + IOException { + WebForm[] forms = resp.getForms(); + if (forms == null || forms.length <= formIndex) { + MockUser.log.debug(resp.getText()); + throw new TestHarnessException(username + " cannot find the form whose index is " + formIndex + + " in the page"); + } + WebForm form = forms[formIndex]; + if (params != null) { + for (Map.Entry entry : params.entrySet()) { + Object value = entry.getValue(); + if (value instanceof String) { + form.setParameter(entry.getKey(), (String) entry.getValue()); + } else if (value instanceof File) { + form.setParameter(entry.getKey(), (File) entry.getValue()); + } else if (value instanceof String[]) { + form.setParameter(entry.getKey(), (String[]) entry.getValue()); + } else if (value instanceof UploadFileSpec[]) { + form.setParameter(entry.getKey(), (UploadFileSpec[]) entry.getValue()); + } else { + throw new TestHarnessException("Unsupported parameter value type:" + + entry.getValue().getClass().getName()); } - WebForm form = forms[formIndex]; - if (params != null) { - for (Map.Entry entry : params.entrySet()) { - Object value = entry.getValue(); - if (value instanceof String) { - form.setParameter(entry.getKey(), (String) entry.getValue()); - } else if (value instanceof File) { - form.setParameter(entry.getKey(), (File) entry.getValue()); - } else if (value instanceof String[]) { - form.setParameter(entry.getKey(), (String[]) entry.getValue()); - } else if (value instanceof UploadFileSpec[]) { - form.setParameter(entry.getKey(), (UploadFileSpec[]) entry.getValue()); - } else { - throw new TestHarnessException("Unsupported parameter value type:" + entry.getValue().getClass().getName()); - } - } - } - return form; + } } - - protected final boolean checkPageContains(WebResponse resp,String flag) throws IOException - { - return resp.getText().indexOf(flag)!=-1; - } + return form; + } - protected final void delay(){ - try{ - int seconds; - if(test.getMaxDelay() <= test.getMinDelay()){//to avoid IllegalArgumentException in nextInt method on Random object - seconds = test.getMinDelay(); - }else{ - seconds = test.getMinDelay() + TestUtil.generateRandomIndex(test.getMaxDelay() - test.getMinDelay() + 1 ); - } - if(seconds>0){ - log.info(composeDelayInfo(seconds)); - Thread.sleep(seconds * 1000); - } - }catch (InterruptedException e){ - //ignore - } + protected final boolean checkPageContains(WebResponse resp, String flag) throws IOException { + return resp.getText().indexOf(flag) != -1; } - - private String composeDelayInfo(int seconds) { - return username+DELAY_MESSAGES[TestUtil.generateRandomIndex(DELAY_MESSAGES.length)] + seconds + (seconds==1? " second" : " seconds"); + + protected final void delay() { + try { + int seconds; + if (test.getMaxDelay() <= test.getMinDelay()) {// to avoid IllegalArgumentException in nextInt method on + // Random object + seconds = test.getMinDelay(); + } else { + seconds = test.getMinDelay() + + TestUtil.generateRandomIndex(test.getMaxDelay() - test.getMinDelay() + 1); + } + if (seconds > 0) { + MockUser.log.info(composeDelayInfo(seconds)); + Thread.sleep(seconds * 1000); + } + } catch (InterruptedException e) { + // ignore } + } - private static class HashUtil { + private String composeDelayInfo(int seconds) { + return username + MockUser.DELAY_MESSAGES[TestUtil.generateRandomIndex(MockUser.DELAY_MESSAGES.length)] + + seconds + (seconds == 1 ? " second" : " seconds"); + } - static String sha1(String plaintext) throws NoSuchAlgorithmException { - MessageDigest md = MessageDigest.getInstance("SHA1"); - return new String(Hex.encodeHex(md.digest(plaintext.getBytes()))); - } + private static class HashUtil { - static String md5(String plaintext) throws NoSuchAlgorithmException { - MessageDigest md = MessageDigest.getInstance("MD5"); - return new String(Hex.encodeHex(md.digest(plaintext.getBytes()))); - } - + static String sha1(String plaintext) throws NoSuchAlgorithmException { + MessageDigest md = MessageDigest.getInstance("SHA1"); + return new String(Hex.encodeHex(md.digest(plaintext.getBytes()))); } - public final String getPassword() { - return password; + static String md5(String plaintext) throws NoSuchAlgorithmException { + MessageDigest md = MessageDigest.getInstance("MD5"); + return new String(Hex.encodeHex(md.digest(plaintext.getBytes()))); } - public final String getUsername() { - return username; - } + } - public final String getUserId() { - return userId; - } + public final String getPassword() { + return password; + } - public final void setUserId(String userId) { - this.userId = userId; - } + public final String getUsername() { + return username; + } + public final String getUserId() { + return userId; + } + + public final void setUserId(String userId) { + this.userId = userId; + } + }