001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.configuration.event; 018 019 import static org.junit.Assert.assertEquals; 020 import static org.junit.Assert.assertFalse; 021 import static org.junit.Assert.assertTrue; 022 023 import java.util.LinkedList; 024 import java.util.List; 025 026 /** 027 * A test event listener class that can be used for testing whether 028 * configurations generated correct events. 029 * 030 * @author <a 031 * href="http://commons.apache.org/configuration/team-list.html">Commons 032 * Configuration team</a> 033 * @version $Id: ConfigurationListenerTestImpl.java 1225648 2011-12-29 20:55:07Z oheger $ 034 */ 035 public class ConfigurationListenerTestImpl implements ConfigurationListener 036 { 037 /** The expected event source. */ 038 private final Object expectedSource; 039 040 /** Stores the received events. */ 041 private final List<ConfigurationEvent> events; 042 043 /** 044 * Creates a new instance of {@code ConfigurationListenerTestImpl} and sets 045 * the expected event source. 046 * 047 * @param source the event source (<b>null</b> if the source need not to be 048 * checked) 049 */ 050 public ConfigurationListenerTestImpl(Object source) 051 { 052 expectedSource = source; 053 events = new LinkedList<ConfigurationEvent>(); 054 } 055 056 public void configurationChanged(ConfigurationEvent event) 057 { 058 events.add(event); 059 } 060 061 /** 062 * Checks if at least {@code minEvents} events have been received. 063 * 064 * @param minEvents the minimum number of expected events 065 */ 066 public void checkEventCount(int minEvents) 067 { 068 assertTrue("Too view events received", events.size() >= minEvents); 069 } 070 071 /** 072 * Checks an expected event. 073 * 074 * @param type the event type 075 * @param propName the expected property name 076 * @param propValue the expected property value 077 * @param before the expected before flag 078 */ 079 public void checkEvent(int type, String propName, Object propValue, 080 boolean before) 081 { 082 ConfigurationEvent e = nextEvent(type); 083 assertEquals("Wrong property name", propName, e.getPropertyName()); 084 assertEquals("Wrong property value", propValue, e.getPropertyValue()); 085 assertEquals("Wrong before flag", before, e.isBeforeUpdate()); 086 } 087 088 /** 089 * Returns the next received event and checks for the expected type. This 090 * method can be used instead of {@code checkEvent()} for comparing 091 * complex event values. 092 * 093 * @param expectedType the expected type of the event 094 * @return the event object 095 */ 096 public ConfigurationEvent nextEvent(int expectedType) 097 { 098 assertFalse("Too few events received", events.isEmpty()); 099 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 }