Skip to content

Commit

Permalink
https://github.com/cloudstore/cloudstore/issues/50
Browse files Browse the repository at this point in the history
Added test for CloudStoreUpdater.
  • Loading branch information
nlmarco committed Jun 28, 2016
1 parent 56c1b85 commit fe6b93d
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
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;
}

}
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);
}
}

0 comments on commit fe6b93d

Please sign in to comment.