Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2cc31866cfeb1cc46029dc773c64b968cbc2e7b2 (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
/*******************************************************************************
 *  Copyright (c) 2007 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.core.tests.internal.context.orm;

import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.jpt.core.context.orm.OrmXml;
import org.eclipse.jpt.core.context.persistence.PersistenceXml;
import org.eclipse.jpt.core.resource.orm.OrmFactory;
import org.eclipse.jpt.core.resource.orm.OrmResource;
import org.eclipse.jpt.core.resource.persistence.PersistenceFactory;
import org.eclipse.jpt.core.resource.persistence.XmlMappingFileRef;
import org.eclipse.jpt.core.tests.internal.context.ContextModelTestCase;

public class OrmXmlTests extends ContextModelTestCase
{
	public OrmXmlTests(String name) {
		super(name);
	}
	
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		xmlPersistenceUnit().setName("foo");
		XmlMappingFileRef mappingFileRef = PersistenceFactory.eINSTANCE.createXmlMappingFileRef();
		mappingFileRef.setFileName(JptCorePlugin.DEFAULT_ORM_XML_FILE_PATH);
		xmlPersistenceUnit().getMappingFiles().add(mappingFileRef);
		persistenceResource().save(null);
	}
	
	protected PersistenceXml persistenceXml() {
		return getRootContextNode().getPersistenceXml();
	}
	
	protected OrmXml ormXml() {
		return (OrmXml) persistenceUnit().mappingFileRefs().next().getMappingFile();
	}
	
	public void testUpdateAddEntityMappings() throws Exception {
		OrmResource ormResource = ormResource();
		ormResource.getContents().clear();
		ormResource.save(null);
		
		// removing root node now results in reducing content type to simple xml
		assertNull(ormXml());
		
		ormResource.getContents().add(OrmFactory.eINSTANCE.createXmlEntityMappings());
		ormResource.save(null);
		
		assertNotNull(ormXml().getRoot());
	}
	
	public void testModifyAddEntityMappings() {
		OrmResource ormResource = ormResource();
		ormResource.getContents().remove(ormResource.getEntityMappings());
		assertNull(ormResource.getEntityMappings());
		
		OrmXml ormXml = ormXml();
		assertNull(ormXml.getRoot());
		
		ormXml.addEntityMappings();
		
		assertNotNull(ormXml.getRoot());
		
		boolean exceptionThrown = false;
		try {
			ormXml.addEntityMappings();
		}
		catch (IllegalStateException ise) {
			exceptionThrown = true;
		}
		
		assertTrue("IllegalStateException was not thrown", exceptionThrown);
	}
	
	public void testUpdateRemoveEntityMappings() throws Exception {
		OrmResource ormResource = ormResource();
		
		assertNotNull(ormXml().getRoot());
		
		ormResource.getContents().clear();
		
		assertNull(ormXml().getRoot());
	}
	
	public void testModifyRemoveEntityMappings() {
		OrmXml ormXml = ormXml();
		
		assertNotNull(ormXml.getRoot());
		
		ormXml.removeEntityMappings();
		
		assertNull(ormXml.getRoot());
		
		boolean exceptionThrown = false;
		try {
			ormXml.removeEntityMappings();
		}
		catch (IllegalStateException ise) {
			exceptionThrown = true;
		}
		
		assertTrue("IllegalStateException was not thrown", exceptionThrown);
	}
}

Back to the top