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