Skip to content

Commit

Permalink
Merge pull request #510 from cescoffier/features/update-guide-to-remo…
Browse files Browse the repository at this point in the history
…te-application-class

Remove the Application class from the documentation and from the generated projects
  • Loading branch information
cescoffier authored Jan 16, 2019
2 parents 8a6334e + c7d7e19 commit 334e668
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 96 deletions.
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/application-configuration-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class GreetingResourceTest {
@Test
public void testHelloEndpoint() {
given()
.when().get("/app/greeting")
.when().get("/greeting")
.then()
.statusCode(200)
.body(is("hello shamrock!")); // Modified line
Expand All @@ -135,7 +135,7 @@ public class GreetingResourceTest {
== Package and run the application

Run the application with: `mvn compile shamrock:dev`.
Open your browser to http://localhost:8080/app/greeting.
Open your browser to http://localhost:8080/greeting.

Changing the configuration file is immediately reflected.
You can add the `greeting.suffix`, remove the other properties, change the values, etc.
Expand Down
28 changes: 9 additions & 19 deletions docs/src/main/asciidoc/getting-started-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,6 @@ As we are going to create a JAX-RS endpoint, you also need to add the following
ArC is a CDI-based dependency injection solution - see also link:cdi-reference.html[Contexts and Dependency Injection].
====

== Creating the Application class

It's now time to create the `Application` class, create the `src/main/java/org/acme/quickstart/MyApplication.java` file with the following content:

[source,java]
----
package org.acme.quickstart;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/app")
public class MyApplication extends Application {
}
----

=== Creating the JAX-RS resource

Create the `src/main/java/org/acme/quickstart/GreetingResource.java` file with the following content:
Expand All @@ -175,6 +158,13 @@ public class GreetingResource {
}
----

[TIP]
.Differences with vanilla Jax-RS
====
With Shamrock no need to create an `Application` class. It's supported but not required. In addition, only one instance
of the resource is created and not one per request. You can configure this using the different `XScoped` annotations.
====

== Running the application

Now we are ready to run our application.
Expand Down Expand Up @@ -207,7 +197,7 @@ INFO [o.j.shamrock] (main) Shamrock started in 697.982ms
Once started, you can request the provided endpoint:

```
$ curl http://localhost:8080/app/hello
$ curl http://localhost:8080/hello
hello
```

Expand Down Expand Up @@ -268,7 +258,7 @@ public class GreetingResource {
}
----

Start the application and check that http://localhost:8080/app/hello/greeting/shamrock returns `hello shamrock`.
Start the application and check that http://localhost:8080/hello/greeting/shamrock returns `hello shamrock`.



Expand Down
6 changes: 1 addition & 5 deletions docs/src/main/asciidoc/ide-configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,14 @@ The following table lists the attributes you can pass to the `create` command:
| `/hello`
| The resource path, only relevant if `className` is set.

| `root`
| `/app`
| The application root prefix, only relevant if `className` is set.

| `extensions`
| _[]_
| The list of extensions to add to the project (comma-separated)

|===

If you decide to generate a REST resource (using the `className` attribute), the endpoint is exposed at: `http://localhost:8080/$root/$path`.
If you use the default `root` and `path`, the URL is: http://localhost:8080/app/hello.
If you use the default `root` and `path`, the URL is: http://localhost:8080/hello.

The project is either generated in the current directory or in a directory named after the passed artifactId.
If the current directory is empty, the project is generated in-place.
Expand Down
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/kubernetes-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The application is now exposed as an internal service. If you are using `minikub

[source, bash]
----
curl $(minikube service shamrock-quickstart --url)/app/hello/greeting/shamrock
curl $(minikube service shamrock-quickstart --url)/hello/greeting/shamrock
----

== Deploying the application in OpenShift
Expand All @@ -78,7 +78,7 @@ oc expose service shamrock-quickstart
# Get the route URL
export URL="http://$(oc get route | grep shamrock-quickstart | awk '{print $2}')"
curl $URL/app/hello/greeting/shamrock
curl $URL/hello/greeting/shamrock
----

Your application is accessible at the printed URL.
Expand Down
6 changes: 3 additions & 3 deletions docs/src/main/asciidoc/scheduled-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public class CountResourceTest {
@Test
public void testHelloEndpoint() {
given()
.when().get("/app/count")
.when().get("/count")
.then()
.statusCode(200)
.body(containsString("count")); // <1>
Expand All @@ -152,8 +152,8 @@ public class CountResourceTest {
== Package and run the application

Run the application with: `mvn compile shamrock:dev`.
In another terminal, run `curl localhost:8080/app/count` to check the counter value.
After a few seconds, re-run `curl localhost:8080/app/count` to verify the counter has been incremented.
In another terminal, run `curl localhost:8080/count` to check the counter value.
After a few seconds, re-run `curl localhost:8080/count` to verify the counter has been incremented.

As usual, the application can be packaged using `mvn clean package` and executed using the `-runner.jar` file.
You can also generate the native executable.
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ public class CreateProjectMojo extends AbstractMojo {
@Parameter(property = "className")
private String className;

@Parameter(property = "root", defaultValue = "/app")
private String root;

@Parameter(property = "extensions")
private List<String> extensions;

Expand Down Expand Up @@ -123,7 +120,7 @@ public void execute() throws MojoExecutionException {
model = project.getOriginalModel().clone();

createDirectories();
templates.generate(project, root, path, className, getLog());
templates.generate(project, path, className, getLog());
Optional<Plugin> maybe = MojoUtils.hasPlugin(project, PLUGIN_KEY);
if (maybe.isPresent()) {
printUserInstructions(pomFile);
Expand Down Expand Up @@ -258,14 +255,6 @@ private File createPomFileFromUserInputs() throws MojoExecutionException {
}
}

if (root == null) {
root = prompter.promptWithDefaultValue("Set the application root ",
"/app");
if (!root.startsWith("/")) {
root = "/" + root;
}
}

if (path == null) {
path = prompter.promptWithDefaultValue("Set the resource path ",
"/hello");
Expand Down Expand Up @@ -298,7 +287,6 @@ private File createPomFileFromUserInputs() throws MojoExecutionException {
context.put("docRoot", MojoUtils.get("doc-root"));

context.put("className", className);
context.put("root", root);
context.put("path", path);

templates.createNewProjectPomFile(context, pomFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void createNewProjectPomFile(Map<String, String> context, File pomFile) t
}
}

public void generate(MavenProject project, String rootPath, String path, String className, Log log) throws MojoExecutionException {
public void generate(MavenProject project, String path, String className, Log log) throws MojoExecutionException {
if (Strings.isNullOrEmpty(className)) {
return;
}
Expand Down Expand Up @@ -92,7 +92,6 @@ public void generate(MavenProject project, String rootPath, String path, String
File testClassFile = new File(testRoot, className + "Test" + JAVA_EXTENSION);
Map<String, String> context = new HashMap<>();
context.put("classname", className);
context.put("root_prefix", rootPath);
context.put("path", path);
if (packageName != null) {
context.put("packageName", packageName);
Expand All @@ -113,16 +112,6 @@ public void generate(MavenProject project, String rootPath, String path, String
} catch (Exception e) {
throw new MojoExecutionException("Unable to generate test code", e);
}

// Generate application.
File appClassFile = new File(root, "ShamrockApplication.java");
try {
Template temp = cfg.getTemplate("templates/application-template.ftl");
Writer out = new FileWriter(appClassFile);
temp.process(context, out);
} catch (Exception e) {
throw new MojoExecutionException("Unable to generate Application class", e);
}
}

public void createIndexPage(Map<String, String> context, File basedir, Log log) throws MojoExecutionException {
Expand Down
11 changes: 0 additions & 11 deletions maven/src/main/templates/templates/application-template.ftl

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ${classname}Test {
@Test
public void testHelloEndpoint() {
given()
.when().get("${root_prefix}${path}")
.when().get("${path}")
.then()
.statusCode(200)
.body(is("hello"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void testProjectGenerationFromScratchWithResource() throws Exception {
assertThat(new File(testDir, "pom.xml")).isFile();
assertThat(new File(testDir, "src/main/java")).isDirectory();
assertThat(new File(testDir, "src/main/java/org/acme/MyResource.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).doesNotExist();
}

@Test
Expand All @@ -151,7 +151,7 @@ public void testProjectGenerationFromMinimalPomWithResource() throws Exception {
.contains("shamrock.version");
assertThat(new File(testDir, "src/main/java")).isDirectory();
assertThat(new File(testDir, "src/main/java/org/acme/MyResource.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).doesNotExist();
}

@Test
Expand All @@ -169,7 +169,7 @@ public void testProjectGenerationFromScratchWithExtensions() throws Exception {
assertThat(new File(testDir, "pom.xml")).isFile();
assertThat(new File(testDir, "src/main/java")).isDirectory();
assertThat(new File(testDir, "src/main/java/org/acme/MyResource.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).doesNotExist();
assertThat(FileUtils.readFileToString(new File(testDir, "pom.xml"), "UTF-8"))
.contains("shamrock-jaxrs-deployment", "shamrock-metrics-deployment").doesNotContain("missing");

Expand Down Expand Up @@ -204,7 +204,7 @@ public void testProjectGenerationFromScratchWithCustomDependencies() throws Exce
setup(properties);
assertThat(new File(testDir, "pom.xml")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/MyResource.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).doesNotExist();
assertThat(FileUtils.readFileToString(new File(testDir, "pom.xml"), "UTF-8"))
.contains("commons-io");

Expand Down Expand Up @@ -238,7 +238,7 @@ public void testProjectGenerationFromMinimalPomWithDependencies() throws Excepti
setup(properties);
assertThat(new File(testDir, "pom.xml")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/MyResource.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).isFile();
assertThat(new File(testDir, "src/main/java/org/acme/ShamrockApplication.java")).doesNotExist();
assertThat(FileUtils.readFileToString(new File(testDir, "pom.xml"), "UTF-8"))
.contains("commons-io");
}
Expand All @@ -251,7 +251,7 @@ public void cleanup() {
}

@Test
public void generateNewProjectAndRun() throws MavenInvocationException, FileNotFoundException, InterruptedException {
public void generateNewProjectAndRun() throws MavenInvocationException, FileNotFoundException {
testDir = initEmptyProject("projects/project-generation-and-run");

// Scaffold the new project
Expand All @@ -272,7 +272,7 @@ public void generateNewProjectAndRun() throws MavenInvocationException, FileNotF
assertThat(resp).containsIgnoringCase("ready").containsIgnoringCase("application").containsIgnoringCase("org.acme")
.containsIgnoringCase("1.0-SNAPSHOT");

String greeting = getHttpResponse("/app/hello");
String greeting = getHttpResponse("/hello");
assertThat(greeting).containsIgnoringCase("hello");
}

Expand Down
21 changes: 10 additions & 11 deletions maven/src/test/java/org/jboss/shamrock/maven/it/DevMojoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public void testThatTheApplicationIsReloadedOnJavaChange() throws MavenInvocatio
// Wait until we get "uuid"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/app/hello").contains(uuid));
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/hello").contains(uuid));
sleep();
filter(source, ImmutableMap.of(uuid, "carambar"));

// Wait until we get "uuid"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/app/hello").contains("carambar"));
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/hello").contains("carambar"));
}

@Test
Expand Down Expand Up @@ -95,7 +95,7 @@ public void testThatTheApplicationIsReloadedOnNewResource() throws MavenInvocati
// Wait until we get "bar"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/app/foo").contains("bar"));
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/foo").contains("bar"));
}

@Test
Expand Down Expand Up @@ -151,7 +151,7 @@ private void runAndCheck() throws FileNotFoundException, MavenInvocationExceptio
assertThat(resp).containsIgnoringCase("ready").containsIgnoringCase("application").containsIgnoringCase("org.acme")
.containsIgnoringCase("1.0-SNAPSHOT");

String greeting = getHttpResponse("/app/hello");
String greeting = getHttpResponse("/hello");
assertThat(greeting).containsIgnoringCase("hello");
}

Expand All @@ -167,7 +167,7 @@ public void testThatTheApplicationIsReloadedOnConfigChange() throws MavenInvocat
assertThat(resp).containsIgnoringCase("ready").containsIgnoringCase("application").containsIgnoringCase("org.acme")
.containsIgnoringCase("1.0-SNAPSHOT");

String greeting = getHttpResponse("/app/hello/greeting");
String greeting = getHttpResponse("/hello/greeting");
assertThat(greeting).containsIgnoringCase("bonjour");

sleep();
Expand All @@ -180,7 +180,7 @@ public void testThatTheApplicationIsReloadedOnConfigChange() throws MavenInvocat
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES)
.until(() -> getHttpResponse("/app/hello/greeting").contains(uuid));
.until(() -> getHttpResponse("/hello/greeting").contains(uuid));
}

@Test
Expand Down Expand Up @@ -227,7 +227,7 @@ public void testThatApplicationRecoversCompilationIssue() throws MavenInvocation
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES).until(() -> {
String content = getHttpResponse("/app/hello");
String content = getHttpResponse("/hello");
last.set(content);
return content.contains(uuid);
});
Expand All @@ -242,7 +242,7 @@ public void testThatApplicationRecoversCompilationIssue() throws MavenInvocation
// Wait until we get "uuid"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/app/hello").contains("carambar"));
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/hello").contains("carambar"));
}

@Test
Expand Down Expand Up @@ -274,19 +274,18 @@ public void testThatNewBeanAreDiscovered() throws IOException, MavenInvocationEx
// Wait until we get "uuid"
await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/app/hello").contains("message"));
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/hello").contains("message"));

sleep();

filter(source, ImmutableMap.of("message", "foobarbaz"));

await()
.pollDelay(100, TimeUnit.MILLISECONDS)
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/app/hello").contains("foobarbaz"));
.atMost(1, TimeUnit.MINUTES).until(() -> getHttpResponse("/hello").contains("foobarbaz"));
}



@Test
public void testErrorMessageWhenNoJavaSources() throws IOException, MavenInvocationException {
testDir = initProject("projects/classic", "projects/project-no-sources");
Expand Down

This file was deleted.

Loading

0 comments on commit 334e668

Please sign in to comment.