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 package org.apache.commons.configuration.event; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import java.util.LinkedList; 24 import java.util.List; 25 26 /** 27 * A test event listener class that can be used for testing whether 28 * configurations generated correct events. 29 * 30 * @author <a 31 * href="http://commons.apache.org/configuration/team-list.html">Commons 32 * Configuration team</a> 33 * @version $Id: ConfigurationListenerTestImpl.java 1225648 2011-12-29 20:55:07Z oheger $ 34 */ 35 public class ConfigurationListenerTestImpl implements ConfigurationListener 36 { 37 /** The expected event source. */ 38 private final Object expectedSource; 39 40 /** Stores the received events. */ 41 private final List<ConfigurationEvent> events; 42 43 /** 44 * Creates a new instance of {@code ConfigurationListenerTestImpl} and sets 45 * the expected event source. 46 * 47 * @param source the event source (<b>null</b> if the source need not to be 48 * checked) 49 */ 50 public ConfigurationListenerTestImpl(Object source) 51 { 52 expectedSource = source; 53 events = new LinkedList<ConfigurationEvent>(); 54 } 55 56 public void configurationChanged(ConfigurationEvent event) 57 { 58 events.add(event); 59 } 60 61 /** 62 * Checks if at least {@code minEvents} events have been received. 63 * 64 * @param minEvents the minimum number of expected events 65 */ 66 public void checkEventCount(int minEvents) 67 { 68 assertTrue("Too view events received", events.size() >= minEvents); 69 } 70 71 /** 72 * Checks an expected event. 73 * 74 * @param type the event type 75 * @param propName the expected property name 76 * @param propValue the expected property value 77 * @param before the expected before flag 78 */ 79 public void checkEvent(int type, String propName, Object propValue, 80 boolean before) 81 { 82 ConfigurationEvent e = nextEvent(type); 83 assertEquals("Wrong property name", propName, e.getPropertyName()); 84 assertEquals("Wrong property value", propValue, e.getPropertyValue()); 85 assertEquals("Wrong before flag", before, e.isBeforeUpdate()); 86 } 87 88 /** 89 * Returns the next received event and checks for the expected type. This 90 * method can be used instead of {@code checkEvent()} for comparing 91 * complex event values. 92 * 93 * @param expectedType the expected type of the event 94 * @return the event object 95 */ 96 public ConfigurationEvent nextEvent(int expectedType) 97 { 98 assertFalse("Too few events received", events.isEmpty()); 99 ConfigurationEvent e = events.remove(0); 100 if (expectedSource != null) 101 { 102 assertEquals("Wrong event source", expectedSource, e.getSource()); 103 } 104 assertEquals("Wrong event type", expectedType, e.getType()); 105 return e; 106 } 107 108 /** 109 * Skips to the last received event and checks that no events of the given 110 * type have been received. This method is used by checks for detail events 111 * to ignore the detail events. 112 * 113 * @param type the event type 114 */ 115 public void skipToLast(int type) 116 { 117 while (events.size() > 1) 118 { 119 ConfigurationEvent e = events.remove(0); 120 assertTrue("Found end event in details", type != e.getType()); 121 } 122 } 123 124 /** 125 * Checks if all events has been processed. 126 */ 127 public void done() 128 { 129 assertTrue("Too many events received", events.isEmpty()); 130 } 131 }