JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / test / java / com / threerings / getdown / data / PathBuilderTest.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.data;
7
8 import java.io.File;
9 import java.io.IOException;
10 import java.nio.file.Path;
11 import java.util.Arrays;
12
13 import org.junit.*;
14 import org.junit.rules.TemporaryFolder;
15 import org.junit.runner.RunWith;
16 import static org.junit.Assert.assertEquals;
17
18 import org.mockito.Mock;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import static org.mockito.Mockito.when;
21
22 @RunWith(MockitoJUnitRunner.class)
23 public class PathBuilderTest
24 {
25     @Before public void setupFilesAndResources () throws IOException
26     {
27         _firstJarFile = _appdir.newFile("a.jar");
28         _secondJarFile = _appdir.newFile("b.jar");
29
30         when(_firstJar.getFinalTarget()).thenReturn(_firstJarFile);
31         when(_secondJar.getFinalTarget()).thenReturn(_secondJarFile);
32         when(_application.getActiveCodeResources()).thenReturn(Arrays.asList(_firstJar, _secondJar));
33         when(_application.getAppDir()).thenReturn(_appdir.getRoot());
34     }
35
36     @Test public void shouldBuildDefaultClassPath () throws IOException
37     {
38         ClassPath classPath = PathBuilder.buildDefaultClassPath(_application);
39         String expectedClassPath = _firstJarFile.getAbsolutePath() + File.pathSeparator +
40             _secondJarFile.getAbsolutePath();
41         assertEquals(expectedClassPath, classPath.asArgumentString());
42     }
43
44     @Test public void shouldBuildCachedClassPath () throws IOException
45     {
46         when(_application.getDigest(_firstJar)).thenReturn("first");
47         when(_application.getDigest(_secondJar)).thenReturn("second");
48         when(_application.getCodeCacheRetentionDays()).thenReturn(1);
49
50         Path firstCachedJarFile = _appdir.getRoot().toPath().
51             resolve(PathBuilder.CODE_CACHE_DIR).resolve("fi").resolve("first.jar");
52
53         Path secondCachedJarFile = _appdir.getRoot().toPath().
54             resolve(PathBuilder.CODE_CACHE_DIR).resolve("se").resolve("second.jar");
55
56         String expectedClassPath = firstCachedJarFile.toAbsolutePath() + File.pathSeparator +
57             secondCachedJarFile.toAbsolutePath();
58
59         ClassPath classPath = PathBuilder.buildCachedClassPath(_application);
60         assertEquals(expectedClassPath, classPath.asArgumentString());
61     }
62
63     @Mock protected Application _application;
64     @Mock protected Resource _firstJar;
65     @Mock protected Resource _secondJar;
66
67     protected File _firstJarFile, _secondJarFile;
68
69     @Rule public TemporaryFolder _appdir = new TemporaryFolder();
70 }