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.

TestUtilTest.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testingutil;
  14. import java.io.File;
  15. import java.io.IOException;
  16. import junit.framework.TestCase;
  17. import org.aspectj.bridge.MessageHandler;
  18. import org.aspectj.bridge.MessageUtil;
  19. import org.aspectj.util.FileUtil;
  20. import org.aspectj.testing.util.TestUtil;
  21. /**
  22. *
  23. */
  24. public class TestUtilTest extends TestCase {
  25. public TestUtilTest(String name) {
  26. super(name);
  27. }
  28. public void testFileCompareNonClass() throws IOException {
  29. MessageHandler holder = new MessageHandler();
  30. File thisFile = new File(UtilTests.TESTING_UTIL_PATH + "/src/test/java/org/aspectj/testingutil/TestUtilTest.java");
  31. //File thisFile = new File("src/testing-util.lst");
  32. assertTrue(TestUtil.sameFiles(holder, thisFile, thisFile));
  33. File tempFile = File.createTempFile("TestUtilTest", ".tmp");
  34. FileUtil.copyFile(thisFile, tempFile);
  35. long len = tempFile.length();
  36. assertTrue(0 != len);
  37. long tlen = thisFile.length();
  38. assertEquals(tlen, len);
  39. assertTrue(TestUtil.sameFiles(holder, tempFile, thisFile));
  40. try {
  41. String path = thisFile.getName();
  42. File basedir = tempFile.getParentFile();
  43. File renamed = new File(basedir, path);
  44. if (!tempFile.renameTo(renamed)) {
  45. MessageUtil.warn(holder, "unable to rename " + tempFile + " to " + renamed);
  46. } else {
  47. len = renamed.length();
  48. assertEquals(tlen, len);
  49. assertTrue(TestUtil.sameFiles(holder, basedir, thisFile.getParentFile(), path));
  50. }
  51. } finally {
  52. if (0 < holder.numMessages(null, true)) {
  53. MessageUtil.print(System.out, holder);
  54. holder.clearMessages();
  55. }
  56. tempFile.delete();
  57. }
  58. }
  59. public void testFileCompareNonClassStaticPositive() throws IOException {
  60. MessageHandler holder = new MessageHandler();
  61. File basedir = new File(UtilTests.TESTING_UTIL_PATH + "/testdata/testCompareTextFiles/sameFile");
  62. File expectedBaseDir = new File(basedir, "expected");
  63. File actualBaseDir = new File(basedir, "actual");
  64. String filename = "TestUtilTest.java";
  65. File expected = new File(expectedBaseDir, filename);
  66. File actual = new File(actualBaseDir, filename);
  67. assertTrue(TestUtil.sameFiles(holder, expected, actual));
  68. assertTrue(TestUtil.sameFiles(holder, expectedBaseDir, actualBaseDir, filename));
  69. }
  70. public void testFileCompareNonClassStaticNegative() throws IOException {
  71. MessageHandler holder = new MessageHandler();
  72. File basedir = new File("testdata/testCompareTextFiles/differentFile");
  73. File expectedBaseDir = new File(basedir, "expected");
  74. File actualBaseDir = new File(basedir, "actual");
  75. String filename = "TestUtilTest.java";
  76. File expected = new File(expectedBaseDir, filename);
  77. File actual = new File(actualBaseDir, filename);
  78. assertTrue(!TestUtil.sameFiles(holder, expected, actual));
  79. assertTrue(!TestUtil.sameFiles(holder, expectedBaseDir, actualBaseDir, filename));
  80. }
  81. public void testParseBoolean() {
  82. {
  83. String[] trues = {"true", "TRUE", "on", "ON" };
  84. for (String aTrue : trues) {
  85. assertTrue(aTrue, TestUtil.parseBoolean(aTrue));
  86. }
  87. }
  88. {
  89. String[] falses = {"false", "FALSE", "off", "off" };
  90. for (String fals : falses) {
  91. assertTrue(fals, !TestUtil.parseBoolean(fals));
  92. }
  93. }
  94. String[] errors = {"fals", "tru", "T", "on of" };
  95. boolean fail = false;
  96. final int MAX = errors.length-1;
  97. for (int i = 0; i <= MAX; i++) {
  98. try {
  99. TestUtil.parseBoolean(errors[i], fail);
  100. assertTrue("no exception: " + errors[i], !fail);
  101. } catch (IllegalArgumentException e) {
  102. assertTrue("exception: " + errors[i], fail);
  103. String m = e.getMessage();
  104. if (!m.contains(errors[i])) {
  105. fail(errors[i] + " not in " + m);
  106. }
  107. }
  108. if ((i == MAX) && !fail) {
  109. i = -1;
  110. fail = true;
  111. }
  112. }
  113. }
  114. public void testFileCompareClass() throws IOException {
  115. if (!TestUtil.ClassLineator.haveDisassembler()) {
  116. System.err.println("skipping testFileCompareClass - no disassembler on classpath");
  117. return;
  118. }
  119. MessageHandler holder = new MessageHandler();
  120. File classBase = new File(UtilTests.TESTING_UTIL_PATH + "/testdata/testCompareClassFiles");
  121. String path = "org/aspectj/testingutil/TestCompareClassFile.class";
  122. File classFile = new File(classBase, path);
  123. try {
  124. assertTrue(TestUtil.sameFiles(holder, classFile, classFile));
  125. assertTrue(TestUtil.sameFiles(holder, classBase, classBase, path));
  126. } finally {
  127. if (0 < holder.numMessages(null, true)) {
  128. MessageUtil.print(System.out, holder);
  129. }
  130. }
  131. }
  132. }