/src/test/java/com/google/ie/common/util/GsonUtilityTest.java

http://thoughtsite.googlecode.com/ · Java · 66 lines · 31 code · 11 blank · 24 comment · 0 complexity · 78fe71a23576db71b67dd4a02d45b1ea MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. /**
  3. *
  4. */
  5. package com.google.ie.common.util;
  6. import static org.junit.Assert.assertEquals;
  7. import com.google.ie.business.domain.IdeaCategory;
  8. import com.google.ie.test.ServiceTest;
  9. import org.junit.Before;
  10. import org.junit.Test;
  11. import java.util.HashMap;
  12. /**
  13. * Test class for GsonUtility class
  14. *
  15. * @author ssbains
  16. *
  17. */
  18. public class GsonUtilityTest extends ServiceTest {
  19. /**
  20. */
  21. @Before
  22. public void setUp() {
  23. super.setUp();
  24. }
  25. /**
  26. * Test method for
  27. * {@link com.google.ie.common.util.GsonUtility#convertToJson(java.lang.Object)}
  28. * .
  29. */
  30. @Test
  31. public void convertToJson() {
  32. HashMap<String, String> hashMap = new HashMap<String, String>();
  33. hashMap.put("Education", "key1");
  34. hashMap.put("Sports", "key123");
  35. String expected = "{" + "\"Education\":\"key1\",\"Sports\":\"key123\"" + "}";
  36. String actual = GsonUtility.convertToJson(hashMap);
  37. assertEquals(expected, actual);
  38. }
  39. @Test
  40. /**
  41. * Test method for
  42. * {@link com.google.ie.common.util.GsonUtility#convertFromJson(java.lang.String,Class<T>)}
  43. * .
  44. */
  45. public void convertFromJson() {
  46. IdeaCategory expectedCategory = new IdeaCategory();
  47. expectedCategory.setKey("jsonTestCategory");
  48. expectedCategory.setName("testGsonUtility");
  49. /* Convert to json */
  50. String jsonString = GsonUtility.convertToJson(expectedCategory);
  51. /* Convert back from json */
  52. IdeaCategory actualCategory = GsonUtility.convertFromJson(jsonString, IdeaCategory.class);
  53. assertEquals(expectedCategory.getKey(), actualCategory.getKey());
  54. }
  55. }