You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BasicEventTestCase.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.events;
  19. import org.junit.Test;
  20. import static org.junit.Assert.assertEquals;
  21. import static org.junit.Assert.assertFalse;
  22. import static org.junit.Assert.assertNotNull;
  23. import static org.junit.Assert.assertTrue;
  24. import static org.junit.Assert.fail;
  25. import org.apache.fop.events.model.EventSeverity;
  26. public class BasicEventTestCase {
  27. @Test
  28. public void testBasics() throws Exception {
  29. MyEventListener listener = new MyEventListener();
  30. EventBroadcaster broadcaster = new DefaultEventBroadcaster();
  31. broadcaster.addEventListener(listener);
  32. assertTrue(broadcaster.hasEventListeners());
  33. Event ev = new Event(this, "123", EventSeverity.INFO,
  34. Event.paramsBuilder()
  35. .param("reason", "I'm tired")
  36. .param("blah", new Integer(23))
  37. .build());
  38. broadcaster.broadcastEvent(ev);
  39. ev = listener.event;
  40. assertNotNull(ev);
  41. assertEquals("123", listener.event.getEventID());
  42. assertEquals(EventSeverity.INFO, listener.event.getSeverity());
  43. assertEquals("I'm tired", ev.getParam("reason"));
  44. assertEquals(new Integer(23), ev.getParam("blah"));
  45. broadcaster.removeEventListener(listener);
  46. assertFalse(broadcaster.hasEventListeners());
  47. //Just check that there are no NPEs
  48. broadcaster.broadcastEvent(ev);
  49. }
  50. @Test
  51. public void testEventProducer() throws Exception {
  52. MyEventListener listener = new MyEventListener();
  53. EventBroadcaster broadcaster = new DefaultEventBroadcaster();
  54. broadcaster.addEventListener(listener);
  55. assertTrue(broadcaster.hasEventListeners());
  56. TestEventProducer producer = TestEventProducer.Provider.get(broadcaster);
  57. producer.complain(this, "I'm tired", 23);
  58. Event ev = listener.event;
  59. assertNotNull(ev);
  60. assertEquals("org.apache.fop.events.TestEventProducer.complain",
  61. listener.event.getEventID());
  62. assertEquals(EventSeverity.WARN, listener.event.getSeverity());
  63. assertEquals("I'm tired", ev.getParam("reason"));
  64. assertEquals(new Integer(23), ev.getParam("blah"));
  65. broadcaster.removeEventListener(listener);
  66. assertFalse(broadcaster.hasEventListeners());
  67. //Just check that there are no NPEs
  68. broadcaster.broadcastEvent(ev);
  69. }
  70. private class MyEventListener implements EventListener {
  71. private Event event;
  72. public void processEvent(Event event) {
  73. if (this.event != null) {
  74. fail("Multiple events received");
  75. }
  76. this.event = event;
  77. }
  78. }
  79. }