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.

ResourceCopyTests.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /********************************************************************
  2. * Copyright (c) 2003 Contributors. All rights reserved.
  3. * This program and the accompanying materials are made available
  4. * 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. * Mik Kersten initial implementation
  10. * Helen Hawkins Converted to new interface (bug 148190)
  11. *******************************************************************/
  12. package org.aspectj.ajde.core.tests;
  13. import java.io.File;
  14. import java.io.FileFilter;
  15. import java.io.IOException;
  16. import java.util.Arrays;
  17. import java.util.HashSet;
  18. import java.util.List;
  19. import java.util.Set;
  20. import java.util.jar.JarInputStream;
  21. import java.util.zip.ZipEntry;
  22. import java.util.zip.ZipInputStream;
  23. import org.aspectj.ajde.core.AjdeCoreTestCase;
  24. import org.aspectj.ajde.core.TestCompilerConfiguration;
  25. import org.aspectj.ajde.core.TestMessageHandler;
  26. import org.aspectj.util.FileUtil;
  27. public class ResourceCopyTests extends AjdeCoreTestCase {
  28. public static final String PROJECT_DIR = "bug-36071a";
  29. public static final String srcDir = PROJECT_DIR + "/src";
  30. public static final String binDir = "bin";
  31. public static final String injar1Name = "input1.jar";
  32. public static final String injar2Name = "input2.jar";
  33. public static final String outjarName = "/bin/output.jar";
  34. private TestMessageHandler handler;
  35. private TestCompilerConfiguration compilerConfig;
  36. private String[] config1 = new String[] {
  37. "src" + File.separator + "Main.java",
  38. "src" + File.separator + "testsrc" + File.separator + "TestProperties.java"
  39. };
  40. private String[] config2 = new String[] {
  41. "src" + File.separator + "aspects" + File.separator + "Logging.java"
  42. };
  43. protected void setUp() throws Exception {
  44. super.setUp();
  45. initialiseProject(PROJECT_DIR);
  46. handler = (TestMessageHandler) getCompiler().getMessageHandler();
  47. compilerConfig = (TestCompilerConfiguration) getCompiler()
  48. .getCompilerConfiguration();
  49. }
  50. protected void tearDown() throws Exception {
  51. super.tearDown();
  52. handler = null;
  53. compilerConfig = null;
  54. }
  55. public void testSrcToBin () {
  56. assertTrue("Expected there to be no compiler messages but found " + handler.getMessages(), handler.getMessages().isEmpty());
  57. compilerConfig.setProjectSourceFiles(getSourceFileList(config1));
  58. doBuild(true);
  59. compareDirs("src","bin");
  60. }
  61. public void testInjarsToOutjar () {
  62. Set injars = new HashSet();
  63. File injar1 = openFile(injar1Name);
  64. injars.add(injar1);
  65. compilerConfig.setInpath(injars);
  66. File outjar = openFile(outjarName);
  67. compilerConfig.setOutjar(outjar.getAbsolutePath());
  68. compilerConfig.setProjectSourceFiles(getSourceFileList(config2));
  69. doBuild(true);
  70. assertTrue("Expected no compiler errors or warnings but found "
  71. + handler.getMessages(), handler.getMessages().isEmpty());
  72. compareJars(injar1,"src",outjar);
  73. }
  74. public void testDuplicateResources () {
  75. Set injars = new HashSet();
  76. File injar1 = openFile(injar1Name);
  77. File injar2 = openFile(injar2Name);
  78. injars.add(injar1);
  79. injars.add(injar2);
  80. compilerConfig.setInpath(injars);
  81. File outjar = openFile(outjarName);
  82. compilerConfig.setOutjar(outjar.getAbsolutePath());
  83. compilerConfig.setProjectSourceFiles(getSourceFileList(config2));
  84. doBuild(true);
  85. assertFalse("Expected compiler errors or warnings but didn't find any"
  86. , handler.getMessages().isEmpty());
  87. List msgs = handler.getMessages();
  88. String exp = "duplicate resource: ";
  89. String found = ((TestMessageHandler.TestMessage)msgs.get(0)).getContainedMessage().getMessage();
  90. assertTrue("Expected message to start with 'duplicate resource:' but found" +
  91. " message " + found, found.startsWith(exp));
  92. compareJars(injar1,"src",outjar);
  93. }
  94. public void testSrcToOutjar () {
  95. File outjar = openFile(outjarName);
  96. compilerConfig.setOutjar(outjar.getAbsolutePath());
  97. compilerConfig.setProjectSourceFiles(getSourceFileList(config1));
  98. doBuild(true);
  99. assertTrue("Expected no compiler errors or warnings but found "
  100. + handler.getMessages(), handler.getMessages().isEmpty());
  101. compareSourceToOutjar("src",outjar);
  102. }
  103. public void testInjarsToBin () {
  104. Set injars = new HashSet();
  105. File injar1 = openFile(injar1Name);
  106. injars.add(injar1);
  107. compilerConfig.setInpath(injars);
  108. compilerConfig.setProjectSourceFiles(getSourceFileList(config2));
  109. doBuild(true);
  110. assertTrue("Expected no compiler errors or warnings but found "
  111. + handler.getMessages(), handler.getMessages().isEmpty());
  112. compareInjarsToBin(injar1,"src","bin");
  113. }
  114. // BAH! keeps whinging about CVS extraneous resources
  115. // public void testInjarsToOddBin () {
  116. // Set injars = new HashSet();
  117. // File injar1 = openFile(injar1Name);
  118. // injars.add(injar1);
  119. // ideManager.getProjectProperties().setOutputPath("crazy.jar");
  120. // ideManager.getProjectProperties().setInJars(injars);
  121. // assertTrue("Build failed",doSynchronousBuild("config2.lst"));
  122. // assertTrue("Build warnings",ideManager.getCompilationSourceLineTasks().isEmpty());
  123. // compareInjarsToBin(injar1,"src","crazy.jar");
  124. // }
  125. public void testInjarsToOutjarOddNames () {
  126. Set injars = new HashSet();
  127. File injar1 = openFile("input1");
  128. File outjar = openFile(outjarName+".fozout");
  129. injars.add(injar1);
  130. compilerConfig.setInpath(injars);
  131. compilerConfig.setOutjar(outjar.getAbsolutePath());
  132. compilerConfig.setProjectSourceFiles(getSourceFileList(config2));
  133. doBuild(true);
  134. assertTrue("Expected no compiler errors or warnings but found "
  135. + handler.getMessages(), handler.getMessages().isEmpty());
  136. compareJars(injar1,"src",outjar);
  137. }
  138. /*
  139. * Ensure bin contains all non-Java resouces from source and injars
  140. */
  141. public void compareDirs (String indirName, String outdirName) {
  142. File binBase = openFile(outdirName);
  143. File[] toResources = FileUtil.listFiles(binBase,aspectjResourceFileFilter);
  144. HashSet resources = new HashSet();
  145. listSourceResources(indirName,resources);
  146. for (int i = 0; i < toResources.length; i++) {
  147. String fileName = FileUtil.normalizedPath(toResources[i],binBase);
  148. boolean b = resources.remove(fileName);
  149. assertTrue("Extraneous resources: " + fileName,b);
  150. }
  151. assertTrue("Missing resources: " + resources.toString(), resources.isEmpty());
  152. }
  153. private void listSourceResources (String indirName, Set resources) {
  154. File srcBase = openFile(indirName);
  155. File[] fromResources = FileUtil.listFiles(srcBase,aspectjResourceFileFilter);
  156. for (int i = 0; i < fromResources.length; i++) {
  157. String name = FileUtil.normalizedPath(fromResources[i],srcBase);
  158. if (!name.startsWith("CVS/") && (-1 == name.indexOf("/CVS/")) && !name.endsWith("/CVS")) {
  159. resources.add(name);
  160. }
  161. }
  162. }
  163. public static final FileFilter aspectjResourceFileFilter = new FileFilter() {
  164. public boolean accept(File pathname) {
  165. String name = pathname.getName().toLowerCase();
  166. boolean isCVSRelated = name.indexOf("/cvs/")!=-1;
  167. return (!isCVSRelated && !name.endsWith(".class") && !name.endsWith(".java") && !name.endsWith(".aj"));
  168. }
  169. };
  170. /*
  171. * Ensure -outjar contains all non-Java resouces from injars
  172. */
  173. public void compareJars (File injarFile, String indirName, File outjarFile) {
  174. HashSet resources = new HashSet();
  175. try {
  176. assertTrue(
  177. "outjar older than injar: outjarLastMod="+outjarFile.lastModified()+" injarLastMod="+injarFile.lastModified(),
  178. (outjarFile.lastModified() >= injarFile.lastModified()));
  179. byte[] inManifest = listJarResources(injarFile,resources,true);
  180. listSourceResources(indirName,resources);
  181. ZipInputStream outjar = new ZipInputStream(new java.io.FileInputStream(outjarFile));
  182. ZipEntry entry;
  183. while (null != (entry = outjar.getNextEntry())) {
  184. String fileName = entry.getName();
  185. if (!fileName.endsWith(".class")) {
  186. /* Ensure we copied right JAR manifest */
  187. if (fileName.equalsIgnoreCase("meta-inf/Manifest.mf")) {
  188. byte[] outManifest = FileUtil.readAsByteArray(outjar);
  189. assertTrue("Wrong manifest has been copied",Arrays.equals(inManifest,outManifest));
  190. }
  191. boolean b = resources.remove(fileName);
  192. assertTrue(fileName,b);
  193. }
  194. outjar.closeEntry();
  195. }
  196. outjar.close();
  197. resources.remove("META-INF/");
  198. assertTrue(resources.toString(),resources.isEmpty());
  199. }
  200. catch (IOException ex) {
  201. fail(ex.toString());
  202. }
  203. }
  204. /*
  205. * Ensure -outjar conatins all non-Java resouces from source and injars
  206. */
  207. public void compareSourceToOutjar (String indirName, File outjarFile) {
  208. HashSet resources = new HashSet();
  209. listSourceResources(indirName,resources);
  210. try {
  211. ZipInputStream outjar = new JarInputStream(new java.io.FileInputStream(outjarFile));
  212. ZipEntry entry;
  213. while (null != (entry = outjar.getNextEntry())) {
  214. String fileName = entry.getName();
  215. if (!fileName.endsWith(".class")) {
  216. boolean b = resources.remove(fileName);
  217. assertTrue(fileName,b);
  218. }
  219. outjar.closeEntry();
  220. }
  221. outjar.close();
  222. assertTrue("Missing resources: " + resources.toString(), resources.isEmpty());
  223. }
  224. catch (IOException ex) {
  225. fail(ex.toString());
  226. }
  227. }
  228. /*
  229. * Ensure bin contains all non-Java resouces from source and injars
  230. */
  231. public void compareInjarsToBin(File injarFile, String indirName, String outdirName) {
  232. HashSet resources = new HashSet();
  233. try {
  234. byte[] inManifest = listJarResources(injarFile,resources,false);
  235. listSourceResources(indirName,resources);
  236. File binBase = openFile(outdirName);
  237. File[] toResources = FileUtil.listFiles(binBase,aspectjResourceFileFilter);
  238. for (int i = 0; i < toResources.length; i++) {
  239. String fileName = FileUtil.normalizedPath(toResources[i],binBase);
  240. /* Ensure we copied the right JAR manifest */
  241. if (fileName.equalsIgnoreCase("meta-inf/Manifest.mf")) {
  242. byte[] outManifest = FileUtil.readAsByteArray(toResources[i]);
  243. assertTrue("Wrong manifest has been copied",Arrays.equals(inManifest,outManifest));
  244. }
  245. boolean b = resources.remove(fileName);
  246. assertTrue("Extraneous resources: " + fileName,b);
  247. }
  248. assertTrue("Missing resources: " + resources.toString(), resources.isEmpty());
  249. }
  250. catch (IOException ex) {
  251. fail(ex.toString());
  252. }
  253. }
  254. /**
  255. * Look in the specified jar file for resources (anything not .class) and add it the resources Set.
  256. *
  257. * @param injarFile jar file to open up
  258. * @param resources the set where resources should be accumulated
  259. * @param wantDirectories should any directories found in the jar be included
  260. * @return the byte data for any discovered manifest
  261. */
  262. private byte[] listJarResources(File injarFile, Set resources, boolean wantDirectories) {
  263. byte[] manifest = null;
  264. try {
  265. ZipInputStream injar = new ZipInputStream(new java.io.FileInputStream(injarFile));
  266. ZipEntry entry;
  267. while (null != (entry = injar.getNextEntry())) {
  268. String fileName = entry.getName();
  269. if (entry.isDirectory()) {
  270. if (wantDirectories) {
  271. resources.add(fileName);
  272. }
  273. } else if (!fileName.endsWith(".class")) {
  274. /* JAR manifests shouldn't be copied */
  275. if (fileName.equalsIgnoreCase("meta-inf/Manifest.mf")) {
  276. manifest = FileUtil.readAsByteArray(injar);
  277. }
  278. resources.add(fileName);
  279. }
  280. injar.closeEntry();
  281. }
  282. injar.close();
  283. }
  284. catch (IOException ex) {
  285. fail(ex.toString());
  286. }
  287. return manifest;
  288. }
  289. }