Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
b89e5e0a
Commit
b89e5e0a
authored
Oct 31, 2014
by
Phillip Webb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Apply eclipse formatting rules
Apply eclipse formatting rules to
b2fe2dd9
. See gh-1751
parent
8e16dfc9
Changes
16
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
408 additions
and
331 deletions
+408
-331
DefaultCommandFactory.java
...a/org/springframework/boot/cli/DefaultCommandFactory.java
+2
-1
Dependency.java
...org/springframework/boot/cli/command/init/Dependency.java
+4
-3
InitCommand.java
...rg/springframework/boot/cli/command/init/InitCommand.java
+2
-2
InitCommandOptionHandler.java
...ework/boot/cli/command/init/InitCommandOptionHandler.java
+67
-41
InitializrServiceHttpInvoker.java
...k/boot/cli/command/init/InitializrServiceHttpInvoker.java
+27
-17
InitializrServiceMetadata.java
...work/boot/cli/command/init/InitializrServiceMetadata.java
+15
-16
ListMetadataCommand.java
...gframework/boot/cli/command/init/ListMetadataCommand.java
+14
-14
ProjectGenerationRequest.java
...ework/boot/cli/command/init/ProjectGenerationRequest.java
+40
-36
ProjectGenerationResponse.java
...work/boot/cli/command/init/ProjectGenerationResponse.java
+4
-4
ProjectType.java
...rg/springframework/boot/cli/command/init/ProjectType.java
+7
-6
AbstractHttpClientMockTests.java
...rk/boot/cli/command/init/AbstractHttpClientMockTests.java
+34
-21
InitCommandTests.java
...ringframework/boot/cli/command/init/InitCommandTests.java
+92
-77
InitializrServiceHttpInvokerTests.java
...t/cli/command/init/InitializrServiceHttpInvokerTests.java
+38
-38
InitializrServiceMetadataTests.java
...boot/cli/command/init/InitializrServiceMetadataTests.java
+6
-6
ListMetadataCommandTests.java
...ework/boot/cli/command/init/ListMetadataCommandTests.java
+3
-3
ProjectGenerationRequestTests.java
.../boot/cli/command/init/ProjectGenerationRequestTests.java
+53
-46
No files found.
spring-boot-cli/src/main/java/org/springframework/boot/cli/DefaultCommandFactory.java
View file @
b89e5e0a
...
@@ -40,7 +40,8 @@ public class DefaultCommandFactory implements CommandFactory {
...
@@ -40,7 +40,8 @@ public class DefaultCommandFactory implements CommandFactory {
private
static
final
List
<
Command
>
DEFAULT_COMMANDS
=
Arrays
.<
Command
>
asList
(
private
static
final
List
<
Command
>
DEFAULT_COMMANDS
=
Arrays
.<
Command
>
asList
(
new
VersionCommand
(),
new
RunCommand
(),
new
TestCommand
(),
new
GrabCommand
(),
new
VersionCommand
(),
new
RunCommand
(),
new
TestCommand
(),
new
GrabCommand
(),
new
JarCommand
(),
new
InstallCommand
(),
new
UninstallCommand
(),
new
InitCommand
());
new
JarCommand
(),
new
InstallCommand
(),
new
UninstallCommand
(),
new
InitCommand
());
@Override
@Override
public
Collection
<
Command
>
getCommands
()
{
public
Collection
<
Command
>
getCommands
()
{
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/Dependency.java
View file @
b89e5e0a
...
@@ -31,7 +31,7 @@ class Dependency {
...
@@ -31,7 +31,7 @@ class Dependency {
private
String
description
;
private
String
description
;
public
String
getId
()
{
public
String
getId
()
{
return
id
;
return
this
.
id
;
}
}
public
void
setId
(
String
id
)
{
public
void
setId
(
String
id
)
{
...
@@ -39,7 +39,7 @@ class Dependency {
...
@@ -39,7 +39,7 @@ class Dependency {
}
}
public
String
getName
()
{
public
String
getName
()
{
return
name
;
return
this
.
name
;
}
}
public
void
setName
(
String
name
)
{
public
void
setName
(
String
name
)
{
...
@@ -47,10 +47,11 @@ class Dependency {
...
@@ -47,10 +47,11 @@ class Dependency {
}
}
public
String
getDescription
()
{
public
String
getDescription
()
{
return
description
;
return
this
.
description
;
}
}
public
void
setDescription
(
String
description
)
{
public
void
setDescription
(
String
description
)
{
this
.
description
=
description
;
this
.
description
=
description
;
}
}
}
}
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommand.java
View file @
b89e5e0a
...
@@ -17,7 +17,6 @@
...
@@ -17,7 +17,6 @@
package
org
.
springframework
.
boot
.
cli
.
command
.
init
;
package
org
.
springframework
.
boot
.
cli
.
command
.
init
;
import
org.apache.http.impl.client.HttpClientBuilder
;
import
org.apache.http.impl.client.HttpClientBuilder
;
import
org.springframework.boot.cli.command.Command
;
import
org.springframework.boot.cli.command.Command
;
import
org.springframework.boot.cli.command.OptionParsingCommand
;
import
org.springframework.boot.cli.command.OptionParsingCommand
;
...
@@ -30,7 +29,8 @@ import org.springframework.boot.cli.command.OptionParsingCommand;
...
@@ -30,7 +29,8 @@ import org.springframework.boot.cli.command.OptionParsingCommand;
public
class
InitCommand
extends
OptionParsingCommand
{
public
class
InitCommand
extends
OptionParsingCommand
{
InitCommand
(
InitCommandOptionHandler
handler
)
{
InitCommand
(
InitCommandOptionHandler
handler
)
{
super
(
"init"
,
"Initialize a new project structure using Spring Initializr"
,
handler
);
super
(
"init"
,
"Initialize a new project structure from Spring Initializr"
,
handler
);
}
}
public
InitCommand
()
{
public
InitCommand
()
{
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitCommandOptionHandler.java
View file @
b89e5e0a
...
@@ -27,8 +27,8 @@ import java.util.zip.ZipInputStream;
...
@@ -27,8 +27,8 @@ import java.util.zip.ZipInputStream;
import
joptsimple.OptionSet
;
import
joptsimple.OptionSet
;
import
joptsimple.OptionSpec
;
import
joptsimple.OptionSpec
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.springframework.boot.cli.command.options.OptionHandler
;
import
org.springframework.boot.cli.command.options.OptionHandler
;
import
org.springframework.boot.cli.command.status.ExitStatus
;
import
org.springframework.boot.cli.command.status.ExitStatus
;
import
org.springframework.boot.cli.util.Log
;
import
org.springframework.boot.cli.util.Log
;
...
@@ -78,46 +78,61 @@ public class InitCommandOptionHandler extends OptionHandler {
...
@@ -78,46 +78,61 @@ public class InitCommandOptionHandler extends OptionHandler {
@Override
@Override
protected
void
options
()
{
protected
void
options
()
{
this
.
target
=
option
(
Arrays
.
asList
(
"target"
),
this
.
target
=
option
(
Arrays
.
asList
(
"target"
),
"URL of the service to use"
)
"URL of the service to use"
).
withRequiredArg
().
defaultsTo
(
ProjectGenerationRequest
.
DEFAULT_SERVICE_URL
);
.
withRequiredArg
().
defaultsTo
(
this
.
listMetadata
=
option
(
Arrays
.
asList
(
"list"
,
"l"
),
"List the capabilities of the service. Use it to "
+
ProjectGenerationRequest
.
DEFAULT_SERVICE_URL
);
"discover the dependencies and the types that are available."
);
this
.
listMetadata
=
option
(
Arrays
.
asList
(
"list"
,
"l"
),
"List the capabilities of the service. Use it to "
+
"discover the dependencies and the types that are available."
);
// Project generation settings
// Project generation settings
this
.
bootVersion
=
option
(
Arrays
.
asList
(
"boot-version"
,
"bv"
),
this
.
bootVersion
=
option
(
Arrays
.
asList
(
"boot-version"
,
"bv"
),
"Spring Boot version to use (e.g. 1.2.0.RELEASE)"
).
withRequiredArg
();
"Spring Boot version to use (e.g. 1.2.0.RELEASE)"
).
withRequiredArg
();
this
.
dependencies
=
option
(
Arrays
.
asList
(
"dependencies"
,
"d"
),
this
.
dependencies
=
option
(
Arrays
.
asList
(
"dependencies"
,
"d"
),
"Comma separated list of dependencies to include in the generated project"
).
withRequiredArg
();
"Comma separated list of dependencies to include in the generated project"
)
.
withRequiredArg
();
this
.
javaVersion
=
option
(
Arrays
.
asList
(
"java-version"
,
"jv"
),
this
.
javaVersion
=
option
(
Arrays
.
asList
(
"java-version"
,
"jv"
),
"Java version to use (e.g. 1.8)"
).
withRequiredArg
();
"Java version to use (e.g. 1.8)"
).
withRequiredArg
();
this
.
packaging
=
option
(
Arrays
.
asList
(
"packaging"
,
"p"
),
"Packaging type to use (e.g. jar)"
).
withRequiredArg
();
this
.
packaging
=
option
(
Arrays
.
asList
(
"packaging"
,
"p"
),
"Packaging type to use (e.g. jar)"
).
withRequiredArg
();
this
.
build
=
option
(
"build"
,
"The build system to use (e.g. maven, gradle). To be used alongside "
+
"--format to uniquely identify one type that is supported by the service. "
+
this
.
build
=
option
(
"Use --type in case of conflict"
).
withRequiredArg
().
defaultsTo
(
"maven"
);
"build"
,
this
.
format
=
option
(
"format"
,
"The format of the generated content (e.g. build for a build file, "
+
"The build system to use (e.g. maven, gradle). To be used alongside "
"project for a project archive). To be used alongside --build to uniquely identify one type "
+
+
"--format to uniquely identify one type that is supported by the service. "
"that is supported by the service. Use --type in case of conflict"
)
+
"Use --type in case of conflict"
).
withRequiredArg
().
defaultsTo
(
"maven"
);
this
.
format
=
option
(
"format"
,
"The format of the generated content (e.g. build for a build file, "
+
"project for a project archive). To be used alongside --build to uniquely identify one type "
+
"that is supported by the service. Use --type in case of conflict"
)
.
withRequiredArg
().
defaultsTo
(
"project"
);
.
withRequiredArg
().
defaultsTo
(
"project"
);
this
.
type
=
option
(
Arrays
.
asList
(
"type"
,
"t"
),
"The project type to use. Not normally needed if you "
+
this
.
type
=
option
(
"use --build and/or --format. Check the capabilities of the service (--list) for "
+
Arrays
.
asList
(
"type"
,
"t"
),
"more details."
).
withRequiredArg
();
"The project type to use. Not normally needed if you "
+
"use --build and/or --format. Check the capabilities of the service (--list) for "
+
"more details."
).
withRequiredArg
();
// Others
// Others
this
.
extract
=
option
(
Arrays
.
asList
(
"extract"
,
"x"
),
"Extract the project archive"
);
this
.
extract
=
option
(
Arrays
.
asList
(
"extract"
,
"x"
),
this
.
force
=
option
(
Arrays
.
asList
(
"force"
,
"f"
),
"Force overwrite of existing files"
);
"Extract the project archive"
);
this
.
output
=
option
(
Arrays
.
asList
(
"output"
,
"o"
),
this
.
force
=
option
(
Arrays
.
asList
(
"force"
,
"f"
),
"Location of the generated project. Can be an absolute or a relative reference and "
+
"Force overwrite of existing files"
);
"should refer to a directory when --extract is used."
).
withRequiredArg
();
this
.
output
=
option
(
Arrays
.
asList
(
"output"
,
"o"
),
"Location of the generated project. Can be an absolute or a relative reference and "
+
"should refer to a directory when --extract is used."
)
.
withRequiredArg
();
}
}
@Override
@Override
protected
ExitStatus
run
(
OptionSet
options
)
throws
Exception
{
protected
ExitStatus
run
(
OptionSet
options
)
throws
Exception
{
if
(
options
.
has
(
listMetadata
))
{
if
(
options
.
has
(
this
.
listMetadata
))
{
return
listServiceCapabilities
(
options
,
httpClient
);
return
listServiceCapabilities
(
options
,
this
.
httpClient
);
}
}
else
{
else
{
return
generateProject
(
options
,
httpClient
);
return
generateProject
(
options
,
this
.
httpClient
);
}
}
}
}
...
@@ -151,7 +166,8 @@ public class InitCommandOptionHandler extends OptionHandler {
...
@@ -151,7 +166,8 @@ public class InitCommandOptionHandler extends OptionHandler {
return
request
;
return
request
;
}
}
protected
ExitStatus
listServiceCapabilities
(
OptionSet
options
,
CloseableHttpClient
httpClient
)
throws
IOException
{
protected
ExitStatus
listServiceCapabilities
(
OptionSet
options
,
CloseableHttpClient
httpClient
)
throws
IOException
{
ListMetadataCommand
command
=
new
ListMetadataCommand
(
httpClient
);
ListMetadataCommand
command
=
new
ListMetadataCommand
(
httpClient
);
Log
.
info
(
command
.
generateReport
(
determineServiceUrl
(
options
)));
Log
.
info
(
command
.
generateReport
(
determineServiceUrl
(
options
)));
return
ExitStatus
.
OK
;
return
ExitStatus
.
OK
;
...
@@ -161,19 +177,22 @@ public class InitCommandOptionHandler extends OptionHandler {
...
@@ -161,19 +177,22 @@ public class InitCommandOptionHandler extends OptionHandler {
ProjectGenerationRequest
request
=
createProjectGenerationRequest
(
options
);
ProjectGenerationRequest
request
=
createProjectGenerationRequest
(
options
);
boolean
forceValue
=
options
.
has
(
this
.
force
);
boolean
forceValue
=
options
.
has
(
this
.
force
);
try
{
try
{
ProjectGenerationResponse
entity
=
new
InitializrServiceHttpInvoker
(
httpClient
).
generate
(
request
);
ProjectGenerationResponse
entity
=
new
InitializrServiceHttpInvoker
(
httpClient
).
generate
(
request
);
if
(
options
.
has
(
this
.
extract
))
{
if
(
options
.
has
(
this
.
extract
))
{
if
(
isZipArchive
(
entity
))
{
if
(
isZipArchive
(
entity
))
{
return
extractProject
(
entity
,
options
.
valueOf
(
this
.
output
),
forceValue
);
return
extractProject
(
entity
,
options
.
valueOf
(
this
.
output
),
forceValue
);
}
}
else
{
else
{
Log
.
info
(
"Could not extract '"
+
entity
.
getContentType
()
+
"'"
);
Log
.
info
(
"Could not extract '"
+
entity
.
getContentType
()
+
"'"
);
}
}
}
}
String
outputFileName
=
entity
.
getFileName
()
!=
null
?
entity
.
getFileName
()
:
options
.
valueOf
(
this
.
output
);
String
outputFileName
=
entity
.
getFileName
()
!=
null
?
entity
.
getFileName
()
:
options
.
valueOf
(
this
.
output
);
if
(
outputFileName
==
null
)
{
if
(
outputFileName
==
null
)
{
Log
.
error
(
"Could not save the project, the server did not set a preferred "
+
Log
.
error
(
"Could not save the project, the server did not set a preferred "
"file name. Use --output to specify the output location for the project."
);
+
"file name. Use --output to specify the output location for the project."
);
return
ExitStatus
.
ERROR
;
return
ExitStatus
.
ERROR
;
}
}
return
writeProject
(
entity
,
outputFileName
,
forceValue
);
return
writeProject
(
entity
,
outputFileName
,
forceValue
);
...
@@ -192,8 +211,8 @@ public class InitCommandOptionHandler extends OptionHandler {
...
@@ -192,8 +211,8 @@ public class InitCommandOptionHandler extends OptionHandler {
return
options
.
valueOf
(
this
.
target
);
return
options
.
valueOf
(
this
.
target
);
}
}
private
ExitStatus
writeProject
(
ProjectGenerationResponse
entity
,
String
outputFileName
,
boolean
overwrite
)
private
ExitStatus
writeProject
(
ProjectGenerationResponse
entity
,
throws
IOException
{
String
outputFileName
,
boolean
overwrite
)
throws
IOException
{
File
f
=
new
File
(
outputFileName
);
File
f
=
new
File
(
outputFileName
);
if
(
f
.
exists
())
{
if
(
f
.
exists
())
{
...
@@ -204,8 +223,9 @@ public class InitCommandOptionHandler extends OptionHandler {
...
@@ -204,8 +223,9 @@ public class InitCommandOptionHandler extends OptionHandler {
}
}
}
}
else
{
else
{
Log
.
error
(
"File '"
+
f
.
getName
()
+
"' already exists. Use --force if you want to "
+
Log
.
error
(
"File '"
+
f
.
getName
()
"overwrite or --output to specify an alternate location."
);
+
"' already exists. Use --force if you want to "
+
"overwrite or --output to specify an alternate location."
);
return
ExitStatus
.
ERROR
;
return
ExitStatus
.
ERROR
;
}
}
}
}
...
@@ -232,12 +252,15 @@ public class InitCommandOptionHandler extends OptionHandler {
...
@@ -232,12 +252,15 @@ public class InitCommandOptionHandler extends OptionHandler {
}
}
}
}
private
ExitStatus
extractProject
(
ProjectGenerationResponse
entity
,
String
outputValue
,
boolean
overwrite
)
throws
IOException
{
private
ExitStatus
extractProject
(
ProjectGenerationResponse
entity
,
File
output
=
outputValue
!=
null
?
new
File
(
outputValue
)
:
new
File
(
System
.
getProperty
(
"user.dir"
));
String
outputValue
,
boolean
overwrite
)
throws
IOException
{
File
output
=
outputValue
!=
null
?
new
File
(
outputValue
)
:
new
File
(
System
.
getProperty
(
"user.dir"
));
if
(!
output
.
exists
())
{
if
(!
output
.
exists
())
{
output
.
mkdirs
();
output
.
mkdirs
();
}
}
ZipInputStream
zipIn
=
new
ZipInputStream
(
new
ByteArrayInputStream
(
entity
.
getContent
()));
ZipInputStream
zipIn
=
new
ZipInputStream
(
new
ByteArrayInputStream
(
entity
.
getContent
()));
try
{
try
{
ZipEntry
entry
=
zipIn
.
getNextEntry
();
ZipEntry
entry
=
zipIn
.
getNextEntry
();
while
(
entry
!=
null
)
{
while
(
entry
!=
null
)
{
...
@@ -245,8 +268,10 @@ public class InitCommandOptionHandler extends OptionHandler {
...
@@ -245,8 +268,10 @@ public class InitCommandOptionHandler extends OptionHandler {
if
(
f
.
exists
()
&&
!
overwrite
)
{
if
(
f
.
exists
()
&&
!
overwrite
)
{
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
f
.
isDirectory
()
?
"Directory"
:
"File"
)
sb
.
append
(
f
.
isDirectory
()
?
"Directory"
:
"File"
)
.
append
(
" '"
).
append
(
f
.
getName
()).
append
(
"' already exists. Use --force if you want to "
+
.
append
(
" '"
)
"overwrite or --output to specify an alternate location."
);
.
append
(
f
.
getName
())
.
append
(
"' already exists. Use --force if you want to "
+
"overwrite or --output to specify an alternate location."
);
Log
.
error
(
sb
.
toString
());
Log
.
error
(
sb
.
toString
());
return
ExitStatus
.
ERROR
;
return
ExitStatus
.
ERROR
;
}
}
...
@@ -268,7 +293,8 @@ public class InitCommandOptionHandler extends OptionHandler {
...
@@ -268,7 +293,8 @@ public class InitCommandOptionHandler extends OptionHandler {
}
}
private
void
extractZipEntry
(
ZipInputStream
in
,
File
outputFile
)
throws
IOException
{
private
void
extractZipEntry
(
ZipInputStream
in
,
File
outputFile
)
throws
IOException
{
BufferedOutputStream
out
=
new
BufferedOutputStream
(
new
FileOutputStream
(
outputFile
));
BufferedOutputStream
out
=
new
BufferedOutputStream
(
new
FileOutputStream
(
outputFile
));
try
{
try
{
StreamUtils
.
copy
(
in
,
out
);
StreamUtils
.
copy
(
in
,
out
);
}
}
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrServiceHttpInvoker.java
View file @
b89e5e0a
...
@@ -31,7 +31,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
...
@@ -31,7 +31,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
import
org.apache.http.message.BasicHeader
;
import
org.apache.http.message.BasicHeader
;
import
org.json.JSONException
;
import
org.json.JSONException
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
org.springframework.boot.cli.util.Log
;
import
org.springframework.boot.cli.util.Log
;
import
org.springframework.util.StreamUtils
;
import
org.springframework.util.StreamUtils
;
import
org.springframework.util.StringUtils
;
import
org.springframework.util.StringUtils
;
...
@@ -57,7 +56,8 @@ class InitializrServiceHttpInvoker {
...
@@ -57,7 +56,8 @@ class InitializrServiceHttpInvoker {
* Generate a project based on the specified {@link ProjectGenerationRequest}
* Generate a project based on the specified {@link ProjectGenerationRequest}
* @return an entity defining the project
* @return an entity defining the project
*/
*/
ProjectGenerationResponse
generate
(
ProjectGenerationRequest
request
)
throws
IOException
{
ProjectGenerationResponse
generate
(
ProjectGenerationRequest
request
)
throws
IOException
{
Log
.
info
(
"Using service at "
+
request
.
getServiceUrl
());
Log
.
info
(
"Using service at "
+
request
.
getServiceUrl
());
InitializrServiceMetadata
metadata
=
loadMetadata
(
request
.
getServiceUrl
());
InitializrServiceMetadata
metadata
=
loadMetadata
(
request
.
getServiceUrl
());
URI
url
=
request
.
generateUrl
(
metadata
);
URI
url
=
request
.
generateUrl
(
metadata
);
...
@@ -65,7 +65,8 @@ class InitializrServiceHttpInvoker {
...
@@ -65,7 +65,8 @@ class InitializrServiceHttpInvoker {
HttpEntity
httpEntity
=
httpResponse
.
getEntity
();
HttpEntity
httpEntity
=
httpResponse
.
getEntity
();
if
(
httpEntity
==
null
)
{
if
(
httpEntity
==
null
)
{
throw
new
ProjectGenerationException
(
"No content received from server using '"
+
url
+
"'"
);
throw
new
ProjectGenerationException
(
"No content received from server using '"
+
url
+
"'"
);
}
}
if
(
httpResponse
.
getStatusLine
().
getStatusCode
()
!=
200
)
{
if
(
httpResponse
.
getStatusLine
().
getStatusCode
()
!=
200
)
{
throw
buildProjectGenerationException
(
request
.
getServiceUrl
(),
httpResponse
);
throw
buildProjectGenerationException
(
request
.
getServiceUrl
(),
httpResponse
);
...
@@ -79,23 +80,26 @@ class InitializrServiceHttpInvoker {
...
@@ -79,23 +80,26 @@ class InitializrServiceHttpInvoker {
InitializrServiceMetadata
loadMetadata
(
String
serviceUrl
)
throws
IOException
{
InitializrServiceMetadata
loadMetadata
(
String
serviceUrl
)
throws
IOException
{
CloseableHttpResponse
httpResponse
=
executeInitializrMetadataRetrieval
(
serviceUrl
);
CloseableHttpResponse
httpResponse
=
executeInitializrMetadataRetrieval
(
serviceUrl
);
if
(
httpResponse
.
getEntity
()
==
null
)
{
if
(
httpResponse
.
getEntity
()
==
null
)
{
throw
new
ProjectGenerationException
(
"No content received from server using '"
+
serviceUrl
+
"'"
);
throw
new
ProjectGenerationException
(
"No content received from server using '"
+
serviceUrl
+
"'"
);
}
}
if
(
httpResponse
.
getStatusLine
().
getStatusCode
()
!=
200
)
{
if
(
httpResponse
.
getStatusLine
().
getStatusCode
()
!=
200
)
{
throw
buildProjectGenerationException
(
serviceUrl
,
httpResponse
);
throw
buildProjectGenerationException
(
serviceUrl
,
httpResponse
);
}
}
try
{
try
{
HttpEntity
httpEntity
=
httpResponse
.
getEntity
();
HttpEntity
httpEntity
=
httpResponse
.
getEntity
();
JSONObject
root
=
getContentAsJson
(
getContent
(
httpEntity
),
getContentType
(
httpEntity
));
JSONObject
root
=
getContentAsJson
(
getContent
(
httpEntity
),
getContentType
(
httpEntity
));
return
new
InitializrServiceMetadata
(
root
);
return
new
InitializrServiceMetadata
(
root
);
}
}
catch
(
JSONException
e
)
{
catch
(
JSONException
e
)
{
throw
new
ProjectGenerationException
(
"Invalid content received from server ("
+
e
.
getMessage
()
+
")"
);
throw
new
ProjectGenerationException
(
"Invalid content received from server ("
+
e
.
getMessage
()
+
")"
);
}
}
}
}
private
ProjectGenerationResponse
createResponse
(
CloseableHttpResponse
httpResponse
,
HttpEntity
httpEntity
)
private
ProjectGenerationResponse
createResponse
(
CloseableHttpResponse
httpResponse
,
throws
IOException
{
HttpEntity
httpEntity
)
throws
IOException
{
ProjectGenerationResponse
response
=
new
ProjectGenerationResponse
();
ProjectGenerationResponse
response
=
new
ProjectGenerationResponse
();
ContentType
contentType
=
ContentType
.
getOrDefault
(
httpEntity
);
ContentType
contentType
=
ContentType
.
getOrDefault
(
httpEntity
);
response
.
setContentType
(
contentType
);
response
.
setContentType
(
contentType
);
...
@@ -108,7 +112,8 @@ class InitializrServiceHttpInvoker {
...
@@ -108,7 +112,8 @@ class InitializrServiceHttpInvoker {
in
.
close
();
in
.
close
();
}
}
String
detectedFileName
=
extractFileName
(
httpResponse
.
getFirstHeader
(
"Content-Disposition"
));
String
detectedFileName
=
extractFileName
(
httpResponse
.
getFirstHeader
(
"Content-Disposition"
));
if
(
detectedFileName
!=
null
)
{
if
(
detectedFileName
!=
null
)
{
response
.
setFileName
(
detectedFileName
);
response
.
setFileName
(
detectedFileName
);
}
}
...
@@ -124,8 +129,8 @@ class InitializrServiceHttpInvoker {
...
@@ -124,8 +129,8 @@ class InitializrServiceHttpInvoker {
return
this
.
httpClient
.
execute
(
get
);
return
this
.
httpClient
.
execute
(
get
);
}
}
catch
(
IOException
e
)
{
catch
(
IOException
e
)
{
throw
new
ProjectGenerationException
(
throw
new
ProjectGenerationException
(
"Failed to invoke server at '"
+
url
"Failed to invoke server at '"
+
url
+
"' ("
+
e
.
getMessage
()
+
")"
);
+
"' ("
+
e
.
getMessage
()
+
")"
);
}
}
}
}
...
@@ -140,11 +145,11 @@ class InitializrServiceHttpInvoker {
...
@@ -140,11 +145,11 @@ class InitializrServiceHttpInvoker {
}
}
catch
(
IOException
e
)
{
catch
(
IOException
e
)
{
throw
new
ProjectGenerationException
(
throw
new
ProjectGenerationException
(
"Failed to retrieve metadata from service at '"
+
serviceUrl
+
"' ("
+
e
.
getMessage
()
+
")"
);
"Failed to retrieve metadata from service at '"
+
serviceUrl
+
"' ("
+
e
.
getMessage
()
+
")"
);
}
}
}
}
private
byte
[]
getContent
(
HttpEntity
httpEntity
)
throws
IOException
{
private
byte
[]
getContent
(
HttpEntity
httpEntity
)
throws
IOException
{
InputStream
in
=
httpEntity
.
getContent
();
InputStream
in
=
httpEntity
.
getContent
();
try
{
try
{
...
@@ -160,12 +165,14 @@ class InitializrServiceHttpInvoker {
...
@@ -160,12 +165,14 @@ class InitializrServiceHttpInvoker {
}
}
private
JSONObject
getContentAsJson
(
byte
[]
content
,
ContentType
contentType
)
{
private
JSONObject
getContentAsJson
(
byte
[]
content
,
ContentType
contentType
)
{
Charset
charset
=
contentType
.
getCharset
()
!=
null
?
contentType
.
getCharset
()
:
Charset
.
forName
(
"UTF-8"
);
Charset
charset
=
contentType
.
getCharset
()
!=
null
?
contentType
.
getCharset
()
:
Charset
.
forName
(
"UTF-8"
);
String
data
=
new
String
(
content
,
charset
);
String
data
=
new
String
(
content
,
charset
);
return
new
JSONObject
(
data
);
return
new
JSONObject
(
data
);
}
}
private
ProjectGenerationException
buildProjectGenerationException
(
String
url
,
CloseableHttpResponse
httpResponse
)
{
private
ProjectGenerationException
buildProjectGenerationException
(
String
url
,
CloseableHttpResponse
httpResponse
)
{
StringBuilder
sb
=
new
StringBuilder
(
"Project generation failed using '"
);
StringBuilder
sb
=
new
StringBuilder
(
"Project generation failed using '"
);
sb
.
append
(
url
).
append
(
"' - service returned "
)
sb
.
append
(
url
).
append
(
"' - service returned "
)
.
append
(
httpResponse
.
getStatusLine
().
getReasonPhrase
());
.
append
(
httpResponse
.
getStatusLine
().
getReasonPhrase
());
...
@@ -174,7 +181,9 @@ class InitializrServiceHttpInvoker {
...
@@ -174,7 +181,9 @@ class InitializrServiceHttpInvoker {
sb
.
append
(
": '"
).
append
(
error
).
append
(
"'"
);
sb
.
append
(
": '"
).
append
(
error
).
append
(
"'"
);
}
}
else
{
else
{
sb
.
append
(
" (unexpected "
).
append
(
httpResponse
.
getStatusLine
().
getStatusCode
()).
append
(
" error)"
);
sb
.
append
(
" (unexpected "
)
.
append
(
httpResponse
.
getStatusLine
().
getStatusCode
())
.
append
(
" error)"
);
}
}
throw
new
ProjectGenerationException
(
sb
.
toString
());
throw
new
ProjectGenerationException
(
sb
.
toString
());
}
}
...
@@ -184,7 +193,8 @@ class InitializrServiceHttpInvoker {
...
@@ -184,7 +193,8 @@ class InitializrServiceHttpInvoker {
return
null
;
return
null
;
}
}
try
{
try
{
JSONObject
error
=
getContentAsJson
(
getContent
(
entity
),
getContentType
(
entity
));
JSONObject
error
=
getContentAsJson
(
getContent
(
entity
),
getContentType
(
entity
));
if
(
error
.
has
(
"message"
))
{
if
(
error
.
has
(
"message"
))
{
return
error
.
getString
(
"message"
);
return
error
.
getString
(
"message"
);
}
}
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/InitializrServiceMetadata.java
View file @
b89e5e0a
...
@@ -50,14 +50,12 @@ class InitializrServiceMetadata {
...
@@ -50,14 +50,12 @@ class InitializrServiceMetadata {
private
static
final
String
DEFAULT_ATTRIBUTE
=
"default"
;
private
static
final
String
DEFAULT_ATTRIBUTE
=
"default"
;
private
final
Map
<
String
,
Dependency
>
dependencies
;
private
final
Map
<
String
,
Dependency
>
dependencies
;
private
final
MetadataHolder
<
String
,
ProjectType
>
projectTypes
;
private
final
MetadataHolder
<
String
,
ProjectType
>
projectTypes
;
private
final
Map
<
String
,
String
>
defaults
;
private
final
Map
<
String
,
String
>
defaults
;
/**
/**
* Creates a new instance using the specified root {@link JSONObject}.
* Creates a new instance using the specified root {@link JSONObject}.
*/
*/
...
@@ -70,7 +68,8 @@ class InitializrServiceMetadata {
...
@@ -70,7 +68,8 @@ class InitializrServiceMetadata {
InitializrServiceMetadata
(
ProjectType
defaultProjectType
)
{
InitializrServiceMetadata
(
ProjectType
defaultProjectType
)
{
this
.
dependencies
=
new
HashMap
<
String
,
Dependency
>();
this
.
dependencies
=
new
HashMap
<
String
,
Dependency
>();
this
.
projectTypes
=
new
MetadataHolder
<
String
,
ProjectType
>();
this
.
projectTypes
=
new
MetadataHolder
<
String
,
ProjectType
>();
this
.
projectTypes
.
getContent
().
put
(
defaultProjectType
.
getId
(),
defaultProjectType
);
this
.
projectTypes
.
getContent
()
.
put
(
defaultProjectType
.
getId
(),
defaultProjectType
);
this
.
projectTypes
.
setDefaultItem
(
defaultProjectType
);
this
.
projectTypes
.
setDefaultItem
(
defaultProjectType
);
this
.
defaults
=
new
HashMap
<
String
,
String
>();
this
.
defaults
=
new
HashMap
<
String
,
String
>();
}
}
...
@@ -79,35 +78,35 @@ class InitializrServiceMetadata {
...
@@ -79,35 +78,35 @@ class InitializrServiceMetadata {
* Return the dependencies supported by the service.
* Return the dependencies supported by the service.
*/
*/
public
Collection
<
Dependency
>
getDependencies
()
{
public
Collection
<
Dependency
>
getDependencies
()
{
return
dependencies
.
values
();
return
this
.
dependencies
.
values
();
}
}
/**
/**
* Return the dependency with the specified id or {@code null} if no
* Return the dependency with the specified id or {@code null} if no
such dependency
*
such dependency
exists.
* exists.
*/
*/
public
Dependency
getDependency
(
String
id
)
{
public
Dependency
getDependency
(
String
id
)
{
return
dependencies
.
get
(
id
);
return
this
.
dependencies
.
get
(
id
);
}
}
/**
/**
* Return the project types supported by the service.
* Return the project types supported by the service.
*/
*/
public
Map
<
String
,
ProjectType
>
getProjectTypes
()
{
public
Map
<
String
,
ProjectType
>
getProjectTypes
()
{
return
projectTypes
.
getContent
();
return
this
.
projectTypes
.
getContent
();
}
}
/**
/**
* Return the default type to use or {@code null} or the metadata does
* Return the default type to use or {@code null} or the metadata does
not define any
*
not define any
default.
* default.
*/
*/
public
ProjectType
getDefaultType
()
{
public
ProjectType
getDefaultType
()
{
if
(
projectTypes
.
getDefaultItem
()
!=
null
)
{
if
(
this
.
projectTypes
.
getDefaultItem
()
!=
null
)
{
return
projectTypes
.
getDefaultItem
();
return
this
.
projectTypes
.
getDefaultItem
();
}
}
String
defaultTypeId
=
getDefaults
().
get
(
"type"
);
String
defaultTypeId
=
getDefaults
().
get
(
"type"
);
if
(
defaultTypeId
!=
null
)
{
if
(
defaultTypeId
!=
null
)
{
return
projectTypes
.
getContent
().
get
(
defaultTypeId
);
return
this
.
projectTypes
.
getContent
().
get
(
defaultTypeId
);
}
}
return
null
;
return
null
;
}
}
...
@@ -116,7 +115,7 @@ class InitializrServiceMetadata {
...
@@ -116,7 +115,7 @@ class InitializrServiceMetadata {
* Returns the defaults applicable to the service.
* Returns the defaults applicable to the service.
*/
*/
public
Map
<
String
,
String
>
getDefaults
()
{
public
Map
<
String
,
String
>
getDefaults
()
{
return
defaults
;
return
this
.
defaults
;
}
}
private
Map
<
String
,
Dependency
>
parseDependencies
(
JSONObject
root
)
{
private
Map
<
String
,
Dependency
>
parseDependencies
(
JSONObject
root
)
{
...
@@ -221,11 +220,11 @@ class InitializrServiceMetadata {
...
@@ -221,11 +220,11 @@ class InitializrServiceMetadata {
}
}
public
Map
<
K
,
T
>
getContent
()
{
public
Map
<
K
,
T
>
getContent
()
{
return
content
;
return
this
.
content
;
}
}
public
T
getDefaultItem
()
{
public
T
getDefaultItem
()
{
return
defaultItem
;
return
this
.
defaultItem
;
}
}
public
void
setDefaultItem
(
T
defaultItem
)
{
public
void
setDefaultItem
(
T
defaultItem
)
{
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ListMetadataCommand.java
View file @
b89e5e0a
...
@@ -51,19 +51,19 @@ class ListMetadataCommand {
...
@@ -51,19 +51,19 @@ class ListMetadataCommand {
* capabilities as advertized by the root endpoint.
* capabilities as advertized by the root endpoint.
*/
*/
String
generateReport
(
String
serviceUrl
)
throws
IOException
{
String
generateReport
(
String
serviceUrl
)
throws
IOException
{
InitializrServiceMetadata
metadata
=
initializrServiceInvoker
.
loadMetadata
(
serviceUrl
);
InitializrServiceMetadata
metadata
=
this
.
initializrServiceInvoker
.
loadMetadata
(
serviceUrl
);
String
header
=
"Capabilities of "
+
serviceUrl
;
String
header
=
"Capabilities of "
+
serviceUrl
;
int
size
=
header
.
length
();
int
size
=
header
.
length
();
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
StringUtils
.
repeat
(
"="
,
size
)).
append
(
NEW_LINE
)
sb
.
append
(
StringUtils
.
repeat
(
"="
,
size
)).
append
(
NEW_LINE
).
append
(
header
)
.
append
(
header
).
append
(
NEW_LINE
)
.
append
(
NEW_LINE
).
append
(
StringUtils
.
repeat
(
"="
,
size
)).
append
(
NEW_LINE
)
.
append
(
StringUtils
.
repeat
(
"="
,
size
)).
append
(
NEW_LINE
)
.
append
(
NEW_LINE
).
append
(
"Available dependencies:"
).
append
(
NEW_LINE
)
.
append
(
NEW_LINE
)
.
append
(
"Available dependencies:"
).
append
(
NEW_LINE
)
.
append
(
"-----------------------"
).
append
(
NEW_LINE
);
.
append
(
"-----------------------"
).
append
(
NEW_LINE
);
List
<
Dependency
>
dependencies
=
new
ArrayList
<
Dependency
>(
metadata
.
getDependencies
());
List
<
Dependency
>
dependencies
=
new
ArrayList
<
Dependency
>(
metadata
.
getDependencies
());
Collections
.
sort
(
dependencies
,
new
Comparator
<
Dependency
>()
{
Collections
.
sort
(
dependencies
,
new
Comparator
<
Dependency
>()
{
@Override
@Override
public
int
compare
(
Dependency
o1
,
Dependency
o2
)
{
public
int
compare
(
Dependency
o1
,
Dependency
o2
)
{
...
@@ -78,8 +78,7 @@ class ListMetadataCommand {
...
@@ -78,8 +78,7 @@ class ListMetadataCommand {
sb
.
append
(
NEW_LINE
);
sb
.
append
(
NEW_LINE
);
}
}
sb
.
append
(
NEW_LINE
)
sb
.
append
(
NEW_LINE
).
append
(
"Available project types:"
).
append
(
NEW_LINE
)
.
append
(
"Available project types:"
).
append
(
NEW_LINE
)
.
append
(
"------------------------"
).
append
(
NEW_LINE
);
.
append
(
"------------------------"
).
append
(
NEW_LINE
);
List
<
String
>
typeIds
=
new
ArrayList
<
String
>(
metadata
.
getProjectTypes
().
keySet
());
List
<
String
>
typeIds
=
new
ArrayList
<
String
>(
metadata
.
getProjectTypes
().
keySet
());
Collections
.
sort
(
typeIds
);
Collections
.
sort
(
typeIds
);
...
@@ -88,7 +87,8 @@ class ListMetadataCommand {
...
@@ -88,7 +87,8 @@ class ListMetadataCommand {
sb
.
append
(
typeId
).
append
(
" - "
).
append
(
type
.
getName
());
sb
.
append
(
typeId
).
append
(
" - "
).
append
(
type
.
getName
());
if
(!
type
.
getTags
().
isEmpty
())
{
if
(!
type
.
getTags
().
isEmpty
())
{
sb
.
append
(
" ["
);
sb
.
append
(
" ["
);
Iterator
<
Map
.
Entry
<
String
,
String
>>
it
=
type
.
getTags
().
entrySet
().
iterator
();
Iterator
<
Map
.
Entry
<
String
,
String
>>
it
=
type
.
getTags
().
entrySet
()
.
iterator
();
while
(
it
.
hasNext
())
{
while
(
it
.
hasNext
())
{
Map
.
Entry
<
String
,
String
>
entry
=
it
.
next
();
Map
.
Entry
<
String
,
String
>
entry
=
it
.
next
();
sb
.
append
(
entry
.
getKey
()).
append
(
":"
).
append
(
entry
.
getValue
());
sb
.
append
(
entry
.
getKey
()).
append
(
":"
).
append
(
entry
.
getValue
());
...
@@ -104,14 +104,14 @@ class ListMetadataCommand {
...
@@ -104,14 +104,14 @@ class ListMetadataCommand {
sb
.
append
(
NEW_LINE
);
sb
.
append
(
NEW_LINE
);
}
}
sb
.
append
(
NEW_LINE
)
sb
.
append
(
NEW_LINE
).
append
(
"Defaults:"
).
append
(
NEW_LINE
).
append
(
"---------"
)
.
append
(
"Defaults:"
).
append
(
NEW_LINE
)
.
append
(
NEW_LINE
);
.
append
(
"---------"
).
append
(
NEW_LINE
);
List
<
String
>
defaultsKeys
=
new
ArrayList
<
String
>(
metadata
.
getDefaults
().
keySet
());
List
<
String
>
defaultsKeys
=
new
ArrayList
<
String
>(
metadata
.
getDefaults
().
keySet
());
Collections
.
sort
(
defaultsKeys
);
Collections
.
sort
(
defaultsKeys
);
for
(
String
defaultsKey
:
defaultsKeys
)
{
for
(
String
defaultsKey
:
defaultsKeys
)
{
sb
.
append
(
defaultsKey
).
append
(
": "
).
append
(
metadata
.
getDefaults
().
get
(
defaultsKey
)).
append
(
NEW_LINE
);
sb
.
append
(
defaultsKey
).
append
(
": "
)
.
append
(
metadata
.
getDefaults
().
get
(
defaultsKey
)).
append
(
NEW_LINE
);
}
}
return
sb
.
toString
();
return
sb
.
toString
();
}
}
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerationRequest.java
View file @
b89e5e0a
...
@@ -61,7 +61,7 @@ class ProjectGenerationRequest {
...
@@ -61,7 +61,7 @@ class ProjectGenerationRequest {
* @see #DEFAULT_SERVICE_URL
* @see #DEFAULT_SERVICE_URL
*/
*/
public
String
getServiceUrl
()
{
public
String
getServiceUrl
()
{
return
serviceUrl
;
return
this
.
serviceUrl
;
}
}
public
void
setServiceUrl
(
String
serviceUrl
)
{
public
void
setServiceUrl
(
String
serviceUrl
)
{
...
@@ -72,7 +72,7 @@ class ProjectGenerationRequest {
...
@@ -72,7 +72,7 @@ class ProjectGenerationRequest {
* The location of the generated project.
* The location of the generated project.
*/
*/
public
String
getOutput
()
{
public
String
getOutput
()
{
return
output
;
return
this
.
output
;
}
}
public
void
setOutput
(
String
output
)
{
public
void
setOutput
(
String
output
)
{
...
@@ -83,7 +83,7 @@ class ProjectGenerationRequest {
...
@@ -83,7 +83,7 @@ class ProjectGenerationRequest {
* The Spring Boot version to use or {@code null} if it should not be customized.
* The Spring Boot version to use or {@code null} if it should not be customized.
*/
*/
public
String
getBootVersion
()
{
public
String
getBootVersion
()
{
return
bootVersion
;
return
this
.
bootVersion
;
}
}
public
void
setBootVersion
(
String
bootVersion
)
{
public
void
setBootVersion
(
String
bootVersion
)
{
...
@@ -94,14 +94,14 @@ class ProjectGenerationRequest {
...
@@ -94,14 +94,14 @@ class ProjectGenerationRequest {
* The identifiers of the dependencies to include in the project.
* The identifiers of the dependencies to include in the project.
*/
*/
public
List
<
String
>
getDependencies
()
{
public
List
<
String
>
getDependencies
()
{
return
dependencies
;
return
this
.
dependencies
;
}
}
/**
/**
* The Java version to use or {@code null} if it should not be customized.
* The Java version to use or {@code null} if it should not be customized.
*/
*/
public
String
getJavaVersion
()
{
public
String
getJavaVersion
()
{
return
javaVersion
;
return
this
.
javaVersion
;
}
}
public
void
setJavaVersion
(
String
javaVersion
)
{
public
void
setJavaVersion
(
String
javaVersion
)
{
...
@@ -112,7 +112,7 @@ class ProjectGenerationRequest {
...
@@ -112,7 +112,7 @@ class ProjectGenerationRequest {
* The packaging type or {@code null} if it should not be customized.
* The packaging type or {@code null} if it should not be customized.
*/
*/
public
String
getPackaging
()
{
public
String
getPackaging
()
{
return
packaging
;
return
this
.
packaging
;
}
}
public
void
setPackaging
(
String
packaging
)
{
public
void
setPackaging
(
String
packaging
)
{
...
@@ -120,11 +120,11 @@ class ProjectGenerationRequest {
...
@@ -120,11 +120,11 @@ class ProjectGenerationRequest {
}
}
/**
/**
* The build type to use. Ignored if a type is set. Can be used alongside
* The build type to use. Ignored if a type is set. Can be used alongside
the
*
the
{@link #getFormat() format} to identify the type to use.
* {@link #getFormat() format} to identify the type to use.
*/
*/
public
String
getBuild
()
{
public
String
getBuild
()
{
return
build
;
return
this
.
build
;
}
}
public
void
setBuild
(
String
build
)
{
public
void
setBuild
(
String
build
)
{
...
@@ -132,11 +132,11 @@ class ProjectGenerationRequest {
...
@@ -132,11 +132,11 @@ class ProjectGenerationRequest {
}
}
/**
/**
* The project format to use. Ignored if a type is set. Can be used alongside
* The project format to use. Ignored if a type is set. Can be used alongside
the
*
the
{@link #getBuild() build} to identify the type to use.
* {@link #getBuild() build} to identify the type to use.
*/
*/
public
String
getFormat
()
{
public
String
getFormat
()
{
return
format
;
return
this
.
format
;
}
}
public
void
setFormat
(
String
format
)
{
public
void
setFormat
(
String
format
)
{
...
@@ -144,11 +144,10 @@ class ProjectGenerationRequest {
...
@@ -144,11 +144,10 @@ class ProjectGenerationRequest {
}
}
/**
/**
* Specify if the type should be detected based on the build
* Specify if the type should be detected based on the build and format value.
* and format value.
*/
*/
public
boolean
isDetectType
()
{
public
boolean
isDetectType
()
{
return
detectType
;
return
this
.
detectType
;
}
}
public
void
setDetectType
(
boolean
detectType
)
{
public
void
setDetectType
(
boolean
detectType
)
{
...
@@ -156,12 +155,11 @@ class ProjectGenerationRequest {
...
@@ -156,12 +155,11 @@ class ProjectGenerationRequest {
}
}
/**
/**
* The type of project to generate. Should match one of the advertized type
* The type of project to generate. Should match one of the advertized type that the
* that the service supports. If not set, the default is retrieved from
* service supports. If not set, the default is retrieved from the service metadata.
* the service metadata.
*/
*/
public
String
getType
()
{
public
String
getType
()
{
return
type
;
return
t
his
.
t
ype
;
}
}
public
void
setType
(
String
type
)
{
public
void
setType
(
String
type
)
{
...
@@ -169,12 +167,11 @@ class ProjectGenerationRequest {
...
@@ -169,12 +167,11 @@ class ProjectGenerationRequest {
}
}
/**
/**
* Generates the URL to use to generate a project represented
* Generates the URL to use to generate a project represented by this request
* by this request
*/
*/
URI
generateUrl
(
InitializrServiceMetadata
metadata
)
{
URI
generateUrl
(
InitializrServiceMetadata
metadata
)
{
try
{
try
{
URIBuilder
builder
=
new
URIBuilder
(
serviceUrl
);
URIBuilder
builder
=
new
URIBuilder
(
this
.
serviceUrl
);
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
if
(
builder
.
getPath
()
!=
null
)
{
if
(
builder
.
getPath
()
!=
null
)
{
sb
.
append
(
builder
.
getPath
());
sb
.
append
(
builder
.
getPath
());
...
@@ -188,7 +185,7 @@ class ProjectGenerationRequest {
...
@@ -188,7 +185,7 @@ class ProjectGenerationRequest {
if
(
this
.
bootVersion
!=
null
)
{
if
(
this
.
bootVersion
!=
null
)
{
builder
.
setParameter
(
"bootVersion"
,
this
.
bootVersion
);
builder
.
setParameter
(
"bootVersion"
,
this
.
bootVersion
);
}
}
for
(
String
dependency
:
dependencies
)
{
for
(
String
dependency
:
this
.
dependencies
)
{
builder
.
addParameter
(
"style"
,
dependency
);
builder
.
addParameter
(
"style"
,
dependency
);
}
}
if
(
this
.
javaVersion
!=
null
)
{
if
(
this
.
javaVersion
!=
null
)
{
...
@@ -204,7 +201,8 @@ class ProjectGenerationRequest {
...
@@ -204,7 +201,8 @@ class ProjectGenerationRequest {
return
builder
.
build
();
return
builder
.
build
();
}
}
catch
(
URISyntaxException
e
)
{
catch
(
URISyntaxException
e
)
{
throw
new
ProjectGenerationException
(
"Invalid service URL ("
+
e
.
getMessage
()
+
")"
);
throw
new
ProjectGenerationException
(
"Invalid service URL ("
+
e
.
getMessage
()
+
")"
);
}
}
}
}
...
@@ -212,12 +210,13 @@ class ProjectGenerationRequest {
...
@@ -212,12 +210,13 @@ class ProjectGenerationRequest {
if
(
this
.
type
!=
null
)
{
if
(
this
.
type
!=
null
)
{
ProjectType
result
=
metadata
.
getProjectTypes
().
get
(
this
.
type
);
ProjectType
result
=
metadata
.
getProjectTypes
().
get
(
this
.
type
);
if
(
result
==
null
)
{
if
(
result
==
null
)
{
throw
new
ProjectGenerationException
((
"No project type with id '"
+
this
.
type
+
throw
new
ProjectGenerationException
((
"No project type with id '"
"' - check the service capabilities (--list)"
));
+
this
.
type
+
"' - check the service capabilities (--list)"
));
}
}
}
}
if
(
isDetectType
())
{
if
(
isDetectType
())
{
Map
<
String
,
ProjectType
>
types
=
new
HashMap
<
String
,
ProjectType
>(
metadata
.
getProjectTypes
());
Map
<
String
,
ProjectType
>
types
=
new
HashMap
<
String
,
ProjectType
>(
metadata
.
getProjectTypes
());
if
(
this
.
build
!=
null
)
{
if
(
this
.
build
!=
null
)
{
filter
(
types
,
"build"
,
this
.
build
);
filter
(
types
,
"build"
,
this
.
build
);
}
}
...
@@ -228,24 +227,29 @@ class ProjectGenerationRequest {
...
@@ -228,24 +227,29 @@ class ProjectGenerationRequest {
return
types
.
values
().
iterator
().
next
();
return
types
.
values
().
iterator
().
next
();
}
}
else
if
(
types
.
size
()
==
0
)
{
else
if
(
types
.
size
()
==
0
)
{
throw
new
ProjectGenerationException
(
"No type found with build '"
+
this
.
build
+
"' and format '"
throw
new
ProjectGenerationException
(
"No type found with build '"
+
this
.
format
+
"' check the service capabilities (--list)"
);
+
this
.
build
+
"' and format '"
+
this
.
format
+
"' check the service capabilities (--list)"
);
}
}
else
{
else
{
throw
new
ProjectGenerationException
(
"Multiple types found with build '"
+
this
.
build
throw
new
ProjectGenerationException
(
"Multiple types found with build '"
+
"' and format '"
+
this
.
format
+
"' use --type with a more specific value "
+
types
.
keySet
());
+
this
.
build
+
"' and format '"
+
this
.
format
+
"' use --type with a more specific value "
+
types
.
keySet
());
}
}
}
}
ProjectType
defaultType
=
metadata
.
getDefaultType
();
ProjectType
defaultType
=
metadata
.
getDefaultType
();
if
(
defaultType
==
null
)
{
if
(
defaultType
==
null
)
{
throw
new
ProjectGenerationException
((
"No project type is set and no default is defined. "
+
throw
new
ProjectGenerationException
(
"Check the service capabilities (--list)"
));
(
"No project type is set and no default is defined. "
+
"Check the service capabilities (--list)"
));
}
}
return
defaultType
;
return
defaultType
;
}
}
private
static
void
filter
(
Map
<
String
,
ProjectType
>
projects
,
String
tag
,
String
tagValue
)
{
private
static
void
filter
(
Map
<
String
,
ProjectType
>
projects
,
String
tag
,
for
(
Iterator
<
Map
.
Entry
<
String
,
ProjectType
>>
it
=
projects
.
entrySet
().
iterator
();
it
.
hasNext
();
)
{
String
tagValue
)
{
for
(
Iterator
<
Map
.
Entry
<
String
,
ProjectType
>>
it
=
projects
.
entrySet
().
iterator
();
it
.
hasNext
();)
{
Map
.
Entry
<
String
,
ProjectType
>
entry
=
it
.
next
();
Map
.
Entry
<
String
,
ProjectType
>
entry
=
it
.
next
();
String
value
=
entry
.
getValue
().
getTags
().
get
(
tag
);
String
value
=
entry
.
getValue
().
getTags
().
get
(
tag
);
if
(!
tagValue
.
equals
(
value
))
{
if
(!
tagValue
.
equals
(
value
))
{
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectGenerationResponse.java
View file @
b89e5e0a
...
@@ -50,7 +50,7 @@ class ProjectGenerationResponse {
...
@@ -50,7 +50,7 @@ class ProjectGenerationResponse {
* The generated project archive or file.
* The generated project archive or file.
*/
*/
public
byte
[]
getContent
()
{
public
byte
[]
getContent
()
{
return
content
;
return
this
.
content
;
}
}
public
void
setContent
(
byte
[]
content
)
{
public
void
setContent
(
byte
[]
content
)
{
...
@@ -58,11 +58,11 @@ class ProjectGenerationResponse {
...
@@ -58,11 +58,11 @@ class ProjectGenerationResponse {
}
}
/**
/**
* The preferred file name to use to store the entity on disk or {@code null}
* The preferred file name to use to store the entity on disk or {@code null}
if no
*
if no
preferred value has been set.
* preferred value has been set.
*/
*/
public
String
getFileName
()
{
public
String
getFileName
()
{
return
fileName
;
return
this
.
fileName
;
}
}
public
void
setFileName
(
String
fileName
)
{
public
void
setFileName
(
String
fileName
)
{
...
...
spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ProjectType.java
View file @
b89e5e0a
...
@@ -38,7 +38,8 @@ class ProjectType {
...
@@ -38,7 +38,8 @@ class ProjectType {
private
final
Map
<
String
,
String
>
tags
=
new
HashMap
<
String
,
String
>();
private
final
Map
<
String
,
String
>
tags
=
new
HashMap
<
String
,
String
>();
public
ProjectType
(
String
id
,
String
name
,
String
action
,
boolean
defaultType
,
Map
<
String
,
String
>
tags
)
{
public
ProjectType
(
String
id
,
String
name
,
String
action
,
boolean
defaultType
,
Map
<
String
,
String
>
tags
)
{
this
.
id
=
id
;
this
.
id
=
id
;
this
.
name
=
name
;
this
.
name
=
name
;
this
.
action
=
action
;
this
.
action
=
action
;
...
@@ -49,22 +50,22 @@ class ProjectType {
...
@@ -49,22 +50,22 @@ class ProjectType {
}
}
public
String
getId
()
{
public
String
getId
()
{
return
id
;
return
this
.
id
;
}
}
public
String
getName
()
{
public
String
getName
()
{
return
name
;
return
this
.
name
;
}
}
public
String
getAction
()
{
public
String
getAction
()
{
return
action
;
return
this
.
action
;
}
}
public
boolean
isDefaultType
()
{
public
boolean
isDefaultType
()
{
return
defaultType
;
return
this
.
defaultType
;
}
}
public
Map
<
String
,
String
>
getTags
()
{
public
Map
<
String
,
String
>
getTags
()
{
return
Collections
.
unmodifiableMap
(
tags
);
return
Collections
.
unmodifiableMap
(
t
his
.
t
ags
);
}
}
}
}
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/AbstractHttpClientMockTests.java
View file @
b89e5e0a
...
@@ -30,12 +30,14 @@ import org.apache.http.message.BasicHeader;
...
@@ -30,12 +30,14 @@ import org.apache.http.message.BasicHeader;
import
org.hamcrest.Matcher
;
import
org.hamcrest.Matcher
;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
org.mockito.ArgumentMatcher
;
import
org.mockito.ArgumentMatcher
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.util.StreamUtils
;
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 {
...
@@ -51,14 +53,16 @@ public abstract class AbstractHttpClientMockTests {
protected
void
mockSuccessfulMetadataGet
(
String
version
)
throws
IOException
{
protected
void
mockSuccessfulMetadataGet
(
String
version
)
throws
IOException
{
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
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
());
byte
[]
content
=
StreamUtils
.
copyToByteArray
(
resource
.
getInputStream
());
mockHttpEntity
(
response
,
content
,
"application/json"
);
mockHttpEntity
(
response
,
content
,
"application/json"
);
mockStatus
(
response
,
200
);
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
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet
();
mockSuccessfulMetadataGet
();
...
@@ -66,33 +70,39 @@ public abstract class AbstractHttpClientMockTests {
...
@@ -66,33 +70,39 @@ public abstract class AbstractHttpClientMockTests {
mockHttpEntity
(
response
,
request
.
content
,
request
.
contentType
);
mockHttpEntity
(
response
,
request
.
content
,
request
.
contentType
);
mockStatus
(
response
,
200
);
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
);
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
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet
();
mockSuccessfulMetadataGet
();
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
mockHttpEntity
(
response
,
createJsonError
(
status
,
message
).
getBytes
(),
"application/json"
);
mockHttpEntity
(
response
,
createJsonError
(
status
,
message
).
getBytes
(),
"application/json"
);
mockStatus
(
response
,
status
);
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
{
protected
void
mockMetadataGetError
(
int
status
,
String
message
)
throws
IOException
{
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
mockHttpEntity
(
response
,
createJsonError
(
status
,
message
).
getBytes
(),
"application/json"
);
mockHttpEntity
(
response
,
createJsonError
(
status
,
message
).
getBytes
(),
"application/json"
);
mockStatus
(
response
,
status
);
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
{
try
{
HttpEntity
entity
=
mock
(
HttpEntity
.
class
);
HttpEntity
entity
=
mock
(
HttpEntity
.
class
);
when
(
entity
.
getContent
()).
thenReturn
(
new
ByteArrayInputStream
(
content
));
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
(
entity
.
getContentType
()).
thenReturn
(
contentTypeHeader
);
when
(
response
.
getEntity
()).
thenReturn
(
entity
);
when
(
response
.
getEntity
()).
thenReturn
(
entity
);
return
entity
;
return
entity
;
...
@@ -108,7 +118,8 @@ public abstract class AbstractHttpClientMockTests {
...
@@ -108,7 +118,8 @@ public abstract class AbstractHttpClientMockTests {
when
(
response
.
getStatusLine
()).
thenReturn
(
statusLine
);
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
;
Header
header
=
value
!=
null
?
new
BasicHeader
(
headerName
,
value
)
:
null
;
when
(
response
.
getFirstHeader
(
headerName
)).
thenReturn
(
header
);
when
(
response
.
getFirstHeader
(
headerName
)).
thenReturn
(
header
);
}
}
...
@@ -140,16 +151,17 @@ public abstract class AbstractHttpClientMockTests {
...
@@ -140,16 +151,17 @@ public abstract class AbstractHttpClientMockTests {
String
fileName
;
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
.
contentType
=
contentType
;
this
.
fileName
=
fileName
;
this
.
fileName
=
fileName
;
this
.
content
=
content
;
this
.
content
=
content
;
}
}
public
MockHttpProjectGenerationRequest
(
String
contentType
,
String
fileName
)
{
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 {
...
@@ -171,11 +183,12 @@ public abstract class AbstractHttpClientMockTests {
}
}
HttpGet
get
=
(
HttpGet
)
argument
;
HttpGet
get
=
(
HttpGet
)
argument
;
Header
acceptHeader
=
get
.
getFirstHeader
(
HttpHeaders
.
ACCEPT
);
Header
acceptHeader
=
get
.
getFirstHeader
(
HttpHeaders
.
ACCEPT
);
if
(
shouldMatch
)
{
if
(
this
.
shouldMatch
)
{
return
acceptHeader
!=
null
&&
value
.
equals
(
acceptHeader
.
getValue
());
return
acceptHeader
!=
null
&&
this
.
value
.
equals
(
acceptHeader
.
getValue
());
}
}
else
{
else
{
return
acceptHeader
==
null
||
!
value
.
equals
(
acceptHeader
.
getValue
());
return
acceptHeader
==
null
||
!
this
.
value
.
equals
(
acceptHeader
.
getValue
());
}
}
}
}
}
}
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java
View file @
b89e5e0a
This diff is collapsed.
Click to expand it.
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceHttpInvokerTests.java
View file @
b89e5e0a
...
@@ -25,11 +25,12 @@ import org.junit.Test;
...
@@ -25,11 +25,12 @@ import org.junit.Test;
import
org.junit.rules.ExpectedException
;
import
org.junit.rules.ExpectedException
;
import
static
junit
.
framework
.
TestCase
.
assertNotNull
;
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
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
mockito
.
Matchers
.
isA
;
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}
* Tests for {@link InitializrServiceHttpInvoker}
...
@@ -41,20 +42,21 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
...
@@ -41,20 +42,21 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
@Rule
@Rule
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
private
final
InitializrServiceHttpInvoker
invoker
=
new
InitializrServiceHttpInvoker
(
httpClient
);
private
final
InitializrServiceHttpInvoker
invoker
=
new
InitializrServiceHttpInvoker
(
this
.
httpClient
);
@Test
@Test
public
void
loadMetadata
()
throws
IOException
{
public
void
loadMetadata
()
throws
IOException
{
mockSuccessfulMetadataGet
();
mockSuccessfulMetadataGet
();
InitializrServiceMetadata
metadata
=
invoker
.
loadMetadata
(
"http://foo/bar"
);
InitializrServiceMetadata
metadata
=
this
.
invoker
.
loadMetadata
(
"http://foo/bar"
);
assertNotNull
(
metadata
);
assertNotNull
(
metadata
);
}
}
@Test
@Test
public
void
generateSimpleProject
()
throws
IOException
{
public
void
generateSimpleProject
()
throws
IOException
{
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
MockHttpProjectGenerationRequest
mockHttpRequest
=
MockHttpProjectGenerationRequest
mockHttpRequest
=
new
MockHttpProjectGenerationRequest
(
new
MockHttpProjectGenerationRequest
(
"application/xml"
,
"foo.zip"
);
"application/xml"
,
"foo.zip"
);
ProjectGenerationResponse
entity
=
generateProject
(
request
,
mockHttpRequest
);
ProjectGenerationResponse
entity
=
generateProject
(
request
,
mockHttpRequest
);
assertProjectEntity
(
entity
,
mockHttpRequest
.
contentType
,
mockHttpRequest
.
fileName
);
assertProjectEntity
(
entity
,
mockHttpRequest
.
contentType
,
mockHttpRequest
.
fileName
);
}
}
...
@@ -63,8 +65,8 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
...
@@ -63,8 +65,8 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
public
void
generateProjectCustomTargetFilename
()
throws
IOException
{
public
void
generateProjectCustomTargetFilename
()
throws
IOException
{
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
request
.
setOutput
(
"bar.zip"
);
request
.
setOutput
(
"bar.zip"
);
MockHttpProjectGenerationRequest
mockHttpRequest
=
MockHttpProjectGenerationRequest
mockHttpRequest
=
new
MockHttpProjectGenerationRequest
(
new
MockHttpProjectGenerationRequest
(
"application/xml"
,
null
);
"application/xml"
,
null
);
ProjectGenerationResponse
entity
=
generateProject
(
request
,
mockHttpRequest
);
ProjectGenerationResponse
entity
=
generateProject
(
request
,
mockHttpRequest
);
assertProjectEntity
(
entity
,
mockHttpRequest
.
contentType
,
null
);
assertProjectEntity
(
entity
,
mockHttpRequest
.
contentType
,
null
);
}
}
...
@@ -72,8 +74,8 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
...
@@ -72,8 +74,8 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
@Test
@Test
public
void
generateProjectNoDefaultFileName
()
throws
IOException
{
public
void
generateProjectNoDefaultFileName
()
throws
IOException
{
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
MockHttpProjectGenerationRequest
mockHttpRequest
=
MockHttpProjectGenerationRequest
mockHttpRequest
=
new
MockHttpProjectGenerationRequest
(
new
MockHttpProjectGenerationRequest
(
"application/xml"
,
null
);
"application/xml"
,
null
);
ProjectGenerationResponse
entity
=
generateProject
(
request
,
mockHttpRequest
);
ProjectGenerationResponse
entity
=
generateProject
(
request
,
mockHttpRequest
);
assertProjectEntity
(
entity
,
mockHttpRequest
.
contentType
,
null
);
assertProjectEntity
(
entity
,
mockHttpRequest
.
contentType
,
null
);
}
}
...
@@ -85,9 +87,9 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
...
@@ -85,9 +87,9 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
request
.
getDependencies
().
add
(
"foo:bar"
);
request
.
getDependencies
().
add
(
"foo:bar"
);
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
jsonMessage
);
th
is
.
th
rown
.
expectMessage
(
jsonMessage
);
invoker
.
generate
(
request
);
this
.
invoker
.
generate
(
request
);
}
}
@Test
@Test
...
@@ -95,9 +97,9 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
...
@@ -95,9 +97,9 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
mockProjectGenerationError
(
400
,
null
);
mockProjectGenerationError
(
400
,
null
);
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
"unexpected 400 error"
);
th
is
.
th
rown
.
expectMessage
(
"unexpected 400 error"
);
invoker
.
generate
(
request
);
this
.
invoker
.
generate
(
request
);
}
}
@Test
@Test
...
@@ -106,25 +108,24 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
...
@@ -106,25 +108,24 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
mockStatus
(
response
,
500
);
mockStatus
(
response
,
500
);
when
(
httpClient
.
execute
(
isA
(
HttpGet
.
class
))).
thenReturn
(
response
);
when
(
this
.
httpClient
.
execute
(
isA
(
HttpGet
.
class
))).
thenReturn
(
response
);
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
"No content received from server"
);
th
is
.
th
rown
.
expectMessage
(
"No content received from server"
);
invoker
.
generate
(
request
);
this
.
invoker
.
generate
(
request
);
}
}
@Test
@Test
public
void
loadMetadataBadRequest
()
throws
IOException
{
public
void
loadMetadataBadRequest
()
throws
IOException
{
String
jsonMessage
=
"whatever error on the server"
;
String
jsonMessage
=
"whatever error on the server"
;
mockMetadataGetError
(
500
,
jsonMessage
);
mockMetadataGetError
(
500
,
jsonMessage
);
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
jsonMessage
);
th
is
.
th
rown
.
expectMessage
(
jsonMessage
);
invoker
.
generate
(
request
);
this
.
invoker
.
generate
(
request
);
}
}
@Test
@Test
...
@@ -132,44 +133,43 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
...
@@ -132,44 +133,43 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
mockHttpEntity
(
response
,
"Foo-Bar-Not-JSON"
.
getBytes
(),
"application/json"
);
mockHttpEntity
(
response
,
"Foo-Bar-Not-JSON"
.
getBytes
(),
"application/json"
);
mockStatus
(
response
,
200
);
mockStatus
(
response
,
200
);
when
(
httpClient
.
execute
(
isA
(
HttpGet
.
class
))).
thenReturn
(
response
);
when
(
this
.
httpClient
.
execute
(
isA
(
HttpGet
.
class
))).
thenReturn
(
response
);
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
"Invalid content received from server"
);
th
is
.
th
rown
.
expectMessage
(
"Invalid content received from server"
);
invoker
.
generate
(
request
);
this
.
invoker
.
generate
(
request
);
}
}
@Test
@Test
public
void
loadMetadataNoContent
()
throws
IOException
{
public
void
loadMetadataNoContent
()
throws
IOException
{
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
CloseableHttpResponse
response
=
mock
(
CloseableHttpResponse
.
class
);
mockStatus
(
response
,
500
);
mockStatus
(
response
,
500
);
when
(
httpClient
.
execute
(
isA
(
HttpGet
.
class
))).
thenReturn
(
response
);
when
(
this
.
httpClient
.
execute
(
isA
(
HttpGet
.
class
))).
thenReturn
(
response
);
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
ProjectGenerationRequest
request
=
new
ProjectGenerationRequest
();
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
"No content received from server"
);
th
is
.
th
rown
.
expectMessage
(
"No content received from server"
);
invoker
.
generate
(
request
);
this
.
invoker
.
generate
(
request
);
}
}
private
ProjectGenerationResponse
generateProject
(
ProjectGenerationRequest
request
,
private
ProjectGenerationResponse
generateProject
(
ProjectGenerationRequest
request
,
MockHttpProjectGenerationRequest
mockRequest
)
throws
IOException
{
MockHttpProjectGenerationRequest
mockRequest
)
throws
IOException
{
mockSuccessfulProjectGeneration
(
mockRequest
);
mockSuccessfulProjectGeneration
(
mockRequest
);
ProjectGenerationResponse
entity
=
invoker
.
generate
(
request
);
ProjectGenerationResponse
entity
=
this
.
invoker
.
generate
(
request
);
assertArrayEquals
(
"wrong body content"
,
mockRequest
.
content
,
entity
.
getContent
());
assertArrayEquals
(
"wrong body content"
,
mockRequest
.
content
,
entity
.
getContent
());
return
entity
;
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
)
{
if
(
mimeType
==
null
)
{
assertNull
(
"No content type expected"
,
entity
.
getContentType
());
assertNull
(
"No content type expected"
,
entity
.
getContentType
());
}
}
else
{
else
{
assertEquals
(
"wrong mime type"
,
mimeType
,
entity
.
getContentType
().
getMimeType
());
assertEquals
(
"wrong mime type"
,
mimeType
,
entity
.
getContentType
()
.
getMimeType
());
}
}
assertEquals
(
"wrong filename"
,
fileName
,
entity
.
getFileName
());
assertEquals
(
"wrong filename"
,
fileName
,
entity
.
getFileName
());
}
}
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitializrServiceMetadataTests.java
View file @
b89e5e0a
...
@@ -22,12 +22,12 @@ import java.nio.charset.Charset;
...
@@ -22,12 +22,12 @@ import java.nio.charset.Charset;
import
org.json.JSONObject
;
import
org.json.JSONObject
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.util.StreamUtils
;
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}
* Tests for {@link InitializrServiceMetadata}
...
@@ -36,7 +36,6 @@ import static org.junit.Assert.*;
...
@@ -36,7 +36,6 @@ import static org.junit.Assert.*;
*/
*/
public
class
InitializrServiceMetadataTests
{
public
class
InitializrServiceMetadataTests
{
@Test
@Test
public
void
parseDefaults
()
{
public
void
parseDefaults
()
{
InitializrServiceMetadata
metadata
=
createInstance
(
"1.0.0"
);
InitializrServiceMetadata
metadata
=
createInstance
(
"1.0.0"
);
...
@@ -53,7 +52,8 @@ public class InitializrServiceMetadataTests {
...
@@ -53,7 +52,8 @@ public class InitializrServiceMetadataTests {
// Security description
// Security description
assertEquals
(
"AOP"
,
metadata
.
getDependency
(
"aop"
).
getName
());
assertEquals
(
"AOP"
,
metadata
.
getDependency
(
"aop"
).
getName
());
assertEquals
(
"Security"
,
metadata
.
getDependency
(
"security"
).
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
(
"JDBC"
,
metadata
.
getDependency
(
"jdbc"
).
getName
());
assertEquals
(
"JPA"
,
metadata
.
getDependency
(
"data-jpa"
).
getName
());
assertEquals
(
"JPA"
,
metadata
.
getDependency
(
"data-jpa"
).
getName
());
assertEquals
(
"MongoDB"
,
metadata
.
getDependency
(
"data-mongodb"
).
getName
());
assertEquals
(
"MongoDB"
,
metadata
.
getDependency
(
"data-mongodb"
).
getName
());
...
@@ -76,7 +76,6 @@ public class InitializrServiceMetadataTests {
...
@@ -76,7 +76,6 @@ public class InitializrServiceMetadataTests {
assertEquals
(
"project"
,
projectType
.
getTags
().
get
(
"format"
));
assertEquals
(
"project"
,
projectType
.
getTags
().
get
(
"format"
));
}
}
private
static
InitializrServiceMetadata
createInstance
(
String
version
)
{
private
static
InitializrServiceMetadata
createInstance
(
String
version
)
{
try
{
try
{
return
new
InitializrServiceMetadata
(
readJson
(
version
));
return
new
InitializrServiceMetadata
(
readJson
(
version
));
...
@@ -87,7 +86,8 @@ public class InitializrServiceMetadataTests {
...
@@ -87,7 +86,8 @@ public class InitializrServiceMetadataTests {
}
}
private
static
JSONObject
readJson
(
String
version
)
throws
IOException
{
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
();
InputStream
stream
=
resource
.
getInputStream
();
try
{
try
{
String
json
=
StreamUtils
.
copyToString
(
stream
,
Charset
.
forName
(
"UTF-8"
));
String
json
=
StreamUtils
.
copyToString
(
stream
,
Charset
.
forName
(
"UTF-8"
));
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ListMetadataCommandTests.java
View file @
b89e5e0a
...
@@ -20,7 +20,7 @@ import java.io.IOException;
...
@@ -20,7 +20,7 @@ import java.io.IOException;
import
org.junit.Test
;
import
org.junit.Test
;
import
static
org
.
junit
.
Assert
.
*
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
/**
* Tests for {@link ListMetadataCommand}
* Tests for {@link ListMetadataCommand}
...
@@ -29,12 +29,12 @@ import static org.junit.Assert.*;
...
@@ -29,12 +29,12 @@ import static org.junit.Assert.*;
*/
*/
public
class
ListMetadataCommandTests
extends
AbstractHttpClientMockTests
{
public
class
ListMetadataCommandTests
extends
AbstractHttpClientMockTests
{
private
final
ListMetadataCommand
command
=
new
ListMetadataCommand
(
httpClient
);
private
final
ListMetadataCommand
command
=
new
ListMetadataCommand
(
this
.
httpClient
);
@Test
@Test
public
void
listMetadata
()
throws
IOException
{
public
void
listMetadata
()
throws
IOException
{
mockSuccessfulMetadataGet
();
mockSuccessfulMetadataGet
();
String
content
=
command
.
generateReport
(
"http://localhost"
);
String
content
=
this
.
command
.
generateReport
(
"http://localhost"
);
assertTrue
(
content
.
contains
(
"aop - AOP"
));
assertTrue
(
content
.
contains
(
"aop - AOP"
));
assertTrue
(
content
.
contains
(
"security - Security: Security description"
));
assertTrue
(
content
.
contains
(
"security - Security: Security description"
));
...
...
spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/ProjectGenerationRequestTests.java
View file @
b89e5e0a
...
@@ -27,12 +27,11 @@ import org.json.JSONObject;
...
@@ -27,12 +27,11 @@ import org.json.JSONObject;
import
org.junit.Rule
;
import
org.junit.Rule
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.rules.ExpectedException
;
import
org.junit.rules.ExpectedException
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.util.StreamUtils
;
import
org.springframework.util.StreamUtils
;
import
static
org
.
junit
.
Assert
.
*
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
/**
/**
* Tests for {@link ProjectGenerationRequest}
* Tests for {@link ProjectGenerationRequest}
...
@@ -41,7 +40,8 @@ import static org.junit.Assert.*;
...
@@ -41,7 +40,8 @@ import static org.junit.Assert.*;
*/
*/
public
class
ProjectGenerationRequestTests
{
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
@Rule
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
...
@@ -50,63 +50,66 @@ public class ProjectGenerationRequestTests {
...
@@ -50,63 +50,66 @@ public class ProjectGenerationRequestTests {
@Test
@Test
public
void
defaultSettings
()
{
public
void
defaultSettings
()
{
assertEquals
(
createDefaultUrl
(
"?type=test-type"
),
request
.
generateUrl
(
createDefaultMetadata
()));
assertEquals
(
createDefaultUrl
(
"?type=test-type"
),
this
.
request
.
generateUrl
(
createDefaultMetadata
()));
}
}
@Test
@Test
public
void
customServer
()
throws
URISyntaxException
{
public
void
customServer
()
throws
URISyntaxException
{
String
customServerUrl
=
"http://foo:8080/initializr"
;
String
customServerUrl
=
"http://foo:8080/initializr"
;
request
.
setServiceUrl
(
customServerUrl
);
this
.
request
.
setServiceUrl
(
customServerUrl
);
request
.
getDependencies
().
add
(
"security"
);
this
.
request
.
getDependencies
().
add
(
"security"
);
assertEquals
(
new
URI
(
customServerUrl
+
"/starter.zip?style=security&type=test-type"
),
assertEquals
(
new
URI
(
customServerUrl
request
.
generateUrl
(
createDefaultMetadata
()));
+
"/starter.zip?style=security&type=test-type"
),
this
.
request
.
generateUrl
(
createDefaultMetadata
()));
}
}
@Test
@Test
public
void
customBootVersion
()
{
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"
),
assertEquals
(
createDefaultUrl
(
"?bootVersion=1.2.0.RELEASE&type=test-type"
),
request
.
generateUrl
(
createDefaultMetadata
()));
this
.
request
.
generateUrl
(
createDefaultMetadata
()));
}
}
@Test
@Test
public
void
singleDependency
()
{
public
void
singleDependency
()
{
request
.
getDependencies
().
add
(
"web"
);
this
.
request
.
getDependencies
().
add
(
"web"
);
assertEquals
(
createDefaultUrl
(
"?style=web&type=test-type"
),
assertEquals
(
createDefaultUrl
(
"?style=web&type=test-type"
),
request
.
generateUrl
(
createDefaultMetadata
()));
this
.
request
.
generateUrl
(
createDefaultMetadata
()));
}
}
@Test
@Test
public
void
multipleDependencies
()
{
public
void
multipleDependencies
()
{
request
.
getDependencies
().
add
(
"web"
);
this
.
request
.
getDependencies
().
add
(
"web"
);
request
.
getDependencies
().
add
(
"data-jpa"
);
this
.
request
.
getDependencies
().
add
(
"data-jpa"
);
assertEquals
(
createDefaultUrl
(
"?style=web&style=data-jpa&type=test-type"
),
assertEquals
(
createDefaultUrl
(
"?style=web&style=data-jpa&type=test-type"
),
request
.
generateUrl
(
createDefaultMetadata
()));
this
.
request
.
generateUrl
(
createDefaultMetadata
()));
}
}
@Test
@Test
public
void
customJavaVersion
()
{
public
void
customJavaVersion
()
{
request
.
setJavaVersion
(
"1.8"
);
this
.
request
.
setJavaVersion
(
"1.8"
);
assertEquals
(
createDefaultUrl
(
"?javaVersion=1.8&type=test-type"
),
assertEquals
(
createDefaultUrl
(
"?javaVersion=1.8&type=test-type"
),
request
.
generateUrl
(
createDefaultMetadata
()));
this
.
request
.
generateUrl
(
createDefaultMetadata
()));
}
}
@Test
@Test
public
void
customPackaging
()
{
public
void
customPackaging
()
{
request
.
setPackaging
(
"war"
);
this
.
request
.
setPackaging
(
"war"
);
assertEquals
(
createDefaultUrl
(
"?packaging=war&type=test-type"
),
assertEquals
(
createDefaultUrl
(
"?packaging=war&type=test-type"
),
request
.
generateUrl
(
createDefaultMetadata
()));
this
.
request
.
generateUrl
(
createDefaultMetadata
()));
}
}
@Test
@Test
public
void
customType
()
throws
URISyntaxException
{
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
);
InitializrServiceMetadata
metadata
=
new
InitializrServiceMetadata
(
projectType
);
request
.
setType
(
"custom"
);
this
.
request
.
setType
(
"custom"
);
request
.
getDependencies
().
add
(
"data-rest"
);
this
.
request
.
getDependencies
().
add
(
"data-rest"
);
assertEquals
(
new
URI
(
ProjectGenerationRequest
.
DEFAULT_SERVICE_URL
+
"/foo?style=data-rest&type=custom"
),
assertEquals
(
new
URI
(
ProjectGenerationRequest
.
DEFAULT_SERVICE_URL
request
.
generateUrl
(
metadata
));
+
"/foo?style=data-rest&type=custom"
),
this
.
request
.
generateUrl
(
metadata
));
}
}
@Test
@Test
...
@@ -114,9 +117,9 @@ public class ProjectGenerationRequestTests {
...
@@ -114,9 +117,9 @@ public class ProjectGenerationRequestTests {
InitializrServiceMetadata
metadata
=
readMetadata
();
InitializrServiceMetadata
metadata
=
readMetadata
();
setBuildAndFormat
(
"does-not-exist"
,
null
);
setBuildAndFormat
(
"does-not-exist"
,
null
);
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
"does-not-exist"
);
th
is
.
th
rown
.
expectMessage
(
"does-not-exist"
);
request
.
generateUrl
(
metadata
);
this
.
request
.
generateUrl
(
metadata
);
}
}
@Test
@Test
...
@@ -124,10 +127,10 @@ public class ProjectGenerationRequestTests {
...
@@ -124,10 +127,10 @@ public class ProjectGenerationRequestTests {
InitializrServiceMetadata
metadata
=
readMetadata
(
"types-conflict"
);
InitializrServiceMetadata
metadata
=
readMetadata
(
"types-conflict"
);
setBuildAndFormat
(
"gradle"
,
null
);
setBuildAndFormat
(
"gradle"
,
null
);
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
"gradle-project"
);
th
is
.
th
rown
.
expectMessage
(
"gradle-project"
);
thrown
.
expectMessage
(
"gradle-project-2"
);
th
is
.
th
rown
.
expectMessage
(
"gradle-project-2"
);
request
.
generateUrl
(
metadata
);
this
.
request
.
generateUrl
(
metadata
);
}
}
@Test
@Test
...
@@ -135,29 +138,30 @@ public class ProjectGenerationRequestTests {
...
@@ -135,29 +138,30 @@ public class ProjectGenerationRequestTests {
InitializrServiceMetadata
metadata
=
readMetadata
();
InitializrServiceMetadata
metadata
=
readMetadata
();
setBuildAndFormat
(
"gradle"
,
null
);
setBuildAndFormat
(
"gradle"
,
null
);
assertEquals
(
createDefaultUrl
(
"?type=gradle-project"
),
request
.
generateUrl
(
metadata
));
assertEquals
(
createDefaultUrl
(
"?type=gradle-project"
),
this
.
request
.
generateUrl
(
metadata
));
}
}
@Test
@Test
public
void
invalidType
()
throws
URISyntaxException
{
public
void
invalidType
()
throws
URISyntaxException
{
request
.
setType
(
"does-not-exist"
);
this
.
request
.
setType
(
"does-not-exist"
);
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
request
.
generateUrl
(
createDefaultMetadata
());
this
.
request
.
generateUrl
(
createDefaultMetadata
());
}
}
@Test
@Test
public
void
noTypeAndNoDefault
()
throws
URISyntaxException
{
public
void
noTypeAndNoDefault
()
throws
URISyntaxException
{
thrown
.
expect
(
ProjectGenerationException
.
class
);
th
is
.
th
rown
.
expect
(
ProjectGenerationException
.
class
);
thrown
.
expectMessage
(
"no default is defined"
);
th
is
.
th
rown
.
expectMessage
(
"no default is defined"
);
request
.
generateUrl
(
readMetadata
(
"types-conflict"
));
this
.
request
.
generateUrl
(
readMetadata
(
"types-conflict"
));
}
}
private
static
URI
createDefaultUrl
(
String
param
)
{
private
static
URI
createDefaultUrl
(
String
param
)
{
try
{
try
{
return
new
URI
(
ProjectGenerationRequest
.
DEFAULT_SERVICE_URL
+
"/starter.zip"
+
param
);
return
new
URI
(
ProjectGenerationRequest
.
DEFAULT_SERVICE_URL
+
"/starter.zip"
+
param
);
}
}
catch
(
URISyntaxException
e
)
{
catch
(
URISyntaxException
e
)
{
throw
new
IllegalStateException
(
e
);
throw
new
IllegalStateException
(
e
);
...
@@ -165,13 +169,14 @@ public class ProjectGenerationRequestTests {
...
@@ -165,13 +169,14 @@ public class ProjectGenerationRequestTests {
}
}
public
void
setBuildAndFormat
(
String
build
,
String
format
)
{
public
void
setBuildAndFormat
(
String
build
,
String
format
)
{
request
.
setBuild
(
build
!=
null
?
build
:
"maven"
);
this
.
request
.
setBuild
(
build
!=
null
?
build
:
"maven"
);
request
.
setFormat
(
format
!=
null
?
format
:
"project"
);
this
.
request
.
setFormat
(
format
!=
null
?
format
:
"project"
);
request
.
setDetectType
(
true
);
this
.
request
.
setDetectType
(
true
);
}
}
private
static
InitializrServiceMetadata
createDefaultMetadata
()
{
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
);
return
new
InitializrServiceMetadata
(
projectType
);
}
}
...
@@ -181,8 +186,10 @@ public class ProjectGenerationRequestTests {
...
@@ -181,8 +186,10 @@ public class ProjectGenerationRequestTests {
private
static
InitializrServiceMetadata
readMetadata
(
String
version
)
{
private
static
InitializrServiceMetadata
readMetadata
(
String
version
)
{
try
{
try
{
Resource
resource
=
new
ClassPathResource
(
"metadata/service-metadata-"
+
version
+
".json"
);
Resource
resource
=
new
ClassPathResource
(
"metadata/service-metadata-"
String
content
=
StreamUtils
.
copyToString
(
resource
.
getInputStream
(),
Charset
.
forName
(
"UTF-8"
));
+
version
+
".json"
);
String
content
=
StreamUtils
.
copyToString
(
resource
.
getInputStream
(),
Charset
.
forName
(
"UTF-8"
));
JSONObject
json
=
new
JSONObject
(
content
);
JSONObject
json
=
new
JSONObject
(
content
);
return
new
InitializrServiceMetadata
(
json
);
return
new
InitializrServiceMetadata
(
json
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment