Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

throwing out HibernateProxyInitializer #10

Merged
merged 7 commits into from
Jul 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@
import br.com.caelum.vraptor.restfulie.headers.RestDefaults;
import br.com.caelum.vraptor.serialization.DefaultRepresentationResult;
import br.com.caelum.vraptor.serialization.HTMLSerialization;
import br.com.caelum.vraptor.serialization.HibernateProxyInitializer;
import br.com.caelum.vraptor.serialization.I18nMessageSerialization;
import br.com.caelum.vraptor.serialization.JSONPSerialization;
import br.com.caelum.vraptor.serialization.JSONSerialization;
Expand Down Expand Up @@ -227,7 +226,7 @@ InstanceCreator.class, getInstanceCreator(),
Evaluator.class, JavaEvaluator.class,
StaticContentHandler.class, DefaultStaticContentHandler.class,
SingleValueConverter.class, NullConverter.class,
ProxyInitializer.class, getProxyInitializerImpl()
ProxyInitializer.class, NullProxyInitializer.class
);

private final static Map<Class<?>, Class<?>> CACHED_COMPONENTS = classMap(
Expand Down Expand Up @@ -305,33 +304,24 @@ ProxyInitializer.class, getProxyInitializerImpl()
PrimitiveShortConverter.class,
ShortConverter.class,
StringConverter.class));


private static final HashMap<Class<? extends Annotation>, StereotypeInfo> STEREOTYPES_INFO = new HashMap<Class<? extends Annotation>,StereotypeInfo>();
static {
STEREOTYPES_INFO.put(Controller.class,new StereotypeInfo(Controller.class,ControllerHandler.class,new AnnotationLiteral<ControllerQualifier>() {}));
STEREOTYPES_INFO.put(Convert.class,new StereotypeInfo(Convert.class,ConverterHandler.class,new AnnotationLiteral<ConvertQualifier>() {}));
STEREOTYPES_INFO.put(Deserializes.class,new StereotypeInfo(Deserializes.class,DeserializesHandler.class,new AnnotationLiteral<DeserializesQualifier>() {}));
STEREOTYPES_INFO.put(Intercepts.class,new StereotypeInfo(Intercepts.class,InterceptorStereotypeHandler.class,new AnnotationLiteral<InterceptsQualifier>() {}));
STEREOTYPES_INFO.put(Intercepts.class,new StereotypeInfo(Intercepts.class,InterceptorStereotypeHandler.class,new AnnotationLiteral<InterceptsQualifier>() {}));

}

private static final Set<Class<? extends Deserializer>> DESERIALIZERS = Collections.<Class<? extends Deserializer>>singleton(XMLDeserializer.class);


public static Set<Class<? extends Deserializer>> getDeserializers() {
return DESERIALIZERS;
}

private static Class<? extends ProxyInitializer> getProxyInitializerImpl() {
try {
Class.forName("org.hibernate.proxy.HibernateProxy");
return HibernateProxyInitializer.class;
} catch (ClassNotFoundException e) {
return NullProxyInitializer.class;
}
}

