You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AnnotationProcessingTests.java 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*******************************************************************************
  2. * Copyright (c) 2014 Contributors
  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. * Andy Clement - initial API and implementation
  10. *******************************************************************************/
  11. package org.aspectj.systemtest.incremental.tools;
  12. import java.io.File;
  13. import java.util.ArrayList;
  14. import java.util.Hashtable;
  15. import java.util.List;
  16. public class AnnotationProcessingTests extends AbstractMultiProjectIncrementalAjdeInteractionTestbed {
  17. // Basic test: turns on annotation processing and tries to run the DemoProcessor
  18. public void testAnnotationProcessing1() throws Exception {
  19. createAndBuildAnnotationProcessorProject("ProcessorProject");
  20. initialiseProject("ProcessorConsumer1");
  21. configureProcessorOptions("ProcessorConsumer1","DemoProcessor");
  22. configureNonStandardCompileOptions("ProcessorConsumer1", "-showWeaveInfo");
  23. Hashtable<String, String> javaOptions = new Hashtable<String, String>();
  24. javaOptions.put("org.eclipse.jdt.core.compiler.compliance", "1.6");
  25. javaOptions.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.6");
  26. javaOptions.put("org.eclipse.jdt.core.compiler.source", "1.6");
  27. javaOptions.put("org.eclipse.jdt.core.compiler.processAnnotations","enabled");
  28. configureJavaOptionsMap("ProcessorConsumer1", javaOptions);
  29. configureNewProjectDependency("ProcessorConsumer1", "ProcessorProject");
  30. configureNonStandardCompileOptions("ProcessorConsumer1", "-showWeaveInfo");
  31. build("ProcessorConsumer1");
  32. checkWasFullBuild();
  33. checkCompiledFiles("ProcessorConsumer1","Advise_ccc.java","Advise_aaa.java","Code.java");
  34. assertEquals(2,getWeavingMessages("ProcessorConsumer1").size());
  35. String out = runMethod("ProcessorConsumer1", "Code", "runner");
  36. assertEquals("aaa running\nccc running\n",out);
  37. }
  38. // services file in processor project
  39. public void testAnnotationProcessing2() throws Exception {
  40. createAndBuildAnnotationProcessorProject("ProcessorProject2"); // This has a META-INF services entry for DemoProcessor
  41. initialiseProject("ProcessorConsumer2");
  42. // Paths here are the path to DemoProcessor (compiled into the output folder of the ProcessorProject2) and the path to
  43. // the META-INF file declaring DemoProcessor (since it is not copied to that same output folder) - this exists in the test src
  44. // folder for ProcessorProject2
  45. configureProcessorPath("ProcessorConsumer2", getCompilerForProjectWithName("ProcessorProject2").getCompilerConfiguration().getOutputLocationManager().getDefaultOutputLocation().toString()+File.pathSeparator+
  46. new File(testdataSrcDir + File.separatorChar + "ProcessorProject2" + File.separatorChar + "base"+File.separatorChar+"src").toString());
  47. Hashtable<String, String> javaOptions = new Hashtable<String, String>();
  48. javaOptions.put("org.eclipse.jdt.core.compiler.compliance", "1.6");
  49. javaOptions.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.6");
  50. javaOptions.put("org.eclipse.jdt.core.compiler.source", "1.6");
  51. javaOptions.put("org.eclipse.jdt.core.compiler.processAnnotations","enabled");
  52. configureJavaOptionsMap("ProcessorConsumer2", javaOptions);
  53. initialiseProject("ProcessorConsumer2");
  54. configureNewProjectDependency("ProcessorConsumer2", "ProcessorProject");
  55. configureNonStandardCompileOptions("ProcessorConsumer2", "-showWeaveInfo");
  56. build("ProcessorConsumer2");
  57. checkWasFullBuild();
  58. checkCompiledFiles("ProcessorConsumer2","Advise_ccc.java","Advise_aaa.java","Code.java");
  59. assertEquals(2,getWeavingMessages("ProcessorConsumer2").size());
  60. String out = runMethod("ProcessorConsumer2", "Code", "runner");
  61. assertEquals("aaa running\nccc running\n",out);
  62. }
  63. // Two processors
  64. public void testAnnotationProcessing3() throws Exception {
  65. createAndBuildAnnotationProcessorProject("ProcessorProject2");
  66. createAndBuildAnnotationProcessorProject("ProcessorProject3");
  67. initialiseProject("ProcessorConsumer1");
  68. // Paths here are the path to DemoProcessor/DemoProcessor2 compiled code and the path to
  69. // the META-INF file declaring DemoProcessor/DemoProcessor2 (since they are not copied to that same output folder) -
  70. // these exists in the test src folders for ProcessorProject2/ProcessorProject3
  71. configureProcessorPath("ProcessorConsumer1",
  72. getCompilerForProjectWithName("ProcessorProject3").getCompilerConfiguration().getOutputLocationManager().getDefaultOutputLocation().toString()+File.pathSeparator+
  73. new File(testdataSrcDir + File.separatorChar + "ProcessorProject3" + File.separatorChar + "base"+File.separatorChar+"src").toString()
  74. +File.pathSeparator+
  75. getCompilerForProjectWithName("ProcessorProject2").getCompilerConfiguration().getOutputLocationManager().getDefaultOutputLocation().toString()+File.pathSeparator+
  76. new File(testdataSrcDir + File.separatorChar + "ProcessorProject2" + File.separatorChar + "base"+File.separatorChar+"src").toString()
  77. );
  78. // The order here is DemoProcessor2 then DemoProcessor - to get the second one to run I changed DemoProcessor2 to operate on a
  79. // specific annotation (java.lang.SuppressWarnings) and return false at the end
  80. configureNonStandardCompileOptions("ProcessorConsumer1", "-showWeaveInfo");
  81. Hashtable<String, String> javaOptions = new Hashtable<String, String>();
  82. javaOptions.put("org.eclipse.jdt.core.compiler.compliance", "1.6");
  83. javaOptions.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.6");
  84. javaOptions.put("org.eclipse.jdt.core.compiler.source", "1.6");
  85. javaOptions.put("org.eclipse.jdt.core.compiler.processAnnotations","enabled");
  86. configureJavaOptionsMap("ProcessorConsumer1", javaOptions);
  87. configureNewProjectDependency("ProcessorConsumer1", "ProcessorProject");
  88. configureNonStandardCompileOptions("ProcessorConsumer1", "-showWeaveInfo");
  89. build("ProcessorConsumer1");
  90. checkWasFullBuild();
  91. checkCompiledFiles("ProcessorConsumer1","Advise_ccc.java","Advise_aaa.java","Code.java","AroundAdvise_ccc.java","AroundAdvise_aaa.java");
  92. assertEquals(4,getWeavingMessages("ProcessorConsumer1").size());
  93. String out = runMethod("ProcessorConsumer1", "Code", "runner");
  94. assertEquals("aaa running\nAround advice on aaa running\nccc running\nAround advice on ccc running\n",out);
  95. }
  96. // Tests:
  97. // TODO Incremental compilation - what does that mean with annotation processors?
  98. // ---
  99. private void createAndBuildAnnotationProcessorProject(String processorProjectName) {
  100. initialiseProject(processorProjectName);
  101. build(processorProjectName);
  102. checkWasFullBuild();
  103. assertNoErrors(processorProjectName);
  104. }
  105. private void configureProcessorOptions(String projectName, String processor) {
  106. configureProcessor(projectName, "DemoProcessor");
  107. // Assume all processors from processor project
  108. configureProcessorPath(projectName, getCompilerForProjectWithName("ProcessorProject").getCompilerConfiguration().getOutputLocationManager().getDefaultOutputLocation().toString());
  109. }
  110. private void checkCompiledFiles(String projectName, String... expectedCompiledFiles) {
  111. List<String> compiledFiles = new ArrayList<String>(getCompiledFiles(projectName));
  112. if (compiledFiles.size()!=expectedCompiledFiles.length) {
  113. fail("Expected #"+expectedCompiledFiles.length+" files to be compiled but found that #"+compiledFiles.size()+" files were compiled.\nCompiled="+compiledFiles);
  114. }
  115. for (String expectedCompiledFile: expectedCompiledFiles) {
  116. String toRemove = null;
  117. for (String compiledFile: compiledFiles) {
  118. String cfile = compiledFile.substring(compiledFile.lastIndexOf("/")+1);
  119. if (cfile.equals(expectedCompiledFile)) {
  120. toRemove = compiledFile;
  121. break;
  122. }
  123. }
  124. if (toRemove!=null) compiledFiles.remove(toRemove);
  125. }
  126. // Anything left in compiledFiles wasn't expected to be built
  127. if (compiledFiles.size()!=0) {
  128. fail("These were not expected to be compiled: "+compiledFiles);
  129. }
  130. }
  131. }