Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6a666ce545905cdacb4d7f274a33dbcec2010e71 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*******************************************************************************
 * Copyright (c) 2008, 2011 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.jpa.eclipselink.core.tests.internal.context.persistence;

import org.eclipse.jpt.common.utility.model.listener.PropertyChangeListener;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnit;
import org.eclipse.jpt.jpa.core.context.persistence.PersistenceUnitProperties;
import org.eclipse.jpt.jpa.eclipselink.core.context.persistence.DdlGenerationType;
import org.eclipse.jpt.jpa.eclipselink.core.context.persistence.OutputMode;
import org.eclipse.jpt.jpa.eclipselink.core.context.persistence.SchemaGeneration;

/**
 * Tests the update of OutputMode model object by the SchemaGeneration adapter
 * when the PersistenceUnit changes.
 */
@SuppressWarnings("nls")
public class SchemaGenerationAdapterTests extends EclipseLinkPersistenceUnitTestCase
{
	private SchemaGeneration schemaGeneration;

	public static final String DDL_GENERATION_TYPE_KEY = SchemaGeneration.ECLIPSELINK_DDL_GENERATION_TYPE;
	public static final DdlGenerationType DDL_GENERATION_TYPE_TEST_VALUE = DdlGenerationType.drop_and_create_tables;
	public static final DdlGenerationType DDL_GENERATION_TYPE_TEST_VALUE_2 = DdlGenerationType.none;

	public static final String OUTPUT_MODE_KEY = SchemaGeneration.ECLIPSELINK_DDL_GENERATION_OUTPUT_MODE;
	public static final OutputMode OUTPUT_MODE_TEST_VALUE = OutputMode.sql_script;
	public static final OutputMode OUTPUT_MODE_TEST_VALUE_2 = OutputMode.database;

	private static final String CREATE_FILE_NAME_KEY = SchemaGeneration.ECLIPSELINK_CREATE_FILE_NAME;
	private static final String CREATE_FILE_NAME_TEST_VALUE = "create-file-name.test";
	private static final String CREATE_FILE_NAME_TEST_VALUE_2 = "create-file-name-2.test";

	private static final String DROP_FILE_NAME_KEY = SchemaGeneration.ECLIPSELINK_DROP_FILE_NAME;
	private static final String DROP_FILE_NAME_TEST_VALUE = "drop-file-name.test";
	private static final String DROP_FILE_NAME_TEST_VALUE_2 = "drop-file-name-2.test";

	private static final String APPLICATION_LOCATION_KEY = SchemaGeneration.ECLIPSELINK_APPLICATION_LOCATION;
	private static final String APPLICATION_LOCATION_TEST_VALUE = "C:/temp";
	private static final String APPLICATION_LOCATION_TEST_VALUE_2 = "C:/tmp";

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

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		this.schemaGeneration = this.subject.getSchemaGeneration();
		
