Initial commit

This commit is contained in:
Silvan Heller
2022-02-24 11:24:51 +01:00
commit 067491fb46
18 changed files with 1251 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
package ch.unibas.dmi.dbis.cs108.example;
/**
* A simple HelloWorld class.
*/
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

View File

@@ -0,0 +1,32 @@
package ch.unibas.dmi.dbis.cs108.example.gui.javafx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
* This is an example JavaFX-Application.
*/
public class GUI extends Application {
/**
* Launching this method will not work on some platforms.
* What you should do is to create a separate main class and launch the GUI class from there as is done in {@link Main}
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
String javaVersion = System.getProperty("java.version");
String javafxVersion = System.getProperty("javafx.version");
Label l = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
Scene scene = new Scene(new StackPane(l), 640, 480);
stage.setScene(scene);
stage.show();
}
}

View File

@@ -0,0 +1,14 @@
package ch.unibas.dmi.dbis.cs108.example.gui.javafx;
import javafx.application.Application;
public class Main {
/**
* This is simply a wrapper to launch the {@link GUI} class.
* The reason this class exists is documented in {@link GUI#main(String[])}
*/
public static void main(String[] args) {
Application.launch(GUI.class, args);
}
}

View File

@@ -0,0 +1,29 @@
package ch.unibas.dmi.dbis.cs108.example.gui.swing;
import javax.swing.JFrame;
import javax.swing.JLabel;
/**
* This is an example Swing Application
*/
public class SwingGUI {
private static void createAndShowGUI() {
// Make sure we have nice window decorations
JFrame.setDefaultLookAndFeelDecorated(true);
// Create and set up the window.
JFrame frame = new JFrame("HelloWorldSwing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Add the ubiquitous "Hello World" label
JLabel label = new JLabel("Hello World");
frame.getContentPane().add(label);
// Display the window
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread: // creating and showing this application's GUI
javax.swing.SwingUtilities.invokeLater(() -> createAndShowGUI());
}
}

View File

@@ -0,0 +1,67 @@
package ch.unibas.dmi.dbis.cs108.example;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* An example test class.
* Checks the output of the {@link HelloWorld} class and makes sure it contains "Hello World"
*/
public class HelloWorldTest {
/*
* Streams to store system.out and system.err content
*/
private ByteArrayOutputStream outStream = new ByteArrayOutputStream();
private ByteArrayOutputStream errStream = new ByteArrayOutputStream();
/*
* Here we store the previous pointers to system.out / system.err
*/
private PrintStream outBackup;
private PrintStream errBackup;
/**
* This method is executed before each test.
* It redirects System.out and System.err to our variables {@link #outStream} and {@link #errStream}.
* This allows us to test their content later.
*/
@BeforeEach
public void redirectStdOutStdErr() {
outBackup = System.out;
errBackup = System.err;
System.setOut(new PrintStream(outStream));
System.setErr(new PrintStream(errStream));
}
/**
* This method is run after each test.
* It redirects System.out / System.err back to the normal streams.
*/
@AfterEach
public void reestablishStdOutStdErr() {
System.setOut(outBackup);
System.setErr(errBackup);
}
/**
* This is a normal JUnit-Test. It executes the HelloWorld-Method and verifies that it actually wrote "Hello World" to stdout
*/
@Test
public void testMain() {
HelloWorld.main(new String[0]);
String toTest = outStream.toString();
toTest = removeNewline(toTest);
assertTrue(toTest.contains("Hello World"));
}
private static String removeNewline(String str) {
return str.replace("\n", "").replace("\r", "");
}
}