Fix code format

This commit is contained in:
Manuel Thalmann 2022-10-11 13:17:58 +02:00
parent 3a3ecefed2
commit f6fe871d80
20 changed files with 141 additions and 93 deletions

View file

@ -5,7 +5,7 @@ package ch.nuth.zhaw.exbox;
* *
*/ */
public class AnyServer implements CommandExecutor { public class AnyServer implements CommandExecutor {
//----- Dies implementiert das CommandExecutor Interface. // ----- Dies implementiert das CommandExecutor Interface.
@Override @Override
public String execute(String command) { public String execute(String command) {
return "Die Eingabe ist \"" + command + "\"\n"; return "Die Eingabe ist \"" + command + "\"\n";

View file

@ -10,4 +10,4 @@ public class App {
f.setLocationRelativeTo(null); f.setLocationRelativeTo(null);
f.setVisible(true); f.setVisible(true);
} }
} }

View file

@ -13,6 +13,7 @@ public class BracketServer implements CommandExecutor {
public BracketServer() { public BracketServer() {
super(); super();
bracketPairs = new HashMap<>() { bracketPairs = new HashMap<>() {
{ {
put("(", ")"); put("(", ")");
@ -37,9 +38,8 @@ public class BracketServer implements CommandExecutor {
for (MatchResult result : results) { for (MatchResult result : results) {
if (bracketPairs.keySet().contains(result.group())) { if (bracketPairs.keySet().contains(result.group())) {
openBrackets.push(result.group()); openBrackets.push(result.group());
} else if ( } else if (!openBrackets.isEmpty() &&
!openBrackets.isEmpty() && result.group().equals(bracketPairs.get(openBrackets.peek()))) {
result.group().equals(bracketPairs.get(openBrackets.peek()))) {
openBrackets.pop(); openBrackets.pop();
} else { } else {
return false; return false;

View file

@ -17,4 +17,4 @@ public interface CommandExecutor {
* @return Resultat, ueblicherweise eine oder mehrere Zeilen. * @return Resultat, ueblicherweise eine oder mehrere Zeilen.
*/ */
String execute(String command) throws Exception; String execute(String command) throws Exception;
} }

View file

@ -10,7 +10,7 @@ public class Competitor implements Comparable<Competitor> {
private final String time; private final String time;
private int rank; private int rank;
public Competitor(int rank, String name, String time) { public Competitor(int rank, String name, String time) {
this.rank = rank; this.rank = rank;
this.name = name; this.name = name;
this.time = time; this.time = time;
@ -32,17 +32,20 @@ public class Competitor implements Comparable<Competitor> {
return name; return name;
} }
private static long parseTime(String s) { private static long parseTime(String s) {
try { try {
DateFormat sdf = new SimpleDateFormat("HH:mm:ss"); DateFormat sdf = new SimpleDateFormat("HH:mm:ss");
Date date = sdf.parse(s); Date date = sdf.parse(s);
return date.getTime(); return date.getTime();
} catch (Exception e) {System.err.println(e);} } catch (Exception e) {
System.err.println(e);
}
return 0; return 0;
} }
public String toString() { public String toString() {
return ""+ rank + " "+name+" "+time; return "" + rank + " " + name + " " + time;
} }
@Override @Override
@ -61,12 +64,11 @@ public class Competitor implements Comparable<Competitor> {
} }
@Override @Override
public boolean equals (Object o) { public boolean equals(Object o) {
if (o instanceof Competitor) { if (o instanceof Competitor) {
Competitor c = (Competitor)o; Competitor c = (Competitor) o;
return compareTo(c) == 0; return compareTo(c) == 0;
} } else {
else {
return false; return false;
} }
} }
@ -86,8 +88,7 @@ class AlphaComparatorCompetitor implements Comparator<Competitor> {
if (result == 0) { if (result == 0) {
return Long.compare(o1.getTimeValue(), o2.getTimeValue()); return Long.compare(o1.getTimeValue(), o2.getTimeValue());
} } else {
else {
return result; return result;
} }
} }

View file

@ -17,16 +17,16 @@ import java.util.Set;
/** /**
* @(#)ExBoxFrame.java * @(#)ExBoxFrame.java
* *
* JFC ExBox application * JFC ExBox application
* *
* @author K.Rege * @author K.Rege
* @version 1.00 2014/2/3 * @version 1.00 2014/2/3
* @version 1.01 2016/8/2 * @version 1.01 2016/8/2
* @version 2.00 2017/8/30 Test * @version 2.00 2017/8/30 Test
* @version 2.01 2018/2/5 AutoscaleFaktor * @version 2.01 2018/2/5 AutoscaleFaktor
* @version 2.02 2018/3/12 Reconnect (inspired by S. Kunz) * @version 2.02 2018/3/12 Reconnect (inspired by S. Kunz)
* @version 2.03 2021/7/24 Test (repeat) * @version 2.03 2021/7/24 Test (repeat)
* @version 2.04 2021/9/11 Test as plugin * @version 2.04 2021/9/11 Test as plugin
*/ */
public class ExBoxFrame extends JFrame implements ActionListener, ItemListener { public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
private final int UHDTHRESHOLD = 1920; private final int UHDTHRESHOLD = 1920;
@ -48,9 +48,11 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
public void setFontSize(int size) { public void setFontSize(int size) {
Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet(); Set<Object> keySet = UIManager.getLookAndFeelDefaults().keySet();
for (Object key : keySet) { for (Object key : keySet) {
if (key != null && key.toString().toLowerCase().contains("font")) { if (key != null && key.toString().toLowerCase().contains("font")) {
Font font = UIManager.getDefaults().getFont(key); Font font = UIManager.getDefaults().getFont(key);
if (font != null) { if (font != null) {
font = font.deriveFont((float) size); font = font.deriveFont((float) size);
UIManager.put(key, font); UIManager.put(key, font);
@ -132,9 +134,11 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
private Path getPathCompiled() { private Path getPathCompiled() {
try { try {
Path path = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI()); Path path = Paths.get(getClass().getProtectionDomain().getCodeSource().getLocation().toURI());
for (String part : getClass().getPackage().getName().split("\\.")) { for (String part : getClass().getPackage().getName().split("\\.")) {
path = path.resolve(part); path = path.resolve(part);
} }
return path; return path;
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -150,11 +154,11 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double scaleFaktor = (screenSize.getWidth() <= UHDTHRESHOLD) ? 1 : 2; double scaleFaktor = (screenSize.getWidth() <= UHDTHRESHOLD) ? 1 : 2;
setFontSize((int) (11 * scaleFaktor)); setFontSize((int) (11 * scaleFaktor));
setSize( setSize(new Dimension((int) (400 * scaleFaktor), (int) (400 * scaleFaktor)));
new Dimension((int) (400 * scaleFaktor), (int) (400 * scaleFaktor)));
setTitle("ExBox"); setTitle("ExBox");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponents(); initComponents();
@ -174,14 +178,17 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
if (lastServer != null) { if (lastServer != null) {
command = ServerFactory.createServer(lastServer); command = ServerFactory.createServer(lastServer);
} }
if (!arguments.getText().equals(history.getItemAt(0)) if (!arguments.getText().equals(history.getItemAt(0))
&& !arguments.getText().equals(history.getSelectedItem())) { && !arguments.getText().equals(history.getSelectedItem())) {
history.insertItemAt(arguments.getText(), 0); history.insertItemAt(arguments.getText(), 0);
} }
if (command == null) { if (command == null) {
error("no Server connected"); error("no Server connected");
} else { } else {
String res = command.execute(args); String res = command.execute(args);
if (graphicOn) { if (graphicOn) {
graphic.setFigure(res); graphic.setFigure(res);
} else { } else {
@ -213,17 +220,20 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
} }
private String openFileDialog(Path startDirectory, String pattern) { private String openFileDialog(Path startDirectory, String pattern) {
FileDialog fd = new FileDialog(this, "Open"); FileDialog fd = new FileDialog(this, "Open");
if (pattern != null) fd.setFile(pattern); if (pattern != null)
if (startDirectory != null) fd.setDirectory(startDirectory.toString()); fd.setFile(pattern);
if (startDirectory != null)
fd.setDirectory(startDirectory.toString());
fd.setVisible(true); fd.setVisible(true);
return fd.getDirectory() + fd.getFile(); return fd.getDirectory() + fd.getFile();
} }
private void testCommand(boolean retest) throws Exception { private void testCommand(boolean retest) throws Exception {
if (!retest) { if (!retest) {
lastTestFile = openFileDialog(getPathCompiled(), "*test.class"); lastTestFile = openFileDialog(getPathCompiled(), "*test.class");
} }
if (lastTestFile == null) { if (lastTestFile == null) {
output.append("ERROR no Test spezified\n"); output.append("ERROR no Test spezified\n");
} else if (unitTest != null) { } else if (unitTest != null) {
@ -236,29 +246,29 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
command = ServerFactory.createServer(name); command = ServerFactory.createServer(name);
lastServer = name; lastServer = name;
String fullClassName = command.getClass().getName(); String fullClassName = command.getClass().getName();
String simpleClassName = fullClassName.substring( String simpleClassName = fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
fullClassName.lastIndexOf('.') + 1);
setTitle("ExBox connected to " + simpleClassName); setTitle("ExBox connected to " + simpleClassName);
} }
private void openFile() throws Exception { private void openFile() throws Exception {
String name = openFileDialog(null, null); String name = openFileDialog(null, null);
try (BufferedReader br = new BufferedReader( try (BufferedReader br = new BufferedReader(
new InputStreamReader(new FileInputStream(name), STANDARDENCODING))) new InputStreamReader(new FileInputStream(name), STANDARDENCODING))) {
{
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
String line; String line;
while ((line = br.readLine()) != null) { while ((line = br.readLine()) != null) {
b.append(line); b.append(line);
b.append('\n'); b.append('\n');
} }
execute(b.toString()); execute(b.toString());
} }
} }
public void itemStateChanged(ItemEvent e) { public void itemStateChanged(ItemEvent e) {
try { try {
arguments.setText((String) e.getItem()); arguments.setText((String) e.getItem());
execute(arguments.getText()); execute(arguments.getText());
@ -267,7 +277,7 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
} }
} }
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { try {
if ((e.getSource() == arguments) || (e.getSource() == enter)) { if ((e.getSource() == arguments) || (e.getSource() == enter)) {
execute(arguments.getText()); execute(arguments.getText());
@ -293,4 +303,4 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
error(ex.toString()); error(ex.toString());
} }
} }
} }

View file

@ -19,9 +19,7 @@ public class ExBoxJUnit implements CommandExecutor {
public String execute(String testFile) throws Exception { public String execute(String testFile) throws Exception {
final List<String> successfulTests = new LinkedList<>(); final List<String> successfulTests = new LinkedList<>();
final List<TestFailure> failedResults = new LinkedList<>(); final List<TestFailure> failedResults = new LinkedList<>();
StringBuilder output = new StringBuilder(); StringBuilder output = new StringBuilder();
output.append("\nRUN TESTS ").append(new File(testFile).getName().split("\\.")[0]).append("\n"); output.append("\nRUN TESTS ").append(new File(testFile).getName().split("\\.")[0]).append("\n");
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
@ -30,6 +28,7 @@ public class ExBoxJUnit implements CommandExecutor {
Launcher launcher = LauncherFactory.create(); Launcher launcher = LauncherFactory.create();
launcher.discover(request); launcher.discover(request);
launcher.registerTestExecutionListeners(new TestExecutionListener() { launcher.registerTestExecutionListeners(new TestExecutionListener() {
@Override @Override
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
@ -45,21 +44,27 @@ public class ExBoxJUnit implements CommandExecutor {
} }
} }
}); });
launcher.execute(request); launcher.execute(request);
for (String testName : successfulTests) { for (String testName : successfulTests) {
output.append(testName).append(": OK\n"); output.append(testName).append(": OK\n");
} }
for (TestFailure result : failedResults) { for (TestFailure result : failedResults) {
output.append(result.getName()).append(": ERROR\n"); output.append(result.getName()).append(": ERROR\n");
String error = result.errorString(); String error = result.errorString();
if (!error.isEmpty()) { if (!error.isEmpty()) {
output.append(error).append("\n"); output.append(error).append("\n");
} }
} }
boolean wasSuccessful = failedResults.isEmpty(); boolean wasSuccessful = failedResults.isEmpty();
output.append("TESTS ").append(wasSuccessful ? "PASSED" : "FAILED").append(": ") output.append("TESTS ").append(wasSuccessful ? "PASSED" : "FAILED").append(": ")
.append(wasSuccessful ? "OK \u263a" : failedResults.size() + " ERRORS").append("\n"); .append(wasSuccessful ? "OK \u263a" : failedResults.size() + " ERRORS").append("\n");
return output.toString(); return output.toString();
} }
@ -80,8 +85,9 @@ public class ExBoxJUnit implements CommandExecutor {
if (throwable == null) { if (throwable == null) {
return ""; return "";
} }
try (StringWriter stringWriter = new StringWriter(); try (StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter)) { PrintWriter printWriter = new PrintWriter(stringWriter)) {
throwable.printStackTrace(printWriter); throwable.printStackTrace(printWriter);
return stringWriter.toString(); return stringWriter.toString();
} catch (IOException e) { } catch (IOException e) {

View file

@ -19,6 +19,7 @@ public class GraphicPanel extends JPanel {
int iy0 = (int) (h * y); int iy0 = (int) (h * y);
int ix1 = (int) (w * (x + width)); int ix1 = (int) (w * (x + width));
int iy1 = (int) (h * (y + height)); int iy1 = (int) (h * (y + height));
if (style.equals("draw")) { if (style.equals("draw")) {
g.drawRect(ix0, h - iy1, ix1 - ix0, iy1 - iy0); g.drawRect(ix0, h - iy1, ix1 - ix0, iy1 - iy0);
} else { } else {
@ -32,8 +33,10 @@ public class GraphicPanel extends JPanel {
int h = getHeight(); int h = getHeight();
g.setColor(Color.black); g.setColor(Color.black);
StringTokenizer tok = new StringTokenizer(figure, " <>=/,\"\n"); StringTokenizer tok = new StringTokenizer(figure, " <>=/,\"\n");
while (tok.hasMoreTokens()) { while (tok.hasMoreTokens()) {
String fig = tok.nextToken(); String fig = tok.nextToken();
if (fig.equals("line")) { if (fig.equals("line")) {
tok.nextToken(); tok.nextToken();
double x1 = Double.parseDouble(tok.nextToken()); double x1 = Double.parseDouble(tok.nextToken());
@ -43,6 +46,7 @@ public class GraphicPanel extends JPanel {
double x2 = Double.parseDouble(tok.nextToken()); double x2 = Double.parseDouble(tok.nextToken());
tok.nextToken(); tok.nextToken();
double y2 = Double.parseDouble(tok.nextToken()); double y2 = Double.parseDouble(tok.nextToken());
g.drawLine((int) (x1 * w), h - (int) (y1 * h), g.drawLine((int) (x1 * w), h - (int) (y1 * h),
(int) (x2 * w), h - (int) (y2 * h)); (int) (x2 * w), h - (int) (y2 * h));
} else if (fig.equals("rect")) { } else if (fig.equals("rect")) {

View file

@ -17,8 +17,7 @@ public class LCMServer implements CommandExecutor {
int y = Integer.parseInt(matcher.group(2)); int y = Integer.parseInt(matcher.group(2));
int result = leastCommonMultiple(x, y); int result = leastCommonMultiple(x, y);
return "Das Resultat ist %d\n".formatted(result); return "Das Resultat ist %d\n".formatted(result);
} } else {
else {
throw new Exception("Der angegebene Befehl \"%s\" ist ungültig!\n".formatted(command)); throw new Exception("Der angegebene Befehl \"%s\" ist ungültig!\n".formatted(command));
} }
} }
@ -31,7 +30,7 @@ public class LCMServer implements CommandExecutor {
while (y != 0) { while (y != 0) {
x %= y; x %= y;
x ^= y; x ^= y;
y ^=x; y ^= x;
x ^= y; x ^= y;
} }

View file

@ -12,8 +12,7 @@ public class ListStack implements Stack {
if (innerList.add(x)) { if (innerList.add(x)) {
return; return;
} }
} } catch (Exception e) {
catch (Exception e) {
throw new StackOverflowError(); throw new StackOverflowError();
} }
} }
@ -53,5 +52,5 @@ public class ListStack implements Stack {
public boolean isFull() { public boolean isFull() {
return innerList.size() == Integer.MIN_VALUE; return innerList.size() == Integer.MIN_VALUE;
} }
} }

View file

@ -192,10 +192,10 @@ public class MyList extends AbstractList<Object> {
* Inserts the specified `item` after the specified `node`. * Inserts the specified `item` after the specified `node`.
* *
* @param node * @param node
* The node to insert the `item` after. * The node to insert the `item` after.
* *
* @param item * @param item
* The item to insert. * The item to insert.
*/ */
protected void insertAfter(IListNode node, Object item) { protected void insertAfter(IListNode node, Object item) {
IListNode newNode = new ListNode(item, node.getNextNode(), node); IListNode newNode = new ListNode(item, node.getNextNode(), node);

View file

@ -23,10 +23,10 @@ public class RankingListServer implements CommandExecutor {
String[] fields = currentLine.split(Delimiter); String[] fields = currentLine.split(Delimiter);
result.add( result.add(
new Competitor( new Competitor(
0, 0,
fields[0], fields[0],
fields[1])); fields[1]));
} }
return result; return result;
@ -56,16 +56,16 @@ public class RankingListServer implements CommandExecutor {
} }
return String.join( return String.join(
System.lineSeparator(), System.lineSeparator(),
competitors.stream().map( competitors.stream().map(
(competitor) -> competitor.toString()).collect(Collectors.toList())); (competitor) -> competitor.toString()).collect(Collectors.toList()));
} }
public String createNameList(List<Competitor> competitorList) { public String createNameList(List<Competitor> competitorList) {
return String.join( return String.join(
System.lineSeparator(), System.lineSeparator(),
competitorList.stream().sorted(new AlphaComparatorCompetitor()).map( competitorList.stream().sorted(new AlphaComparatorCompetitor()).map(
(competitor) -> competitor.toString()).collect(Collectors.toList())); (competitor) -> competitor.toString()).collect(Collectors.toList()));
} }
public String execute(String rankingList) { public String execute(String rankingList) {

View file

@ -16,7 +16,8 @@ class MyClassLoader extends ClassLoader {
/** /**
* @param name filename of class * @param name filename of class
* return content of file as array of bytes; if file does not exist return null * return content of file as array of bytes; if file does not exist
* return null
*/ */
private byte[] getBytes(String name) { private byte[] getBytes(String name) {
try { try {
@ -38,15 +39,19 @@ class MyClassLoader extends ClassLoader {
System.out.println("load:" + name + " " + resolve); System.out.println("load:" + name + " " + resolve);
Class<?> clazz; Class<?> clazz;
byte[] classData = getBytes(name); byte[] classData = getBytes(name);
if (classData != null) { if (classData != null) {
clazz = defineClass(null, classData, 0, classData.length); clazz = defineClass(null, classData, 0, classData.length);
path = name.substring(0, path = name.substring(0,
name.length() - clazz.getName().length() - ".class".length()); name.length() - clazz.getName().length() - ".class".length());
return clazz; return clazz;
} }
if (!resolve) { if (!resolve) {
classData = getBytes( classData = getBytes(
path + name.replace(".", File.separator) + ".class"); path + name.replace(".", File.separator) + ".class");
if (classData != null) { if (classData != null) {
return defineClass(null, classData, 0, classData.length); return defineClass(null, classData, 0, classData.length);
} }
@ -67,10 +72,11 @@ public class ServerFactory {
public static Class<?> loadClass(String name) throws Exception { public static Class<?> loadClass(String name) throws Exception {
MyClassLoader myClassLoader = new MyClassLoader( MyClassLoader myClassLoader = new MyClassLoader(
MyClassLoader.class.getClassLoader()); MyClassLoader.class.getClassLoader());
return myClassLoader.loadClass(name, true); return myClassLoader.loadClass(name, true);
} }
public static CommandExecutor createServer(String name) throws Exception { public static CommandExecutor createServer(String name) throws Exception {
return (CommandExecutor) loadClass(name).getConstructor(new Class[]{}).newInstance(); return (CommandExecutor) loadClass(name).getConstructor(new Class[] {}).newInstance();
} }
} }

View file

@ -46,4 +46,4 @@ public interface Stack {
* @return Gibt true zurück, falls der Stack voll ist. * @return Gibt true zurück, falls der Stack voll ist.
*/ */
boolean isFull(); boolean isFull();
} }

View file

@ -75,9 +75,11 @@ public class ADS2_3_test {
public void testMixed() { public void testMixed() {
list.clear(); list.clear();
List<Character> list2 = new LinkedList<>(); List<Character> list2 = new LinkedList<>();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Character c = (char) ('A' + (Math.random() * 26)); Character c = (char) ('A' + (Math.random() * 26));
int op = (int) (Math.random() * 2); int op = (int) (Math.random() * 2);
switch (op) { switch (op) {
case 0: case 0:
list.add(c); list.add(c);
@ -89,7 +91,9 @@ public class ADS2_3_test {
break; break;
} }
} }
assertEquals(list2.size(), list.size()); assertEquals(list2.size(), list.size());
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
char c1 = (char) list.get(i); char c1 = (char) list.get(i);
char c2 = (char) list2.get(i); char c2 = (char) list2.get(i);

View file

@ -55,9 +55,11 @@ public class ADS2_4_test {
@Test @Test
public void testMixed() { public void testMixed() {
List<Character> list2 = new LinkedList<>(); List<Character> list2 = new LinkedList<>();
for (int i = 0; i < 100; i++) { for (int i = 0; i < 100; i++) {
Character c = (char) ('A' + (Math.random()*26)); Character c = (char) ('A' + (Math.random() * 26));
int op = (int)(Math.random()*2); int op = (int) (Math.random() * 2);
switch (op) { switch (op) {
case 0: case 0:
list.add(c); list.add(c);
@ -69,11 +71,13 @@ public class ADS2_4_test {
break; break;
} }
} }
Collections.sort(list2); Collections.sort(list2);
assertEquals(list2.size(), list.size()); assertEquals(list2.size(), list.size());
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
char c1 = (char)list.get(i); char c1 = (char) list.get(i);
char c2 = (char)list2.get(i); char c2 = (char) list2.get(i);
assertEquals(c1, c2); assertEquals(c1, c2);
} }
} }

View file

@ -18,17 +18,17 @@ public class ADS3_1_test {
@Test @Test
public void testEquals() { public void testEquals() {
assertTrue(c3.equals(c3), c3.getTime() + " == "+c3.getTime()); assertTrue(c3.equals(c3), c3.getTime() + " == " + c3.getTime());
assertTrue(!c3.equals(c2), c3.getTime() + " != "+c2.getTime()); assertTrue(!c3.equals(c2), c3.getTime() + " != " + c2.getTime());
assertTrue(!c3.equals(null), "equals(null)"); assertTrue(!c3.equals(null), "equals(null)");
assertTrue(!c3.equals(new Object()), "equals(new Object())"); assertTrue(!c3.equals(new Object()), "equals(new Object())");
} }
@Test @Test
public void testCompare() { public void testCompare() {
assertTrue(c1.compareTo(c2) > 0, c1.getName() +" "+c1.getTime() + " > "+c2.getTime()); assertTrue(c1.compareTo(c2) > 0, c1.getName() + " " + c1.getTime() + " > " + c2.getTime());
assertTrue(c2.compareTo(c1) < 0, c2.getName() +" "+c2.getTime() + " < "+c1.getTime()); assertTrue(c2.compareTo(c1) < 0, c2.getName() + " " + c2.getTime() + " < " + c1.getTime());
assertTrue(c3.compareTo(c3) == 0, c3.getName() +" "+c3.getTime() + " == "+c3.getTime()); assertTrue(c3.compareTo(c3) == 0, c3.getName() + " " + c3.getTime() + " == " + c3.getTime());
assertTrue(c4.compareTo(c2) < 0, c4.getName() +" "+c4.getTime() + " < "+c2.getTime()); assertTrue(c4.compareTo(c2) < 0, c4.getName() + " " + c4.getTime() + " < " + c2.getTime());
} }
} }

View file

@ -18,7 +18,7 @@ public class ADS3_2_test {
for (String line : lines) { for (String line : lines) {
String name = line.split(";")[0]; String name = line.split(";")[0];
String time = line.split(";")[1]; String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time)); competitorList.add(new Competitor(0, name, time));
} }
return competitorList; return competitorList;
} }
@ -35,6 +35,7 @@ public class ADS3_2_test {
"Oliver Ruben;02:32:12\n" + "Oliver Ruben;02:32:12\n" +
"Elmer Beat;02:33:53\n" + "Elmer Beat;02:33:53\n" +
"Kuehni Martin;02:33:36\n"; "Kuehni Martin;02:33:36\n";
rankGood = createList(rangliste); rankGood = createList(rangliste);
rankTest = new RankingListServer().createList(rangliste); rankTest = new RankingListServer().createList(rangliste);
} }
@ -42,8 +43,9 @@ public class ADS3_2_test {
@Test @Test
public void testCreateList() { public void testCreateList() {
assertEquals(rankGood.size(), rankTest.size(), "length"); assertEquals(rankGood.size(), rankTest.size(), "length");
for (int i = 0; i < rankGood.size();i++) {
assertEquals(rankGood.get(i).toString(), rankTest.get(i).toString(), "rangliste["+i+"]"); for (int i = 0; i < rankGood.size(); i++) {
assertEquals(rankGood.get(i).toString(), rankTest.get(i).toString(), "rangliste[" + i + "]");
} }
} }
} }

View file

@ -18,11 +18,13 @@ public class ADS3_3_test {
public List<Competitor> createList(String rankingText) { public List<Competitor> createList(String rankingText) {
List<Competitor> competitorList = new LinkedList<>(); List<Competitor> competitorList = new LinkedList<>();
String[] lines = rankingText.split("\n"); String[] lines = rankingText.split("\n");
for (String line : lines) { for (String line : lines) {
String name = line.split(";")[0]; String name = line.split(";")[0];
String time = line.split(";")[1]; String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time)); competitorList.add(new Competitor(0, name, time));
} }
return competitorList; return competitorList;
} }
@ -30,10 +32,12 @@ public class ADS3_3_test {
Collections.sort(competitorList); Collections.sort(competitorList);
int rank = 1; int rank = 1;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Competitor c : competitorList) { for (Competitor c : competitorList) {
c.setRank(rank++); c.setRank(rank++);
sb.append(c).append("\n"); sb.append(c).append("\n");
} }
return sb.toString(); return sb.toString();
} }
@ -49,11 +53,13 @@ public class ADS3_3_test {
"Oliver Ruben;02:32:12\n" + "Oliver Ruben;02:32:12\n" +
"Elmer Beat;02:33:53\n" + "Elmer Beat;02:33:53\n" +
"Kuehni Martin;02:33:36\n"; "Kuehni Martin;02:33:36\n";
rankGood = createList(rangliste); rankGood = createList(rangliste);
textGood = createSortedText(rankGood); textGood = createSortedText(rankGood);
rankTest = new RankingListServer().createList(rangliste); rankTest = new RankingListServer().createList(rangliste);
textTest = new RankingListServer().createSortedText(rankTest); textTest = new RankingListServer().createSortedText(rankTest);
} }
private String clean(String s) { private String clean(String s) {
return s.trim(); return s.trim();
} }
@ -63,8 +69,9 @@ public class ADS3_3_test {
String[] good = textGood.split("\n"); String[] good = textGood.split("\n");
String[] test = textTest.split("\n"); String[] test = textTest.split("\n");
assertEquals(good.length, test.length, "length"); assertEquals(good.length, test.length, "length");
for (int i = 0; i < good.length;i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste["+i+"]"); for (int i = 0; i < good.length; i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste[" + i + "]");
} }
} }
} }

View file

@ -17,35 +17,39 @@ public class ADS3_4_test {
public List<Competitor> createList(String rankingText) { public List<Competitor> createList(String rankingText) {
List<Competitor> competitorList = new LinkedList<>(); List<Competitor> competitorList = new LinkedList<>();
String[] lines = rankingText.split("\n"); String[] lines = rankingText.split("\n");
for (String line : lines) { for (String line : lines) {
String name = line.split(";")[0]; String name = line.split(";")[0];
String time = line.split(";")[1]; String time = line.split(";")[1];
competitorList.add(new Competitor(0, name, time)); competitorList.add(new Competitor(0, name, time));
} }
return competitorList; return competitorList;
} }
public String createSortedText(List<Competitor> competitorList) { public String createSortedText(List<Competitor> competitorList) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (Competitor c : competitorList) { for (Competitor c : competitorList) {
sb.append(c).append("\n"); sb.append(c).append("\n");
} }
return sb.toString(); return sb.toString();
} }
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
String ranglisteOrdered = String ranglisteOrdered = "Ancay Tarcis;02:20:02\n" +
"Ancay Tarcis;02:20:02\n" + "Elmer Beat;02:33:53\n" +
"Elmer Beat;02:33:53\n" + "Kiptum Daniel;02:11:31\n" +
"Kiptum Daniel;02:11:31\n" + "Kreibuhl Christian;02:21:47\n" +
"Kreibuhl Christian;02:21:47\n" + "Kuehni Martin;02:33:36\n" +
"Kuehni Martin;02:33:36\n" + "Marti Adrian;02:30:09\n" +
"Marti Adrian;02:30:09\n" + "Menzi Christoph;02:27:26\n" +
"Menzi Christoph;02:27:26\n" + "Mueller Stefan;02:31:14\n" +
"Mueller Stefan;02:31:14\n" + "Oliver Ruben;02:32:12\n" +
"Oliver Ruben;02:32:12\n" + "Ott Michael;02:33:48\n";
"Ott Michael;02:33:48\n";
String ranglisteUnordered = "Mueller Stefan;02:31:14\n" + String ranglisteUnordered = "Mueller Stefan;02:31:14\n" +
"Marti Adrian;02:30:09\n" + "Marti Adrian;02:30:09\n" +
"Kiptum Daniel;02:11:31\n" + "Kiptum Daniel;02:11:31\n" +
@ -56,6 +60,7 @@ public class ADS3_4_test {
"Oliver Ruben;02:32:12\n" + "Oliver Ruben;02:32:12\n" +
"Elmer Beat;02:33:53\n" + "Elmer Beat;02:33:53\n" +
"Kuehni Martin;02:33:36\n"; "Kuehni Martin;02:33:36\n";
rankGood = createList(ranglisteOrdered); rankGood = createList(ranglisteOrdered);
textGood = createSortedText(rankGood); textGood = createSortedText(rankGood);
rankTest = createList(ranglisteUnordered); rankTest = createList(ranglisteUnordered);
@ -71,8 +76,9 @@ public class ADS3_4_test {
String[] good = textGood.split("\n"); String[] good = textGood.split("\n");
String[] test = textTest.split("\n"); String[] test = textTest.split("\n");
assertEquals(good.length, test.length, "length"); assertEquals(good.length, test.length, "length");
for (int i = 0; i < good.length;i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste["+i+"]"); for (int i = 0; i < good.length; i++) {
assertEquals(clean(good[i]), clean(test[i]), "rangliste[" + i + "]");
} }
} }
} }