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.XPasswordContainer;
28 import com.sun.star.task.XMasterPasswordHandling;
29 import com.sun.star.task.XInteractionHandler;
30 import com.sun.star.task.UrlRecord;
31 import com.sun.star.task.UserRecord;
32 
33 import com.sun.star.uno.UnoRuntime;
34 
35 
36 public class Test02 implements PasswordContainerTest {
37     XMultiServiceFactory m_xMSF = null;
38     XPasswordContainer m_xPasswordContainer = null;
39     TestHelper m_aTestHelper = null;
40 
Test02( XMultiServiceFactory xMSF )41     public Test02 ( XMultiServiceFactory xMSF )
42     {
43         m_xMSF = xMSF;
44         m_aTestHelper = new TestHelper ( "Test02: ");
45     }
46 
test()47     public boolean test() {
48         final String sURL = "http://www.openoffice.org";
49         final String sUserPre = "OOoUser";
50         final String sPwdPre = "Password";
51         final int iUserNum1 = 10;
52         final int iUserNum2 = 5;
53 
54         UserRecord aInputUserList1[] = new UserRecord[iUserNum1];
55         for(int i = 0; i < iUserNum1; i++) {
56             String sTemp[] = {sPwdPre + "_1_" + i};     // currently one password for one user
57             aInputUserList1[i] = new UserRecord(sUserPre + "_1_" + i, sTemp);
58         }
59         UserRecord aInputUserList2[] = new UserRecord[iUserNum2];
60         for(int i = 0; i < iUserNum2; i++) {
61             String sTemp[] = {sPwdPre + "_2_" + i};
62             aInputUserList2[i] = new UserRecord(sUserPre + "_2_" + i, sTemp);
63         }
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             XMasterPasswordHandling xMHandling = UnoRuntime.queryInterface(XMasterPasswordHandling.class, oPasswordContainer);
72 
73             // allow the storing of the passwords
74             xMHandling.allowPersistentStoring(true);
75 
76             // add a set of users and passwords for the same URL persistently
77             for(int i = 0; i < iUserNum1; ++i) {
78                 xContainer.addPersistent(sURL, aInputUserList1[i].UserName, aInputUserList1[i].Passwords, aMHandler);
79             }
80             for(int i = 0; i < iUserNum2; ++i) {
81                 xContainer.addPersistent(sURL, aInputUserList2[i].UserName, aInputUserList2[i].Passwords, aMHandler);
82             }
83 
84             // remove some of the passwords
85             for(int i = 0; i < iUserNum1; ++i) {
86                 xContainer.remove(sURL, aInputUserList1[i].UserName);
87             }
88 
89             // get the result with find() and check it with the expected one
90             UrlRecord aRecord = xContainer.find(sURL, aMHandler);
91             if(!aRecord.Url.equals(sURL)) {
92                 m_aTestHelper.Error("URL mismatch. Got " + aRecord.Url + "; should be " + sURL);
93                 return false;
94             }
95             if(!m_aTestHelper.sameLists(aRecord.UserList, aInputUserList2)) {
96                 m_aTestHelper.Error("User list is not the expected");
97                 return false;
98             }
99 
100             // get the result with getAllPersistent() and check
101             UrlRecord aRecords[] = xContainer.getAllPersistent(aMHandler);
102             if(!aRecords[0].Url.equals(sURL)) {
103                 m_aTestHelper.Error("URL mismatch");
104                 return false;
105             }
106             if(!m_aTestHelper.sameLists(aRecords[0].UserList, aInputUserList2)) {
107                 m_aTestHelper.Error("User list is not the expected");
108                 return false;
109             }
110 
111             // remove all the persistent passwords
112             xContainer.removeAllPersistent();
113 
114             // remove the runtime passwords
115             for(int i = 0; i < aRecords[0].UserList.length; ++i) {
116                 xContainer.remove(sURL, aRecords[0].UserList[i].UserName);
117             }
118 
119             // disallow the storing of the passwords
120             xMHandling.allowPersistentStoring(false);
121         } catch(Exception e) {
122             m_aTestHelper.Error("Exception: " + e);
123             return false;
124         }
125         return true;
126     }
127 }
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151