|
| 1 | +package org.apache.hadoop.fs; |
| 2 | + |
| 3 | +import org.apache.commons.lang3.RandomStringUtils; |
| 4 | +import org.apache.hadoop.conf.Configuration; |
| 5 | +import org.junit.After; |
| 6 | +import org.junit.AfterClass; |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.BeforeClass; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.util.Arrays; |
| 12 | + |
| 13 | +public class CosNFileSystemTestBase extends CosNFileSystemTestWithTimeout { |
| 14 | + protected static Configuration configuration; |
| 15 | + protected static FileSystem fs; |
| 16 | + |
| 17 | + protected static final Path unittestDirPath = new Path("/unittest-dir" + RandomStringUtils.randomAlphanumeric(8)); |
| 18 | + protected final Path testDirPath = new Path(unittestDirPath, "test-dir"); |
| 19 | + protected final Path testFilePath = new Path(unittestDirPath, "test-file"); |
| 20 | + |
| 21 | + @BeforeClass |
| 22 | + public static void beforeClass() throws IOException { |
| 23 | + String configFilePath = System.getProperty("config.file"); |
| 24 | + configuration = new Configuration(); |
| 25 | + // 初始化文件系统对象,因为 core-site.xml 是在 test 的 resource 下面,因此应该能够正确加载到。 |
| 26 | + if (configFilePath != null) { |
| 27 | + // 使用 addResource 方法加载配置文件 |
| 28 | + configuration.addResource(new Path(configFilePath)); |
| 29 | + } |
| 30 | + // 考虑到是针对 CosNFileSystem 测试,因此强制设置为 CosNFileSystem。 |
| 31 | + configuration.set("fs.cosn.impl", "org.apache.hadoop.fs.CosNFileSystem"); |
| 32 | + fs = FileSystem.get(configuration); |
| 33 | + |
| 34 | + if (null != fs && !fs.exists(unittestDirPath)) { |
| 35 | + fs.mkdirs(unittestDirPath); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @AfterClass |
| 40 | + public static void afterClass() throws IOException { |
| 41 | + if (null != fs && fs.exists(unittestDirPath)) { |
| 42 | + fs.delete(unittestDirPath, true); |
| 43 | + } |
| 44 | + if (null != fs) { |
| 45 | + fs.close(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Before |
| 50 | + public void before() throws IOException { |
| 51 | + if (!fs.exists(testDirPath)) { |
| 52 | + fs.mkdirs(testDirPath); |
| 53 | + } |
| 54 | + if (!fs.exists(testFilePath)) { |
| 55 | + try (FSDataOutputStream fsDataOutputStream = fs.create(testFilePath)) { |
| 56 | + fsDataOutputStream.write("Hello, World!".getBytes()); |
| 57 | + fsDataOutputStream.write("\n".getBytes()); |
| 58 | + fsDataOutputStream.write("Hello, COS!".getBytes()); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @After |
| 64 | + public void after() throws IOException { |
| 65 | + if (fs.exists(testFilePath)) { |
| 66 | + fs.delete(testFilePath, true); |
| 67 | + } |
| 68 | + if (fs.exists(testDirPath)) { |
| 69 | + fs.delete(testDirPath, true); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Return a path bonded to this method name, unique to this fork during |
| 75 | + * parallel execution. |
| 76 | + * |
| 77 | + * @return a method name unique to (fork, method). |
| 78 | + * @throws IOException IO problems |
| 79 | + */ |
| 80 | + protected Path methodPath() throws IOException { |
| 81 | + return new Path(unittestDirPath, methodName.getMethodName()); |
| 82 | + } |
| 83 | + |
| 84 | + /* |
| 85 | + * Helper method that creates test data of size provided by the |
| 86 | + * "size" parameter. |
| 87 | + */ |
| 88 | + protected static byte[] getTestData(int size) { |
| 89 | + byte[] testData = new byte[size]; |
| 90 | + System.arraycopy(RandomStringUtils.randomAlphabetic(size).getBytes(), 0, testData, 0, size); |
| 91 | + return testData; |
| 92 | + } |
| 93 | + |
| 94 | + // Helper method to create file and write fileSize bytes of data on it. |
| 95 | + protected byte[] createBaseFileWithData(int fileSize, Path testPath) throws Throwable { |
| 96 | + |
| 97 | + try (FSDataOutputStream createStream = fs.create(testPath)) { |
| 98 | + byte[] fileData = null; |
| 99 | + |
| 100 | + if (fileSize != 0) { |
| 101 | + fileData = getTestData(fileSize); |
| 102 | + createStream.write(fileData); |
| 103 | + } |
| 104 | + return fileData; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /* |
| 109 | + * Helper method to verify a testFile data. |
| 110 | + */ |
| 111 | + protected boolean verifyFile(byte[] testData, Path testFile) { |
| 112 | + |
| 113 | + try (FSDataInputStream srcStream = fs.open(testFile)) { |
| 114 | + |
| 115 | + int baseBufferSize = 2048; |
| 116 | + int testDataSize = testData.length; |
| 117 | + int testDataIndex = 0; |
| 118 | + |
| 119 | + while (testDataSize > baseBufferSize) { |
| 120 | + |
| 121 | + if (!verifyFileData(baseBufferSize, testData, testDataIndex, srcStream)) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + testDataIndex += baseBufferSize; |
| 125 | + testDataSize -= baseBufferSize; |
| 126 | + } |
| 127 | + |
| 128 | + if (!verifyFileData(testDataSize, testData, testDataIndex, srcStream)) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + |
| 132 | + return true; |
| 133 | + } catch (Exception ex) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /* |
| 139 | + * Helper method to verify a file data equal to "dataLength" parameter |
| 140 | + */ |
| 141 | + protected boolean verifyFileData(int dataLength, byte[] testData, int testDataIndex, FSDataInputStream srcStream) { |
| 142 | + |
| 143 | + try { |
| 144 | + |
| 145 | + byte[] fileBuffer = new byte[dataLength]; |
| 146 | + byte[] testDataBuffer = new byte[dataLength]; |
| 147 | + |
| 148 | + int fileBytesRead = srcStream.read(fileBuffer); |
| 149 | + |
| 150 | + if (fileBytesRead < dataLength) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + System.arraycopy(testData, testDataIndex, testDataBuffer, 0, dataLength); |
| 155 | + |
| 156 | + if (!Arrays.equals(fileBuffer, testDataBuffer)) { |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + return true; |
| 161 | + |
| 162 | + } catch (Exception ex) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + } |
| 167 | +} |
0 commit comments