Skip to content

Commit

Permalink
Update java application prototype to match online sample
Browse files Browse the repository at this point in the history
  • Loading branch information
tresat committed Aug 15, 2024
1 parent 7769c8c commit df6959e
Show file tree
Hide file tree
Showing 16 changed files with 325 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# declarative-samples-java-app
A sample Java application written in the Declarative Gradle DSL, using the prototype Declarative Gradle `javaApplication` Software Type defined in the `org.gradle.experimental.jvm-ecosystem` ecosystem plugin.

## Building and Running

This sample shows the definition of a multiproject Java application implemented using Java 17 source code.
The project is the result of converting the project produced by the `gradle init` command in Gradle 8.9.

To build and test the application without running, use:

```shell
> ./gradlew build
```

To run the application, use:

```shell
> ./gradlew run
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
javaApplication {
mainClass = "org.example.app.App"

dependencies {
implementation("org.apache.commons:commons-text:1.11.0")
implementation(project(":utilities"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example.app;

import org.example.list.LinkedList;

import static org.example.utilities.StringUtils.join;
import static org.example.utilities.StringUtils.split;
import static org.example.app.MessageUtils.getMessage;

import org.apache.commons.text.WordUtils;

public class App {
public static void main(String[] args) {
LinkedList tokens;
tokens = split(getMessage());
String result = join(tokens);
System.out.println(WordUtils.capitalize(result));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example.app;

class MessageUtils {
public static String getMessage() {
return "Hello World!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example.app;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class MessageUtilsTest {
@Test void testGetMessage() {
assertEquals("Hello World!", MessageUtils.getMessage());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
javaLibrary {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example.list;

public class LinkedList {
private Node head;

public void add(String element) {
Node newNode = new Node(element);

Node it = tail(head);
if (it == null) {
head = newNode;
} else {
it.next = newNode;
}
}

private static Node tail(Node head) {
Node it;

for (it = head; it != null && it.next != null; it = it.next) {}

return it;
}

public boolean remove(String element) {
boolean result = false;
Node previousIt = null;
Node it = null;
for (it = head; !result && it != null; previousIt = it, it = it.next) {
if (0 == element.compareTo(it.data)) {
result = true;
unlink(previousIt, it);
break;
}
}

return result;
}

private void unlink(Node previousIt, Node currentIt) {
if (currentIt == head) {
head = currentIt.next;
} else {
previousIt.next = currentIt.next;
}
}

public int size() {
int size = 0;

for (Node it = head; it != null; ++size, it = it.next) {}

return size;
}

public String get(int index) {
Node it = head;
while (index > 0 && it != null) {
it = it.next;
index--;
}

if (it == null) {
throw new IndexOutOfBoundsException("Index is out of range");
}

return it.data;
}

private static class Node {
final String data;
Node next;

Node(String data) {
this.data = data;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example.list;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class LinkedListTest {
@Test void testConstructor() {
LinkedList list = new LinkedList();
assertEquals(0, list.size());
}

@Test void testAdd() {
LinkedList list = new LinkedList();

list.add("one");
assertEquals(1, list.size());
assertEquals("one", list.get(0));

list.add("two");
assertEquals(2, list.size());
assertEquals("two", list.get(1));
}

@Test void testRemove() {
LinkedList list = new LinkedList();

list.add("one");
list.add("two");
assertTrue(list.remove("one"));

assertEquals(1, list.size());
assertEquals("two", list.get(0));

assertTrue(list.remove("two"));
assertEquals(0, list.size());
}

@Test public void testRemoveMissing() {
LinkedList list = new LinkedList();

list.add("one");
list.add("two");
assertFalse(list.remove("three"));
assertEquals(2, list.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
pluginManagement {
repositories {
google() // for Android plugin
google() // Needed for the Android plugin, applied by the unified plugin
gradlePluginPortal()
}
}

plugins {
id("org.gradle.experimental.jvm-ecosystem") version "0.1.10"
id("org.gradle.experimental.jvm-ecosystem") version "0.1.7"
}

rootProject.name = "example-java-app"

include("app")
include("list")
include("utilities")

defaults {
javaLibrary {
javaVersion = 17

dependencies {
implementation("org.apache.commons:commons-text:1.11.0")
}

testing {
dependencies {
implementation("org.junit.jupiter:junit-jupiter:5.10.2")
runtimeOnly("org.junit.platform:junit-platform-launcher")
}
}
}

javaApplication {
javaVersion = 17

dependencies {
implementation("org.apache.commons:commons-text:1.11.0")
}

testing {
testJavaVersion = 21
dependencies {
implementation("org.junit.jupiter:junit-jupiter:5.10.2")
runtimeOnly("org.junit.platform:junit-platform-launcher")
}
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
javaLibrary {
dependencies {
api(project(":list"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example.utilities;

import org.example.list.LinkedList;

class JoinUtils {
public static String join(LinkedList source) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < source.size(); ++i) {
if (result.length() > 0) {
result.append(" ");
}
result.append(source.get(i));
}

return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example.utilities;

import org.example.list.LinkedList;

class SplitUtils {
public static LinkedList split(String source) {
int lastFind = 0;
int currentFind = 0;
LinkedList result = new LinkedList();

while ((currentFind = source.indexOf(" ", lastFind)) != -1) {
String token = source.substring(lastFind);
if (currentFind != -1) {
token = token.substring(0, currentFind - lastFind);
}

addIfValid(token, result);
lastFind = currentFind + 1;
}

String token = source.substring(lastFind);
addIfValid(token, result);

return result;
}

private static void addIfValid(String token, LinkedList list) {
if (isTokenValid(token)) {
list.add(token);
}
}

private static boolean isTokenValid(String token) {
return !token.isEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* This source file was generated by the Gradle 'init' task
*/
package org.example.utilities;

import org.example.list.LinkedList;

public class StringUtils {
public static String join(LinkedList source) {
return JoinUtils.join(source);
}

public static LinkedList split(String source) {
return SplitUtils.split(source);
}
}

0 comments on commit df6959e

Please sign in to comment.