Skip to content

Commit 63e2749

Browse files
committed
Add a simple round-trip test
1 parent 1246478 commit 63e2749

File tree

2 files changed

+169
-0
lines changed

2 files changed

+169
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package com.fasterxml.jackson.databind;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.annotation.JsonFormat;
6+
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
7+
8+
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
9+
@JsonPropertyOrder({"content", "images"})
10+
public class MediaItem
11+
{
12+
public enum Player { JAVA, FLASH; }
13+
public enum Size { SMALL, LARGE; }
14+
15+
private List<Photo> _photos;
16+
private Content _content;
17+
18+
public MediaItem() { }
19+
20+
public MediaItem(Content c)
21+
{
22+
_content = c;
23+
}
24+
25+
public void addPhoto(Photo p) {
26+
if (_photos == null) {
27+
_photos = new ArrayList<Photo>();
28+
}
29+
_photos.add(p);
30+
}
31+
32+
public List<Photo> getImages() { return _photos; }
33+
public void setImages(List<Photo> p) { _photos = p; }
34+
35+
public Content getContent() { return _content; }
36+
public void setContent(Content c) { _content = c; }
37+
38+
/*
39+
/**********************************************************
40+
/* Helper types
41+
/**********************************************************
42+
*/
43+
44+
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
45+
@JsonPropertyOrder({"uri","title","width","height","size"})
46+
public static class Photo
47+
{
48+
private String _uri;
49+
private String _title;
50+
private int _width;
51+
private int _height;
52+
private Size _size;
53+
54+
public Photo() {}
55+
public Photo(String uri, String title, int w, int h, Size s)
56+
{
57+
_uri = uri;
58+
_title = title;
59+
_width = w;
60+
_height = h;
61+
_size = s;
62+
}
63+
64+
public String getUri() { return _uri; }
65+
public String getTitle() { return _title; }
66+
public int getWidth() { return _width; }
67+
public int getHeight() { return _height; }
68+
public Size getSize() { return _size; }
69+
70+
public void setUri(String u) { _uri = u; }
71+
public void setTitle(String t) { _title = t; }
72+
public void setWidth(int w) { _width = w; }
73+
public void setHeight(int h) { _height = h; }
74+
public void setSize(Size s) { _size = s; }
75+
}
76+
77+
@JsonFormat(shape=JsonFormat.Shape.ARRAY)
78+
@JsonPropertyOrder({"uri","title","width","height","format","duration","size","bitrate","persons","player","copyright"})
79+
public static class Content
80+
{
81+
private Player _player;
82+
private String _uri;
83+
private String _title;
84+
private int _width;
85+
private int _height;
86+
private String _format;
87+
private long _duration;
88+
private long _size;
89+
private int _bitrate;
90+
private List<String> _persons;
91+
private String _copyright;
92+
93+
public Content() { }
94+
95+
public void addPerson(String p) {
96+
if (_persons == null) {
97+
_persons = new ArrayList<String>();
98+
}
99+
_persons.add(p);
100+
}
101+
102+
public Player getPlayer() { return _player; }
103+
public String getUri() { return _uri; }
104+
public String getTitle() { return _title; }
105+
public int getWidth() { return _width; }
106+
public int getHeight() { return _height; }
107+
public String getFormat() { return _format; }
108+
public long getDuration() { return _duration; }
109+
public long getSize() { return _size; }
110+
public int getBitrate() { return _bitrate; }
111+
public List<String> getPersons() { return _persons; }
112+
public String getCopyright() { return _copyright; }
113+
114+
public void setPlayer(Player p) { _player = p; }
115+
public void setUri(String u) { _uri = u; }
116+
public void setTitle(String t) { _title = t; }
117+
public void setWidth(int w) { _width = w; }
118+
public void setHeight(int h) { _height = h; }
119+
public void setFormat(String f) { _format = f; }
120+
public void setDuration(long d) { _duration = d; }
121+
public void setSize(long s) { _size = s; }
122+
public void setBitrate(int b) { _bitrate = b; }
123+
public void setPersons(List<String> p) { _persons = p; }
124+
public void setCopyright(String c) { _copyright = c; }
125+
}
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.fasterxml.jackson.databind;
2+
3+
public class RoundtripTest extends BaseMapTest
4+
{
5+
private final ObjectMapper MAPPER = new ObjectMapper();
6+
7+
public void testMedaItemRoundtrip() throws Exception
8+
{
9+
MediaItem.Content c = new MediaItem.Content();
10+
c.setBitrate(9600);
11+
c.setCopyright("none");
12+
c.setDuration(360000L);
13+
c.setFormat("lzf");
14+
c.setHeight(640);
15+
c.setSize(128000L);
16+
c.setTitle("Amazing Stuff For Something Or Oth\u00CBr!");
17+
c.setUri("http://multi.fario.us/index.html");
18+
c.setWidth(1400);
19+
20+
c.addPerson("Joe Sixp\u00e2ck");
21+
c.addPerson("Ezekiel");
22+
c.addPerson("Sponge-Bob Squarepant\u00DF");
23+
24+
MediaItem input = new MediaItem(c);
25+
input.addPhoto(new MediaItem.Photo());
26+
input.addPhoto(new MediaItem.Photo());
27+
input.addPhoto(new MediaItem.Photo());
28+
29+
String json = MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(input);
30+
31+
MediaItem output = MAPPER.readValue(new java.io.StringReader(json), MediaItem.class);
32+
assertNotNull(output);
33+
34+
assertNotNull(output.getImages());
35+
assertEquals(input.getImages().size(), output.getImages().size());
36+
assertNotNull(output.getContent());
37+
assertEquals(input.getContent().getTitle(), output.getContent().getTitle());
38+
assertEquals(input.getContent().getUri(), output.getContent().getUri());
39+
40+
// compare re-serialization as a simple check as well
41+
assertEquals(json, MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(output));
42+
}
43+
}

0 commit comments

Comments
 (0)