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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 Common Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/cpl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.testing.util;
  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. /**
  21. *
  22. */
  23. public class TestUtilTest extends TestCase {
  24. public TestUtilTest(String name) {
  25. super(name);
  26. }
  27. public void testFileCompareNonClass() throws IOException {
  28. MessageHandler holder = new MessageHandler();
  29. File thisFile = new File(UtilTests.TESTING_UTIL_PATH + "/testsrc/org/aspectj/testing/util/TestUtilTest.java");
  30. //File thisFile = new File("src/testing-util.lst");
  31. assertTrue(TestUtil.sameFiles(holder, thisFile, thisFile));
  32. File tempFile = File.createTempFile("TestUtilTest", ".tmp");
  33. FileUtil.copyFile(thisFile, tempFile);
  34. long len = tempFile.length();
  35. assertTrue(0 != len);
  36. long tlen = thisFile.length();
  37. assertEquals(tlen, len);
  38. assertTrue(TestUtil.sameFiles(holder, tempFile, thisFile));
  39. try {
  40. String path = thisFile.getName();
  41. File basedir = tempFile.getParentFile();
  42. File renamed = new File(basedir, path);
  43. if (!tempFile.renameTo(renamed)) {
  44. MessageUtil.warn(holder, "unable to rename " + tempFile + " to " + renamed);
  45. } else {
  46. len = renamed.length();
  47. assertEquals(tlen, len);
  48. assertTrue(TestUtil.sameFiles(holder, basedir, thisFile.getParentFile(), path));
  49. }
  50. } finally {
  51. if (0 < holder.numMessages(null, true)) {
  52. MessageUtil.print(System.out, holder);
  53. holder.clearMessages();
  54. }
  55. tempFile.delete();
  56. }
  57. }
  58. public void testFileCompareNonClassStaticPositive() throws IOException {
  59. MessageHandler holder = new MessageHandler();
  60. File basedir = new File(UtilTests.TESTING_UTIL_PATH + "/testdata/testCompareTextFiles/sameFile");
  61. File expectedBaseDir = new File(basedir, "expected");
  62. File actualBaseDir = new File(basedir, "actual");
  63. String filename = "TestUtilTest.java";
  64. File expected = new File(expectedBaseDir, filename);
  65. File actual = new File(actualBaseDir, filename);
  66. assertTrue(TestUtil.sameFiles(holder, expected, actual));
  67. assertTrue(TestUtil.sameFiles(holder, expectedBaseDir, actualBaseDir, filename));
  68. }
  69. public void testFileCompareNonClassStaticNegative() throws IOException {
  70. MessageHandler holder = new MessageHandler();
  71. File basedir = new File("testdata/testCompareTextFiles/differentFile");
  72. File expectedBaseDir = new File(basedir, "expected");
  73. File actualBaseDir = new File(basedir, "actual");
  74. String filename = "TestUtilTest.java";
  75. File expected = new File(expectedBaseDir, filename);
  76. File actual = new File(actualBaseDir, filename);
  77. assertTrue(!TestUtil.sameFiles(holder, expected, actual));
  78. assertTrue(!TestUtil.sameFiles(holder, expectedBaseDir, actualBaseDir, filename));
  79. }
  80. public void testFileCompareClass() throws IOException {
  81. if (!TestUtil.ClassLineator.haveDisassembler()) {
  82. System.err.println("skipping testFileCompareClass - no disassembler on classpath");
  83. return;
  84. }
  85. MessageHandler holder = new MessageHandler();
  86. File classBase = new File(UtilTests.TESTING_UTIL_PATH + "/testdata/testCompareClassFiles");
  87. String path = "org/aspectj/testing/util/TestCompareClassFile.class";
  88. File classFile = new File(classBase, path);
  89. try {
  90. assertTrue(TestUtil.sameFiles(holder, classFile, classFile));
  91. assertTrue(TestUtil.sameFiles(holder, classBase, classBase, path));
  92. } finally {
  93. if (0 < holder.numMessages(null, true)) {
  94. MessageUtil.print(System.out, holder);
  95. }
  96. }
  97. }
  98. }