		PropertyChangeListener propertyChangeListener = this.buildPropertyChangeListener();
		this.schemaGeneration.addPropertyChangeListener(SchemaGeneration.OUTPUT_MODE_PROPERTY, propertyChangeListener);
		this.schemaGeneration.addPropertyChangeListener(SchemaGeneration.DDL_GENERATION_TYPE_PROPERTY, propertyChangeListener);
		this.schemaGeneration.addPropertyChangeListener(SchemaGeneration.CREATE_FILE_NAME_PROPERTY, propertyChangeListener);
		this.schemaGeneration.addPropertyChangeListener(SchemaGeneration.DROP_FILE_NAME_PROPERTY, propertyChangeListener);
		this.schemaGeneration.addPropertyChangeListener(SchemaGeneration.APPLICATION_LOCATION_PROPERTY, propertyChangeListener);
		this.clearEvent();
	}

	/**
	 * Initializes directly the PU properties before testing. Cannot use
	 * Property Holder to initialize because it is not created yet
	 */
	@Override
	protected void populatePu() {
		this.propertiesTotal = 9;
		this.modelPropertiesSizeOriginal = 5;
		this.modelPropertiesSize = this.modelPropertiesSizeOriginal;
		
		this.persistenceUnitSetProperty("property.0", "value.0");
		this.persistenceUnitSetProperty(OUTPUT_MODE_KEY, this.getPropertyStringValueOf(OUTPUT_MODE_TEST_VALUE));
		this.persistenceUnitSetProperty("property.2", "value.2");
		this.persistenceUnitSetProperty("property.3", "value.3");
		this.persistenceUnitSetProperty("property.4", "value.4");
		this.persistenceUnitSetProperty(DDL_GENERATION_TYPE_KEY, this.getPropertyStringValueOf(DDL_GENERATION_TYPE_TEST_VALUE));
		this.persistenceUnitSetProperty(CREATE_FILE_NAME_KEY, CREATE_FILE_NAME_TEST_VALUE);
		this.persistenceUnitSetProperty(DROP_FILE_NAME_KEY, DROP_FILE_NAME_TEST_VALUE);
		this.persistenceUnitSetProperty(APPLICATION_LOCATION_KEY, APPLICATION_LOCATION_TEST_VALUE);
		return;
	}

	// ********** CreateFileName **********
	/**
	 * Tests the update of CreateFileName property by the SchemaGeneration
	 * adapter when the PU or the model changes.
	 */
	public void testSetCreateFileName() throws Exception {
		this.verifyModelInitialized(
			CREATE_FILE_NAME_KEY,
			CREATE_FILE_NAME_TEST_VALUE);
		this.verifySetProperty(
			CREATE_FILE_NAME_KEY,
			CREATE_FILE_NAME_TEST_VALUE,
			CREATE_FILE_NAME_TEST_VALUE_2);
	}

	public void testAddRemoveCreateFileName() throws Exception {
		this.verifyAddRemoveProperty(
			CREATE_FILE_NAME_KEY,
			CREATE_FILE_NAME_TEST_VALUE,
			CREATE_FILE_NAME_TEST_VALUE_2);
	}
	
	// ********** DropFileName **********
	/**
	 * Tests the update of DropFileName property by the SchemaGeneration adapter
	 * when the PU or the model changes.
	 */
	public void testSetDropFileName() throws Exception {
		this.verifyModelInitialized(
			DROP_FILE_NAME_KEY,
			DROP_FILE_NAME_TEST_VALUE);
		this.verifySetProperty(
			DROP_FILE_NAME_KEY,
			DROP_FILE_NAME_TEST_VALUE,
			DROP_FILE_NAME_TEST_VALUE_2);
	}

	public void testAddRemoveDropFileName() throws Exception {
		this.verifyAddRemoveProperty(
			DROP_FILE_NAME_KEY, 
			DROP_FILE_NAME_TEST_VALUE, 
			DROP_FILE_NAME_TEST_VALUE_2);
	}

	// ********** ApplicationLocation **********
	/**
	 * Tests the update of ApplicationLocation property by the SchemaGeneration
	 * adapter when the PU or the model changes.
	 */
	public void testSetApplicationLocation() throws Exception {
		this.verifyModelInitialized(
			APPLICATION_LOCATION_KEY,
			APPLICATION_LOCATION_TEST_VALUE);
		this.verifySetProperty(
			APPLICATION_LOCATION_KEY,
			APPLICATION_LOCATION_TEST_VALUE,
			APPLICATION_LOCATION_TEST_VALUE_2);
	}

	public void testAddRemoveApplicationLocation() throws Exception {
		this.verifyAddRemoveProperty(
			APPLICATION_LOCATION_KEY,
			APPLICATION_LOCATION_TEST_VALUE,
			APPLICATION_LOCATION_TEST_VALUE_2);
	}

	public void testSetEmptyApplicationLocation() throws Exception {
		String puKey = APPLICATION_LOCATION_KEY;
		PersistenceUnit.Property property = this.getPersistenceUnit().getProperty(puKey);
		String propertyName = this.getModel().propertyIdOf(property);

		// Set ApplicationLocation to "" & verify that the property is deleted
		this.verifyPuHasProperty(puKey,  "persistenceUnit.properties doesn't contains: ");
		this.setProperty(propertyName, "");

		this.verifyPuHasNotProperty(puKey,  "Property was not deleted");
		this.verifyPutProperty(propertyName, null);
		assertNull(this.getPersistenceUnit().getProperty(puKey));
	}

	// ********** OutputMode **********
	/**
	 * Tests the update of OutputMode property by the SchemaGeneration adapter
	 * when the PU or the model changes.
	 */
	public void testSetOutputMode() throws Exception {
		this.verifyModelInitialized(
			OUTPUT_MODE_KEY,
			OUTPUT_MODE_TEST_VALUE);
		this.verifySetProperty(
			OUTPUT_MODE_KEY,
			OUTPUT_MODE_TEST_VALUE,
			OUTPUT_MODE_TEST_VALUE_2);
	}

	public void testAddRemoveOutputMode() throws Exception {
		this.verifyAddRemoveProperty(
			OUTPUT_MODE_KEY, 
			OUTPUT_MODE_TEST_VALUE, 
			OUTPUT_MODE_TEST_VALUE_2);
	}

	// ********** DdlGenerationType **********
	/**
	 * Tests the update of DdlGenerationType property by the SchemaGeneration
	 * adapter when the PU or the model changes.
	 */
	public void testSetDdlGenerationType() throws Exception {
		this.verifyModelInitialized(
			DDL_GENERATION_TYPE_KEY,
			DDL_GENERATION_TYPE_TEST_VALUE);
		this.verifySetProperty(
			DDL_GENERATION_TYPE_KEY,
			DDL_GENERATION_TYPE_TEST_VALUE,
			DDL_GENERATION_TYPE_TEST_VALUE_2);
	}

	public void testAddRemoveDdlGenerationType() throws Exception {
		this.verifyAddRemoveProperty(
			DDL_GENERATION_TYPE_KEY, 
			DDL_GENERATION_TYPE_TEST_VALUE, 
			DDL_GENERATION_TYPE_TEST_VALUE_2);
	}

	// ********** get/set property **********
	@Override
	protected void setProperty(String propertyName, Object newValue) throws NoSuchFieldException {
		if (propertyName.equals(SchemaGeneration.OUTPUT_MODE_PROPERTY))
			this.schemaGeneration.setOutputMode((OutputMode) newValue);
		// else if( propertyName.equals( Caching.CACHE_SIZE_PROPERTY))
		// TODO
		else if (propertyName.equals(SchemaGeneration.DDL_GENERATION_TYPE_PROPERTY))
			this.schemaGeneration.setDdlGenerationType((DdlGenerationType) newValue);
		else if (propertyName.equals(SchemaGeneration.APPLICATION_LOCATION_PROPERTY))
			this.schemaGeneration.setApplicationLocation((String) newValue);
		else if (propertyName.equals(SchemaGeneration.CREATE_FILE_NAME_PROPERTY))
			this.schemaGeneration.setCreateFileName((String) newValue);
		else if (propertyName.equals(SchemaGeneration.DROP_FILE_NAME_PROPERTY))
			this.schemaGeneration.setDropFileName((String) newValue);
		else
			this.throwMissingDefinition("setProperty", propertyName);
	}

	@Override
	protected Object getProperty(String propertyName) throws NoSuchFieldException {
		Object modelValue = null;
		if (propertyName.equals(SchemaGeneration.OUTPUT_MODE_PROPERTY))
			modelValue = this.schemaGeneration.getOutputMode();
		else if (propertyName.equals(SchemaGeneration.DDL_GENERATION_TYPE_PROPERTY))
			modelValue = this.schemaGeneration.getDdlGenerationType();
		else if (propertyName.equals(SchemaGeneration.APPLICATION_LOCATION_PROPERTY))
			modelValue = this.schemaGeneration.getApplicationLocation();
		else if (propertyName.equals(SchemaGeneration.CREATE_FILE_NAME_PROPERTY))
			modelValue = this.schemaGeneration.getCreateFileName();
		else if (propertyName.equals(SchemaGeneration.DROP_FILE_NAME_PROPERTY))
			modelValue = this.schemaGeneration.getDropFileName();
		else
			this.throwMissingDefinition("getProperty", propertyName);
		return modelValue;
	}

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

Back to the top