From: <bo...@us...> - 2010-05-07 16:15:01
|
Revision: 384 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=384&view=rev Author: bodewig Date: 2010-05-07 16:14:55 +0000 (Fri, 07 May 2010) Log Message: ----------- listener management Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListenerSupport.java trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListenerSupport.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListenerSupport.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListenerSupport.java 2010-05-07 16:14:55 UTC (rev 384) @@ -0,0 +1,78 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit.diff; + +import java.util.Iterator; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; + +/** + * Encapsulates support for DifferenceListeners so it can be reused by + * different implementations of IDifferenceEngine. + */ +public class ComparisonListenerSupport { + private final List<ComparisonListener> compListeners = + new CopyOnWriteArrayList<ComparisonListener>(); + private final List<ComparisonListener> matchListeners = + new CopyOnWriteArrayList<ComparisonListener>(); + private final List<ComparisonListener> diffListeners = + new CopyOnWriteArrayList<ComparisonListener>(); + + /** + * Registers a listener that is notified of each comparison. + */ + public void addComparisonListener(ComparisonListener l) { + compListeners.add(l); + } + + /** + * Registers a listener that is notified of each comparison with + * outcome {@link ComparisonResult#EQUAL}. + */ + public void addMatchListener(ComparisonListener l) { + matchListeners.add(l); + } + + /** + * Registers a listener that is notified of each comparison with + * outcome other than {@link ComparisonResult#EQUAL}. + */ + public void addDifferenceListener(ComparisonListener l) { + diffListeners.add(l); + } + + /** + * Propagates the result of a comparision to all registered + * listeners. + */ + public void fireComparisonPerformed(Comparison comparison, + ComparisonResult outcome) { + fire(comparison, outcome, compListeners); + if (outcome == ComparisonResult.EQUAL) { + fire(comparison, outcome, matchListeners); + } else { + fire(comparison, outcome, diffListeners); + } + } + + private static void fire(Comparison comparison, ComparisonResult outcome, + List<ComparisonListener> listeners) { + if (!listeners.isEmpty()) { + for (Iterator<ComparisonListener> it = listeners.iterator(); + it.hasNext(); ) { + it.next().comparisonPerformed(comparison, outcome); + } + } + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/diff/ComparisonListenerSupport.java ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java =================================================================== --- trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java (rev 0) +++ trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java 2010-05-07 16:14:55 UTC (rev 384) @@ -0,0 +1,71 @@ +/* + This file is licensed to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ +package net.sf.xmlunit.diff; + +import java.util.Arrays; +import java.util.HashSet; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ComparisonListenerSupportTest { + + @Test public void dispatchesOnOutcome() { + ComparisonListenerSupport s = new ComparisonListenerSupport(); + Listener c, m, d; + s.addComparisonListener(c = new Listener(ComparisonResult.EQUAL, + ComparisonResult.SIMILAR, + ComparisonResult.DIFFERENT, + ComparisonResult.CRITICAL)); + s.addMatchListener(m = new Listener(ComparisonResult.EQUAL)); + s.addDifferenceListener(d = new Listener(ComparisonResult.SIMILAR, + ComparisonResult.DIFFERENT, + ComparisonResult.CRITICAL)); + for (ComparisonResult r : new ComparisonResult[] { + ComparisonResult.EQUAL, + ComparisonResult.SIMILAR, + ComparisonResult.DIFFERENT, + ComparisonResult.CRITICAL + }) { + s.fireComparisonPerformed(null, r); + } + + assertEquals(4, c.invocations); + assertEquals(1, m.invocations); + assertEquals(3, d.invocations); + } + + @Test public void noListenersDontCauseProblems() { + ComparisonListenerSupport s = new ComparisonListenerSupport(); + s.fireComparisonPerformed(null, ComparisonResult.EQUAL); + } + + private static class Listener implements ComparisonListener { + private final HashSet<ComparisonResult> acceptable = + new HashSet<ComparisonResult>(); + private int invocations = 0; + + private Listener(ComparisonResult... accept) { + acceptable.addAll(Arrays.asList(accept)); + } + + public void comparisonPerformed(Comparison comparison, + ComparisonResult outcome) { + invocations++; + if (!acceptable.contains(outcome)) { + fail("unexpected outcome: " + outcome); + } + } + } +} Property changes on: trunk/xmlunit/src/tests/java-core/net/sf/xmlunit/diff/ComparisonListenerSupportTest.java ___________________________________________________________________ Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |