001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.store.test;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.test.util.RandomTestUtil;
019    import com.liferay.portal.kernel.util.SetUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.test.rule.ExpectedLog;
022    import com.liferay.portal.test.rule.ExpectedLogs;
023    import com.liferay.portal.test.rule.ExpectedType;
024    import com.liferay.portlet.documentlibrary.DuplicateFileException;
025    import com.liferay.portlet.documentlibrary.NoSuchFileException;
026    import com.liferay.portlet.documentlibrary.store.BaseStore;
027    import com.liferay.portlet.documentlibrary.store.Store;
028    
029    import java.io.BufferedInputStream;
030    import java.io.ByteArrayInputStream;
031    import java.io.File;
032    import java.io.FileInputStream;
033    import java.io.FileOutputStream;
034    import java.io.IOException;
035    import java.io.InputStream;
036    import java.io.OutputStream;
037    
038    import java.util.Arrays;
039    import java.util.Set;
040    
041    import org.junit.After;
042    import org.junit.Assert;
043    import org.junit.Before;
044    import org.junit.Test;
045    
046    /**
047     * @author Preston Crary
048     */
049    public abstract class BaseStoreTestCase {
050    
051            @Before
052            public void setUp() throws Exception {
053                    store = getStore();
054    
055                    companyId = RandomTestUtil.nextLong();
056                    repositoryId = RandomTestUtil.nextLong();
057            }
058    
059            @After
060            public void tearDown() throws Exception {
061                    store.deleteDirectory(companyId, repositoryId, StringPool.SLASH);
062            }
063    
064            @Test
065            public void testAddFileWithBufferedInputStream() throws Exception {
066                    String fileName = RandomTestUtil.randomString();
067    
068                    store.addFile(
069                            companyId, repositoryId, fileName,
070                            new BufferedInputStream(new ByteArrayInputStream(_DATA_VERSION_1)));
071    
072                    Assert.assertTrue(
073                            store.hasFile(
074                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT));
075            }
076    
077            @Test
078            public void testAddFileWithByteArray() throws Exception {
079                    String fileName = RandomTestUtil.randomString();
080    
081                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
082    
083                    Assert.assertTrue(
084                            store.hasFile(
085                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT));
086            }
087    
088            @Test
089            public void testAddFileWithByteArrayInputStream() throws Exception {
090                    String fileName = RandomTestUtil.randomString();
091    
092                    store.addFile(
093                            companyId, repositoryId, fileName,
094                            new ByteArrayInputStream(_DATA_VERSION_1));
095    
096                    Assert.assertTrue(
097                            store.hasFile(
098                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT));
099            }
100    
101            @Test
102            public void testAddFileWithFile() throws Exception {
103                    String fileName = RandomTestUtil.randomString();
104                    File file = createFile(_DATA_VERSION_1);
105    
106                    store.addFile(companyId, repositoryId, fileName, file);
107    
108                    Assert.assertTrue(
109                            store.hasFile(
110                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT));
111            }
112    
113            @Test
114            public void testAddFileWithFileInputStream() throws Exception {
115                    String fileName = RandomTestUtil.randomString();
116                    File file = createFile(_DATA_VERSION_1);
117    
118                    store.addFile(
119                            companyId, repositoryId, fileName, new FileInputStream(file));
120    
121                    Assert.assertTrue(
122                            store.hasFile(
123                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT));
124            }
125    
126            @Test
127            public void testAddFileWithUnsyncByteArrayInputStream() throws Exception {
128                    String fileName = RandomTestUtil.randomString();
129    
130                    store.addFile(
131                            companyId, repositoryId, fileName,
132                            new UnsyncByteArrayInputStream(_DATA_VERSION_1));
133    
134                    Assert.assertTrue(
135                            store.hasFile(
136                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT));
137            }
138    
139            @Test
140            public void testCopyFileVersion() throws Exception {
141                    String fileName = RandomTestUtil.randomString();
142    
143                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
144    
145                    addVersions(fileName, 1);
146    
147                    store.copyFileVersion(companyId, repositoryId, fileName, "1.0", "1.2");
148    
149                    Assert.assertTrue(
150                            store.hasFile(companyId, repositoryId, fileName, "1.2"));
151            }
152    
153            @Test(expected = DuplicateFileException.class)
154            public void testCopyFileVersionDuplicateFileException() throws Exception {
155                    String fileName = RandomTestUtil.randomString();
156    
157                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
158    
159                    addVersions(fileName, 1);
160    
161                    store.copyFileVersion(companyId, repositoryId, fileName, "1.0", "1.1");
162            }
163    
164            @Test(expected = NoSuchFileException.class)
165            public void testCopyFileVersionNoSuchFileException() throws Exception {
166                    store.copyFileVersion(
167                            companyId, repositoryId, RandomTestUtil.randomString(), "1.0",
168                            "1.1");
169            }
170    
171            @Test
172            public void testDeleteDirectory() throws Exception {
173                    String dirName = RandomTestUtil.randomString();
174    
175                    String fileName1 = dirName + "/" + RandomTestUtil.randomString();
176    
177                    store.addFile(companyId, repositoryId, fileName1, _DATA_VERSION_1);
178    
179                    String fileName2 = dirName + "/" + RandomTestUtil.randomString();
180    
181                    store.addFile(companyId, repositoryId, fileName2, _DATA_VERSION_1);
182    
183                    store.deleteDirectory(companyId, repositoryId, dirName);
184    
185                    Assert.assertFalse(
186                            store.hasFile(
187                                    companyId, repositoryId, fileName1, Store.VERSION_DEFAULT));
188                    Assert.assertFalse(
189                            store.hasFile(
190                                    companyId, repositoryId, fileName2, Store.VERSION_DEFAULT));
191            }
192    
193            @Test
194            public void testDeleteDirectoryWithTwoLevelDeep() throws Exception {
195                    String dirName = RandomTestUtil.randomString();
196    
197                    String subdirName = dirName + "/" + RandomTestUtil.randomString();
198    
199                    String fileName1 = dirName + "/" + RandomTestUtil.randomString();
200    
201                    store.addFile(companyId, repositoryId, fileName1, _DATA_VERSION_1);
202    
203                    String fileName2 = subdirName + "/" + RandomTestUtil.randomString();
204    
205                    store.addFile(companyId, repositoryId, fileName2, _DATA_VERSION_1);
206    
207                    store.deleteDirectory(companyId, repositoryId, dirName);
208    
209                    Assert.assertFalse(
210                            store.hasFile(
211                                    companyId, repositoryId, fileName1, Store.VERSION_DEFAULT));
212                    Assert.assertFalse(
213                            store.hasFile(
214                                    companyId, repositoryId, fileName2, Store.VERSION_DEFAULT));
215            }
216    
217            @Test
218            public void testDeleteFile() throws Exception {
219                    String fileName = RandomTestUtil.randomString();
220    
221                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
222    
223                    addVersions(fileName, 1);
224    
225                    store.deleteFile(companyId, repositoryId, fileName);
226    
227                    Assert.assertFalse(
228                            store.hasFile(
229                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT));
230                    Assert.assertFalse(
231                            store.hasFile(companyId, repositoryId, fileName, "1.1"));
232            }
233    
234            @Test
235            public void testDeleteFileWithVersion() throws Exception {
236                    String fileName = RandomTestUtil.randomString();
237    
238                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
239    
240                    addVersions(fileName, 1);
241    
242                    store.deleteFile(
243                            companyId, repositoryId, fileName, Store.VERSION_DEFAULT);
244    
245                    Assert.assertFalse(
246                            store.hasFile(
247                                    companyId, repositoryId, fileName, Store.VERSION_DEFAULT));
248                    Assert.assertTrue(
249                            store.hasFile(companyId, repositoryId, fileName, "1.1"));
250            }
251    
252            @Test(expected = NoSuchFileException.class)
253            public void testGetFileAsBytesNoSuchFileException() throws Exception {
254                    store.getFileAsBytes(
255                            companyId, repositoryId, RandomTestUtil.randomString());
256            }
257    
258            @Test
259            public void testGetFileAsStream() throws Exception {
260                    String fileName = RandomTestUtil.randomString();
261    
262                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
263    
264                    addVersions(fileName, 1);
265    
266                    try (InputStream inputStream = store.getFileAsStream(
267                            companyId, repositoryId, fileName)) {
268    
269                            for (int i = 0; i < _DATA_SIZE; i++) {
270                                    Assert.assertEquals(
271                                            _DATA_VERSION_1[i], (byte)inputStream.read());
272                            }
273    
274                            Assert.assertEquals(-1, inputStream.read());
275                    }
276            }
277    
278            @Test
279            public void testGetFileAsStreamWithVersion() throws Exception {
280                    String fileName = RandomTestUtil.randomString();
281    
282                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
283    
284                    addVersions(fileName, 5);
285    
286                    try (InputStream inputStream = store.getFileAsStream(
287                            companyId, repositoryId, fileName, "1.5")) {
288    
289                            for (int i = 0; i < _DATA_SIZE; i++) {
290                                    Assert.assertEquals(
291                                            _DATA_VERSION_1[i], (byte)inputStream.read());
292                            }
293    
294                            Assert.assertEquals(-1, inputStream.read());
295                    }
296            }
297    
298            @Test
299            public void testGetFileNames() throws Exception {
300                    String fileName1 = RandomTestUtil.randomString();
301    
302                    store.addFile(companyId, repositoryId, fileName1, _DATA_VERSION_1);
303    
304                    String fileName2 = RandomTestUtil.randomString();
305    
306                    store.addFile(companyId, repositoryId, fileName2, _DATA_VERSION_1);
307    
308                    String[] fileNames = store.getFileNames(companyId, repositoryId);
309    
310                    Assert.assertEquals(2, fileNames.length);
311    
312                    Set<String> fileNamesSet = SetUtil.fromArray(fileNames);
313    
314                    Assert.assertTrue(fileNamesSet.contains(fileName1));
315                    Assert.assertTrue(fileNamesSet.contains(fileName2));
316            }
317    
318            @Test
319            public void testGetFileNamesWithDirectoryOneLevelDeep() throws Exception {
320                    String dirName = RandomTestUtil.randomString();
321    
322                    String fileName1 = dirName + "/" + RandomTestUtil.randomString();
323    
324                    store.addFile(companyId, repositoryId, fileName1, _DATA_VERSION_1);
325    
326                    String fileName2 = dirName + "/" + RandomTestUtil.randomString();
327    
328                    store.addFile(companyId, repositoryId, fileName2, _DATA_VERSION_1);
329    
330                    String[] fileNames = store.getFileNames(
331                            companyId, repositoryId, dirName);
332    
333                    Assert.assertEquals(2, fileNames.length);
334    
335                    Set<String> fileNamesSet = SetUtil.fromArray(fileNames);
336    
337                    Assert.assertTrue(fileNamesSet.contains(fileName1));
338                    Assert.assertTrue(fileNamesSet.contains(fileName2));
339            }
340    
341            @Test
342            public void testGetFileNamesWithDirectoryTwoLevelDeep() throws Exception {
343                    String dirName = RandomTestUtil.randomString();
344    
345                    String subdirName = dirName + "/" + RandomTestUtil.randomString();
346    
347                    String fileName1 = dirName + "/" + RandomTestUtil.randomString();
348    
349                    store.addFile(companyId, repositoryId, fileName1, _DATA_VERSION_1);
350    
351                    String fileName2 = subdirName + "/" + RandomTestUtil.randomString();
352    
353                    store.addFile(companyId, repositoryId, fileName2, _DATA_VERSION_1);
354    
355                    String fileName3 =
356                            RandomTestUtil.randomString() + "/" + RandomTestUtil.randomString();
357    
358                    store.addFile(companyId, repositoryId, fileName3, _DATA_VERSION_1);
359    
360                    String[] fileNames = store.getFileNames(
361                            companyId, repositoryId, dirName);
362    
363                    Assert.assertEquals(2, fileNames.length);
364    
365                    Set<String> fileNamesSet = SetUtil.fromArray(fileNames);
366    
367                    Assert.assertTrue(fileNamesSet.contains(fileName1));
368                    Assert.assertTrue(fileNamesSet.contains(fileName2));
369    
370                    fileNames = store.getFileNames(companyId, repositoryId, subdirName);
371    
372                    Assert.assertEquals(1, fileNames.length);
373                    Assert.assertEquals(fileName2, fileNames[0]);
374            }
375    
376            @Test
377            public void testGetFileNamesWithInvalidDirectory() {
378                    String dirName = RandomTestUtil.randomString();
379    
380                    String[] fileNames = store.getFileNames(
381                            companyId, repositoryId, dirName);
382    
383                    Assert.assertEquals(0, fileNames.length);
384            }
385    
386            @Test
387            public void testGetFileNamesWithInvalidRepository() throws Exception {
388                    String[] fileNames = store.getFileNames(companyId, repositoryId);
389    
390                    Assert.assertEquals(0, fileNames.length);
391            }
392    
393            @Test
394            public void testGetFileNamesWithTwoLevelsDeep() throws Exception {
395                    String dirName = RandomTestUtil.randomString();
396    
397                    String subdirName = dirName + "/" + RandomTestUtil.randomString();
398    
399                    String fileName1 = dirName + "/" + RandomTestUtil.randomString();
400    
401                    store.addFile(companyId, repositoryId, fileName1, _DATA_VERSION_1);
402    
403                    String fileName2 = subdirName + "/" + RandomTestUtil.randomString();
404    
405                    store.addFile(companyId, repositoryId, fileName2, _DATA_VERSION_1);
406    
407                    String[] fileNames = store.getFileNames(companyId, repositoryId);
408    
409                    Assert.assertEquals(2, fileNames.length);
410    
411                    Set<String> fileNamesSet = SetUtil.fromArray(fileNames);
412    
413                    Assert.assertTrue(fileNamesSet.contains(fileName1));
414                    Assert.assertTrue(fileNamesSet.contains(fileName2));
415            }
416    
417            @Test
418            public void testGetFileSize() throws Exception {
419                    String fileName = RandomTestUtil.randomString();
420    
421                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
422    
423                    long size = store.getFileSize(companyId, repositoryId, fileName);
424    
425                    Assert.assertEquals(_DATA_SIZE, size);
426            }
427    
428            @Test(expected = NoSuchFileException.class)
429            public void testGetFileSizeNoSuchFileException() throws Exception {
430                    store.getFileSize(
431                            companyId, repositoryId, RandomTestUtil.randomString());
432            }
433    
434            @Test(expected = NoSuchFileException.class)
435            public void testGetFileVersionAsBytesNoSuchFileException()
436                    throws Exception {
437    
438                    String fileName = RandomTestUtil.randomString();
439    
440                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
441    
442                    store.getFileAsBytes(companyId, repositoryId, fileName, "1.1");
443            }
444    
445            @Test
446            public void testHasFile() throws Exception {
447                    String fileName = RandomTestUtil.randomString();
448    
449                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
450    
451                    Assert.assertTrue(store.hasFile(companyId, repositoryId, fileName));
452            }
453    
454            @Test
455            public void testHasFileWithVersion() throws Exception {
456                    String fileName = RandomTestUtil.randomString();
457    
458                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
459    
460                    addVersions(fileName, 5);
461    
462                    String versionLabel = "1.";
463    
464                    for (int i = 0; i < 5; i++) {
465                            Assert.assertTrue(
466                                    store.hasFile(
467                                            companyId, repositoryId, fileName, versionLabel + i));
468                    }
469            }
470    
471            @ExpectedLogs (
472                    expectedLogs = {
473                            @ExpectedLog (
474                                    expectedLog = "Unable to delete file {companyId=",
475                                    expectedType = ExpectedType.PREFIX
476                            )
477                    },
478                    level = "WARN", loggerClass = BaseStore.class
479            )
480            @Test
481            public void testLogFailedDeletion() {
482                    store.deleteFile(
483                            companyId, repositoryId, RandomTestUtil.randomString());
484            }
485    
486            @ExpectedLogs (
487                    expectedLogs = {
488                            @ExpectedLog (
489                                    expectedLog = "Unable to delete file {companyId=",
490                                    expectedType = ExpectedType.PREFIX
491                            )
492                    },
493                    level = "WARN", loggerClass = BaseStore.class
494            )
495            @Test
496            public void testLogFailedDeletionWithVersionLabel() {
497                    store.deleteFile(
498                            companyId, repositoryId, RandomTestUtil.randomString(),
499                            Store.VERSION_DEFAULT);
500            }
501    
502            @Test(expected = DuplicateFileException.class)
503            public void testUpdateFileVersionDuplicateFileException() throws Exception {
504                    String fileName = RandomTestUtil.randomString();
505    
506                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
507    
508                    store.updateFileVersion(
509                            companyId, repositoryId, fileName, "1.0", "1.0");
510            }
511    
512            @Test(expected = NoSuchFileException.class)
513            public void testUpdateFileVersionNoSuchFileException() throws Exception {
514                    store.updateFileVersion(
515                            companyId, repositoryId, RandomTestUtil.randomString(),
516                            Store.VERSION_DEFAULT, Store.VERSION_DEFAULT);
517            }
518    
519            @Test
520            public void testUpdateFileVersionWithNewFileName() throws Exception {
521                    String fileName = RandomTestUtil.randomString();
522    
523                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
524    
525                    addVersions(fileName, 2);
526    
527                    String newFileName = RandomTestUtil.randomString();
528    
529                    store.updateFile(companyId, repositoryId, fileName, newFileName);
530    
531                    Assert.assertFalse(store.hasFile(companyId, repositoryId, fileName));
532                    Assert.assertTrue(store.hasFile(companyId, repositoryId, newFileName));
533    
534                    Assert.assertTrue(
535                            store.hasFile(companyId, repositoryId, newFileName, "1.0"));
536                    Assert.assertTrue(
537                            store.hasFile(companyId, repositoryId, newFileName, "1.1"));
538                    Assert.assertTrue(
539                            store.hasFile(companyId, repositoryId, newFileName, "1.2"));
540            }
541    
542            @Test
543            public void testUpdateFileWithByteArray() throws Exception {
544                    String fileName = RandomTestUtil.randomString();
545    
546                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
547    
548                    store.updateFile(
549                            companyId, repositoryId, fileName, "1.1", _DATA_VERSION_2);
550    
551                    byte[] firstVersionBytes = store.getFileAsBytes(
552                            companyId, repositoryId, fileName, "1.0");
553    
554                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_1, firstVersionBytes));
555    
556                    byte[] secondVersionBytes = store.getFileAsBytes(
557                            companyId, repositoryId, fileName, "1.1");
558    
559                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_2, secondVersionBytes));
560    
561                    byte[] currentVersionBytes = store.getFileAsBytes(
562                            companyId, repositoryId, fileName);
563    
564                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_2, currentVersionBytes));
565            }
566    
567            @Test
568            public void testUpdateFileWithFile() throws Exception {
569                    String fileName = RandomTestUtil.randomString();
570    
571                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
572    
573                    File file = createFile(_DATA_VERSION_2);
574    
575                    store.updateFile(companyId, repositoryId, fileName, "1.1", file);
576    
577                    byte[] firstVersionBytes = store.getFileAsBytes(
578                            companyId, repositoryId, fileName, "1.0");
579    
580                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_1, firstVersionBytes));
581    
582                    byte[] secondVersionBytes = store.getFileAsBytes(
583                            companyId, repositoryId, fileName, "1.1");
584    
585                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_2, secondVersionBytes));
586    
587                    byte[] currentVersionBytes = store.getFileAsBytes(
588                            companyId, repositoryId, fileName);
589    
590                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_2, currentVersionBytes));
591            }
592    
593            @Test
594            public void testUpdateFileWithInputStream() throws Exception {
595                    String fileName = RandomTestUtil.randomString();
596    
597                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
598    
599                    store.updateFile(
600                            companyId, repositoryId, fileName, "1.1",
601                            new ByteArrayInputStream(_DATA_VERSION_2));
602    
603                    byte[] firstVersionBytes = store.getFileAsBytes(
604                            companyId, repositoryId, fileName, "1.0");
605    
606                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_1, firstVersionBytes));
607    
608                    byte[] secondVersionBytes = store.getFileAsBytes(
609                            companyId, repositoryId, fileName, "1.1");
610    
611                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_2, secondVersionBytes));
612    
613                    byte[] currentVersionBytes = store.getFileAsBytes(
614                            companyId, repositoryId, fileName);
615    
616                    Assert.assertTrue(Arrays.equals(_DATA_VERSION_2, currentVersionBytes));
617            }
618    
619            @Test
620            public void testUpdateFileWithNewFileName() throws Exception {
621                    String fileName = RandomTestUtil.randomString();
622    
623                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
624    
625                    String newFileName = RandomTestUtil.randomString();
626    
627                    store.updateFile(companyId, repositoryId, fileName, newFileName);
628    
629                    Assert.assertFalse(store.hasFile(companyId, repositoryId, fileName));
630                    Assert.assertTrue(store.hasFile(companyId, repositoryId, newFileName));
631            }
632    
633            @Test(expected = DuplicateFileException.class)
634            public void testUpdateFileWithNewFileNameDuplicateFileException()
635                    throws Exception {
636    
637                    String fileName = RandomTestUtil.randomString();
638    
639                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
640    
641                    store.updateFile(companyId, repositoryId, fileName, fileName);
642            }
643    
644            @Test(expected = NoSuchFileException.class)
645            public void testUpdateFileWithNewFileNameNoSuchFileException()
646                    throws Exception {
647    
648                    store.updateFile(
649                            companyId, repositoryId, RandomTestUtil.randomString(),
650                            RandomTestUtil.randomString());
651            }
652    
653            @Test
654            public void testUpdateFileWithNewRepositoryId() throws Exception {
655                    String fileName = RandomTestUtil.randomString();
656    
657                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
658    
659                    long newRepositoryId = RandomTestUtil.nextLong();
660    
661                    store.updateFile(companyId, repositoryId, newRepositoryId, fileName);
662    
663                    Assert.assertFalse(store.hasFile(companyId, repositoryId, fileName));
664                    Assert.assertTrue(store.hasFile(companyId, newRepositoryId, fileName));
665    
666                    store.deleteDirectory(companyId, newRepositoryId, StringPool.SLASH);
667            }
668    
669            @Test(expected = DuplicateFileException.class)
670            public void testUpdateFileWithNewRepositoryIdDuplicateFileException()
671                    throws Exception {
672    
673                    String fileName = RandomTestUtil.randomString();
674    
675                    store.addFile(companyId, repositoryId, fileName, _DATA_VERSION_1);
676    
677                    store.updateFile(companyId, repositoryId, repositoryId, fileName);
678            }
679    
680            @Test(expected = NoSuchFileException.class)
681            public void testUpdateFileWithNewRepositoryIdNoSuchFileException()
682                    throws Exception {
683    
684                    store.updateFile(
685                            companyId, repositoryId, RandomTestUtil.nextLong(),
686                            RandomTestUtil.randomString());
687            }
688    
689            protected void addVersions(String fileName, int newVersionCount)
690                    throws Exception {
691    
692                    String versionLabel = "1.";
693    
694                    for (int i = 1; i <= newVersionCount; i++) {
695                            store.updateFile(
696                                    companyId, repositoryId, fileName, versionLabel + i,
697                                    _DATA_VERSION_1);
698                    }
699            }
700    
701            protected File createFile(byte[] fileData) throws IOException {
702                    File file = File.createTempFile("DBStoreTest-testFile", null);
703    
704                    try (OutputStream outputStream = new FileOutputStream(file)) {
705                            outputStream.write(fileData);
706                    }
707    
708                    return file;
709            }
710    
711            protected abstract Store getStore();
712    
713            protected long companyId;
714            protected long repositoryId;
715            protected Store store;
716    
717            private static final int _DATA_SIZE = 1024 * 65;
718    
719            private static final byte[] _DATA_VERSION_1 = new byte[_DATA_SIZE];
720    
721            private static final byte[] _DATA_VERSION_2 = new byte[_DATA_SIZE];
722    
723            static {
724                    for (int i = 0; i < _DATA_SIZE; i++) {
725                            _DATA_VERSION_1[i] = (byte)i;
726                            _DATA_VERSION_2[i] = (byte)(i + 1);
727                    }
728            }
729    
730    }