View Javadoc
1   /*
2    * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com> 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  package org.eclipse.jgit.events;
11  
12  import static org.junit.Assert.assertEquals;
13  import static org.junit.Assert.assertNotNull;
14  import static org.junit.Assert.assertNull;
15  
16  import org.eclipse.jgit.junit.RepositoryTestCase;
17  import org.eclipse.jgit.storage.file.FileBasedConfig;
18  import org.junit.Test;
19  
20  public class ConfigChangeEventTest extends RepositoryTestCase {
21  	@Test
22  	public void testFileRepository_ChangeEventsOnlyOnSave() throws Exception {
23  		final ConfigChangedEvent[] events = new ConfigChangedEvent[1];
24  		db.getListenerList()
25  				.addConfigChangedListener((ConfigChangedEvent event) -> {
26  					events[0] = event;
27  				});
28  		FileBasedConfig config = db.getConfig();
29  		assertNull(events[0]);
30  
31  		// set a value to some arbitrary key
32  		config.setString("test", "section", "event", "value");
33  		// no changes until we save
34  		assertNull(events[0]);
35  		config.save();
36  		assertNotNull(events[0]);
37  		// correct repository?
38  		assertEquals(events[0].getRepository(), db);
39  
40  		// reset for the next test
41  		events[0] = null;
42  
43  		// unset the value we have just set above
44  		config.unset("test", "section", "event");
45  		// no changes until we save
46  		assertNull(events[0]);
47  		config.save();
48  		assertNotNull(events[0]);
49  		// correct repository?
50  		assertEquals(events[0].getRepository(), db);
51  	}
52  }