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