private static Class<? extends InstanceCreator> getInstanceCreator() {
if (isClassPresent("org.objenesis.ObjenesisStd")) {
return ObjenesisInstanceCreator.class;
Expand All @@ -342,13 +332,13 @@ private static Class<? extends InstanceCreator> getInstanceCreator() {

public static Map<Class<?>, Class<?>> getCachedComponents() {
return Collections.unmodifiableMap(CACHED_COMPONENTS);
}
}

public static Map<Class<?>, Class<?>> getApplicationScoped() {
if (!isClassPresent("ognl.OgnlRuntime")) {
APPLICATION_COMPONENTS.put(DependencyProvider.class, VRaptorDependencyProvider.class);
}

// try put beanval 1.1 or beanval 1.0 if available
if (isClassPresent("javax.validation.executable.ExecutableValidator")) {
APPLICATION_COMPONENTS.put(ValidatorCreator.class, ValidatorCreator.class);
Expand All @@ -360,7 +350,7 @@ public static Map<Class<?>, Class<?>> getApplicationScoped() {
APPLICATION_COMPONENTS.put(ValidatorFactoryCreator.class, ValidatorFactoryCreator.class);
APPLICATION_COMPONENTS.put(MessageInterpolatorFactory.class, MessageInterpolatorFactory.class);
}

return Collections.unmodifiableMap(APPLICATION_COMPONENTS);
}

Expand All @@ -374,7 +364,7 @@ public static Map<Class<?>, Class<?>> getRequestScoped() {
} else {
REQUEST_COMPONENTS.put(BeanValidator.class, NullBeanValidator.class);
}

if (isClassPresent("org.apache.commons.fileupload.FileItem")) {
REQUEST_COMPONENTS.put(MultipartInterceptor.class, CommonsUploadMultipartInterceptor.class);
REQUEST_COMPONENTS.put(ServletFileUploadCreator.class, DefaultServletFileUploadCreator.class);
Expand All @@ -386,7 +376,7 @@ public static Map<Class<?>, Class<?>> getRequestScoped() {
"your classpath or use a Servlet 3 Container");
REQUEST_COMPONENTS.put(MultipartInterceptor.class, NullMultipartInterceptor.class);
}

if (isClassPresent("ognl.OgnlRuntime")) {
REQUEST_COMPONENTS.put(ParametersProvider.class, OgnlParametersProvider.class);
REQUEST_COMPONENTS.put(EmptyElementsRemoval.class, EmptyElementsRemoval.class);
Expand Down Expand Up @@ -450,18 +440,18 @@ public static Set<Class<? extends Converter<?>>> getBundledConverters() {
public static Set<StereotypeInfo> getStereotypesInfo() {
return new HashSet<StereotypeInfo>(STEREOTYPES_INFO.values());
}

public static Set<Class<? extends Annotation>> getStereotypes() {
Set<StereotypeInfo> stereotypesInfo = getStereotypesInfo();
HashSet<Class<? extends Annotation>> stereotypes = new HashSet<Class<? extends Annotation>>();
for (StereotypeInfo stereotypeInfo : stereotypesInfo) {
stereotypes.add(stereotypeInfo.getStereotype());
}
return stereotypes;
}
}
public static Map<Class<? extends Annotation>,StereotypeInfo> getStereotypesInfoMap() {
return STEREOTYPES_INFO;
}
}

private static Map<Class<?>, Class<?>> classMap(Class<?>... items) {
HashMap<Class<?>, Class<?>> map = new HashMap<Class<?>, Class<?>>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,26 @@ public class I18nMessageSerializationTest {
@Before
public void setup() throws Exception {
stream = new ByteArrayOutputStream();

HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(new PrintWriter(stream));
DefaultTypeNameExtractor extractor = new DefaultTypeNameExtractor();
HibernateProxyInitializer initializer = new HibernateProxyInitializer();
NullProxyInitializer initializer = new NullProxyInitializer();
XStreamBuilder builder = XStreamBuilderImpl.cleanInstance(new MessageConverter());
XStreamJSONSerialization jsonSerialization = new XStreamJSONSerialization(response, extractor, initializer, builder);
XStreamXMLSerialization xmlSerialization = new XStreamXMLSerialization(response, extractor, initializer, builder);

Container container = mock(Container.class);
when(container.instanceFor(JSONSerialization.class)).thenReturn(jsonSerialization);
when(container.instanceFor(XMLSerialization.class)).thenReturn(xmlSerialization);

MockLocalization mockLocalization = mock(MockLocalization.class);
when(mockLocalization.getBundle()).thenReturn(new SingletonResourceBundle("message.cat", "Just another {0} in {1}"));

serialization = new I18nMessageSerialization(container , mockLocalization);

}

@Test
public void shouldCallXStreamJsonSerialization() {
String expectedResult = "{\"message\": {\"message\": \"Just another {0} in {1}\",\"category\": \"success\"}}";
Expand All @@ -62,28 +62,28 @@ public void shouldCallXStreamJsonSerialization() {

@Test
public void shouldCallXStreamXmlSerialization() {
String expectedResult = "<message>\n" +
" <message>Just another {0} in {1}</message>\n" +
" <category>success</category>\n" +
String expectedResult = "<message>\n" +
" <message>Just another {0} in {1}</message>\n" +
" <category>success</category>\n" +
"</message>";
serialization.from("success", "message.cat").as(xml());
assertThat(result(), is(equalTo(expectedResult)));
}

@Test
public void shouldCallXStreamSerializationWithParams() {
String expectedResult = "<message>\n" +
" <message>Just another object in memory</message>\n" +
" <category>success</category>\n" +
String expectedResult = "<message>\n" +
" <message>Just another object in memory</message>\n" +
" <category>success</category>\n" +
"</message>";
Object[] params = {"object", "memory"};
serialization.from("success", "message.cat", params).as(xml());
assertThat(result(), is(equalTo(expectedResult)));
}


private String result() {
return new String(stream.toByteArray());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.junit.Test;

import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.serialization.HibernateProxyInitializer;
import br.com.caelum.vraptor.serialization.NullProxyInitializer;
import br.com.caelum.vraptor.serialization.gson.adapters.CalendarSerializer;

import com.google.common.collect.ForwardingCollection;
Expand All @@ -48,7 +48,7 @@ public class GsonJSONSerializationTest {

private DefaultTypeNameExtractor extractor;

private HibernateProxyInitializer initializer;
private NullProxyInitializer initializer;

@Before
public void setup() throws Exception {
Expand All @@ -57,7 +57,7 @@ public void setup() throws Exception {
response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(new PrintWriter(stream));
extractor = new DefaultTypeNameExtractor();
initializer = new HibernateProxyInitializer();
initializer = new NullProxyInitializer();

this.serialization = new GsonJSONSerialization(response, extractor, initializer,
Collections.<JsonSerializer<?>> emptyList(), Collections.<ExclusionStrategy> emptyList());
Expand Down Expand Up @@ -429,7 +429,7 @@ public void shouldRunHibernateLazyInitialization() throws Exception {

serialization.from(proxy).serialize();

assertThat(result(), is("{\"client\":{\"aField\":\"abc\",\"name\":\"my name\"}}"));
assertThat(result(), is("{\"someProxy\":{\"aField\":\"abc\",\"name\":\"my name\"}}"));
}

static class MyCollection extends ForwardingCollection<Order> {
Expand Down Expand Up @@ -495,23 +495,23 @@ public void shouldExcludeAttributeUsingExclusionStrategy() {

assertThat(result(), not(containsString("address")));
}

@Test
public void shouldExcludeAllPrimitiveFields() {
String expectedResult = "{\"order\":{}}";
Order order = new Order(new Client("nykolas lima"), 15.0, "gift bags, please");
serialization.from(order).excludeAll().serialize();
assertThat(result(), is(equalTo(expectedResult)));
}

@Test
public void shouldExcludeAllPrimitiveParentFields() {
String expectedResult = "{\"advancedOrder\":{}}";
Order order = new AdvancedOrder(null, 15.0, "pack it nicely, please", "complex package");
serialization.from(order).excludeAll().serialize();
assertThat(result(), is(equalTo(expectedResult)));
}

@Test
public void shouldExcludeAllThanIncludeAndSerialize() {
String expectedResult = "{\"order\":{\"price\":15.0}}";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
import org.junit.Test;

import br.com.caelum.vraptor.interceptor.DefaultTypeNameExtractor;
import br.com.caelum.vraptor.serialization.HibernateProxyInitializer;
import br.com.caelum.vraptor.serialization.NullProxyInitializer;

public class XStreamJSONPSerializationTest {


private XStreamJSONPSerialization serialization;
private ByteArrayOutputStream stream;

@Before
public void setup() throws Exception {
this.stream = new ByteArrayOutputStream();

HttpServletResponse response = mock(HttpServletResponse.class);
when(response.getWriter()).thenReturn(new PrintWriter(stream));
this.serialization = new XStreamJSONPSerialization(response, new DefaultTypeNameExtractor(), new HibernateProxyInitializer(), XStreamBuilderImpl.cleanInstance());

this.serialization = new XStreamJSONPSerialization(response, new DefaultTypeNameExtractor(), new NullProxyInitializer(), XStreamBuilderImpl.cleanInstance());
}

public static class Address {
Expand Down Expand Up @@ -67,7 +67,7 @@ public void shouldIncludeCallbackPadding() {
private String result() {
return new String(stream.toByteArray());
}

public static class GenericWrapper<T> {

Collection<T> entityList;
Expand Down
Loading