Skip to main content
summaryrefslogtreecommitdiffstats
blob: 70f17442a3b941809d099ad0535ca2a496e3ca75 (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
/*******************************************************************************
 * Copyright (c) 2008 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.options;

import org.eclipse.jpt.eclipselink.core.internal.context.persistence.PersistenceUnitProperties;
import org.eclipse.jpt.eclipselink.core.internal.context.persistence.options.Options;
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;

/**
 * OptionsValueModelTests
 */
public class OptionsValueModelTests extends PersistenceUnitTestCase
{
	private Options options;

	private WritablePropertyValueModel<Boolean> includeDescriptorQueriesHolder;
	private PropertyChangeListener includeDescriptorQueriesListener;
	private PropertyChangeEvent includeDescriptorQueriesEvent;

	public static final Boolean INCLUDE_DESCRIPTOR_QUERIES_TEST_VALUE = Boolean.FALSE;

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

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.options = this.subject.getOptions(); // Subject
		PropertyValueModel<Options> optionsHolder = new SimplePropertyValueModel<Options>(this.options);
		
		this.includeDescriptorQueriesHolder = this.buildIncludeDescriptorQueriesAA(optionsHolder);
		this.includeDescriptorQueriesListener = this.buildIncludeDescriptorQueriesChangeListener();
		this.includeDescriptorQueriesHolder.addPropertyChangeListener(PropertyValueModel.VALUE, this.includeDescriptorQueriesListener);
		this.includeDescriptorQueriesEvent = null;
	}

	public void testHasListeners() {
		AbstractModel subjectOptions = (AbstractModel) this.options; // Subject
		
		PropertyAspectAdapter<Options, Boolean> includeDescriptorQueriesAA = 
			(PropertyAspectAdapter<Options, Boolean>) this.includeDescriptorQueriesHolder;
		assertTrue(includeDescriptorQueriesAA.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
		assertTrue(subjectOptions.hasAnyPropertyChangeListeners(Options.SESSION_INCLUDE_DESCRIPTOR_QUERIES_PROPERTY));
		
		includeDescriptorQueriesAA.removePropertyChangeListener(PropertyValueModel.VALUE, this.includeDescriptorQueriesListener);
		assertFalse(subjectOptions.hasAnyPropertyChangeListeners(Options.SESSION_INCLUDE_DESCRIPTOR_QUERIES_PROPERTY));
		assertFalse(includeDescriptorQueriesAA.hasAnyPropertyChangeListeners(PropertyValueModel.VALUE));
	}

	/**
	 * Initializes directly the PU properties before testing. 
	 */
	@Override
	protected void populatePu() {
		this.persistenceUnitPut(
			Options.ECLIPSELINK_SESSION_INCLUDE_DESCRIPTOR_QUERIES, 
			INCLUDE_DESCRIPTOR_QUERIES_TEST_VALUE);
		return;
	}

	@Override
	protected PersistenceUnitProperties model() {
		return this.options;
	}

	// ****** IncludeDescriptorQueries *******
	private WritablePropertyValueModel<Boolean> buildIncludeDescriptorQueriesAA(PropertyValueModel<Options> subjectHolder) {
		return new PropertyAspectAdapter<Options, Boolean>(subjectHolder, Options.SESSION_INCLUDE_DESCRIPTOR_QUERIES_PROPERTY) {
			@Override
			protected Boolean buildValue_() {
				return this.subject.getIncludeDescriptorQueries();
			}

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

	private PropertyChangeListener buildIncludeDescriptorQueriesChangeListener() {
		return new PropertyChangeListener() {
			public void propertyChanged(PropertyChangeEvent e) {
				OptionsValueModelTests.this.includeDescriptorQueriesEvent = e;
			}
		};
	}

	// ****** Tests ******* 
	public void testValue() {
		// ****** IncludeDescriptorQueries ******* 
		this.verifyIncludeDescriptorQueriesAAValue(INCLUDE_DESCRIPTOR_QUERIES_TEST_VALUE);
		assertEquals(Options.DEFAULT_SESSION_INCLUDE_DESCRIPTOR_QUERIES, this.options.getDefaultIncludeDescriptorQueries());
	}

	public void testSetValue() throws Exception {
		// ****** IncludeDescriptorQueries ******* 
		this.includeDescriptorQueriesEvent = null;
		this.verifyHasListeners(this.includeDescriptorQueriesHolder, PropertyValueModel.VALUE);
		Boolean newIncludeDescriptorQueries = !INCLUDE_DESCRIPTOR_QUERIES_TEST_VALUE;
		// Modify the property holder
		this.includeDescriptorQueriesHolder.setValue(newIncludeDescriptorQueries);
		this.verifyIncludeDescriptorQueriesAAValue(newIncludeDescriptorQueries);
		assertNotNull(this.includeDescriptorQueriesEvent);
	}

	public void testSetNullValue() {
		String notDeleted = "Property not deleted";
		// ****** IncludeDescriptorQueries *******
		this.includeDescriptorQueriesEvent = null;
		// Setting the property holder
		this.includeDescriptorQueriesHolder.setValue(null);
		// testing Holder
		this.verifyIncludeDescriptorQueriesAAValue(null);
		assertNotNull(this.includeDescriptorQueriesEvent);
		// testing PU properties
		this.verifyPuHasNotProperty(Options.ECLIPSELINK_SESSION_INCLUDE_DESCRIPTOR_QUERIES, notDeleted);
	}

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

	/**
	 * Performs three value tests:<br>
	 * 1. subject value<br>
	 * 2. aspect adapter value<br>
	 * 3. persistenceUnit property value<br>
	 */
	protected void verifyIncludeDescriptorQueriesAAValue(Boolean testValue) {
		this.verifyAAValue(
			testValue, 
			this.options.getIncludeDescriptorQueries(), 
			this.includeDescriptorQueriesHolder, 
			Options.ECLIPSELINK_SESSION_INCLUDE_DESCRIPTOR_QUERIES);
	}

	// ********** 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