/src/test/java/org/exoplatform/social/client/core/service/PropertyChangeListenerTest.java

http://github.com/exosocial/exo.social.client · Java · 94 lines · 58 code · 17 blank · 19 comment · 0 complexity · 26675c79dc6f503d392f8c61bdc8ae69 MD5 · raw file

  1. /*
  2. * Copyright (C) 2003-2011 eXo Platform SAS.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Affero General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Affero General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. package org.exoplatform.social.client.core.service;
  18. import org.testng.annotations.AfterMethod;
  19. import org.testng.annotations.BeforeMethod;
  20. import org.testng.annotations.Test;
  21. import static org.testng.Assert.assertEquals;
  22. import static org.testng.Assert.assertNull;
  23. import static org.testng.Assert.assertTrue;
  24. /**
  25. * Unit Test for {@link org.exoplatform.social.client.api.event.PropertyChangeListener}.
  26. */
  27. public class PropertyChangeListenerTest extends AbstractLifecycleTest {
  28. @BeforeMethod
  29. public void setUp() throws Exception {
  30. super.setUp();
  31. capturePropertyChange = new MockPropertyChangeListener();
  32. mockModel = new MockModel();
  33. mockModel.addPropertyChangeListener(capturePropertyChange);
  34. }
  35. @AfterMethod
  36. public void tearDown() throws Exception {
  37. super.tearDown();
  38. capturePropertyChange = null;
  39. mockModel = null;
  40. }
  41. @Test
  42. public void checkPropertyChangeListener() throws Exception {
  43. assertEquals(1, mockModel.findPropertyChangeListeners().length);
  44. }
  45. @Test
  46. public void handlePropertyChangeListener() throws Exception {
  47. mockModel.setField(MockModel.Field.ID.toString(), "newValue");
  48. assertTrue(capturePropertyChange.eventHolder.containsKey(MockModel.Field.ID.toString()));
  49. assertEquals(MockModel.Field.ID.toString(),
  50. capturePropertyChange.eventHolder.get(MockModel.Field.ID.toString()).getPropertyName());
  51. assertEquals("newValue", capturePropertyChange.eventHolder.get(MockModel.Field.ID.toString()).getNewValue());
  52. assertNull(capturePropertyChange.eventHolder.get(MockModel.Field.ID.toString()).getOldValue());
  53. }
  54. @Test
  55. public void checkOldValue() throws Exception {
  56. mockModel.setField(MockModel.Field.ID.toString(), "oldValue");
  57. mockModel.setField(MockModel.Field.ID.toString(), "newValue");
  58. assertEquals(1, capturePropertyChange.eventHolder.size());
  59. assertTrue(capturePropertyChange.eventHolder.containsKey(MockModel.Field.ID.toString()));
  60. assertEquals(MockModel.Field.ID.toString(),
  61. capturePropertyChange.eventHolder.get(MockModel.Field.ID.toString()).getPropertyName());
  62. assertEquals("newValue", capturePropertyChange.eventHolder.get(MockModel.Field.ID.toString()).getNewValue());
  63. assertEquals("oldValue", capturePropertyChange.eventHolder.get(MockModel.Field.ID.toString()).getOldValue());
  64. }
  65. @Test
  66. public void checkRaiseEventCountSameField() throws Exception {
  67. mockModel.setField(MockModel.Field.ID.toString(), "oldValue");
  68. mockModel.setField(MockModel.Field.ID.toString(), "newValue");
  69. assertEquals(capturePropertyChange.eventHolder.size(), 1);
  70. }
  71. @Test
  72. public void checkRaiseEventCountNotSameField() throws Exception {
  73. mockModel.setField(MockModel.Field.ID.toString(), "oldValue");
  74. mockModel.setField(MockModel.Field.CONTENT.toString(), "oldContentValue");
  75. assertEquals(capturePropertyChange.eventHolder.size(), 2);
  76. }
  77. }