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.

UtilsTest.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* *******************************************************************
  2. * Copyright (c) 2006 Contributors.
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Wes Isberg initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.internal.tools.build;
  13. import java.io.IOException;
  14. import java.util.jar.Attributes.Name;
  15. import junit.framework.TestCase;
  16. import org.aspectj.internal.tools.build.Util.OSGIBundle;
  17. import org.aspectj.internal.tools.build.Util.OSGIBundle.RequiredBundle;
  18. public class UtilsTest extends TestCase {
  19. private static final String PREFIX = UtilsTest.class.getName().replace('.',
  20. '/');
  21. private static final ClassLoader LOADER = UtilsTest.class.getClassLoader();
  22. private static final ManifestTest[] TESTS = {
  23. new ManifestTest("01",
  24. new Name[] { OSGIBundle.BUNDLE_CLASSPATH,
  25. OSGIBundle.BUNDLE_SYMBOLIC_NAME,
  26. OSGIBundle.BUNDLE_VERSION }, new String[] {
  27. "lib/commons/commons.jar",
  28. "org.aspectj.testing.client", "1.0.0" }),
  29. new ManifestTest("02",
  30. new Name[] { OSGIBundle.BUNDLE_SYMBOLIC_NAME,
  31. OSGIBundle.BUNDLE_VERSION }, new String[] {
  32. "org.aspectj.testing.client", "1.0.0" },
  33. new String[] { "lib/commons/commons.jar",
  34. "lib/ant/lib/ant.jar" }, new String[] { "util",
  35. "org.aspectj.runtime" }),
  36. new ManifestTest("03",
  37. new Name[] { OSGIBundle.BUNDLE_SYMBOLIC_NAME,
  38. OSGIBundle.BUNDLE_VERSION }, new String[] {
  39. "org.aspectj.testing.client", "1.0.0" },
  40. new String[] { "lib/commons/commons.jar",
  41. "lib/ant/lib/ant.jar" }, new String[] { "util",
  42. "aspectjrt" }) {
  43. void checkOthers(OSGIBundle osgiBundle, StringBuffer sb) {
  44. RequiredBundle[] bundles = osgiBundle.getRequiredBundles();
  45. for (RequiredBundle bundle : bundles) {
  46. if ("aspectjrt".equals(bundle.name)) {
  47. if (!bundle.optional) {
  48. sb
  49. .append("expected required bundle aspectjrt to be optional");
  50. }
  51. String version = "[1.5.0,1.5.5]";
  52. if (!(version.equals(bundle.versions))) {
  53. sb.append("expected version " + version
  54. + " got " + bundle.versions
  55. + " for required bundle aspectjrt");
  56. }
  57. }
  58. }
  59. }
  60. } };
  61. private static class ManifestTest {
  62. final String name;
  63. final Name[] expectedNames;
  64. final String[] expectedValues;
  65. final String[] classpathEntries;
  66. final String[] requiredBundleNames;
  67. ManifestTest(String name, Name[] expectedNames, String[] expectedValues) {
  68. this(name, expectedNames, expectedValues, null, null);
  69. }
  70. ManifestTest(String name, Name[] expectedNames,
  71. String[] expectedValues, String[] classpathEntries,
  72. String[] requiredBundleNames) {
  73. this.name = name;
  74. this.expectedNames = expectedNames;
  75. this.expectedValues = expectedValues;
  76. this.classpathEntries = classpathEntries;
  77. this.requiredBundleNames = requiredBundleNames;
  78. }
  79. void run(StringBuffer sb) throws IOException {
  80. String path = PREFIX + "." + name + ".MF";
  81. OSGIBundle bundle = new OSGIBundle(LOADER.getResourceAsStream(path));
  82. int len = sb.length();
  83. checkNamesAndValues(bundle, sb);
  84. checkOthers(bundle, sb);
  85. if (sb.length() != len) {
  86. sb.append("failure was in test " + name);
  87. }
  88. }
  89. void checkOthers(OSGIBundle bundle, StringBuffer sb) {
  90. }
  91. void checkNamesAndValues(OSGIBundle bundle, StringBuffer sb) {
  92. for (int i = 0; i < expectedNames.length; i++) {
  93. Name name = expectedNames[i];
  94. String expected = expectedValues[i];
  95. String actual = bundle.getAttribute(expectedNames[i]);
  96. if (!((expected == actual) || expected.equals(actual))) {
  97. sb.append(name);
  98. sb.append(" ");
  99. sb.append("expected ");
  100. sb.append(expected);
  101. sb.append("actual ");
  102. sb.append(actual);
  103. sb.append("\n");
  104. }
  105. }
  106. if (null != classpathEntries) {
  107. String[] cp = bundle.getClasspath();
  108. Util.reportMemberDiffs(classpathEntries, cp, sb);
  109. }
  110. if (null != requiredBundleNames) {
  111. RequiredBundle[] bundles = bundle.getRequiredBundles();
  112. String[] names = new String[bundles.length];
  113. for (int i = 0; i < names.length; i++) {
  114. names[i] = bundles[i].name;
  115. }
  116. Util.reportMemberDiffs(requiredBundleNames, names, sb);
  117. }
  118. }
  119. }
  120. /** disabled pending research */
  121. public void skip_testOSGIManifests() throws Exception {
  122. StringBuffer sb = new StringBuffer();
  123. for (ManifestTest test : TESTS) {
  124. test.run(sb);
  125. }
  126. if (0 < sb.length()) {
  127. fail(sb.toString());
  128. }
  129. }
  130. public void testReportMemberDiffs() {
  131. StringBuffer sb = new StringBuffer();
  132. String[] exp = null;
  133. String[] act = null;
  134. assertFalse(Util.reportMemberDiffs(exp, act, sb));
  135. assertEquals("", sb.toString());
  136. sb.setLength(0);
  137. exp = new String[] { "" };
  138. act = null;
  139. assertTrue(Util.reportMemberDiffs(exp, act, sb));
  140. assertEquals("unexpected [] missing [\"\"]", sb.toString());
  141. sb.setLength(0);
  142. exp = null;
  143. act = new String[] { "" };
  144. assertTrue(Util.reportMemberDiffs(exp, act, sb));
  145. assertEquals("unexpected [\"\"] missing []", sb.toString());
  146. sb.setLength(0);
  147. exp = new String[] { "1", "2", "3" };
  148. act = new String[] { "2", "4" };
  149. assertTrue(Util.reportMemberDiffs(exp, act, sb));
  150. assertEquals("unexpected [\"4\"] missing [\"1\", \"3\"]", sb.toString());
  151. }
  152. // public void testResourceStream() throws Exception {
  153. // String path = PREFIX + ".one.mf";
  154. // System.out.println(path);
  155. // InputStream in = LOADER.getResourceAsStream(path);
  156. // int i;
  157. // while (-1 != (i = in.read())) {
  158. // System.out.print((char) i);
  159. // }
  160. // System.out.println();
  161. // }
  162. // Map map = bundle.manifest.getEntries();
  163. // for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) {
  164. // MapEntry entry = (MapEntry) iter.next();
  165. // System.out.println("entry: " + entry);
  166. // }
  167. // System.out.println("main attributes");
  168. // Attributes attributes = bundle.manifest.getMainAttributes();
  169. // Set keys = attributes.keySet();
  170. // for (Iterator iter = keys.iterator(); iter.hasNext();) {
  171. // Object key = iter.next();
  172. // System.out.println(" key " + key);
  173. // System.out.println(" value " + attributes.getValue(key.toString()));
  174. // }
  175. }