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