xref: /AOO41X/main/svl/qa/complex/passwordcontainer/Test01.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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.XInteractionHandler;
32*cdf0e10cSrcweir import com.sun.star.task.XPasswordContainer;
33*cdf0e10cSrcweir import com.sun.star.task.UrlRecord;
34*cdf0e10cSrcweir import com.sun.star.task.UserRecord;
35*cdf0e10cSrcweir import com.sun.star.task.XMasterPasswordHandling;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir // import share.LogWriter;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir public class Test01 implements PasswordContainerTest {
42*cdf0e10cSrcweir     XMultiServiceFactory m_xMSF = null;
43*cdf0e10cSrcweir     XPasswordContainer m_xPasswordContainer = null;
44*cdf0e10cSrcweir     TestHelper m_aTestHelper = null;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir     public Test01 ( XMultiServiceFactory xMSF )
47*cdf0e10cSrcweir     {
48*cdf0e10cSrcweir         m_xMSF = xMSF;
49*cdf0e10cSrcweir         m_aTestHelper = new TestHelper ( "Test01: ");
50*cdf0e10cSrcweir     }
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     public boolean test() {
53*cdf0e10cSrcweir         final String sURL = "http://www.openoffice.org";
54*cdf0e10cSrcweir         final String sUserPre = "OOoUser";
55*cdf0e10cSrcweir         final String sPwdPre = "Password";
56*cdf0e10cSrcweir         final int iUserNum1 = 10;
57*cdf0e10cSrcweir         final int iUserNum2 = 5;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir         UserRecord aInputUserList1[] = new UserRecord[iUserNum1];
60*cdf0e10cSrcweir         for(int i = 0; i < iUserNum1; i++) {
61*cdf0e10cSrcweir             String sTemp[] = {sPwdPre + "_1_" + i};     // currently one password for one user
62*cdf0e10cSrcweir             aInputUserList1[i] = new UserRecord(sUserPre + "_1_" + i, sTemp);
63*cdf0e10cSrcweir         }
64*cdf0e10cSrcweir         UserRecord aInputUserList2[] = new UserRecord[iUserNum2];
65*cdf0e10cSrcweir         for(int i = 0; i < iUserNum2; i++) {
66*cdf0e10cSrcweir             String sTemp[] = {sPwdPre + "_2_" + i};
67*cdf0e10cSrcweir             aInputUserList2[i] = new UserRecord(sUserPre + "_2_" + i, sTemp);
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 
76*cdf0e10cSrcweir             // add a set of users and passwords for the same URL for runtime
77*cdf0e10cSrcweir             for(int i = 0; i < iUserNum1; i++) {
78*cdf0e10cSrcweir                 xContainer.add(sURL, aInputUserList1[i].UserName, aInputUserList1[i].Passwords, aMHandler);
79*cdf0e10cSrcweir             }
80*cdf0e10cSrcweir             for (int i = 0; i < iUserNum2; i++) {
81*cdf0e10cSrcweir                 xContainer.add(sURL, aInputUserList2[i].UserName, aInputUserList2[i].Passwords, aMHandler);
82*cdf0e10cSrcweir             }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir             // remove some of the passwords
85*cdf0e10cSrcweir             for (int i = 0; i < iUserNum1; i++) {
86*cdf0e10cSrcweir                 xContainer.remove(sURL, aInputUserList1[i].UserName);
87*cdf0e10cSrcweir             }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir             // get the result and check it with the expected one
90*cdf0e10cSrcweir             UrlRecord aRecord = xContainer.find(sURL, aMHandler);
91*cdf0e10cSrcweir             if(!aRecord.Url.equals(sURL)) {
92*cdf0e10cSrcweir                 m_aTestHelper.Error("URL mismatch. Got " + aRecord.Url + "; should be " + sURL);
93*cdf0e10cSrcweir                 return false;
94*cdf0e10cSrcweir             }
95*cdf0e10cSrcweir             if(!m_aTestHelper.sameLists(aRecord.UserList, aInputUserList2)) {
96*cdf0e10cSrcweir                 m_aTestHelper.Error("User list is not the expected");
97*cdf0e10cSrcweir                 return false;
98*cdf0e10cSrcweir             }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir             // remove the runtime passwords
101*cdf0e10cSrcweir             aRecord = xContainer.find(sURL, aMHandler);
102*cdf0e10cSrcweir             for(int i = 0; i < aRecord.UserList.length; i++) {
103*cdf0e10cSrcweir                 xContainer.remove(sURL, aRecord.UserList[i].UserName);
104*cdf0e10cSrcweir             }
105*cdf0e10cSrcweir         } catch(Exception e) {
106*cdf0e10cSrcweir             m_aTestHelper.Error("Exception: " + e);
107*cdf0e10cSrcweir             return false;
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir         return true;
110*cdf0e10cSrcweir     }
111*cdf0e10cSrcweir }
112