PageRenderTime 29ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/files/src/test/java/org/jboss/test/capedwarf/files/test/FilesTest.java

https://github.com/capedwarf/capedwarf-blue
Java | 306 lines | 224 code | 50 blank | 32 comment | 5 complexity | 70a18ea8ba71b462e819ebb05dd1c5e7 MD5 | raw file
  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.test.capedwarf.files.test;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.FileNotFoundException;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.nio.ByteBuffer;
  28. import java.nio.channels.Channels;
  29. import com.google.appengine.api.NamespaceManager;
  30. import com.google.appengine.api.blobstore.BlobInfo;
  31. import com.google.appengine.api.blobstore.BlobInfoFactory;
  32. import com.google.appengine.api.blobstore.BlobKey;
  33. import com.google.appengine.api.files.AppEngineFile;
  34. import com.google.appengine.api.files.FileReadChannel;
  35. import com.google.appengine.api.files.FileService;
  36. import com.google.appengine.api.files.FileServiceFactory;
  37. import com.google.appengine.api.files.FileStat;
  38. import com.google.appengine.api.files.FileWriteChannel;
  39. import com.google.appengine.api.files.FinalizationException;
  40. import com.google.appengine.api.files.RecordWriteChannel;
  41. import org.jboss.arquillian.container.test.api.Deployment;
  42. import org.jboss.arquillian.junit.Arquillian;
  43. import org.jboss.shrinkwrap.api.Archive;
  44. import org.jboss.test.capedwarf.common.support.All;
  45. import org.jboss.test.capedwarf.common.test.TestBase;
  46. import org.junit.Assert;
  47. import org.junit.Before;
  48. import org.junit.Test;
  49. import org.junit.experimental.categories.Category;
  50. import org.junit.runner.RunWith;
  51. import static org.junit.Assert.assertEquals;
  52. import static org.junit.Assert.assertNotNull;
  53. import static org.junit.Assert.assertTrue;
  54. /**
  55. * @author <a href="mailto:ales.justin@jboss.org">Ales Justin</a>
  56. * @author <a href="mailto:marko.luksa@gmail.com">Marko Luksa</a>
  57. */
  58. @RunWith(Arquillian.class)
  59. @Category(All.class)
  60. public class FilesTest extends TestBase {
  61. private FileService service;
  62. @Before
  63. public void setUp() throws Exception {
  64. service = FileServiceFactory.getFileService();
  65. }
  66. @Deployment
  67. public static Archive getDeployment() {
  68. return getCapedwarfDeployment();
  69. }
  70. @Test
  71. public void testCreateNewBlobFile() throws Exception {
  72. AppEngineFile file = service.createNewBlobFile("image/jpeg", "created.jpg");
  73. assertEquals(AppEngineFile.FileSystem.BLOBSTORE, file.getFileSystem());
  74. }
  75. @Test
  76. public void testCloseWithoutWritingAnything() throws Exception {
  77. AppEngineFile file = service.createNewBlobFile("text/plain", "empty.txt");
  78. FileWriteChannel channel = service.openWriteChannel(file, true);
  79. channel.closeFinally();
  80. assertTrue(getFileContents(file).isEmpty());
  81. }
  82. @Test
  83. public void testSingleWrite() throws Exception {
  84. AppEngineFile file = service.createNewBlobFile("text/plain", "single.txt");
  85. writeToFileAndFinalize(file, "some-bytes");
  86. assertEquals("some-bytes", getFileContents(file));
  87. }
  88. @Test
  89. public void testAppend() throws Exception {
  90. AppEngineFile file = service.createNewBlobFile("text/plain", "append.txt");
  91. writeToFile(file, "some-bytes");
  92. writeToFileAndFinalize(file, " appended-bytes");
  93. assertEquals("some-bytes appended-bytes", getFileContents(file));
  94. }
  95. @Test
  96. public void testPosition() throws Exception {
  97. AppEngineFile file = service.createNewBlobFile("text/plain", "position.txt");
  98. writeToFileAndFinalize(file, "0123456789");
  99. FileReadChannel channel = service.openReadChannel(file, false);
  100. channel.position(5);
  101. assertEquals("567", getStringFromChannel(channel, 3));
  102. channel.position(2);
  103. assertEquals("234", getStringFromChannel(channel, 3));
  104. }
  105. @Test(expected = IOException.class)
  106. public void testFileNotFound() throws Exception {
  107. AppEngineFile nonExistentFile = new AppEngineFile(AppEngineFile.FileSystem.BLOBSTORE, "nonExistentFile.txt");
  108. FileReadChannel channel = service.openReadChannel(nonExistentFile, false); // appspot throws IOException here
  109. channel.read(ByteBuffer.allocate(1000)); // dev appserver throws exception here
  110. }
  111. @Test(expected = FinalizationException.class)
  112. public void testFileNotReadableUntilFinalized() throws Exception {
  113. AppEngineFile file = service.createNewBlobFile("text/plain", "notFinalized.txt");
  114. writeToFile(file, "some-bytes"); // NOTE: file is not finalized
  115. service.openReadChannel(file, false);
  116. }
  117. @Test
  118. public void testBlobKey() throws Exception {
  119. AppEngineFile file = service.createNewBlobFile("image/jpeg");
  120. writeToFileAndFinalize(file, "some-bytes");
  121. BlobKey blobKey = service.getBlobKey(file);
  122. AppEngineFile file2 = service.getBlobFile(blobKey);
  123. String contents = getFileContents(file2);
  124. assertEquals("some-bytes", contents);
  125. }
  126. @Test
  127. public void testBlobInfo() throws Exception {
  128. BlobInfoFactory blobInfoFactory = new BlobInfoFactory();
  129. AppEngineFile file = service.createNewBlobFile("text/plain", "blobInfo.txt");
  130. writeToFileAndFinalize(file, "some-bytes");
  131. BlobKey blobKey = service.getBlobKey(file);
  132. BlobInfo blobInfo = blobInfoFactory.loadBlobInfo(blobKey);
  133. assertEquals(blobKey, blobInfo.getBlobKey());
  134. assertEquals("text/plain", blobInfo.getContentType());
  135. assertEquals("blobInfo.txt", blobInfo.getFilename());
  136. assertEquals("some-bytes".length(), blobInfo.getSize());
  137. assertNotNull(blobInfo.getCreation());
  138. // TODO: test MD5 hash
  139. }
  140. @Test
  141. public void testRecordChannel() throws Exception {
  142. AppEngineFile file = service.createNewBlobFile("text/plain", "records.txt");
  143. RecordWriteChannel channel = service.openRecordWriteChannel(file, true);
  144. channel.closeFinally();
  145. assertTrue(getFileContents(file).isEmpty());
  146. }
  147. @Test
  148. public void testStats() throws Exception {
  149. AppEngineFile file = service.createNewBlobFile("text/plain", "records123.txt");
  150. writeToFileAndFinalize(file, "This is content.");
  151. FileStat stat = service.stat(file);
  152. Assert.assertNotNull(stat);
  153. Assert.assertTrue(stat.isFinalized());
  154. Assert.assertEquals(file.getFullPath(), stat.getFilename());
  155. Assert.assertTrue(stat.getLength() > 0);
  156. }
  157. @Test
  158. public void testStatsNotFinalized() throws Exception {
  159. AppEngineFile file = service.createNewBlobFile("text/plain", "records321.txt");
  160. writeToFile(file, "This is content.");
  161. try {
  162. service.stat(file);
  163. } catch (Exception e) {
  164. Assert.assertTrue(e instanceof FinalizationException);
  165. }
  166. }
  167. @Test
  168. public void testStatsFileNotFound() throws Exception {
  169. AppEngineFile file = service.createNewBlobFile("text/plain", "records456.txt");
  170. writeToFile(file, "This is content.");
  171. try {
  172. AppEngineFile tmp = new AppEngineFile(AppEngineFile.FileSystem.BLOBSTORE, "some-not-existing-path.txt");
  173. service.stat(tmp);
  174. } catch (Exception e) {
  175. Assert.assertTrue(e.getMessage(), e instanceof FileNotFoundException);
  176. }
  177. }
  178. @Test
  179. public void testWrite195() throws Exception {
  180. final String oldNamespace = NamespaceManager.get();
  181. NamespaceManager.set("Test");
  182. try {
  183. // Get a file service
  184. FileService fileService = FileServiceFactory.getFileService();
  185. String imageBase64 = "qwerty";
  186. // Create a new Blob file with mime-type "text/png"
  187. AppEngineFile file = fileService.createNewBlobFile("image/png");
  188. // Open a channel to write to it
  189. writeToFile(file, imageBase64, true);
  190. } finally {
  191. NamespaceManager.set(oldNamespace);
  192. }
  193. }
  194. @Test
  195. public void testLoop() throws Exception {
  196. String imageBase64 = "qwerty";
  197. FileService fileService = FileServiceFactory.getFileService();
  198. // Create a new Blob file with mime-type "text/png"
  199. AppEngineFile file = fileService.createNewBlobFile("image/png");
  200. // Open a channel to write to it
  201. writeToFile(file, imageBase64, true);
  202. //read the file to an inputstream
  203. AppEngineFile aeFile = fileService.getBlobFile(fileService.getBlobKey(file));
  204. FileReadChannel readChannel = fileService.openReadChannel(aeFile, false);
  205. InputStream is = Channels.newInputStream(readChannel);
  206. try {
  207. Assert.assertArrayEquals(imageBase64.getBytes(), toBytes(is));
  208. } finally {
  209. is.close();
  210. }
  211. }
  212. private void writeToFile(AppEngineFile file, String content) throws IOException {
  213. writeToFile(file, content, false);
  214. }
  215. private void writeToFileAndFinalize(AppEngineFile file, String content) throws IOException {
  216. writeToFile(file, content, true);
  217. }
  218. private void writeToFile(AppEngineFile file, String content, boolean finalize) throws IOException {
  219. FileWriteChannel channel = service.openWriteChannel(file, true);
  220. try {
  221. channel.write(ByteBuffer.wrap(content.getBytes()));
  222. } finally {
  223. if (finalize) {
  224. channel.closeFinally();
  225. } else {
  226. channel.close();
  227. }
  228. }
  229. }
  230. private String getFileContents(AppEngineFile file) throws IOException {
  231. FileReadChannel channel = service.openReadChannel(file, true);
  232. try {
  233. return getStringFromChannel(channel, 1000);
  234. } finally {
  235. channel.close();
  236. }
  237. }
  238. private String getStringFromChannel(FileReadChannel channel, int length) throws IOException {
  239. ByteBuffer buffer = ByteBuffer.allocate(length);
  240. int bytesRead = channel.read(buffer);
  241. byte[] bytes = new byte[bytesRead == -1 ? 0 : bytesRead];
  242. buffer.flip();
  243. buffer.get(bytes);
  244. return new String(bytes);
  245. }
  246. private static byte[] toBytes(InputStream is) throws IOException {
  247. ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  248. int nRead;
  249. byte[] data = new byte[16384];
  250. while ((nRead = is.read(data, 0, data.length)) != -1) {
  251. buffer.write(data, 0, nRead);
  252. }
  253. buffer.flush();
  254. return buffer.toByteArray();
  255. }
  256. }