-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/cloudstore/cloudstore/issues/50
Added test for CloudStoreUpdater.
- Loading branch information
Showing
2 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...tore.updater/src/test/java/co/codewizards/cloudstore/updater/AbstractTestWithTempDir.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package co.codewizards.cloudstore.updater; | ||
|
||
import static co.codewizards.cloudstore.core.oio.OioFileFactory.*; | ||
import static co.codewizards.cloudstore.core.util.IOUtil.*; | ||
import static co.codewizards.cloudstore.core.util.StringUtil.*; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.net.URL; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
|
||
import co.codewizards.cloudstore.core.oio.File; | ||
|
||
public abstract class AbstractTestWithTempDir { | ||
|
||
protected File tempDir; | ||
|
||
@Before | ||
public void before() throws Exception { | ||
tempDir = createTempDirectory("cloudstore-test-"); | ||
} | ||
|
||
@After | ||
public void after() throws Exception { | ||
File td = tempDir; | ||
tempDir = null; | ||
|
||
if (td != null) | ||
td.deleteRecursively(); | ||
} | ||
|
||
protected File downloadFileToTempDir(String urlStr) throws IOException { | ||
long startTimestamp = System.currentTimeMillis(); | ||
URL url = new URL(urlStr); | ||
|
||
String fileName = url.getPath(); | ||
int lastSlash = fileName.lastIndexOf('/'); | ||
if (lastSlash < 0) | ||
throw new IllegalArgumentException("urlStr's path does not contain a '/': " + urlStr); | ||
|
||
fileName = fileName.substring(lastSlash + 1); | ||
if (isEmpty(fileName)) | ||
throw new IllegalArgumentException("urlStr's path ends on '/': " + urlStr); | ||
|
||
File file = tempDir.createFile(fileName); | ||
try (InputStream in = url.openStream();) { | ||
try (OutputStream out = file.createOutputStream();) { | ||
transferStreamData(in, out); | ||
} | ||
} | ||
System.out.println("Download took " + (System.currentTimeMillis() - startTimestamp) + " ms: " + urlStr); | ||
return file; | ||
} | ||
|
||
} |
133 changes: 133 additions & 0 deletions
133
...dstore.updater/src/test/java/co/codewizards/cloudstore/updater/CloudStoreUpdaterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package co.codewizards.cloudstore.updater; | ||
|
||
import static co.codewizards.cloudstore.core.util.AssertUtil.*; | ||
import static org.assertj.core.api.Assertions.*; | ||
|
||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import co.codewizards.cloudstore.core.oio.File; | ||
import co.codewizards.cloudstore.core.updater.CloudStoreUpdaterCore; | ||
import co.codewizards.cloudstore.core.updater.Version; | ||
import co.codewizards.cloudstore.core.util.PropertiesUtil; | ||
import mockit.Invocation; | ||
import mockit.Mock; | ||
import mockit.MockUp; | ||
import mockit.integration.junit4.JMockit; | ||
|
||
@RunWith(JMockit.class) | ||
public class CloudStoreUpdaterTest extends AbstractTestWithTempDir { | ||
|
||
/** | ||
* The new version to which we update. This overrides the information that would otherwise be downloaded | ||
* from our server (e.g. from | ||
* <a href="http://cloudstore.codewizards.co/update/co.codewizards.cloudstore.server/version">http://cloudstore.codewizards.co/update/co.codewizards.cloudstore.server/version</a>). | ||
*/ | ||
private Version remoteVersion; | ||
private File installationDir; | ||
|
||
@Override | ||
public void before() throws Exception { | ||
super.before(); | ||
|
||
new MockUp<CloudStoreUpdaterCore>() { | ||
@Mock | ||
Version getRemoteVersion(Invocation invocation) { | ||
return assertNotNull("remoteVersion", remoteVersion); | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void update_server_from_0_9_5_to_0_9_6() throws Exception { | ||
remoteVersion = new Version("0.9.6"); | ||
File oldTarGzFile = downloadFileToTempDir("http://cloudstore.codewizards.co/0.9.5/download/co.codewizards.cloudstore.server-0.9.5-bin.tar.gz"); | ||
|
||
install(oldTarGzFile); | ||
|
||
assertThatInstallationIs("co.codewizards.cloudstore.server", "0.9.5"); | ||
|
||
new CloudStoreUpdater(new String[] { "-installationDir", installationDir.getAbsolutePath() }).execute(); | ||
|
||
assertThatInstallationIs("co.codewizards.cloudstore.server", "0.9.6"); | ||
} | ||
|
||
@Test | ||
public void update_client_from_0_9_5_to_0_9_6() throws Exception { | ||
remoteVersion = new Version("0.9.6"); | ||
File oldTarGzFile = downloadFileToTempDir("http://cloudstore.codewizards.co/0.9.5/download/co.codewizards.cloudstore.client-0.9.5-bin.tar.gz"); | ||
|
||
install(oldTarGzFile); | ||
|
||
assertThatInstallationIs("co.codewizards.cloudstore.client", "0.9.5"); | ||
|
||
new CloudStoreUpdater(new String[] { "-installationDir", installationDir.getAbsolutePath() }).execute(); | ||
|
||
assertThatInstallationIs("co.codewizards.cloudstore.client", "0.9.6"); | ||
} | ||
|
||
@Test | ||
public void update_server_from_0_9_6_to_0_9_7() throws Exception { | ||
remoteVersion = new Version("0.9.7"); | ||
File oldTarGzFile = downloadFileToTempDir("http://cloudstore.codewizards.co/0.9.6/download/co.codewizards.cloudstore.server-0.9.6-bin.tar.gz"); | ||
|
||
install(oldTarGzFile); | ||
|
||
assertThatInstallationIs("co.codewizards.cloudstore.server", "0.9.6"); | ||
|
||
new CloudStoreUpdater(new String[] { "-installationDir", installationDir.getAbsolutePath() }).execute(); | ||
|
||
assertThatInstallationIs("co.codewizards.cloudstore.server", "0.9.7"); | ||
} | ||
|
||
@Test | ||
public void update_client_from_0_9_6_to_0_9_7() throws Exception { | ||
remoteVersion = new Version("0.9.7"); | ||
File oldTarGzFile = downloadFileToTempDir("http://cloudstore.codewizards.co/0.9.6/download/co.codewizards.cloudstore.client-0.9.6-bin.tar.gz"); | ||
|
||
install(oldTarGzFile); | ||
|
||
assertThatInstallationIs("co.codewizards.cloudstore.client", "0.9.6"); | ||
|
||
new CloudStoreUpdater(new String[] { "-installationDir", installationDir.getAbsolutePath() }).execute(); | ||
|
||
assertThatInstallationIs("co.codewizards.cloudstore.client", "0.9.7"); | ||
} | ||
|
||
private void install(File oldTarGzFile) throws IOException { | ||
File installationBaseDir = tempDir.createFile("installation"); | ||
new TarGzFile(oldTarGzFile).extract(installationBaseDir); | ||
File cloudstoreSubDir = installationBaseDir.createFile("cloudstore"); | ||
assertThat(cloudstoreSubDir.getIoFile()).isDirectory(); | ||
this.installationDir = cloudstoreSubDir; | ||
} | ||
|
||
private void assertThatInstallationIs(String expectedArtifactId, String expectedVersion) throws IOException { | ||
assertNotNull("installationDir", installationDir); | ||
|
||
File installationPropertiesFile = installationDir.createFile("installation.properties"); | ||
assertThat(installationPropertiesFile.getIoFile()).exists(); | ||
Properties installationProperties = PropertiesUtil.load(installationPropertiesFile); | ||
String installedArtifactId = installationProperties.getProperty("artifactId"); | ||
String installedVersion = installationProperties.getProperty("version"); | ||
assertThat(installedArtifactId).isEqualTo(expectedArtifactId); | ||
assertThat(installedVersion).isEqualTo(expectedVersion); | ||
|
||
File libDir = installationDir.createFile("lib"); | ||
int cloudstoreLibCount = 0; | ||
int allLibCount = 0; | ||
String expectedLibFileSuffix = "-" + expectedVersion + ".jar"; | ||
for (File libFile : libDir.listFiles()) { | ||
++allLibCount; | ||
if (libFile.getName().startsWith("co.codewizards.cloudstore.")) { | ||
++cloudstoreLibCount; | ||
assertThat(libFile.getName()).endsWith(expectedLibFileSuffix); | ||
} | ||
} | ||
assertThat(cloudstoreLibCount).isGreaterThanOrEqualTo(7); | ||
assertThat(allLibCount).isGreaterThanOrEqualTo(20); | ||
} | ||
} |