1 package org.exoplatform.commons.file.services;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Date;
8 import java.util.concurrent.ExecutorService;
9 import java.util.concurrent.Executors;
10 import java.util.concurrent.atomic.AtomicInteger;
11
12 import org.apache.commons.io.IOUtils;
13
14 import org.exoplatform.commons.file.model.FileItem;
15 import org.exoplatform.commons.file.resource.BinaryProvider;
16 import org.exoplatform.commons.testing.BaseExoTestCase;
17 import org.exoplatform.component.test.ConfigurationUnit;
18 import org.exoplatform.component.test.ConfiguredBy;
19 import org.exoplatform.component.test.ContainerScope;
20
21
22
23
24 @ConfiguredBy({ @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/portal/files-configuration.xml"),
25 @ConfigurationUnit(scope = ContainerScope.PORTAL, path = "conf/standalone/test-configuration.xml") })
26 public class FileServiceImplIntegrationTest extends BaseExoTestCase {
27
28 private ExecutorService executorService = Executors.newFixedThreadPool(3);
29
30 private AtomicInteger counter = new AtomicInteger(0);
31
32 protected void setUp() throws IOException {
33 begin();
34 }
35
36 protected void tearDown() {
37 end();
38 }
39
40 public void testShouldReturnFile() throws Exception {
41 FileService fileService = getContainer().getComponentInstanceOfType(FileService.class);
42 FileItem createdFile = fileService.writeFile(new FileItem(null,
43 "file1",
44 "plain/text",
45 null,
46 1,
47 new Date(),
48 "john",
49 false,
50 new ByteArrayInputStream("test".getBytes())));
51 FileItem fetchedFile = fileService.getFile(createdFile.getFileInfo().getId());
52 assertNotNull(fetchedFile);
53 assertEquals("file1", fetchedFile.getFileInfo().getName());
54 assertEquals("plain/text", fetchedFile.getFileInfo().getMimetype());
55 assertEquals("john", fetchedFile.getFileInfo().getUpdater());
56 assertEquals(false, fetchedFile.getFileInfo().isDeleted());
57 assertEquals(1, fetchedFile.getFileInfo().getSize());
58 assertEquals("file", fetchedFile.getFileInfo().getNameSpace());
59 InputStream fileStream = fetchedFile.getAsStream();
60 assertNotNull(fileStream);
61 assertEquals("test", IOUtils.toString(fileStream));
62 }
63
64 public void testUpdateFile() throws Exception {
65 FileService fileService = getContainer().getComponentInstanceOfType(FileService.class);
66 FileItem createdFile = fileService.writeFile(new FileItem(null,
67 "file1",
68 "plain/text",
69 "file",
70 1,
71 new Date(),
72 "john",
73 false,
74 new ByteArrayInputStream("test".getBytes())));
75 FileItem fetchedFile = fileService.getFile(createdFile.getFileInfo().getId());
76 assertNotNull(fetchedFile);
77
78 FileItem updatedSameFile = fileService.updateFile(new FileItem(fetchedFile.getFileInfo().getId(),
79 "file1",
80 "plain/text",
81 "file",
82 1,
83 new Date(),
84 "john",
85 false,
86 new ByteArrayInputStream("test".getBytes())));
87 assertNotNull(updatedSameFile);
88 assertEquals(fetchedFile.getFileInfo().getChecksum(), updatedSameFile.getFileInfo().getChecksum());
89
90 FileItem updatedNewFile =
91 fileService.updateFile(new FileItem(fetchedFile.getFileInfo().getId(),
92 "file1",
93 "plain/text",
94 "file",
95 1,
96 new Date(),
97 "john",
98 false,
99 new ByteArrayInputStream("New test".getBytes())));
100 assertNotNull(updatedNewFile);
101 assertNotSame(fetchedFile.getFileInfo().getChecksum(), updatedNewFile.getFileInfo().getChecksum());
102 }
103
104 public void testConcurrentAddFile() throws Exception {
105 FileService fileService = getContainer().getComponentInstanceOfType(FileService.class);
106 BinaryProvider binaryProvider = getContainer().getComponentInstanceOfType(BinaryProvider.class);
107 String text = "Concurrent add test" + System.currentTimeMillis();
108 FileItem fileItem = fileService.writeFile(new FileItem(null,
109 "file1",
110 "plain/text",
111 "file",
112 1,
113 new Date(),
114 "john",
115 false,
116 new ByteArrayInputStream(text.getBytes())));
117 for (int i = 0; i < 10; i++) {
118 executorService.execute(new Runnable() {
119 @Override
120 public void run() {
121 try {
122 fileService.writeFile(new FileItem(null,
123 "file1",
124 "plain/text",
125 "file",
126 1,
127 new Date(),
128 "john",
129 false,
130 new ByteArrayInputStream(text.getBytes())));
131 } catch (Exception e) {
132 fail("Error while adding File: " + e.getMessage());
133 } finally {
134 counter.incrementAndGet();
135 }
136 }
137 });
138 }
139 do {
140 Thread.sleep(100);
141 } while (counter.get() < 10);
142
143 File file = new File(binaryProvider.getFilePath(fileItem.getFileInfo().getChecksum()));
144 assertEquals(1, file.getParentFile().list().length);
145 }
146
147 public void testConcurrentUpdateFile() throws Exception {
148 FileService fileService = getContainer().getComponentInstanceOfType(FileService.class);
149 BinaryProvider binaryProvider = getContainer().getComponentInstanceOfType(BinaryProvider.class);
150 String text = "Concurrent update test" + System.currentTimeMillis();
151 FileItem fileItem = fileService.writeFile(new FileItem(null,
152 "file1",
153 "plain/text",
154 "file",
155 1,
156 new Date(),
157 "john",
158 false,
159 new ByteArrayInputStream(text.getBytes())));
160 for (int i = 0; i < 10; i++) {
161 executorService.execute(new Runnable() {
162 @Override
163 public void run() {
164 try {
165 fileService.updateFile(new FileItem(fileItem.getFileInfo().getId(),
166 "file1",
167 "plain/text",
168 "file",
169 1,
170 new Date(),
171 "john",
172 false,
173 new ByteArrayInputStream(text.getBytes())));
174 } catch (Exception e) {
175 fail("Error while adding File: " + e.getMessage());
176 } finally {
177 counter.incrementAndGet();
178 }
179 }
180 });
181 }
182 do {
183 Thread.sleep(100);
184 } while (counter.get() < 10);
185
186 File file = new File(binaryProvider.getFilePath(fileItem.getFileInfo().getChecksum()));
187 assertEquals(1, file.getParentFile().list().length);
188 }
189 }