Apply eclipse formatting rules

Apply eclipse formatting rules to b2fe2dd9.

See gh-1751
This commit is contained in:
Phillip Webb
2014-10-31 12:29:29 -07:00
parent 8e16dfc951
commit b89e5e0ab7
19 changed files with 839 additions and 762 deletions

View File

@@ -30,12 +30,14 @@ import org.apache.http.message.BasicHeader;
import org.hamcrest.Matcher;
import org.json.JSONObject;
import org.mockito.ArgumentMatcher;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import static org.mockito.Mockito.*;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
*
@@ -51,14 +53,16 @@ public abstract class AbstractHttpClientMockTests {
protected void mockSuccessfulMetadataGet(String version) throws IOException {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
Resource resource = new ClassPathResource("metadata/service-metadata-" + version + ".json");
Resource resource = new ClassPathResource("metadata/service-metadata-" + version
+ ".json");
byte[] content = StreamUtils.copyToByteArray(resource.getInputStream());
mockHttpEntity(response, content, "application/json");
mockStatus(response, 200);
when(httpClient.execute(argThat(getForJsonData()))).thenReturn(response);
when(this.httpClient.execute(argThat(getForJsonData()))).thenReturn(response);
}
protected void mockSuccessfulProjectGeneration(MockHttpProjectGenerationRequest request) throws IOException {
protected void mockSuccessfulProjectGeneration(
MockHttpProjectGenerationRequest request) throws IOException {
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet();
@@ -66,33 +70,39 @@ public abstract class AbstractHttpClientMockTests {
mockHttpEntity(response, request.content, request.contentType);
mockStatus(response, 200);
String header = request.fileName != null ? contentDispositionValue(request.fileName) : null;
String header = request.fileName != null ? contentDispositionValue(request.fileName)
: null;
mockHttpHeader(response, "Content-Disposition", header);
when(httpClient.execute(argThat(getForNonJsonData()))).thenReturn(response);
when(this.httpClient.execute(argThat(getForNonJsonData()))).thenReturn(response);
}
protected void mockProjectGenerationError(int status, String message) throws IOException {
protected void mockProjectGenerationError(int status, String message)
throws IOException {
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet();
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, createJsonError(status, message).getBytes(), "application/json");
mockHttpEntity(response, createJsonError(status, message).getBytes(),
"application/json");
mockStatus(response, status);
when(httpClient.execute(isA(HttpGet.class))).thenReturn(response);
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
}
protected void mockMetadataGetError(int status, String message) throws IOException {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, createJsonError(status, message).getBytes(), "application/json");
mockHttpEntity(response, createJsonError(status, message).getBytes(),
"application/json");
mockStatus(response, status);
when(httpClient.execute(isA(HttpGet.class))).thenReturn(response);
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
}
protected HttpEntity mockHttpEntity(CloseableHttpResponse response, byte[] content, String contentType) {
protected HttpEntity mockHttpEntity(CloseableHttpResponse response, byte[] content,
String contentType) {
try {
HttpEntity entity = mock(HttpEntity.class);
when(entity.getContent()).thenReturn(new ByteArrayInputStream(content));
Header contentTypeHeader = contentType != null ? new BasicHeader("Content-Type", contentType) : null;
Header contentTypeHeader = contentType != null ? new BasicHeader(
"Content-Type", contentType) : null;
when(entity.getContentType()).thenReturn(contentTypeHeader);
when(response.getEntity()).thenReturn(entity);
return entity;
@@ -108,7 +118,8 @@ public abstract class AbstractHttpClientMockTests {
when(response.getStatusLine()).thenReturn(statusLine);
}
protected void mockHttpHeader(CloseableHttpResponse response, String headerName, String value) {
protected void mockHttpHeader(CloseableHttpResponse response, String headerName,
String value) {
Header header = value != null ? new BasicHeader(headerName, value) : null;
when(response.getFirstHeader(headerName)).thenReturn(header);
}
@@ -140,16 +151,17 @@ public abstract class AbstractHttpClientMockTests {
String fileName;
byte[] content = new byte[] {0, 0, 0, 0};
byte[] content = new byte[] { 0, 0, 0, 0 };
public MockHttpProjectGenerationRequest(String contentType, String fileName, byte[] content) {
public MockHttpProjectGenerationRequest(String contentType, String fileName,
byte[] content) {
this.contentType = contentType;
this.fileName = fileName;
this.content = content;
}
public MockHttpProjectGenerationRequest(String contentType, String fileName) {
this(contentType, fileName, new byte[] {0, 0, 0, 0});
this(contentType, fileName, new byte[] { 0, 0, 0, 0 });
}
}
@@ -171,11 +183,12 @@ public abstract class AbstractHttpClientMockTests {
}
HttpGet get = (HttpGet) argument;
Header acceptHeader = get.getFirstHeader(HttpHeaders.ACCEPT);
if (shouldMatch) {
return acceptHeader != null && value.equals(acceptHeader.getValue());
if (this.shouldMatch) {
return acceptHeader != null && this.value.equals(acceptHeader.getValue());
}
else {
return acceptHeader == null || !value.equals(acceptHeader.getValue());
return acceptHeader == null
|| !this.value.equals(acceptHeader.getValue());
}
}
}

View File

@@ -25,14 +25,17 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import joptsimple.OptionSet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.cli.command.status.ExitStatus;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link InitCommand}
@@ -44,14 +47,15 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
private final TestableInitCommandOptionHandler handler = new TestableInitCommandOptionHandler(httpClient);
private final TestableInitCommandOptionHandler handler = new TestableInitCommandOptionHandler(
this.httpClient);
private final InitCommand command = new InitCommand(handler);
private final InitCommand command = new InitCommand(this.handler);
@Test
public void listServiceCapabilities() throws Exception {
mockSuccessfulMetadataGet();
command.run("--list", "--target=http://fake-service");
this.command.run("--list", "--target=http://fake-service");
}
@Test
@@ -60,12 +64,12 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
File f = new File(fileName);
assertFalse("file should not exist", f.exists());
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/zip", fileName);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/zip", fileName);
mockSuccessfulProjectGeneration(mockHttpRequest);
try {
assertEquals(ExitStatus.OK, command.run());
assertEquals(ExitStatus.OK, this.command.run());
assertTrue("file should have been created", f.exists());
}
finally {
@@ -75,40 +79,44 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
@Test
public void generateProjectNoFileNameAvailable() throws Exception {
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/zip", null);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/zip", null);
mockSuccessfulProjectGeneration(mockHttpRequest);
assertEquals(ExitStatus.ERROR, command.run());
assertEquals(ExitStatus.ERROR, this.command.run());
}
@Test
public void generateProjectAndExtract() throws Exception {
File f = folder.newFolder();
File f = this.folder.newFolder();
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/zip", "demo.zip", archive);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/zip", "demo.zip", archive);
mockSuccessfulProjectGeneration(mockHttpRequest);
assertEquals(ExitStatus.OK, command.run("--extract", "--output=" + f.getAbsolutePath()));
assertEquals(ExitStatus.OK,
this.command.run("--extract", "--output=" + f.getAbsolutePath()));
File archiveFile = new File(f, "test.txt");
assertTrue("Archive not extracted properly " + f.getAbsolutePath() + " not found", archiveFile.exists());
assertTrue(
"Archive not extracted properly " + f.getAbsolutePath() + " not found",
archiveFile.exists());
}
@Test
public void generateProjectAndExtractUnsupportedArchive() throws Exception {
File f = folder.newFolder();
File f = this.folder.newFolder();
String fileName = UUID.randomUUID().toString() + ".zip";
File archiveFile = new File(fileName);
assertFalse("file should not exist", archiveFile.exists());
try {
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/foobar", fileName, archive);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/foobar", fileName, archive);
mockSuccessfulProjectGeneration(mockHttpRequest);
assertEquals(ExitStatus.OK, command.run("--extract", "--output=" + f.getAbsolutePath()));
assertEquals(ExitStatus.OK,
this.command.run("--extract", "--output=" + f.getAbsolutePath()));
assertTrue("file should have been saved instead", archiveFile.exists());
}
finally {
@@ -118,18 +126,19 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
@Test
public void generateProjectAndExtractUnknownContentType() throws Exception {
File f = folder.newFolder();
File f = this.folder.newFolder();
String fileName = UUID.randomUUID().toString() + ".zip";
File archiveFile = new File(fileName);
assertFalse("file should not exist", archiveFile.exists());
try {
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest(null, fileName, archive);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
null, fileName, archive);
mockSuccessfulProjectGeneration(mockHttpRequest);
assertEquals(ExitStatus.OK, command.run("--extract", "--output=" + f.getAbsolutePath()));
assertEquals(ExitStatus.OK,
this.command.run("--extract", "--output=" + f.getAbsolutePath()));
assertTrue("file should have been saved instead", archiveFile.exists());
}
finally {
@@ -139,120 +148,125 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
@Test
public void fileNotOverwrittenByDefault() throws Exception {
File f = folder.newFile();
File f = this.folder.newFile();
long fileLength = f.length();
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/zip", f.getAbsolutePath());
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/zip", f.getAbsolutePath());
mockSuccessfulProjectGeneration(mockHttpRequest);
assertEquals("Should have failed", ExitStatus.ERROR, command.run());
assertEquals("Should have failed", ExitStatus.ERROR, this.command.run());
assertEquals("File should not have changed", fileLength, f.length());
}
@Test
public void overwriteFile() throws Exception {
File f = folder.newFile();
File f = this.folder.newFile();
long fileLength = f.length();
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/zip", f.getAbsolutePath());
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/zip", f.getAbsolutePath());
mockSuccessfulProjectGeneration(mockHttpRequest);
assertEquals("Should not have failed", ExitStatus.OK, command.run("--force"));
assertEquals("Should not have failed", ExitStatus.OK, this.command.run("--force"));
assertTrue("File should have changed", fileLength != f.length());
}
@Test
public void fileInArchiveNotOverwrittenByDefault() throws Exception {
File f = folder.newFolder();
File f = this.folder.newFolder();
File conflict = new File(f, "test.txt");
assertTrue("Should have been able to create file", conflict.createNewFile());
long fileLength = conflict.length();
// also contains test.txt
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/zip", "demo.zip", archive);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/zip", "demo.zip", archive);
mockSuccessfulProjectGeneration(mockHttpRequest);
assertEquals(ExitStatus.ERROR, command.run("--extract", "--output=" + f.getAbsolutePath()));
assertEquals(ExitStatus.ERROR,
this.command.run("--extract", "--output=" + f.getAbsolutePath()));
assertEquals("File should not have changed", fileLength, conflict.length());
}
@Test
public void overwriteFileInArchive() throws Exception {
File f = folder.newFolder();
File f = this.folder.newFolder();
File conflict = new File(f, "test.txt");
assertTrue("Should have been able to create file", conflict.createNewFile());
long fileLength = conflict.length();
// also contains test.txt
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/zip", "demo.zip", archive);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/zip", "demo.zip", archive);
mockSuccessfulProjectGeneration(mockHttpRequest);
assertEquals(ExitStatus.OK, command.run("--force", "--extract", "--output=" + f.getAbsolutePath()));
assertEquals(
ExitStatus.OK,
this.command.run("--force", "--extract",
"--output=" + f.getAbsolutePath()));
assertTrue("File should have changed", fileLength != conflict.length());
}
@Test
public void parseProjectOptions() throws Exception {
handler.disableProjectGeneration();
command.run("-bv=1.2.0.RELEASE", "-d=web,data-jpa", "-jv=1.9", "-p=war",
this.handler.disableProjectGeneration();
this.command.run("-bv=1.2.0.RELEASE", "-d=web,data-jpa", "-jv=1.9", "-p=war",
"--build=grunt", "--format=web", "-t=ant-project");
assertEquals("1.2.0.RELEASE", handler.lastRequest.getBootVersion());
List<String> dependencies = handler.lastRequest.getDependencies();
assertEquals("1.2.0.RELEASE", this.handler.lastRequest.getBootVersion());
List<String> dependencies = this.handler.lastRequest.getDependencies();
assertEquals(2, dependencies.size());
assertTrue(dependencies.contains("web"));
assertTrue(dependencies.contains("data-jpa"));
assertEquals("1.9", handler.lastRequest.getJavaVersion());
assertEquals("war", handler.lastRequest.getPackaging());
assertEquals("grunt", handler.lastRequest.getBuild());
assertEquals("web", handler.lastRequest.getFormat());
assertEquals("ant-project", handler.lastRequest.getType());
assertEquals("1.9", this.handler.lastRequest.getJavaVersion());
assertEquals("war", this.handler.lastRequest.getPackaging());
assertEquals("grunt", this.handler.lastRequest.getBuild());
assertEquals("web", this.handler.lastRequest.getFormat());
assertEquals("ant-project", this.handler.lastRequest.getType());
}
@Test
public void parseTypeOnly() throws Exception {
handler.disableProjectGeneration();
command.run("-t=ant-project");
assertEquals("maven", handler.lastRequest.getBuild());
assertEquals("project", handler.lastRequest.getFormat());
assertFalse(handler.lastRequest.isDetectType());
assertEquals("ant-project", handler.lastRequest.getType());
this.handler.disableProjectGeneration();
this.command.run("-t=ant-project");
assertEquals("maven", this.handler.lastRequest.getBuild());
assertEquals("project", this.handler.lastRequest.getFormat());
assertFalse(this.handler.lastRequest.isDetectType());
assertEquals("ant-project", this.handler.lastRequest.getType());
}
@Test
public void parseBuildOnly() throws Exception {
handler.disableProjectGeneration();
command.run("--build=ant");
assertEquals("ant", handler.lastRequest.getBuild());
assertEquals("project", handler.lastRequest.getFormat());
assertTrue(handler.lastRequest.isDetectType());
assertNull(handler.lastRequest.getType());
this.handler.disableProjectGeneration();
this.command.run("--build=ant");
assertEquals("ant", this.handler.lastRequest.getBuild());
assertEquals("project", this.handler.lastRequest.getFormat());
assertTrue(this.handler.lastRequest.isDetectType());
assertNull(this.handler.lastRequest.getType());
}
@Test
public void parseFormatOnly() throws Exception {
handler.disableProjectGeneration();
command.run("--format=web");
assertEquals("maven", handler.lastRequest.getBuild());
assertEquals("web", handler.lastRequest.getFormat());
assertTrue(handler.lastRequest.isDetectType());
assertNull(handler.lastRequest.getType());
this.handler.disableProjectGeneration();
this.command.run("--format=web");
assertEquals("maven", this.handler.lastRequest.getBuild());
assertEquals("web", this.handler.lastRequest.getFormat());
assertTrue(this.handler.lastRequest.isDetectType());
assertNull(this.handler.lastRequest.getType());
}
@Test
public void parseOutput() throws Exception {
handler.disableProjectGeneration();
command.run("--output=foobar.zip");
this.handler.disableProjectGeneration();
this.command.run("--output=foobar.zip");
assertEquals("foobar.zip", handler.lastRequest.getOutput());
assertEquals("foobar.zip", this.handler.lastRequest.getOutput());
}
private byte[] createFakeZipArchive(String fileName, String content) throws IOException {
private byte[] createFakeZipArchive(String fileName, String content)
throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(out);
try {
@@ -268,8 +282,8 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
return out.toByteArray();
}
private static class TestableInitCommandOptionHandler extends InitCommandOptionHandler {
private static class TestableInitCommandOptionHandler extends
InitCommandOptionHandler {
private boolean disableProjectGeneration;
@@ -280,13 +294,14 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
}
void disableProjectGeneration() {
disableProjectGeneration = true;
this.disableProjectGeneration = true;
}
@Override
protected ExitStatus generateProject(OptionSet options, CloseableHttpClient httpClient) {
lastRequest = createProjectGenerationRequest(options);
if (!disableProjectGeneration) {
protected ExitStatus generateProject(OptionSet options,
CloseableHttpClient httpClient) {
this.lastRequest = createProjectGenerationRequest(options);
if (!this.disableProjectGeneration) {
return super.generateProject(options, httpClient);
}
return ExitStatus.OK;

View File

@@ -25,11 +25,12 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests for {@link InitializrServiceHttpInvoker}
@@ -41,20 +42,21 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
@Rule
public final ExpectedException thrown = ExpectedException.none();
private final InitializrServiceHttpInvoker invoker = new InitializrServiceHttpInvoker(httpClient);
private final InitializrServiceHttpInvoker invoker = new InitializrServiceHttpInvoker(
this.httpClient);
@Test
public void loadMetadata() throws IOException {
mockSuccessfulMetadataGet();
InitializrServiceMetadata metadata = invoker.loadMetadata("http://foo/bar");
InitializrServiceMetadata metadata = this.invoker.loadMetadata("http://foo/bar");
assertNotNull(metadata);
}
@Test
public void generateSimpleProject() throws IOException {
ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/xml", "foo.zip");
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", "foo.zip");
ProjectGenerationResponse entity = generateProject(request, mockHttpRequest);
assertProjectEntity(entity, mockHttpRequest.contentType, mockHttpRequest.fileName);
}
@@ -63,8 +65,8 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
public void generateProjectCustomTargetFilename() throws IOException {
ProjectGenerationRequest request = new ProjectGenerationRequest();
request.setOutput("bar.zip");
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/xml", null);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", null);
ProjectGenerationResponse entity = generateProject(request, mockHttpRequest);
assertProjectEntity(entity, mockHttpRequest.contentType, null);
}
@@ -72,8 +74,8 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
@Test
public void generateProjectNoDefaultFileName() throws IOException {
ProjectGenerationRequest request = new ProjectGenerationRequest();
MockHttpProjectGenerationRequest mockHttpRequest =
new MockHttpProjectGenerationRequest("application/xml", null);
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
"application/xml", null);
ProjectGenerationResponse entity = generateProject(request, mockHttpRequest);
assertProjectEntity(entity, mockHttpRequest.contentType, null);
}
@@ -85,9 +87,9 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
ProjectGenerationRequest request = new ProjectGenerationRequest();
request.getDependencies().add("foo:bar");
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage(jsonMessage);
invoker.generate(request);
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage(jsonMessage);
this.invoker.generate(request);
}
@Test
@@ -95,9 +97,9 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
mockProjectGenerationError(400, null);
ProjectGenerationRequest request = new ProjectGenerationRequest();
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage("unexpected 400 error");
invoker.generate(request);
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage("unexpected 400 error");
this.invoker.generate(request);
}
@Test
@@ -106,25 +108,24 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500);
when(httpClient.execute(isA(HttpGet.class))).thenReturn(response);
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
ProjectGenerationRequest request = new ProjectGenerationRequest();
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage("No content received from server");
invoker.generate(request);
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage("No content received from server");
this.invoker.generate(request);
}
@Test
public void loadMetadataBadRequest() throws IOException {
String jsonMessage = "whatever error on the server";
mockMetadataGetError(500, jsonMessage);
ProjectGenerationRequest request = new ProjectGenerationRequest();
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage(jsonMessage);
invoker.generate(request);
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage(jsonMessage);
this.invoker.generate(request);
}
@Test
@@ -132,44 +133,43 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, "Foo-Bar-Not-JSON".getBytes(), "application/json");
mockStatus(response, 200);
when(httpClient.execute(isA(HttpGet.class))).thenReturn(response);
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
ProjectGenerationRequest request = new ProjectGenerationRequest();
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage("Invalid content received from server");
invoker.generate(request);
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage("Invalid content received from server");
this.invoker.generate(request);
}
@Test
public void loadMetadataNoContent() throws IOException {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500);
when(httpClient.execute(isA(HttpGet.class))).thenReturn(response);
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
ProjectGenerationRequest request = new ProjectGenerationRequest();
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage("No content received from server");
invoker.generate(request);
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage("No content received from server");
this.invoker.generate(request);
}
private ProjectGenerationResponse generateProject(ProjectGenerationRequest request,
MockHttpProjectGenerationRequest mockRequest) throws IOException {
mockSuccessfulProjectGeneration(mockRequest);
ProjectGenerationResponse entity = invoker.generate(request);
ProjectGenerationResponse entity = this.invoker.generate(request);
assertArrayEquals("wrong body content", mockRequest.content, entity.getContent());
return entity;
}
private static void assertProjectEntity(ProjectGenerationResponse entity, String mimeType, String fileName) {
private static void assertProjectEntity(ProjectGenerationResponse entity,
String mimeType, String fileName) {
if (mimeType == null) {
assertNull("No content type expected", entity.getContentType());
}
else {
assertEquals("wrong mime type", mimeType, entity.getContentType().getMimeType());
assertEquals("wrong mime type", mimeType, entity.getContentType()
.getMimeType());
}
assertEquals("wrong filename", fileName, entity.getFileName());
}

View File

@@ -22,12 +22,12 @@ import java.nio.charset.Charset;
import org.json.JSONObject;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* Tests for {@link InitializrServiceMetadata}
@@ -36,7 +36,6 @@ import static org.junit.Assert.*;
*/
public class InitializrServiceMetadataTests {
@Test
public void parseDefaults() {
InitializrServiceMetadata metadata = createInstance("1.0.0");
@@ -53,7 +52,8 @@ public class InitializrServiceMetadataTests {
// Security description
assertEquals("AOP", metadata.getDependency("aop").getName());
assertEquals("Security", metadata.getDependency("security").getName());
assertEquals("Security description", metadata.getDependency("security").getDescription());
assertEquals("Security description", metadata.getDependency("security")
.getDescription());
assertEquals("JDBC", metadata.getDependency("jdbc").getName());
assertEquals("JPA", metadata.getDependency("data-jpa").getName());
assertEquals("MongoDB", metadata.getDependency("data-mongodb").getName());
@@ -76,7 +76,6 @@ public class InitializrServiceMetadataTests {
assertEquals("project", projectType.getTags().get("format"));
}
private static InitializrServiceMetadata createInstance(String version) {
try {
return new InitializrServiceMetadata(readJson(version));
@@ -87,7 +86,8 @@ public class InitializrServiceMetadataTests {
}
private static JSONObject readJson(String version) throws IOException {
Resource resource = new ClassPathResource("metadata/service-metadata-" + version + ".json");
Resource resource = new ClassPathResource("metadata/service-metadata-" + version
+ ".json");
InputStream stream = resource.getInputStream();
try {
String json = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));

View File

@@ -20,7 +20,7 @@ import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link ListMetadataCommand}
@@ -29,12 +29,12 @@ import static org.junit.Assert.*;
*/
public class ListMetadataCommandTests extends AbstractHttpClientMockTests {
private final ListMetadataCommand command = new ListMetadataCommand(httpClient);
private final ListMetadataCommand command = new ListMetadataCommand(this.httpClient);
@Test
public void listMetadata() throws IOException {
mockSuccessfulMetadataGet();
String content = command.generateReport("http://localhost");
String content = this.command.generateReport("http://localhost");
assertTrue(content.contains("aop - AOP"));
assertTrue(content.contains("security - Security: Security description"));

View File

@@ -27,12 +27,11 @@ import org.json.JSONObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/**
* Tests for {@link ProjectGenerationRequest}
@@ -41,7 +40,8 @@ import static org.junit.Assert.*;
*/
public class ProjectGenerationRequestTests {
public static final Map<String, String> EMPTY_TAGS = Collections.<String, String>emptyMap();
public static final Map<String, String> EMPTY_TAGS = Collections
.<String, String> emptyMap();
@Rule
public final ExpectedException thrown = ExpectedException.none();
@@ -50,63 +50,66 @@ public class ProjectGenerationRequestTests {
@Test
public void defaultSettings() {
assertEquals(createDefaultUrl("?type=test-type"), request.generateUrl(createDefaultMetadata()));
assertEquals(createDefaultUrl("?type=test-type"),
this.request.generateUrl(createDefaultMetadata()));
}
@Test
public void customServer() throws URISyntaxException {
String customServerUrl = "http://foo:8080/initializr";
request.setServiceUrl(customServerUrl);
request.getDependencies().add("security");
assertEquals(new URI(customServerUrl + "/starter.zip?style=security&type=test-type"),
request.generateUrl(createDefaultMetadata()));
this.request.setServiceUrl(customServerUrl);
this.request.getDependencies().add("security");
assertEquals(new URI(customServerUrl
+ "/starter.zip?style=security&type=test-type"),
this.request.generateUrl(createDefaultMetadata()));
}
@Test
public void customBootVersion() {
request.setBootVersion("1.2.0.RELEASE");
this.request.setBootVersion("1.2.0.RELEASE");
assertEquals(createDefaultUrl("?bootVersion=1.2.0.RELEASE&type=test-type"),
request.generateUrl(createDefaultMetadata()));
this.request.generateUrl(createDefaultMetadata()));
}
@Test
public void singleDependency() {
request.getDependencies().add("web");
this.request.getDependencies().add("web");
assertEquals(createDefaultUrl("?style=web&type=test-type"),
request.generateUrl(createDefaultMetadata()));
this.request.generateUrl(createDefaultMetadata()));
}
@Test
public void multipleDependencies() {
request.getDependencies().add("web");
request.getDependencies().add("data-jpa");
this.request.getDependencies().add("web");
this.request.getDependencies().add("data-jpa");
assertEquals(createDefaultUrl("?style=web&style=data-jpa&type=test-type"),
request.generateUrl(createDefaultMetadata()));
this.request.generateUrl(createDefaultMetadata()));
}
@Test
public void customJavaVersion() {
request.setJavaVersion("1.8");
this.request.setJavaVersion("1.8");
assertEquals(createDefaultUrl("?javaVersion=1.8&type=test-type"),
request.generateUrl(createDefaultMetadata()));
this.request.generateUrl(createDefaultMetadata()));
}
@Test
public void customPackaging() {
request.setPackaging("war");
this.request.setPackaging("war");
assertEquals(createDefaultUrl("?packaging=war&type=test-type"),
request.generateUrl(createDefaultMetadata()));
this.request.generateUrl(createDefaultMetadata()));
}
@Test
public void customType() throws URISyntaxException {
ProjectType projectType = new ProjectType("custom", "Custom Type", "/foo", true, EMPTY_TAGS);
ProjectType projectType = new ProjectType("custom", "Custom Type", "/foo", true,
EMPTY_TAGS);
InitializrServiceMetadata metadata = new InitializrServiceMetadata(projectType);
request.setType("custom");
request.getDependencies().add("data-rest");
assertEquals(new URI(ProjectGenerationRequest.DEFAULT_SERVICE_URL + "/foo?style=data-rest&type=custom"),
request.generateUrl(metadata));
this.request.setType("custom");
this.request.getDependencies().add("data-rest");
assertEquals(new URI(ProjectGenerationRequest.DEFAULT_SERVICE_URL
+ "/foo?style=data-rest&type=custom"), this.request.generateUrl(metadata));
}
@Test
@@ -114,9 +117,9 @@ public class ProjectGenerationRequestTests {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("does-not-exist", null);
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage("does-not-exist");
request.generateUrl(metadata);
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage("does-not-exist");
this.request.generateUrl(metadata);
}
@Test
@@ -124,10 +127,10 @@ public class ProjectGenerationRequestTests {
InitializrServiceMetadata metadata = readMetadata("types-conflict");
setBuildAndFormat("gradle", null);
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage("gradle-project");
thrown.expectMessage("gradle-project-2");
request.generateUrl(metadata);
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage("gradle-project");
this.thrown.expectMessage("gradle-project-2");
this.request.generateUrl(metadata);
}
@Test
@@ -135,29 +138,30 @@ public class ProjectGenerationRequestTests {
InitializrServiceMetadata metadata = readMetadata();
setBuildAndFormat("gradle", null);
assertEquals(createDefaultUrl("?type=gradle-project"), request.generateUrl(metadata));
assertEquals(createDefaultUrl("?type=gradle-project"),
this.request.generateUrl(metadata));
}
@Test
public void invalidType() throws URISyntaxException {
request.setType("does-not-exist");
this.request.setType("does-not-exist");
thrown.expect(ProjectGenerationException.class);
request.generateUrl(createDefaultMetadata());
this.thrown.expect(ProjectGenerationException.class);
this.request.generateUrl(createDefaultMetadata());
}
@Test
public void noTypeAndNoDefault() throws URISyntaxException {
thrown.expect(ProjectGenerationException.class);
thrown.expectMessage("no default is defined");
request.generateUrl(readMetadata("types-conflict"));
this.thrown.expect(ProjectGenerationException.class);
this.thrown.expectMessage("no default is defined");
this.request.generateUrl(readMetadata("types-conflict"));
}
private static URI createDefaultUrl(String param) {
try {
return new URI(ProjectGenerationRequest.DEFAULT_SERVICE_URL + "/starter.zip" + param);
return new URI(ProjectGenerationRequest.DEFAULT_SERVICE_URL + "/starter.zip"
+ param);
}
catch (URISyntaxException e) {
throw new IllegalStateException(e);
@@ -165,13 +169,14 @@ public class ProjectGenerationRequestTests {
}
public void setBuildAndFormat(String build, String format) {
request.setBuild(build != null ? build : "maven");
request.setFormat(format != null ? format : "project");
request.setDetectType(true);
this.request.setBuild(build != null ? build : "maven");
this.request.setFormat(format != null ? format : "project");
this.request.setDetectType(true);
}
private static InitializrServiceMetadata createDefaultMetadata() {
ProjectType projectType = new ProjectType("test-type", "The test type", "/starter.zip", true, EMPTY_TAGS);
ProjectType projectType = new ProjectType("test-type", "The test type",
"/starter.zip", true, EMPTY_TAGS);
return new InitializrServiceMetadata(projectType);
}
@@ -181,8 +186,10 @@ public class ProjectGenerationRequestTests {
private static InitializrServiceMetadata readMetadata(String version) {
try {
Resource resource = new ClassPathResource("metadata/service-metadata-" + version + ".json");
String content = StreamUtils.copyToString(resource.getInputStream(), Charset.forName("UTF-8"));
Resource resource = new ClassPathResource("metadata/service-metadata-"
+ version + ".json");
String content = StreamUtils.copyToString(resource.getInputStream(),
Charset.forName("UTF-8"));
JSONObject json = new JSONObject(content);
return new InitializrServiceMetadata(json);
}