Source Code Cross Referenced for TestHttpState.java in  » Net » Apache-common-HttpClient » org » apache » commons » httpclient » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » Apache common HttpClient » org.apache.commons.httpclient 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestHttpState.java,v 1.7 2004/06/23 06:50:25 olegk Exp $
003:         * $Revision: 480424 $
004:         * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005:         * ====================================================================
006:         *
007:         *  Licensed to the Apache Software Foundation (ASF) under one or more
008:         *  contributor license agreements.  See the NOTICE file distributed with
009:         *  this work for additional information regarding copyright ownership.
010:         *  The ASF licenses this file to You under the Apache License, Version 2.0
011:         *  (the "License"); you may not use this file except in compliance with
012:         *  the License.  You may obtain a copy of the License at
013:         *
014:         *      http://www.apache.org/licenses/LICENSE-2.0
015:         *
016:         *  Unless required by applicable law or agreed to in writing, software
017:         *  distributed under the License is distributed on an "AS IS" BASIS,
018:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019:         *  See the License for the specific language governing permissions and
020:         *  limitations under the License.
021:         * ====================================================================
022:         *
023:         * This software consists of voluntary contributions made by many
024:         * individuals on behalf of the Apache Software Foundation.  For more
025:         * information on the Apache Software Foundation, please see
026:         * <http://www.apache.org/>.
027:         *
028:         * [Additional notices, if required by prior licensing conditions]
029:         *
030:         */
031:
032:        package org.apache.commons.httpclient;
033:
034:        import org.apache.commons.httpclient.auth.AuthScope;
035:
036:        import junit.framework.*;
037:
038:        /**
039:         * 
040:         * Simple tests for {@link HttpState}.
041:         *
042:         * @author Rodney Waldhoff
043:         * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
044:         * @author Sean C. Sullivan
045:         * @author Oleg Kalnichevski
046:         * 
047:         * @version $Id: TestHttpState.java 480424 2006-11-29 05:56:49Z bayard $
048:         * 
049:         */
050:        public class TestHttpState extends TestCase {
051:
052:            public final static Credentials CREDS1 = new UsernamePasswordCredentials(
053:                    "user1", "pass1");
054:            public final static Credentials CREDS2 = new UsernamePasswordCredentials(
055:                    "user2", "pass2");
056:
057:            public final static AuthScope SCOPE1 = new AuthScope(
058:                    AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm1");
059:            public final static AuthScope SCOPE2 = new AuthScope(
060:                    AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm2");
061:            public final static AuthScope BOGUS = new AuthScope(
062:                    AuthScope.ANY_HOST, AuthScope.ANY_PORT, "bogus");
063:            public final static AuthScope DEFSCOPE = new AuthScope("host",
064:                    AuthScope.ANY_PORT, "realm");
065:
066:            // ------------------------------------------------------------ Constructor
067:            public TestHttpState(String testName) {
068:                super (testName);
069:            }
070:
071:            // ------------------------------------------------------------------- Main
072:            public static void main(String args[]) {
073:                String[] testCaseName = { TestHttpState.class.getName() };
074:                junit.textui.TestRunner.main(testCaseName);
075:            }
076:
077:            // ------------------------------------------------------- TestCase Methods
078:
079:            public static Test suite() {
080:                return new TestSuite(TestHttpState.class);
081:            }
082:
083:            // ----------------------------------------------------------- Test Methods
084:
085:            public void testHttpStateCredentials() {
086:                HttpState state = new HttpState();
087:                state.setCredentials(SCOPE1, CREDS1);
088:                state.setCredentials(SCOPE2, CREDS2);
089:                assertEquals(CREDS1, state.getCredentials(SCOPE1));
090:                assertEquals(CREDS2, state.getCredentials(SCOPE2));
091:            }
092:
093:            public void testToString() {
094:                HttpState state = new HttpState();
095:                assertNotNull(state.toString());
096:
097:                state.addCookie(new Cookie("foo", "bar", "yeah"));
098:                assertNotNull(state.toString());
099:
100:                state.addCookie(new Cookie("flub", "duck", "yuck"));
101:                assertNotNull(state.toString());
102:
103:                state.setCredentials(SCOPE1, CREDS1);
104:                assertNotNull(state.toString());
105:
106:                state.setProxyCredentials(SCOPE2, CREDS2);
107:                assertNotNull(state.toString());
108:            }
109:
110:            public void testHttpStateNoCredentials() {
111:                HttpState state = new HttpState();
112:                assertEquals(null, state.getCredentials(BOGUS));
113:            }
114:
115:            public void testHttpStateDefaultCredentials() {
116:                HttpState state = new HttpState();
117:                state.setCredentials(AuthScope.ANY, CREDS1);
118:                state.setCredentials(SCOPE2, CREDS2);
119:                assertEquals(CREDS1, state.getCredentials(BOGUS));
120:            }
121:
122:            public void testHttpStateProxyCredentials() {
123:                HttpState state = new HttpState();
124:                state.setProxyCredentials(SCOPE1, CREDS1);
125:                state.setProxyCredentials(SCOPE2, CREDS2);
126:                assertEquals(CREDS1, state.getProxyCredentials(SCOPE1));
127:                assertEquals(CREDS2, state.getProxyCredentials(SCOPE2));
128:            }
129:
130:            public void testHttpStateProxyNoCredentials() {
131:                HttpState state = new HttpState();
132:                assertEquals(null, state.getProxyCredentials(BOGUS));
133:            }
134:
135:            public void testHttpStateProxyDefaultCredentials() {
136:                HttpState state = new HttpState();
137:                state.setProxyCredentials(AuthScope.ANY, CREDS1);
138:                state.setProxyCredentials(SCOPE2, CREDS2);
139:                assertEquals(CREDS1, state.getProxyCredentials(BOGUS));
140:            }
141:
142:            // --------------------------------- Test Methods for Selecting Credentials
143:
144:            public void testDefaultCredentials() throws Exception {
145:                HttpState state = new HttpState();
146:                Credentials expected = new UsernamePasswordCredentials("name",
147:                        "pass");
148:                state.setCredentials(AuthScope.ANY, expected);
149:                Credentials got = state.getCredentials(DEFSCOPE);
150:                assertEquals(got, expected);
151:            }
152:
153:            public void testRealmCredentials() throws Exception {
154:                HttpState state = new HttpState();
155:                Credentials expected = new UsernamePasswordCredentials("name",
156:                        "pass");
157:                state.setCredentials(DEFSCOPE, expected);
158:                Credentials got = state.getCredentials(DEFSCOPE);
159:                assertEquals(expected, got);
160:            }
161:
162:            public void testHostCredentials() throws Exception {
163:                HttpState state = new HttpState();
164:                Credentials expected = new UsernamePasswordCredentials("name",
165:                        "pass");
166:                state.setCredentials(new AuthScope("host", AuthScope.ANY_PORT,
167:                        AuthScope.ANY_REALM), expected);
168:                Credentials got = state.getCredentials(DEFSCOPE);
169:                assertEquals(expected, got);
170:            }
171:
172:            public void testWrongHostCredentials() throws Exception {
173:                HttpState state = new HttpState();
174:                Credentials expected = new UsernamePasswordCredentials("name",
175:                        "pass");
176:                state.setCredentials(new AuthScope("host1", AuthScope.ANY_PORT,
177:                        "realm"), expected);
178:                Credentials got = state.getCredentials(new AuthScope("host2",
179:                        AuthScope.ANY_PORT, "realm"));
180:                assertNotSame(expected, got);
181:            }
182:
183:            public void testWrongRealmCredentials() throws Exception {
184:                HttpState state = new HttpState();
185:                Credentials cred = new UsernamePasswordCredentials("name",
186:                        "pass");
187:                state.setCredentials(new AuthScope("host", AuthScope.ANY_PORT,
188:                        "realm1"), cred);
189:                Credentials got = state.getCredentials(new AuthScope("host",
190:                        AuthScope.ANY_PORT, "realm2"));
191:                assertNotSame(cred, got);
192:            }
193:
194:            // ------------------------------- Test Methods for matching Credentials
195:
196:            public void testScopeMatching() {
197:                AuthScope authscope1 = new AuthScope("somehost", 80,
198:                        "somerealm", "somescheme");
199:                AuthScope authscope2 = new AuthScope("someotherhost", 80,
200:                        "somerealm", "somescheme");
201:                assertTrue(authscope1.match(authscope2) < 0);
202:
203:                int m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
204:                        AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
205:                int m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
206:                        AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
207:                assertTrue(m2 > m1);
208:
209:                m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
210:                        AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
211:                m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
212:                        AuthScope.ANY_PORT, "somerealm", AuthScope.ANY_SCHEME));
213:                assertTrue(m2 > m1);
214:
215:                m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
216:                        AuthScope.ANY_PORT, "somerealm", "somescheme"));
217:                m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, 80,
218:                        AuthScope.ANY_REALM, AuthScope.ANY_SCHEME));
219:                assertTrue(m2 > m1);
220:
221:                m1 = authscope1.match(new AuthScope(AuthScope.ANY_HOST, 80,
222:                        "somerealm", "somescheme"));
223:                m2 = authscope1.match(new AuthScope("somehost",
224:                        AuthScope.ANY_PORT, AuthScope.ANY_REALM,
225:                        AuthScope.ANY_SCHEME));
226:                assertTrue(m2 > m1);
227:
228:                m1 = authscope1.match(AuthScope.ANY);
229:                m2 = authscope1.match(new AuthScope(AuthScope.ANY_HOST,
230:                        AuthScope.ANY_PORT, AuthScope.ANY_REALM, "somescheme"));
231:                assertTrue(m2 > m1);
232:            }
233:
234:            public void testCredentialsMatching() {
235:                Credentials creds1 = new UsernamePasswordCredentials("name1",
236:                        "pass1");
237:                Credentials creds2 = new UsernamePasswordCredentials("name2",
238:                        "pass2");
239:                Credentials creds3 = new UsernamePasswordCredentials("name3",
240:                        "pass3");
241:
242:                AuthScope scope1 = new AuthScope(AuthScope.ANY_HOST,
243:                        AuthScope.ANY_PORT, AuthScope.ANY_REALM);
244:                AuthScope scope2 = new AuthScope(AuthScope.ANY_HOST,
245:                        AuthScope.ANY_PORT, "somerealm");
246:                AuthScope scope3 = new AuthScope("somehost",
247:                        AuthScope.ANY_PORT, AuthScope.ANY_REALM);
248:
249:                HttpState state = new HttpState();
250:                state.setCredentials(scope1, creds1);
251:                state.setCredentials(scope2, creds2);
252:                state.setCredentials(scope3, creds3);
253:
254:                Credentials got = state.getCredentials(new AuthScope(
255:                        "someotherhost", 80, "someotherrealm", "basic"));
256:                Credentials expected = creds1;
257:                assertEquals(expected, got);
258:
259:                got = state.getCredentials(new AuthScope("someotherhost", 80,
260:                        "somerealm", "basic"));
261:                expected = creds2;
262:                assertEquals(expected, got);
263:
264:                got = state.getCredentials(new AuthScope("somehost", 80,
265:                        "someotherrealm", "basic"));
266:                expected = creds3;
267:                assertEquals(expected, got);
268:            }
269:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.