PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CreateCompilationUnitTests.java

https://github.com/effine/eclipse.jdt.core
Java | 237 lines | 181 code | 5 blank | 51 comment | 8 complexity | 734bec3c8f0ca6704cd41c2fd61a9080 MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2009 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM Corporation - initial API and implementation
  10. *******************************************************************************/
  11. package org.eclipse.jdt.core.tests.model;
  12. import java.io.File;
  13. import java.io.IOException;
  14. import org.eclipse.core.resources.IResource;
  15. import org.eclipse.core.resources.IWorkspaceRunnable;
  16. import org.eclipse.core.runtime.CoreException;
  17. import org.eclipse.core.runtime.IProgressMonitor;
  18. import org.eclipse.jdt.core.*;
  19. import junit.framework.Test;
  20. public class CreateCompilationUnitTests extends ModifyingResourceTests {
  21. public CreateCompilationUnitTests(String name) {
  22. super(name);
  23. }
  24. public static Test suite() {
  25. return buildModelTestSuite(CreateCompilationUnitTests.class);
  26. }
  27. public void setUp() throws Exception {
  28. super.setUp();
  29. createJavaProject("P");
  30. createFolder("/P/p");
  31. startDeltas();
  32. }
  33. public void tearDown() throws Exception {
  34. stopDeltas();
  35. deleteProject("P");
  36. super.tearDown();
  37. }
  38. /**
  39. * Ensures that a compilation unit can be created with specified source
  40. * in a package.
  41. * Verifies that the proper change deltas are generated as a side effect
  42. * of running the operation.
  43. * Ensure that the import container has been created correctly.
  44. */
  45. public void testCUAndImportContainer() throws JavaModelException {
  46. IPackageFragment pkg = getPackage("/P/p");
  47. ICompilationUnit cu= pkg.createCompilationUnit("HelloImports.java",
  48. ("package p;\n" +
  49. "\n" +
  50. "import java.util.Enumeration;\n" +
  51. "import java.util.Vector;\n" +
  52. "\n" +
  53. "public class HelloImports {\n" +
  54. "\n" +
  55. " public static main(String[] args) {\n" +
  56. " System.out.println(\"HelloWorld\");\n" +
  57. " }\n" +
  58. "}\n"), false,null);
  59. assertCreation(cu);
  60. assertDeltas(
  61. "Unexpected delta",
  62. "P[*]: {CHILDREN}\n" +
  63. " <project root>[*]: {CHILDREN}\n" +
  64. " p[*]: {CHILDREN}\n" +
  65. " HelloImports.java[+]: {}"
  66. );
  67. IImportDeclaration[] imprts= cu.getImports();
  68. assertTrue("Import does not exist", imprts.length == 2 && imprts[0].exists());
  69. cu.close();
  70. imprts= cu.getImports();
  71. assertTrue("Import does not exist", imprts.length == 2 && imprts[0].exists());
  72. }
  73. /**
  74. * Ensures that a default compilation unit is created for a type if
  75. * it does not yet exist.
  76. */
  77. public void testDefaultCU() throws CoreException {
  78. IPackageFragment pkg = getPackage("/P/p");
  79. ICompilationUnit cu= pkg.getCompilationUnit("Default.java");
  80. IType type= cu.createType("public class Default {}", null, false, null);
  81. assertCreation(cu);
  82. assertCreation(type);
  83. assertDeltas(
  84. "Unexpected delta",
  85. "P[*]: {CHILDREN}\n" +
  86. " <project root>[*]: {CHILDREN}\n" +
  87. " p[*]: {CHILDREN}\n" +
  88. " Default.java[+]: {}\n" +
  89. "\n" +
  90. "P[*]: {CHILDREN}\n" +
  91. " <project root>[*]: {CHILDREN}\n" +
  92. " p[*]: {CHILDREN}\n" +
  93. " Default.java[*]: {CHILDREN | FINE GRAINED | PRIMARY RESOURCE}\n" +
  94. " Default[+]: {}"
  95. );
  96. // CU should have a package statement and type
  97. assertElementDescendants(
  98. "Unexpected children",
  99. "Default.java\n" +
  100. " package p\n" +
  101. " class Default",
  102. cu);
  103. // should fail if we try again
  104. try {
  105. pkg.createCompilationUnit("Default.java", "", false, null);
  106. } catch (JavaModelException jme) {
  107. assertTrue("Exception status not correct for creating a cu that already exists", jme.getStatus().getCode() == IJavaModelStatusConstants.NAME_COLLISION);
  108. }
  109. // should fail if we try again
  110. try {
  111. pkg.createCompilationUnit("Default.java", "public class Default {}", true, null);
  112. return;
  113. } catch (JavaModelException jme) {
  114. }
  115. assertTrue("Creation should not fail if the compilation unit already exists", false);
  116. }
  117. /**
  118. * Ensures that a default compilation unit is created for a type if
  119. * it does not yet exist.
  120. */
  121. public void testEmptyCU() {
  122. IPackageFragment pkg = getPackage("/P/p");
  123. // should fail if we try again
  124. try {
  125. pkg.createCompilationUnit("Empty.java", "", true, null);
  126. } catch (JavaModelException jme) {
  127. }
  128. ICompilationUnit cu= pkg.getCompilationUnit("Empty.java");
  129. assertCreation(cu);
  130. }
  131. /*
  132. * Ensures that a compilation unit can be created even if a file already exists on the file system.
  133. * (regression test for bug 41611 CreateCompilationUnitOperation.executeOperation() should probably force creation more agressively)
  134. */
  135. public void testForce() throws JavaModelException, IOException {
  136. IPackageFragment pkg = getPackage("/P/p");
  137. File folder = pkg.getResource().getLocation().toFile();
  138. new File(folder, "X.java").createNewFile();
  139. ICompilationUnit cu = pkg.createCompilationUnit(
  140. "X.java",
  141. "package p;\n" +
  142. "public class X {\n" +
  143. "}",
  144. true, // force,
  145. null);
  146. assertCreation(cu);
  147. assertDeltas(
  148. "Unexpected delta",
  149. "P[*]: {CHILDREN}\n" +
  150. " <project root>[*]: {CHILDREN}\n" +
  151. " p[*]: {CHILDREN}\n" +
  152. " X.java[+]: {}"
  153. );
  154. }
  155. /**
  156. * Ensures that a compilation unit cannot be created with an invalid name
  157. * in a package.
  158. */
  159. public void testInvalidName() {
  160. IPackageFragment pkg = getPackage("/P/p");
  161. try {
  162. pkg.createCompilationUnit("HelloWorld.j", null, false, null);
  163. } catch (JavaModelException jme) {
  164. assertTrue("Incorrect JavaModelException thrown for creating a cu with invalid name", jme.getStatus().getCode() == IJavaModelStatusConstants.INVALID_NAME);
  165. try {
  166. pkg.createCompilationUnit(null, null, false,null);
  167. } catch (JavaModelException jme2) {
  168. assertTrue("Incorrect JavaModelException thrown for creating a cu with invalid name", jme2.getStatus().getCode() == IJavaModelStatusConstants.INVALID_NAME);
  169. return;
  170. }
  171. }
  172. assertTrue("No JavaModelException thrown for creating a cu with an invalid name", false);
  173. }
  174. /**
  175. * Ensures that a compilation unit cannot be created with <code>null</code> source
  176. * in a package.
  177. */
  178. public void testNullContents() {
  179. IPackageFragment pkg = getPackage("/P/p");
  180. try {
  181. pkg.createCompilationUnit("HelloWorld.java", null, false, null);
  182. } catch (JavaModelException jme) {
  183. assertTrue("Incorrect JavaModelException thrown for creating a cu with null contents: " + jme, jme.getStatus().getCode() == IJavaModelStatusConstants.INVALID_CONTENTS);
  184. return;
  185. }
  186. assertTrue("No JavaModelException thrown for creating a cu with null contents", false);
  187. }
  188. /**
  189. * Ensures that a compilation unit can be created with specified source
  190. * in a package.
  191. * Verifies that the proper change deltas are generated as a side effect
  192. * of running the operation.
  193. */
  194. public void testSimpleCreation() throws JavaModelException {
  195. IPackageFragment pkg = getPackage("/P/p");
  196. ICompilationUnit cu= pkg.createCompilationUnit("HelloWorld.java",
  197. ("package p;\n" +
  198. "\n" +
  199. "public class HelloWorld {\n" +
  200. "\n" +
  201. " public static main(String[] args) {\n" +
  202. " System.out.println(\"HelloWorld\");\n" +
  203. " }\n" +
  204. "}\n"), false, null);
  205. assertCreation(cu);
  206. assertDeltas(
  207. "Unexpected delta",
  208. "P[*]: {CHILDREN}\n" +
  209. " <project root>[*]: {CHILDREN}\n" +
  210. " p[*]: {CHILDREN}\n" +
  211. " HelloWorld.java[+]: {}"
  212. );
  213. }
  214. /*
  215. * Ensures that the correct scheduling rule is used when running createCompilationUnit
  216. * (regression test for https://bugs.eclipse.org/bugs/show_bug.cgi?id=233270 )
  217. */
  218. public void testSchedulingRule() throws Exception {
  219. IWorkspaceRunnable runnable = new IWorkspaceRunnable(){
  220. public void run(IProgressMonitor monitor) throws CoreException {
  221. IPackageFragment pkg = getPackage("/P/p");
  222. ICompilationUnit cu= pkg.createCompilationUnit("HelloWorld.java",
  223. ("package p;\n" +
  224. "\n" +
  225. "public class HelloWorld {\n" +
  226. "}\n"), false, null);
  227. assertCreation(cu);
  228. }
  229. };
  230. getWorkspace().run(runnable, getFolder("/P/p"), IResource.NONE, null);
  231. }
  232. }