-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
global: JSON utility moved to typetools-json module, tests
Signed-off-by: Ketoth Xupack <ketoth.xupack@gmail.com>
- Loading branch information
1 parent
6d58432
commit 0e06d4f
Showing
20 changed files
with
383 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>typetools-json</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<description>A set of tools aimed to make daily programming easier.</description> | ||
|
||
<prerequisites> | ||
<maven>3.0</maven> | ||
</prerequisites> | ||
|
||
<parent> | ||
<groupId>org.no-hope</groupId> | ||
<artifactId>java-toolkit</artifactId> | ||
<version>0.1.4-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<!-- enforce @Nonnull checks --> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>aspectj-maven-plugin</artifactId> | ||
<configuration> | ||
<aspectLibraries> | ||
<aspectLibrary> | ||
<groupId>org.no-hope</groupId> | ||
<artifactId>jsr305-aspectj-validation</artifactId> | ||
</aspectLibrary> | ||
</aspectLibraries> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.no-hope</groupId> | ||
<artifactId>jsr305-aspectj-validation</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.no-hope</groupId> | ||
<artifactId>slf4j-logging</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>${joda-time.version}</version> | ||
</dependency> | ||
|
||
<!-- Json parser --> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-annotations</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.fasterxml.jackson.datatype</groupId> | ||
<artifactId>jackson-datatype-joda</artifactId> | ||
<version>${jackson.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>ch.qos.logback</groupId> | ||
<artifactId>logback-classic</artifactId> | ||
<version>${logback.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
projects/typetools-json/src/test/java/org/nohope/typetools/JSONTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package org.nohope.typetools; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* Date: 11/8/12 | ||
* Time: 3:28 PM | ||
*/ | ||
public class JSONTest { | ||
|
||
public static class Bean { | ||
private final String a; | ||
private final Integer b; | ||
|
||
// used for de-serialization only | ||
private Bean() { | ||
a = null; | ||
b = null; | ||
} | ||
|
||
public Bean(final String a, final Integer b) { | ||
this.a = a; | ||
this.b = b; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
final Bean bean = (Bean) o; | ||
return a.equals(bean.a) && b.equals(bean.b); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = a.hashCode(); | ||
result = 31 * result + b.hashCode(); | ||
return result; | ||
} | ||
} | ||
|
||
public static class CycleBean { | ||
private final CycleBean a; | ||
|
||
public CycleBean() { | ||
this.a = this; | ||
} | ||
} | ||
|
||
|
||
@Test | ||
public void jsonSerialization() throws Exception { | ||
final Bean object = new Bean("x", 1); | ||
|
||
for (final JSON json : Arrays.asList(JSON.JSON, JSON.customize(new ObjectMapper()))) { | ||
assertEquals("{\"@class\":\"org.nohope.typetools.JSONTest$Bean\",\"a\":\"x\",\"b\":1}", | ||
json.jsonify(object).toString()); | ||
assertEquals("{\n" | ||
+ " \"@class\" : \"org.nohope.typetools.JSONTest$Bean\",\n" | ||
+ " \"a\" : \"x\",\n" | ||
+ " \"b\" : 1\n" | ||
+ "}", | ||
json.pretty(object).toString()); | ||
} | ||
|
||
final JSON custom = JSON.customize(new ObjectMapper(), false); | ||
assertEquals("<? org.nohope.typetools.JSONTest.Bean />", custom.jsonify(object).toString()); | ||
assertEquals("<? org.nohope.typetools.JSONTest.Bean />", custom.pretty(object).toString()); | ||
|
||
assertEquals(object, JSON.JSON.copyAs(object, Bean.class)); | ||
|
||
assertEquals("null", JSON.JSON.pretty(null).toString()); | ||
assertEquals("null", JSON.JSON.jsonify(null).toString()); | ||
} | ||
|
||
@Test | ||
public void errorneousJsonSerialization() throws Exception { | ||
final CycleBean object = new CycleBean(); | ||
assertEquals("<? org.nohope.typetools.JSONTest.CycleBean />", JSON.JSON.jsonify(object).toString()); | ||
assertEquals("<? org.nohope.typetools.JSONTest.CycleBean />", JSON.JSON.pretty(object).toString()); | ||
|
||
assertEquals(null, JSON.JSON.copyAs(null, CycleBean.class)); | ||
} | ||
} |
Oops, something went wrong.