Skip to main content
summaryrefslogtreecommitdiffstats
blob: 60baf3d9cf4f2fdd442e2d6252b0158d5bb0d7e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2008, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 *******************************************************************************/
package org.eclipse.jpt.eclipselink.core.tests.internal.context.persistence.customization;

import org.eclipse.jpt.eclipselink.core.internal.context.persistence.PersistenceUnitProperties;
import org.eclipse.jpt.eclipselink.core.internal.context.persistence.customization.Customization;
import org.eclipse.jpt.eclipselink.core.tests.internal.context.persistence.PersistenceUnitTestCase;
import org.eclipse.jpt.utility.internal.model.AbstractModel;
import org.eclipse.jpt.utility.internal.model.value.PropertyAspectAdapter;
import org.eclipse.jpt.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.jpt.utility.model.event.PropertyChangeEvent;
import org.eclipse.jpt.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.utility.model.value.PropertyValueModel;
import org.eclipse.jpt.utility.model.value.WritablePropertyValueModel;

/**
 * CustomizationValueModelTests
 */
public class CustomizationValueModelTests extends PersistenceUnitTestCase
{
	private Customization customization;

	private WritablePropertyValueModel<Boolean> throwExceptionsHolder;
	private PropertyChangeListener throwExceptionsListener;
	private PropertyChangeEvent throwExceptionsEvent;

	public static final String ENTITY_NAME_TEST_VALUE = "Employee";
	public static final Boolean THROW_EXCEPTIONS_TEST_VALUE = Boolean.FALSE;

	public CustomizationValueModelTests(String name) {
		super(name);
	}

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.customization = this.subject.getCustomization(); // Subject
		PropertyValueModel<Customization> customizationHolder = new SimplePropertyValueModel<Customization>(this.customization);
		
		this.throwExceptionsHolder = this.buildThrowExceptionsAA(customizationHolder);
		this.throwExceptionsListener = this.buildThrowExceptionsChangeListener();
		this.throwExceptionsHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.throwExceptionsListener);
		this.throwExceptionsEvent = null;
	}

	public void testHasListeners() {
		AbstractModel subjectCustomization = (AbstractModel) this.customization; // Subject
		
		PropertyAspectAdapter<Customization, Boolean> throwExceptionsAA = 
			(PropertyAspectAdapter<Customization, Boolean>) this.throwExceptionsHolder;
		assertTrue(throwExceptionsAA.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(subjectCustomization.hasAnyPropertyChangeListeners(Customization.THROW_EXCEPTIONS_PROPERTY));
		
		throwExceptionsAA.removePropertyChangeListener(PropertyValueModel.VALUE, this.throwExceptionsListener);
		assertFalse(subjectCustomization.hasAnyPropertyChangeListeners(Customization.THROW_EXCEPTIONS_PROPERTY));
		assertFalse(throwExceptionsAA.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
	}

	/**
	 * Initializes directly the PU properties before testing. 
	 */
	@Override
	protected void populatePu() {
		this.persistenceUnitSetProperty(
			Customization.ECLIPSELINK_THROW_EXCEPTIONS, 
			THROW_EXCEPTIONS_TEST_VALUE);
		return;
	}

	@Override
	protected PersistenceUnitProperties getModel() {
		return this.customization;
	}

	// ****** ThrowExceptions *******
	private WritablePropertyValueModel<Boolean> buildThrowExceptionsAA(PropertyValueModel<Customization> subjectHolder) {
		return new PropertyAspectAdapter<Customization, Boolean>(subjectHolder, Customization.THROW_EXCEPTIONS_PROPERTY) {
			@Override
			protected Boolean buildValue_() {
				return this.subject.getThrowExceptions();
			}

			@Override
			protected void setValue_(Boolean enumValue) {
				this.subject.setThrowExceptions(enumValue);
			}
		};
	}

	private PropertyChangeListener buildThrowExceptionsChangeListener() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {
				CustomizationValueModelTests.this.throwExceptionsEvent = e;
			}
		};
	}

	// ****** Tests ******* 
	public void testValue() {
		// ****** ThrowExceptions ******* 
		this.verifyThrowExceptionsAAValue(THROW_EXCEPTIONS_TEST_VALUE);
		assertEquals(Customization.DEFAULT_THROW_EXCEPTIONS, this.customization.getDefaultThrowExceptions());
	}

	public void testSetValue() throws Exception {
		// ****** ThrowExceptions ******* 
		this.throwExceptionsEvent = null;
		this.verifyHasListeners(this.throwExceptionsHolder, PropertyValueModel.VALUE);
		Boolean newThrowExceptions = !THROW_EXCEPTIONS_TEST_VALUE;
		// Modify the property holder
		this.throwExceptionsHolder.setValue(newThrowExceptions);
		this.verifyThrowExceptionsAAValue(newThrowExceptions);
		assertNotNull(this.throwExceptionsEvent);
	}

	public void testSetNullValue() {
		String notDeleted = "Property not deleted";
		// ****** ThrowExceptions *******
		this.throwExceptionsEvent = null;
		// Setting the property holder
		this.throwExceptionsHolder.setValue(null);
		// testing Holder
		this.verifyThrowExceptionsAAValue(null);
		assertNotNull(this.throwExceptionsEvent);
		// testing PU properties
		this.verifyPuHasNotProperty(Customization.ECLIPSELINK_THROW_EXCEPTIONS, notDeleted);
	}

	// ****** convenience methods *******

	/**
	 * Performs three value tests:<br>
	 * 1. subject value<br>
	 * 2. aspect adapter value<br>
	 * 3. persistenceUnit property value<br>
	 */
	protected void verifyThrowExceptionsAAValue(Boolean testValue) {
		this.verifyAAValue(
			testValue, 
			this.customization.getThrowExceptions(), 
			this.throwExceptionsHolder, 
			Customization.ECLIPSELINK_THROW_EXCEPTIONS);
	}

	// ********** get/set property **********
	@Override
	protected void setProperty(String propertyName, Object newValue) throws Exception {
		throw new UnsupportedOperationException();
	}

	@Override
	protected Object getProperty(String propertyName) throws NoSuchFieldException {
		throw new UnsupportedOperationException();
	}
}

Back to the top