Skip to content

Instantly share code, notes, and snippets.

@massie
Created July 30, 2012 15:21
Show Gist options
  • Save massie/3207745 to your computer and use it in GitHub Desktop.
Save massie/3207745 to your computer and use it in GitHub Desktop.
CXF Test Case
import org.apache.cxf.feature.AbstractFeature;
import org.apache.cxf.feature.LoggingFeature;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
import org.junit.Test;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.Arrays;
public class TestCaseClass {
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public interface GrandchildResource {
@GET
@Path("/")
String readGrandchildren();
}
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public interface ChildResource {
@Path("/{childName}/grandChildren")
public GrandchildResource getGrandchildResource();
}
@Path("/")
public interface RootResource {
@Path("/children")
public ChildResource getChildResource();
}
public class GrandchildResourceImpl implements GrandchildResource {
@Override
public String readGrandchildren() {
return "Marky,Ricky,Danny,Terry,Mikey,Davey,Timmy,Tommmy,Joey,Robby,Johnny,Brian";
}
}
public class ChildResourceImpl implements ChildResource {
@Override
public GrandchildResource getGrandchildResource() {
return new GrandchildResourceImpl();
}
}
public class RootResourceImpl implements RootResource {
@Override
public ChildResource getChildResource() {
return new ChildResourceImpl();
}
}
// Always fails with java.lang.IllegalArgumentException: Unresolved variables; only 0 value(s) given for 1 unique variable(s)
@Test
public void testCase() {
final String url = "http://localhost:12345/";
JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
sf.setResourceClasses(RootResource.class);
sf.setResourceProvider(RootResource.class,
new SingletonResourceProvider(
new RootResourceImpl()));
sf.setAddress(url);
sf.setStaticSubresourceResolution(false);
sf.setFeatures(Arrays.<AbstractFeature>asList(new LoggingFeature()));
sf.create();
RootResource resource = JAXRSClientFactory.create(
url, RootResource.class);
WebClient.client(resource).accept(MediaType.APPLICATION_JSON_TYPE)
.type(MediaType.APPLICATION_JSON_TYPE);
ChildResource childResource = resource.getChildResource();
GrandchildResource grandchildResource =
childResource.getGrandchildResource();
System.out.println(grandchildResource.readGrandchildren());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment