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