View Javadoc
1   /*
2    * Copyright (C) 2017, Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.events;
12  
13  import static org.junit.Assert.assertEquals;
14  
15  import java.util.Arrays;
16  import java.util.HashSet;
17  import java.util.Set;
18  
19  /**
20   * A {@link WorkingTreeModifiedListener} that can be used in tests to check
21   * expected events.
22   */
23  public class ChangeRecorder implements WorkingTreeModifiedListener {
24  
25  	public static final String[] EMPTY = new String[0];
26  
27  	private Set<String> modified = new HashSet<>();
28  
29  	private Set<String> deleted = new HashSet<>();
30  
31  	private int eventCount;
32  
33  	@Override
34  	public void onWorkingTreeModified(WorkingTreeModifiedEvent event) {
35  		eventCount++;
36  		modified.removeAll(event.getDeleted());
37  		deleted.removeAll(event.getModified());
38  		modified.addAll(event.getModified());
39  		deleted.addAll(event.getDeleted());
40  	}
41  
42  	private String[] getModified() {
43  		return modified.toArray(new String[0]);
44  	}
45  
46  	private String[] getDeleted() {
47  		return deleted.toArray(new String[0]);
48  	}
49  
50  	private void reset() {
51  		eventCount = 0;
52  		modified.clear();
53  		deleted.clear();
54  	}
55  
56  	public void assertNoEvent() {
57  		assertEquals("Unexpected WorkingTreeModifiedEvent ", 0, eventCount);
58  	}
59  
60  	public void assertEvent(String[] expectedModified,
61  			String[] expectedDeleted) {
62  		String[] actuallyModified = getModified();
63  		String[] actuallyDeleted = getDeleted();
64  		Arrays.sort(actuallyModified);
65  		Arrays.sort(expectedModified);
66  		Arrays.sort(actuallyDeleted);
67  		Arrays.sort(expectedDeleted);
68  		assertEquals("Unexpected modifications reported",
69  				Arrays.toString(expectedModified),
70  				Arrays.toString(actuallyModified));
71  		assertEquals("Unexpected deletions reported",
72  				Arrays.toString(expectedDeleted),
73  				Arrays.toString(actuallyDeleted));
74  		reset();
75  	}
76  }