Source Code Cross Referenced for RestClientServerBookTest.java in  » Web-Services-apache-cxf-2.0.1 » systests » org » apache » cxf » systest » rest » 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 » Web Services apache cxf 2.0.1 » systests » org.apache.cxf.systest.rest 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */package org.apache.cxf.systest.rest;
019:
020:        import java.io.File;
021:        import java.io.InputStream;
022:        import java.net.URL;
023:        import java.util.logging.Logger;
024:
025:        import org.apache.commons.httpclient.HttpClient;
026:        import org.apache.commons.httpclient.methods.FileRequestEntity;
027:        import org.apache.commons.httpclient.methods.PostMethod;
028:        import org.apache.commons.httpclient.methods.RequestEntity;
029:        import org.apache.cxf.binding.http.HttpBindingFactory;
030:        import org.apache.cxf.customer.book.Book;
031:        import org.apache.cxf.customer.book.BookService;
032:        import org.apache.cxf.customer.book.GetAnotherBook;
033:        import org.apache.cxf.customer.book.GetBook;
034:        import org.apache.cxf.helpers.IOUtils;
035:        import org.apache.cxf.io.CachedOutputStream;
036:        import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
037:        import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
038:        import org.junit.BeforeClass;
039:        import org.junit.Ignore;
040:        import org.junit.Test;
041:
042:        public class RestClientServerBookTest extends
043:                AbstractBusClientServerTestBase {
044:            static final Logger LOG = Logger
045:                    .getLogger(RestClientServerBookTest.class.getName());
046:
047:            @BeforeClass
048:            public static void startServers() throws Exception {
049:                assertTrue("server did not launch correctly",
050:                        launchServer(BookServer.class));
051:            }
052:
053:            @Test
054:            @Ignore
055:            public void testGetBookWithXmlRootElement() throws Exception {
056:                JaxWsProxyFactoryBean sf = new JaxWsProxyFactoryBean();
057:                sf.setServiceClass(BookService.class);
058:
059:                // Turn off wrapped mode to make our xml prettier
060:                sf.getServiceFactory().setWrapped(false);
061:
062:                // Use the HTTP Binding which understands the Java Rest Annotations
063:                sf.getClientFactoryBean().setBindingId(
064:                        HttpBindingFactory.HTTP_BINDING_ID);
065:                sf.setAddress("http://localhost:9080/xml/");
066:                BookService bs = (BookService) sf.create();
067:                GetBook getBook = new GetBook();
068:                getBook.setId(123);
069:                Book book = bs.getBook(getBook);
070:                assertEquals(book.getId(), (long) 123);
071:                assertEquals(book.getName(), "CXF in Action");
072:            }
073:
074:            @Test
075:            public void testGetBookWithOutXmlRootElement() throws Exception {
076:                JaxWsProxyFactoryBean sf = new JaxWsProxyFactoryBean();
077:                sf.setServiceClass(BookService.class);
078:
079:                // Turn off wrapped mode to make our xml prettier
080:                sf.getServiceFactory().setWrapped(false);
081:
082:                // Use the HTTP Binding which understands the Java Rest Annotations
083:                sf.getClientFactoryBean().setBindingId(
084:                        HttpBindingFactory.HTTP_BINDING_ID);
085:                sf.setAddress("http://localhost:9080/xml/");
086:                BookService bs = (BookService) sf.create();
087:                GetAnotherBook getAnotherBook = new GetAnotherBook();
088:                getAnotherBook.setId(123);
089:                Book book = bs.getAnotherBook(getAnotherBook);
090:                assertEquals(book.getId(), (long) 123);
091:                assertEquals(book.getName(), "CXF in Action");
092:            }
093:
094:            @Test
095:            public void testGetBooksJSON() throws Exception {
096:                String endpointAddress = "http://localhost:9080/json/books";
097:                URL url = new URL(endpointAddress);
098:                InputStream in = url.openStream();
099:                assertNotNull(in);
100:
101:                InputStream expected = getClass().getResourceAsStream(
102:                        "resources/expected_json_books.txt");
103:
104:                assertEquals(getStringFromInputStream(expected),
105:                        getStringFromInputStream(in));
106:            }
107:
108:            @Test
109:            @Ignore
110:            public void testGetBookJSON() throws Exception {
111:                String endpointAddress = "http://localhost:9080/json/books/123";
112:                URL url = new URL(endpointAddress);
113:                InputStream in = url.openStream();
114:                assertNotNull(in);
115:
116:                InputStream expected = getClass().getResourceAsStream(
117:                        "resources/expected_json_book123.txt");
118:
119:                assertEquals(getStringFromInputStream(expected),
120:                        getStringFromInputStream(in));
121:            }
122:
123:            @Test
124:            public void testAddBookJSON() throws Exception {
125:                String endpointAddress = "http://localhost:9080/json/books";
126:
127:                String inputFile = getClass().getResource(
128:                        "resources/add_book_json.txt").getFile();
129:                File input = new File(inputFile);
130:                PostMethod post = new PostMethod(endpointAddress);
131:                RequestEntity entity = new FileRequestEntity(input,
132:                        "text/plain; charset=ISO-8859-1");
133:                post.setRequestEntity(entity);
134:                HttpClient httpclient = new HttpClient();
135:
136:                try {
137:                    int result = httpclient.executeMethod(post);
138:                    System.out.println("Response status code: " + result);
139:                    System.out.println("Response body: ");
140:                    System.out.println(post.getResponseBodyAsString());
141:
142:                    //InputStream expected = getClass().getResourceAsStream("resources/expected_add_book_json.txt");
143:
144:                    //FIXME: result returned is not correct: {"acme.addBookResponse":"2"}
145:                    //assertEquals(getStringFromInputStream(expected), post.getResponseBodyAsString());
146:                } finally {
147:                    // Release current connection to the connection pool once you are done
148:                    post.releaseConnection();
149:                }
150:            }
151:
152:            private String getStringFromInputStream(InputStream in)
153:                    throws Exception {
154:                CachedOutputStream bos = new CachedOutputStream();
155:                IOUtils.copy(in, bos);
156:                in.close();
157:                bos.close();
158:                System.out.println(bos.getOut().toString());
159:                return bos.getOut().toString();
160:            }
161:
162:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.