import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import javafx.beans.InvalidationListener;
import javafx.beans.property.BooleanPropertyBase;
import javafx.beans.value.ChangeListener;


public class JFXPropertyTest { 
	@Test 
	public void testJFXProperty() { 
		AtomicInteger test = new AtomicInteger(0); 
		ChangeListener<Boolean> testListener = (observable, oldValue, newValue) -> {System.out.println("inside changelistener");test.incrementAndGet();}; 
		
		assert test.get() == 0; 
		TestProperty testProperty = new TestProperty(true, (object) -> {}); 
		testProperty.addListener(testListener); 
		
		ChangeListener<Boolean> testListener2 = (observable, oldValue, newValue) -> {System.out.println("inside changelistener2");};
		//testProperty.addListener(testListener2); 

		testProperty.setValue(false); 
		assert test.get() == 1; 
	} 
	
	private static class TestProperty extends BooleanPropertyBase { 
		private final InvalidationListener invalidationListener; 

		private TestProperty(boolean value, InvalidationListener invalidationListener) { 
			super(value); 
			this.invalidationListener = invalidationListener; 
			addListener(invalidationListener); 
		} 

		@Override 
		protected void invalidated() { 
			if (!getValue()) { 
				removeListener(invalidationListener); 
			} 
			super.invalidated(); 
		} 

		public Object getBean() { 
			return null; 
		} 

		public String getName() { 
			return "test"; 
		} 
	} 
} 