1*4b5ab84dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*4b5ab84dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*4b5ab84dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*4b5ab84dSAndrew Rist  * distributed with this work for additional information
6*4b5ab84dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*4b5ab84dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*4b5ab84dSAndrew Rist  * "License"); you may not use this file except in compliance
9*4b5ab84dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*4b5ab84dSAndrew Rist  *
11*4b5ab84dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*4b5ab84dSAndrew Rist  *
13*4b5ab84dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*4b5ab84dSAndrew Rist  * software distributed under the License is distributed on an
15*4b5ab84dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*4b5ab84dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*4b5ab84dSAndrew Rist  * specific language governing permissions and limitations
18*4b5ab84dSAndrew Rist  * under the License.
19*4b5ab84dSAndrew Rist  *
20*4b5ab84dSAndrew Rist  *************************************************************/
21*4b5ab84dSAndrew Rist 
22*4b5ab84dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package complex.passwordcontainer;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
27cdf0e10cSrcweir import com.sun.star.task.XPasswordContainer;
28cdf0e10cSrcweir import com.sun.star.task.XMasterPasswordHandling;
29cdf0e10cSrcweir import com.sun.star.task.XInteractionHandler;
30cdf0e10cSrcweir import com.sun.star.task.UrlRecord;
31cdf0e10cSrcweir import com.sun.star.task.UserRecord;
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class Test02 implements PasswordContainerTest {
37cdf0e10cSrcweir     XMultiServiceFactory m_xMSF = null;
38cdf0e10cSrcweir     XPasswordContainer m_xPasswordContainer = null;
39cdf0e10cSrcweir     TestHelper m_aTestHelper = null;
40cdf0e10cSrcweir 
Test02( XMultiServiceFactory xMSF )41cdf0e10cSrcweir     public Test02 ( XMultiServiceFactory xMSF )
42cdf0e10cSrcweir     {
43cdf0e10cSrcweir         m_xMSF = xMSF;
44cdf0e10cSrcweir         m_aTestHelper = new TestHelper ( "Test02: ");
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
test()47cdf0e10cSrcweir     public boolean test() {
48cdf0e10cSrcweir         final String sURL = "http://www.openoffice.org";
49cdf0e10cSrcweir         final String sUserPre = "OOoUser";
50cdf0e10cSrcweir         final String sPwdPre = "Password";
51cdf0e10cSrcweir         final int iUserNum1 = 10;
52cdf0e10cSrcweir         final int iUserNum2 = 5;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir         UserRecord aInputUserList1[] = new UserRecord[iUserNum1];
55cdf0e10cSrcweir         for(int i = 0; i < iUserNum1; i++) {
56cdf0e10cSrcweir             String sTemp[] = {sPwdPre + "_1_" + i};     // currently one password for one user
57cdf0e10cSrcweir             aInputUserList1[i] = new UserRecord(sUserPre + "_1_" + i, sTemp);
58cdf0e10cSrcweir         }
59cdf0e10cSrcweir         UserRecord aInputUserList2[] = new UserRecord[iUserNum2];
60cdf0e10cSrcweir         for(int i = 0; i < iUserNum2; i++) {
61cdf0e10cSrcweir             String sTemp[] = {sPwdPre + "_2_" + i};
62cdf0e10cSrcweir             aInputUserList2[i] = new UserRecord(sUserPre + "_2_" + i, sTemp);
63cdf0e10cSrcweir         }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir         try {
66cdf0e10cSrcweir             Object oPasswordContainer = m_xMSF.createInstance("com.sun.star.task.PasswordContainer");
67cdf0e10cSrcweir             XPasswordContainer xContainer = UnoRuntime.queryInterface(XPasswordContainer.class, oPasswordContainer);
68cdf0e10cSrcweir             Object oHandler = m_xMSF.createInstance("com.sun.star.task.InteractionHandler");
69cdf0e10cSrcweir             XInteractionHandler xHandler = UnoRuntime.queryInterface(XInteractionHandler.class, oHandler);
70cdf0e10cSrcweir             MasterPasswdHandler aMHandler = new MasterPasswdHandler(xHandler);
71cdf0e10cSrcweir             XMasterPasswordHandling xMHandling = UnoRuntime.queryInterface(XMasterPasswordHandling.class, oPasswordContainer);
72cdf0e10cSrcweir 
73cdf0e10cSrcweir             // allow the storing of the passwords
74cdf0e10cSrcweir             xMHandling.allowPersistentStoring(true);
75cdf0e10cSrcweir 
76cdf0e10cSrcweir             // add a set of users and passwords for the same URL persistently
77cdf0e10cSrcweir             for(int i = 0; i < iUserNum1; ++i) {
78cdf0e10cSrcweir                 xContainer.addPersistent(sURL, aInputUserList1[i].UserName, aInputUserList1[i].Passwords, aMHandler);
79cdf0e10cSrcweir             }
80cdf0e10cSrcweir             for(int i = 0; i < iUserNum2; ++i) {
81cdf0e10cSrcweir                 xContainer.addPersistent(sURL, aInputUserList2[i].UserName, aInputUserList2[i].Passwords, aMHandler);
82cdf0e10cSrcweir             }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             // remove some of the passwords
85cdf0e10cSrcweir             for(int i = 0; i < iUserNum1; ++i) {
86cdf0e10cSrcweir                 xContainer.remove(sURL, aInputUserList1[i].UserName);
87cdf0e10cSrcweir             }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir             // get the result with find() and check it with the expected one
90cdf0e10cSrcweir             UrlRecord aRecord = xContainer.find(sURL, aMHandler);
91cdf0e10cSrcweir             if(!aRecord.Url.equals(sURL)) {
92cdf0e10cSrcweir                 m_aTestHelper.Error("URL mismatch. Got " + aRecord.Url + "; should be " + sURL);
93cdf0e10cSrcweir                 return false;
94cdf0e10cSrcweir             }
95cdf0e10cSrcweir             if(!m_aTestHelper.sameLists(aRecord.UserList, aInputUserList2)) {
96cdf0e10cSrcweir                 m_aTestHelper.Error("User list is not the expected");
97cdf0e10cSrcweir                 return false;
98cdf0e10cSrcweir             }
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             // get the result with getAllPersistent() and check
101cdf0e10cSrcweir             UrlRecord aRecords[] = xContainer.getAllPersistent(aMHandler);
102cdf0e10cSrcweir             if(!aRecords[0].Url.equals(sURL)) {
103cdf0e10cSrcweir                 m_aTestHelper.Error("URL mismatch");
104cdf0e10cSrcweir                 return false;
105cdf0e10cSrcweir             }
106cdf0e10cSrcweir             if(!m_aTestHelper.sameLists(aRecords[0].UserList, aInputUserList2)) {
107cdf0e10cSrcweir                 m_aTestHelper.Error("User list is not the expected");
108cdf0e10cSrcweir                 return false;
109cdf0e10cSrcweir             }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             // remove all the persistent passwords
112cdf0e10cSrcweir             xContainer.removeAllPersistent();
113cdf0e10cSrcweir 
114cdf0e10cSrcweir             // remove the runtime passwords
115cdf0e10cSrcweir             for(int i = 0; i < aRecords[0].UserList.length; ++i) {
116cdf0e10cSrcweir                 xContainer.remove(sURL, aRecords[0].UserList[i].UserName);
117cdf0e10cSrcweir             }
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             // disallow the storing of the passwords
120cdf0e10cSrcweir             xMHandling.allowPersistentStoring(false);
121cdf0e10cSrcweir         } catch(Exception e) {
122cdf0e10cSrcweir             m_aTestHelper.Error("Exception: " + e);
123cdf0e10cSrcweir             return false;
124cdf0e10cSrcweir         }
125cdf0e10cSrcweir         return true;
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
140cdf0e10cSrcweir 
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 
151