Skip to main content
summaryrefslogtreecommitdiffstats
blob: d9f29e361220a0d403e9488936a7fde4ebf228d5 (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
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.core.tests.model;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.*;

import junit.framework.Test;

public class CreatePackageTests extends ModifyingResourceTests {
public CreatePackageTests(String name) {
	super(name);
}
public static Test suite() {
	return buildModelTestSuite(CreatePackageTests.class);
}
public void setUp() throws Exception {
	super.setUp();
	createJavaProject("P");
	createFolder("/P/p");
	startDeltas();
}
public void tearDown() throws Exception {
	stopDeltas();
	deleteProject("P");
	super.tearDown();
}
/**
 * Ensures that a package fragment can be created in a package
 * fragment root.
 * Verifies that the proper change deltas are generated as a side effect
 * of running the operation.
 */
public void testCreatePackageFragment1() throws JavaModelException {
	IPackageFragmentRoot root= getPackageFragmentRoot("P", "");
	IPackageFragment frag= root.createPackageFragment("one.two.three", false, null);
	assertCreation(frag);
	assertTrue("Fragment return not correct", frag.getElementName().equals("one.two.three"));
	assertDeltas(
		"Unexpected delta",
		"P[*]: {CHILDREN}\n" +
		"	<project root>[*]: {CHILDREN}\n" +
		"		one.two.three[+]: {}\n" +
		"		one.two[+]: {}\n" +
		"		one[+]: {}"
	);
}
/**
 * Ensures that a package fragment that shares a prefix path with
 * an existing package fragment can be created in a package
 * fragment root.
 * Verifies that the proper change deltas are generated as a side effect
 * of running the operation.
 */
public void testCreatePackageFragment2() throws JavaModelException {
	IPackageFragmentRoot root = getPackageFragmentRoot("P", "");
	IPackageFragment frag= root.createPackageFragment("one.two.three.four", false, null);
	assertCreation(frag);
	assertDeltas(
		"Unexpected delta",
		"P[*]: {CHILDREN}\n" +
		"	<project root>[*]: {CHILDREN}\n" +
		"		one.two.three.four[+]: {}\n" +
		"		one.two.three[+]: {}\n" +
		"		one.two[+]: {}\n" +
		"		one[+]: {}"
	);
}
/**
 * Ensures that a package fragment can be created for the default package
 */
public void testCreatePackageFragment3() throws JavaModelException {
	IPackageFragmentRoot root= getPackageFragmentRoot("P", "");
	IPackageFragment frag= root.createPackageFragment("", false, null);
	assertCreation(frag);
	assertTrue("Fragment return not correct", frag.getElementName().equals(""));
}
/**
 * Ensures that a package fragment can be created even if its name is unconventional.
 * (regression test for 9479 exception on package creation (discouraged name))
 */
public void testCreatePackageFragment4() throws JavaModelException {
	IPackageFragmentRoot root= getPackageFragmentRoot("P", "");
	IPackageFragment frag= root.createPackageFragment("A", false, null);
	assertCreation(frag);
	assertTrue("Fragment return not correct", frag.getElementName().equals("A"));
}
/*
 * Ensures that a package fragment can be created inside an operation with a
 * package fragment root scheduling rule.
 * (regression test for 95288 [model] CreatePackageFragmentOperation runs with wrong ISchedulingRule)
 */
public void testCreatePackageFragment5() throws CoreException {
	IWorkspaceRunnable runnable = new IWorkspaceRunnable(){
		public void run(IProgressMonitor monitor) throws CoreException {
			IPackageFragmentRoot root= getPackageFragmentRoot("P", "");
			IPackageFragment frag= root.createPackageFragment("p", false, monitor);
			assertCreation(frag);
		}
	};
	getWorkspace().run(runnable, getProject("P"), IResource.NONE, null);
}
/**
 * Ensures that a package fragment that already exists is not duplicated in the package
 * fragment root.
 */
public void testDuplicatePackageFragment() throws JavaModelException {
	IPackageFragmentRoot root= getPackageFragmentRoot("P", "");
	IPackageFragment frag= root.createPackageFragment("p",  false, null);
	assertCreation(frag);
	assertDeltas(
		"Unexpected delta",
		""
	);
}
/**
 * Ensures that a package fragment cannot be created with an invalid parameters.
 */
public void testInvalidPackageFragment() throws CoreException {
	createFile("/P/p/other", "");
	IPackageFragmentRoot root = getPackageFragmentRoot("P", "");
	try {
		root.createPackageFragment(null,  false, null);
	} catch (JavaModelException jme) {
		assertTrue("Incorrect JavaModelException thrown for creating an package fragment with invalid name", jme.getStatus().getCode() == IJavaModelStatusConstants.INVALID_NAME);
		try {
			root.createPackageFragment("java.jfg.",  false, null);
		} catch (JavaModelException jme2) {
			assertTrue("Incorrect JavaModelException thrown for creating a package fragment with invalid name", jme2.getStatus().getCode() == IJavaModelStatusConstants.INVALID_NAME);
			try {
				root.createPackageFragment("p.other", false, null);
			} catch (JavaModelException jme3) {
				assertTrue("Incorrect JavaModelException thrown for creating a package fragment that collides with a file", jme3.getStatus().getCode() == IJavaModelStatusConstants.NAME_COLLISION);
				return;
			}
		}
	}
	assertTrue("No JavaModelException thrown for creating a package fragment with an invalid parameters", false);
}
}

Back to the top