/* * Do not ALTER or REMOVE the COPYRIGHT notices or this HEADER. * * Copyright (c) 2010-2014 Oracle and/or its affiliates.
All rights reserved. * * The contents of this file is subject to the terms of either the GNU * general public License Version 2 only ("GPL") or the Common development * and Distribution License ("CDDL") (collectively, the "License"). You are not a use of this file except in compliance with the License. You can * obtain a copy of the License at * http://glassfish.java.net/public/CDDL+GPL_1_1.html * or Packager/legal/lice
NSE.txt. See the License for the specific * language governing permissions and limitations under the License. * When distributing the software, include this License Header Notice in each * file and include the License file at PA
Ckager/legal/license.txt. * * GPL Classpath Exception: * Oracle designates this particular file as subject to the "Classpath" * Exception as Prov IDed by Oracle under the GPL Version 2 section of the License * fIle that accompanied this code. * * Modifications: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] Replaced by your own identifying information: * "portions copyright [year] [name of copyright owner]" * * contributo R (s): * If you wish your version of this file to is governed by only the CDDL or * only the GPL version 2, indicate your decision by adding "[contributor] * elects to include this software in this distribution under the [CDDL or GPL * versi On 2] license. " If you don ' t indicate a single choice of license, A * recipient have the option to distribute your version of this file UN Der * Either the CDDL, the GPL Version 2 or to extend the choice of license to * its licensees as provided above. However, if you add GPL version 2 Code * and therefore, elected the GPL version 2 license, then the option applies * onl
Y if the new code is made subject to such option by the copyright * holder. */Package Org.glassfisH.jersey.examples.helloworld;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Java.util.concurrent.CountDownLatch;
Import Java.util.concurrent.TimeUnit;
Import Java.util.logging.Logger;
Import javax.ws.rs.client.Client;
Import Javax.ws.rs.client.ClientBuilder;
Import Javax.ws.rs.client.InvocationCallback;
Import Javax.ws.rs.client.WebTarget;
Import Javax.ws.rs.core.MediaType;
Import Javax.ws.rs.core.Response;
Import Javax.ws.rs.core.UriBuilder;
Import Org.glassfish.jersey.server.ResourceConfig;
Import Org.glassfish.jersey.test.JerseyTest;
Import org.glassfish.jersey.test.TestProperties;
Import Org.junit.Ignore;
Import Org.junit.Test;
Import static org.junit.Assert.assertEquals;
Import static org.junit.Assert.assertTrue; public class HelloWorldTest extends Jerseytest {@Override protected resourceconfig Configure () {//MVN Test-djersey.config.test.container.factory=org.glassfish.jersey.test.inmemory.inmemorytestcontainerfactory//MV N Test-djersey.config.test.container.factory=org.glassfish.jersey.test.grizzly.grizzlytestcontainerfactory//MVN test- Djersey.config.test.container.factory=org.glassfish.jersey.test.jdkhttp.jdkhttpservertestcontainerfactory//MVN Test-djersey.config.test.container.factory=org.glassfish.jersey.test.simple.simpletestcontainerfactory Enable (
TESTPROPERTIES.LOG_TRAFFIC);
Enable (testproperties.dump_entity);
return new ResourceConfig (Helloworldresource.class); }//Uncomment to use Grizzly Async client//@Override//protected void Configureclient (ClientConfig clientcon FIG) {//Clientconfig.connector (new Grizzlyconnector (ClientConfig));//} @Test @Ignore ("Not Compatibl E with the test framework (doesn ' t use client ()) "public void Testhelloworld () throws Exception {URL GetUrl = Ur
Ibuilder.fromuri (Getbaseuri ()). Path (App.root_path). Build (). Tourl (); HttpURLConnection connection = (httpurlconnection) getuRl.openconnection ();
try {connection.setdooutput (true);
Connection.setinstancefollowredirects (FALSE);
Connection.setrequestmethod ("GET");
Connection.setrequestproperty ("Content-type", "Text/plain");
Assertequals (HTTPURLCONNECTION.HTTP_OK, Connection.getresponsecode ());
} finally {Connection.disconnect (); }} @Test public void Testconnection () {Response Response = target (). Path (App.root_path). Request ("Te
Xt/plain "). get ();
Assertequals (Response.getstatus ()); } @Test public void Testclientstringresponse () {String s = target (). Path (App.root_path). Request (). Get (St
Ring.class);
SYSTEM.OUT.PRINTLN ("Final data returned:" +s);
Assertequals (helloworldresource.cliched_message, s);
} @Test public void Testasyncclientrequests () throws interruptedexception {Final int requests = 10; Final Countdownlatch latch = new CoUntdownlatch (requests);
Final Long tic = System.currenttimemillis ();
for (int i = 0; i < requests; i++) {final int id = i;
Target (). Path (App.root_path). Request (). Async (). Get (new invocationcallback<response> () {@Override public void completed (Response Response) {try {final String re
Sult = response.readentity (String.class);
Assertequals (helloworldresource.cliched_message, result);
} finally {Latch.countdown (); }} @Override public void failed (Throwable error) {E
Rror.printstacktrace ();
Latch.countdown ();
}
});
} latch.await (Ten * Getasynctimeoutmultiplier (), timeunit.seconds);
Final Long TOC = System.currenttimemillis (); Logger.GetLogger (HelloWorldTest.class.getName ()). info ("Executed in:" + (toc-tic));
} @Test public void Testhead () {Response Response = target (). Path (App.root_path). Request (). Head ();
Assertequals (Response.getstatus ());
Assertequals (Mediatype.text_plain_type, Response.getmediatype ()); } @Test public void Testfoobaroptions () {Response Response = target (). Path (App.root_path). Request (). Head
ER ("Accept", "Foo/bar"). Options ();
Assertequals (Response.getstatus ());
Final String Allowheader = response.getheaderstring ("Allow");
_checkallowcontent (Allowheader);
Assertequals ("Foo/bar", Response.getmediatype (). toString ());
Assertequals (0, Response.getlength ()); } @Test public void Testtextplainoptions () {Response Response = target (). Path (App.root_path). Request (). h
Eader ("Accept", Mediatype.text_plain). Options ();
Assertequals (Response.getstatus ()); Final StRing Allowheader = response.getheaderstring ("Allow");
_checkallowcontent (Allowheader);
Assertequals (Mediatype.text_plain_type, Response.getmediatype ());
Final String responsebody = response.readentity (String.class);
_checkallowcontent (responsebody);
} private void _checkallowcontent (final String content) {asserttrue (Content.contains ("GET"));
Asserttrue (Content.contains ("HEAD"));
Asserttrue (Content.contains ("Options"));
} @Test public void Testmissingresourcenotfound () {Response Response;
Response = target (). Path (App.root_path + "arbitrary"). Request (). get ();
Assertequals (404, Response.getstatus ());
Response = target (). Path (App.root_path). Path ("arbitrary"). Request (). get ();
Assertequals (404, Response.getstatus ());
} @Test public void Testloggingfilterclientclass () {Client client = client (); Client.register (Customloggingfilter.class). ProperTy ("foo", "Bar");
customloggingfilter.prefiltercalled = customloggingfilter.postfiltercalled = 0;
String s = target (). Path (App.root_path). Request (). get (String.class);
Assertequals (helloworldresource.cliched_message, s);
Assertequals (1, customloggingfilter.prefiltercalled);
Assertequals (1, customloggingfilter.postfiltercalled);
} @Test public void Testloggingfilterclientinstance () {Client client = client ();
Client.register (New Customloggingfilter ()). Property ("foo", "Bar");
customloggingfilter.prefiltercalled = customloggingfilter.postfiltercalled = 0;
String s = target (). Path (App.root_path). Request (). get (String.class);
Assertequals (helloworldresource.cliched_message, s);
Assertequals (1, customloggingfilter.prefiltercalled);
Assertequals (1, customloggingfilter.postfiltercalled); } @Test public void Testloggingfiltertargetclass () {Webtarget target = target ().Path (App.root_path);
Target.register (Customloggingfilter.class). Property ("foo", "Bar");
customloggingfilter.prefiltercalled = customloggingfilter.postfiltercalled = 0;
String s = target.request (). get (String.class);
Assertequals (helloworldresource.cliched_message, s);
Assertequals (1, customloggingfilter.prefiltercalled);
Assertequals (1, customloggingfilter.postfiltercalled);
} @Test public void Testloggingfiltertargetinstance () {Webtarget target = target (). path (App.root_path);
Target.register (New Customloggingfilter ()). Property ("foo", "Bar");
customloggingfilter.prefiltercalled = customloggingfilter.postfiltercalled = 0;
String s = target.request (). get (String.class);
Assertequals (helloworldresource.cliched_message, s);
Assertequals (1, customloggingfilter.prefiltercalled);
Assertequals (1, customloggingfilter.postfiltercalled); } @Test public void TestconfigurAtionupdate () {Client client1 = client ();
Client1.register (Customloggingfilter.class). Property ("foo", "Bar");
Client client = Clientbuilder.newclient (Client1.getconfiguration ());
customloggingfilter.prefiltercalled = customloggingfilter.postfiltercalled = 0;
String s = target (). Path (App.root_path). Request (). get (String.class);
Assertequals (helloworldresource.cliched_message, s);
Assertequals (1, customloggingfilter.prefiltercalled);
Assertequals (1, customloggingfilter.postfiltercalled);
}
}