-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
linong.cao
committed
Mar 1, 2012
1 parent
122c038
commit 60cc58b
Showing
3 changed files
with
66 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.db.letdb; | ||
|
||
import com.alibaba.fastjson.JSON; | ||
|
||
/** | ||
* @created: Mar 1, 2012 | ||
* @author : jias chao<[email protected]> | ||
*/ | ||
public class Document { | ||
|
||
private String id; | ||
|
||
protected String getJsonStr(){ | ||
String jstr = JSON.toJSONString(this); | ||
return jstr; | ||
} | ||
|
||
protected Document getInstance(String jstr){ | ||
Document doc = JSON.parseObject(jstr, Document.class); | ||
return doc; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -103,4 +103,6 @@ public int sync() throws IOException{ | |
return out.toByteArray().length; | ||
} | ||
|
||
|
||
|
||
} |
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,34 @@ | ||
package com.db.letdb; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.fail; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* @created: Mar 1, 2012 | ||
* @author : jias chao<[email protected]> | ||
*/ | ||
public class DocumentTest { | ||
|
||
|
||
@Test | ||
public void testDoc2json(){ | ||
Document doc = new Document(); | ||
assertEquals("{}",doc.getJsonStr()); | ||
|
||
|
||
doc.setId("abc-12"); | ||
assertEquals("{\"id\":\"abc-12\"}",doc.getJsonStr()); | ||
} | ||
|
||
|
||
@Test | ||
public void testJson2Doc(){ | ||
|
||
String str = "{\"id\":\"abc-12\"}"; | ||
Document doc = new Document().getInstance(str); | ||
assertEquals("abc-12",doc.getId()); | ||
} | ||
|
||
} |