Skip to content

Commit

Permalink
Added simple unit test to verify (parts of) #33
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 12, 2013
1 parent 85cc17a commit ddf256f
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotat
*/
@Override
public void writeTo(Object value, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType,
MediaType mediaType,
MultivaluedMap<String,Object> httpHeaders, OutputStream entityStream)
throws IOException
{
Expand Down Expand Up @@ -616,7 +616,7 @@ public void writeTo(Object value, Class<?> type, Type genericType, Annotation[]
// [Issue#32]: allow modification by filter-injectible thing
ObjectWriterModifier mod = ObjectWriterInjector.getAndClear();
if (mod != null) {
writer = mod.modify(endpoint, value, writer, g);
writer = mod.modify(endpoint, httpHeaders, value, writer, g);
}

writer.writeValue(g, value);
Expand Down Expand Up @@ -724,7 +724,9 @@ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotati
* Method that JAX-RS container calls to deserialize given value.
*/
@Override
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String,String> httpHeaders, InputStream entityStream)
public Object readFrom(Class<Object> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String,String> httpHeaders,
InputStream entityStream)
throws IOException
{
AnnotationBundleKey key = new AnnotationBundleKey(annotations, type);
Expand Down Expand Up @@ -757,7 +759,7 @@ public Object readFrom(Class<Object> type, Type genericType, Annotation[] annota
// [Issue#32]: allow modification by filter-injectible thing
ObjectReaderModifier mod = ObjectReaderInjector.getAndClear();
if (mod != null) {
reader = mod.modify(endpoint, resolvedType, reader, jp);
reader = mod.modify(endpoint, httpHeaders, resolvedType, reader, jp);
}
return reader.readValue(jp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ public abstract class EndpointConfigBase<THIS extends EndpointConfigBase<THIS>>
protected Class<?> _activeView;

protected String _rootName;

// // Deserialization-only config

protected DeserializationFeature[] _deserEnable;
protected DeserializationFeature[] _deserDisable;

protected ObjectReader _reader;

// // Serialization-only config

protected SerializationFeature[] _serEnable;
protected SerializationFeature[] _serDisable;

protected ObjectWriter _writer;

/*
/**********************************************************
/* Construction
Expand Down Expand Up @@ -129,6 +129,20 @@ protected THIS initWriter(ObjectWriter writer)
/* Accessors
/**********************************************************
*/

/**
* @since 2.3
*/
public String getRootName() {
return _rootName;
}

/**
* @since 2.3
*/
public Class<?> getActiveView() {
return _activeView;
}

public final ObjectReader getReader() {
if (_reader == null) { // sanity check, should never happen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import java.io.IOException;

import com.fasterxml.jackson.core.*;
import javax.ws.rs.core.MultivaluedMap;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;

/**
Expand All @@ -16,11 +17,13 @@ public abstract class ObjectReaderModifier
* used for reading request objects for specified endpoint.
*
* @param endpoint End point for which reader is used
* @param httpHeaders HTTP headers sent with request (read-only)
* @param resultType Type that input is to be bound to
* @param r ObjectReader as constructed for endpoint, type to handle
* @param p Parser to use for reading content
*/
public abstract ObjectReader modify(EndpointConfigBase<?> endpoint,
MultivaluedMap<String,String> httpHeaders,
JavaType resultType, ObjectReader r, JsonParser p)
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import java.io.IOException;

import com.fasterxml.jackson.core.*;
import javax.ws.rs.core.MultivaluedMap;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;

/**
Expand All @@ -14,8 +15,11 @@ public abstract class ObjectWriterModifier
/**
* Method called to let modifier make any changes it wants to to objects
* used for writing response for specified endpoint.
*
* @param responseHeaders HTTP headers being returned with response (mutable)
*/
public abstract ObjectWriter modify(EndpointConfigBase<?> endpoint,
Object valueToWrite, ObjectWriter r, JsonGenerator g)
MultivaluedMap<String,Object> responseHeaders,
Object valueToWrite, ObjectWriter w, JsonGenerator g)
throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package com.fasterxml.jackson.jaxrs.json;

import java.io.IOException;
import java.util.Arrays;
import java.util.*;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;

import org.junit.Assert;



// JAX-RS (jersey), Jetty stuff:
import javax.ws.rs.core.Application;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import com.sun.jersey.spi.container.servlet.ServletContainer;

import com.sun.jersey.spi.container.servlet.ServletContainer;
import com.fasterxml.jackson.core.*;

public abstract class JaxrsTestBase
Expand All @@ -25,6 +31,12 @@ public abstract class JaxrsTestBase
*/

protected Server startServer(int port, Class<? extends Application> appClass)
{
return startServer(port, appClass, null);
}

protected Server startServer(int port, Class<? extends Application> appClass,
Class<? extends Filter> filterClass)
{
Server server = new Server(port);
final ContextHandlerCollection contexts = new ContextHandlerCollection();
Expand All @@ -33,6 +45,11 @@ protected Server startServer(int port, Class<? extends Application> appClass)
jaxrs.setInitParameter("javax.ws.rs.Application", appClass.getName());
final ServletContextHandler mainHandler = new ServletContextHandler(contexts, "/", true, false);
mainHandler.addServlet(jaxrs, "/*");

if (filterClass != null) {
mainHandler.addFilter(filterClass, "/*", java.util.EnumSet.allOf(DispatcherType.class));
}

server.setHandler(mainHandler);
try {
server.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.fasterxml.jackson.jaxrs.json;

import java.io.*;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.core.Application;

public class ResourceTestBase extends JaxrsTestBase
{
protected static abstract class JsonApplication extends Application
{
protected final Object _resource;

protected JsonApplication(Object r) { _resource = r; }

@Override
public Set<Object> getSingletons() {
HashSet<Object> singletons = new HashSet<Object>();
singletons.add(new JacksonJsonProvider());
singletons.add(_resource);
return singletons;
}
}

protected String aposToQuotes(String json) {
return json.replace("'", "\"");
}

protected String readUTF8(InputStream in) throws IOException
{
return new String(readAll(in), "UTF-8");
}

protected byte[] readAll(InputStream in) throws IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream(100);
byte[] buffer = new byte[500];
int count;

while ((count = in.read(buffer)) > 0) {
bytes.write(buffer, 0, count);
}
in.close();
return bytes.toByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@

import java.io.*;
import java.net.*;
import java.util.*;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;

import org.eclipse.jetty.server.Server;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.fasterxml.jackson.jaxrs.json.JaxrsTestBase;
import com.fasterxml.jackson.jaxrs.json.ResourceTestBase;

public class TestSimpleEndpoint extends JaxrsTestBase
public class TestSimpleEndpoint extends ResourceTestBase
{
final static int TEST_PORT = 6011;

static class Point {
public int x, y;

Expand All @@ -41,21 +40,6 @@ public Point getPoint() {
public static class SimpleResourceApp extends JsonApplication {
public SimpleResourceApp() { super(new SimpleResource()); }
}

static abstract class JsonApplication extends Application
{
protected final Object _resource;

protected JsonApplication(Object r) { _resource = r; }

@Override
public Set<Object> getSingletons() {
HashSet<Object> singletons = new HashSet<Object>();
singletons.add(new JacksonJsonProvider());
singletons.add(_resource);
return singletons;
}
}

/*
/**********************************************************
Expand All @@ -66,8 +50,8 @@ public Set<Object> getSingletons() {
public void testStandardJson() throws Exception
{
final ObjectMapper mapper = new ObjectMapper();
Server server = startServer(6061, SimpleResourceApp.class);
InputStream in = new URL("http://localhost:6061/point").openStream();
Server server = startServer(TEST_PORT, SimpleResourceApp.class);
InputStream in = new URL("http://localhost:"+TEST_PORT+"/point").openStream();
Point p;

try {
Expand Down
Loading

0 comments on commit ddf256f

Please sign in to comment.