View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertNull;
19  
20  import org.apache.commons.lang3.SerializationUtils;
21  import org.apache.http.auth.AuthScope;
22  import org.junit.Test;
23  
24  /**
25   * Tests for {@link DefaultCredentialsProvider}.
26   *
27   * @author Marc Guillemot
28   */
29  public class DefaultCredentialsProvider3Test {
30  
31      /**
32       * Test.
33       */
34      @Test
35      public void serialization() {
36          final String username = "foo";
37          final char[] password = "password".toCharArray();
38          final String host = "my.host";
39          final int port = 1234;
40          final String realm = "blah";
41          final String scheme = "NTLM";
42  
43          DefaultCredentialsProvider provider = new DefaultCredentialsProvider();
44          provider.addCredentials(username, password, host, port, realm);
45  
46          assertNotNull(provider.getCredentials(new AuthScope(host, port, realm, scheme)));
47          assertNull(provider.getCredentials(new AuthScope("invalidHost", port, realm, scheme)));
48          assertNotNull(provider.getCredentials(new AuthScope(host, port, realm, scheme)));
49          assertNull(provider.getCredentials(new AuthScope("invalidHost", port, realm, scheme)));
50  
51          provider = SerializationUtils.clone(provider);
52  
53          assertNotNull(provider.getCredentials(new AuthScope(host, port, realm, scheme)));
54          assertNull(provider.getCredentials(new AuthScope("invalidHost", port, realm, scheme)));
55          assertNotNull(provider.getCredentials(new AuthScope(host, port, realm, scheme)));
56          assertNull(provider.getCredentials(new AuthScope("invalidHost", port, realm, scheme)));
57      }
58  }