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