|
| 1 | +/* Copyright (c) 2024 IBM Corporation and others. |
| 2 | + * All rights reserved. This program and the accompanying materials |
| 3 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 4 | + * which accompanies this distribution, and is available at |
| 5 | + * http://www.eclipse.org/legal/epl-2.0/ |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: EPL-2.0 |
| 8 | + * |
| 9 | + * Contributors: |
| 10 | + * IBM Corporation - initial API and implementation |
| 11 | + *******************************************************************************/ |
| 12 | +package io.openliberty.java.internal; |
| 13 | + |
| 14 | +import java.io.PrintWriter; |
| 15 | +import java.io.StringWriter; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.Arrays; |
| 18 | + |
| 19 | +import javax.enterprise.context.ApplicationScoped; |
| 20 | +import javax.ws.rs.GET; |
| 21 | +import javax.ws.rs.Path; |
| 22 | + |
| 23 | +@Path("/") |
| 24 | +@ApplicationScoped |
| 25 | +public class TestService { |
| 26 | + |
| 27 | + private StringWriter sw = new StringWriter(); |
| 28 | + |
| 29 | + @GET |
| 30 | + public String test() { |
| 31 | + try { |
| 32 | + log(">>> ENTER"); |
| 33 | + doTest(); |
| 34 | + log("<<< EXIT SUCCESSFUL"); |
| 35 | + } catch (Exception e) { |
| 36 | + e.printStackTrace(System.out); |
| 37 | + e.printStackTrace(new PrintWriter(sw)); |
| 38 | + log("<<< EXIT FAILED"); |
| 39 | + } |
| 40 | + String result = sw.toString(); |
| 41 | + sw = new StringWriter(); |
| 42 | + return result; |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + private void doTest() throws Exception { |
| 47 | + log("Beginning Java 23 testing"); |
| 48 | + |
| 49 | + double f = Math.random()/Math.nextDown(1.0); |
| 50 | + int milesRun = (int)(0 * (1.0 - f) + 101*f); // Random integer between 0 and 100 |
| 51 | + String comment = eval(milesRun); |
| 52 | + log(comment); |
| 53 | + |
| 54 | + if (comment == null || !comment.toLowerCase().contains("you")) { |
| 55 | + log("Failed testing"); |
| 56 | + throw new Exception("Comment did not contain the string \"you\"!. It was: " + comment); |
| 57 | + } |
| 58 | + |
| 59 | + log("Goodbye testing"); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Primitive Types in Patterns, instanceof, and switch (Preview) : JEP 455 -> https://openjdk.org/jeps/455 |
| 64 | + * |
| 65 | + * @param miles |
| 66 | + * @return |
| 67 | + */ |
| 68 | + private static String eval(int miles) { |
| 69 | + return switch (miles) { |
| 70 | + case 0 -> "Are you Still sleeping?"; |
| 71 | + case 1 -> "You have a long ways to go..."; |
| 72 | + case 2 -> "Have you warmed up yet?"; |
| 73 | + case int i when i >= 3 && i < 8 -> "Feeling it, aren't you?"; |
| 74 | + case int i when i >= 8 && i < 13 -> "Pacing yourself"; |
| 75 | + case 13 -> "You are halfway!"; |
| 76 | + case int i when i >= 14 && i < 20 -> "Your legs are burning"; |
| 77 | + case int i when i >= 20 && i < 26 -> "You're almost there!"; |
| 78 | + case 26 -> "You are done!"; |
| 79 | + case int i when i > 26 -> "Ultra marathon runner, you are!"; |
| 80 | + case int i -> "Huh? Don't know what to do with value: " + i; // default |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + public void log(String msg) { |
| 86 | + System.out.println(msg); |
| 87 | + sw.append(msg); |
| 88 | + sw.append("<br/>"); |
| 89 | + } |
| 90 | +} |
0 commit comments