1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package complex.passwordcontainer; 29 30 import com.sun.star.lang.XMultiServiceFactory; 31 import com.sun.star.task.XPasswordContainer; 32 import com.sun.star.task.XMasterPasswordHandling; 33 import com.sun.star.task.XInteractionHandler; 34 import com.sun.star.task.UrlRecord; 35 import com.sun.star.task.UserRecord; 36 37 import com.sun.star.uno.UnoRuntime; 38 39 40 public class Test02 implements PasswordContainerTest { 41 XMultiServiceFactory m_xMSF = null; 42 XPasswordContainer m_xPasswordContainer = null; 43 TestHelper m_aTestHelper = null; 44 45 public Test02 ( XMultiServiceFactory xMSF ) 46 { 47 m_xMSF = xMSF; 48 m_aTestHelper = new TestHelper ( "Test02: "); 49 } 50 51 public boolean test() { 52 final String sURL = "http://www.openoffice.org"; 53 final String sUserPre = "OOoUser"; 54 final String sPwdPre = "Password"; 55 final int iUserNum1 = 10; 56 final int iUserNum2 = 5; 57 58 UserRecord aInputUserList1[] = new UserRecord[iUserNum1]; 59 for(int i = 0; i < iUserNum1; i++) { 60 String sTemp[] = {sPwdPre + "_1_" + i}; // currently one password for one user 61 aInputUserList1[i] = new UserRecord(sUserPre + "_1_" + i, sTemp); 62 } 63 UserRecord aInputUserList2[] = new UserRecord[iUserNum2]; 64 for(int i = 0; i < iUserNum2; i++) { 65 String sTemp[] = {sPwdPre + "_2_" + i}; 66 aInputUserList2[i] = new UserRecord(sUserPre + "_2_" + i, sTemp); 67 } 68 69 try { 70 Object oPasswordContainer = m_xMSF.createInstance("com.sun.star.task.PasswordContainer"); 71 XPasswordContainer xContainer = UnoRuntime.queryInterface(XPasswordContainer.class, oPasswordContainer); 72 Object oHandler = m_xMSF.createInstance("com.sun.star.task.InteractionHandler"); 73 XInteractionHandler xHandler = UnoRuntime.queryInterface(XInteractionHandler.class, oHandler); 74 MasterPasswdHandler aMHandler = new MasterPasswdHandler(xHandler); 75 XMasterPasswordHandling xMHandling = UnoRuntime.queryInterface(XMasterPasswordHandling.class, oPasswordContainer); 76 77 // allow the storing of the passwords 78 xMHandling.allowPersistentStoring(true); 79 80 // add a set of users and passwords for the same URL persistently 81 for(int i = 0; i < iUserNum1; ++i) { 82 xContainer.addPersistent(sURL, aInputUserList1[i].UserName, aInputUserList1[i].Passwords, aMHandler); 83 } 84 for(int i = 0; i < iUserNum2; ++i) { 85 xContainer.addPersistent(sURL, aInputUserList2[i].UserName, aInputUserList2[i].Passwords, aMHandler); 86 } 87 88 // remove some of the passwords 89 for(int i = 0; i < iUserNum1; ++i) { 90 xContainer.remove(sURL, aInputUserList1[i].UserName); 91 } 92 93 // get the result with find() and check it with the expected one 94 UrlRecord aRecord = xContainer.find(sURL, aMHandler); 95 if(!aRecord.Url.equals(sURL)) { 96 m_aTestHelper.Error("URL mismatch. Got " + aRecord.Url + "; should be " + sURL); 97 return false; 98 } 99 if(!m_aTestHelper.sameLists(aRecord.UserList, aInputUserList2)) { 100 m_aTestHelper.Error("User list is not the expected"); 101 return false; 102 } 103 104 // get the result with getAllPersistent() and check 105 UrlRecord aRecords[] = xContainer.getAllPersistent(aMHandler); 106 if(!aRecords[0].Url.equals(sURL)) { 107 m_aTestHelper.Error("URL mismatch"); 108 return false; 109 } 110 if(!m_aTestHelper.sameLists(aRecords[0].UserList, aInputUserList2)) { 111 m_aTestHelper.Error("User list is not the expected"); 112 return false; 113 } 114 115 // remove all the persistent passwords 116 xContainer.removeAllPersistent(); 117 118 // remove the runtime passwords 119 for(int i = 0; i < aRecords[0].UserList.length; ++i) { 120 xContainer.remove(sURL, aRecords[0].UserList[i].UserName); 121 } 122 123 // disallow the storing of the passwords 124 xMHandling.allowPersistentStoring(false); 125 } catch(Exception e) { 126 m_aTestHelper.Error("Exception: " + e); 127 return false; 128 } 129 return true; 130 } 131 } 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155