1 /* 2 * Copyright (C) 2009 The Android Open Source Project 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 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.apache.harmony.xnet.provider.jsse; 18 19 import junit.framework.TestCase; 20 21 import javax.net.ssl.SSLSession; 22 import java.util.Enumeration; 23 import java.util.Set; 24 import java.util.HashSet; 25 26 public class ClientSessionContextTest extends TestCase { 27 28 public void testGetSessionById() { 29 ClientSessionContext context = new ClientSessionContext(null, null); 30 31 SSLSession a = new FakeSession("a"); 32 SSLSession b = new FakeSession("b"); 33 34 context.putSession(a); 35 context.putSession(b); 36 37 assertSame(a, context.getSession("a".getBytes())); 38 assertSame(b, context.getSession("b".getBytes())); 39 40 assertSame(a, context.getSession("a", 443)); 41 assertSame(b, context.getSession("b", 443)); 42 43 assertEquals(2, context.sessions.size()); 44 45 Set<SSLSession> sessions = new HashSet<SSLSession>(); 46 Enumeration ids = context.getIds(); 47 while (ids.hasMoreElements()) { 48 sessions.add(context.getSession((byte[]) ids.nextElement())); 49 } 50 51 Set<SSLSession> expected = new HashSet<SSLSession>(); 52 expected.add(a); 53 expected.add(b); 54 55 assertEquals(expected, sessions); 56 } 57 58 public void testTrimToSize() { 59 ClientSessionContext context = new ClientSessionContext(null, null); 60 61 FakeSession a = new FakeSession("a"); 62 FakeSession b = new FakeSession("b"); 63 FakeSession c = new FakeSession("c"); 64 FakeSession d = new FakeSession("d"); 65 66 context.putSession(a); 67 context.putSession(b); 68 context.putSession(c); 69 context.putSession(d); 70 71 context.setSessionCacheSize(2); 72 73 Set<SSLSession> sessions = new HashSet<SSLSession>(); 74 Enumeration ids = context.getIds(); 75 while (ids.hasMoreElements()) { 76 sessions.add(context.getSession((byte[]) ids.nextElement())); 77 } 78 79 Set<SSLSession> expected = new HashSet<SSLSession>(); 80 expected.add(c); 81 expected.add(d); 82 83 assertEquals(expected, sessions); 84 } 85 86 } 87