- Notifications
You must be signed in to change notification settings - Fork 1.5k
Added unit tests#1
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
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| target | ||
| *.iml | ||
| .idea |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| target | ||
| *.iml | ||
| .idea |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| http://hmkcode.com/java-servlet-send-receive-json-using-jquery-ajax/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [{"title":"bbc","url":"news.bbc.co.uk","categories":["news"],"tags":["uk news"]},{"title":null,"url":"www.google.co.uk","categories":["youtube","gmail","gdrive"],"tags":["search"]}] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package com.hmkcode;/** | ||
| * Author: wge | ||
| * Date: 22/11/2013 | ||
| * Time: 18:09 | ||
| */ | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.hmkcode.vo.Article; | ||
| import org.junit.Test; | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| public class ArticleMapperTest | ||
| { | ||
| private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(ArticleMapperTest.class.getName()); | ||
| @Test | ||
| public void testValidJson() throws Exception | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| { | ||
| String json = readlineFromResourceFile("/valid.json"); | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| Article article = mapper.readValue(json, Article.class); | ||
| assert(article.url.equals("news.bbc.co.uk")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| assert(article.categories.contains("news")); | ||
| assert(article.categories.size() == 1); | ||
| assert(article.tags.size() == 1); | ||
| assert(article.tags.contains("uk news")); | ||
| List<Article> articles = new ArrayList<Article>(); | ||
| articles.add(article); | ||
| Article extra = new Article(); | ||
| extra.url = "www.google.co.uk" | ||
| List categoryList = new ArrayList<String>(); | ||
| categoryList.add("youtube"); | ||
| categoryList.add("gmail"); | ||
| categoryList.add("gdrive"); | ||
| extra.categories = categoryList; | ||
| extra.addTag("search"); | ||
| articles.add(extra); | ||
| //this will appear in the root of your project. | ||
| mapper.writeValue(new File("modified.json"), articles); | ||
| } | ||
| @Test (expected=com.fasterxml.jackson.core.JsonParseException.class) | ||
| public void testBadInput() throws Exception | ||
| { | ||
| String json = readlineFromResourceFile("/malformed.json"); | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| Article article = mapper.readValue(json, Article.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| } | ||
| @Test | ||
| public void missingUrl() throws Exception | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
| ||
| { | ||
| String json = readlineFromResourceFile("/missingUrl.json"); | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| Article article = mapper.readValue(json, Article.class); | ||
| assert(article.url == null); | ||
| assert(article.categories.contains("news")); | ||
| assert(article.categories.size() == 1); | ||
| assert(article.tags.size() == 1); | ||
| assert(article.tags.contains("uk news")); | ||
| } | ||
| String readlineFromResourceFile(String filename) throws IOException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. | ||
| { | ||
| InputStream is = getClass().getResourceAsStream(filename); | ||
| byte[] buff = new byte[1000]; | ||
| is.read(buff); | ||
| return new String(buff); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"title":"bbc","url":news.bbc.co.uk,"categories":["news"],"tags":["uk news"]}; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"title":"bbc","categories":["news"],"tags":["uk news"]}; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| {"title":"bbc","url":"news.bbc.co.uk","categories":["news"],"tags":["uk news"]}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.