From a5a8af76edc869207db50943f339e83af0a2fbd6 Mon Sep 17 00:00:00 2001 From: THINK Date: Mon, 26 Oct 2020 19:12:52 +0100 Subject: [PATCH] fix repo --- .gitignore | 3 + pom.xml | 61 ++++++++- src/main/java/introduction/HelloWorld.java | 8 -- src/main/java/second/debug/Breakpoints.java | 20 +++ .../java/second/debug/ConditionalBreak.java | 27 ++++ .../second/debug/EvaluateExpressions.java | 44 ++++++ .../java/second/debug/hidden/MakeThings.java | 10 ++ .../second/debug/hidden/ObjectAnalyzer.java | 38 ++++++ .../java/second/debug/hidden/ObjectMaker.java | 74 ++++++++++ src/main/java/second/junit/AdvanceMath.java | 23 ++++ .../second/shortcuts/ClassThatHaveItAll.java | 51 +++++++ .../shortcuts/ClassThatHaveItAllBis.java | 29 ++++ .../java/second/shortcuts/InterfaceOne.java | 7 + src/main/resources/log4j.properties | 16 +++ .../java/second/junit/AdvanceMathTest.java | 38 ++++++ src/test/java/second/junit/FrodoTest.java | 128 ++++++++++++++++++ 16 files changed, 565 insertions(+), 12 deletions(-) create mode 100644 .gitignore delete mode 100644 src/main/java/introduction/HelloWorld.java create mode 100644 src/main/java/second/debug/Breakpoints.java create mode 100644 src/main/java/second/debug/ConditionalBreak.java create mode 100644 src/main/java/second/debug/EvaluateExpressions.java create mode 100644 src/main/java/second/debug/hidden/MakeThings.java create mode 100644 src/main/java/second/debug/hidden/ObjectAnalyzer.java create mode 100644 src/main/java/second/debug/hidden/ObjectMaker.java create mode 100644 src/main/java/second/junit/AdvanceMath.java create mode 100644 src/main/java/second/shortcuts/ClassThatHaveItAll.java create mode 100644 src/main/java/second/shortcuts/ClassThatHaveItAllBis.java create mode 100644 src/main/java/second/shortcuts/InterfaceOne.java create mode 100644 src/main/resources/log4j.properties create mode 100644 src/test/java/second/junit/AdvanceMathTest.java create mode 100644 src/test/java/second/junit/FrodoTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4656bef --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea +log.log +.iml \ No newline at end of file diff --git a/pom.xml b/pom.xml index 35ef47a..e9536b0 100644 --- a/pom.xml +++ b/pom.xml @@ -4,12 +4,65 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - org.example - Pracownia2020-2021 + Pracownia + PracowniaMain 1.0-SNAPSHOT - 11 - 11 + 8 + 8 + + + ${project.artifactId}-${project.version} + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + false + false + + + + second.debug.Breakpoints + + + + + jar-with-dependencies + + + + + + + + + + + log4j + log4j + 1.2.17 + + + org.assertj + assertj-core + 3.17.2 + test + + + junit + junit + 4.12 + test + + + \ No newline at end of file diff --git a/src/main/java/introduction/HelloWorld.java b/src/main/java/introduction/HelloWorld.java deleted file mode 100644 index 86201c2..0000000 --- a/src/main/java/introduction/HelloWorld.java +++ /dev/null @@ -1,8 +0,0 @@ -package introduction; - -public class HelloWorld { - - public static void main(String [ ] args) { - System.out.println("Hello World"); - } -} \ No newline at end of file diff --git a/src/main/java/second/debug/Breakpoints.java b/src/main/java/second/debug/Breakpoints.java new file mode 100644 index 0000000..54f3756 --- /dev/null +++ b/src/main/java/second/debug/Breakpoints.java @@ -0,0 +1,20 @@ +package second.debug; + +import second.debug.hidden.MakeThings; + +public class Breakpoints { + + public static void main(final String... args) { + //create objects + Object o = MakeThings.makeAThing(); + System.out.println("Going inside print"); + printThis(o); + } + + + + private static void printThis(final Object o) { + System.out.println("I will print you something very soon"); + System.out.println(o.toString()); + } +} diff --git a/src/main/java/second/debug/ConditionalBreak.java b/src/main/java/second/debug/ConditionalBreak.java new file mode 100644 index 0000000..0c3fcb7 --- /dev/null +++ b/src/main/java/second/debug/ConditionalBreak.java @@ -0,0 +1,27 @@ +package second.debug; + +import second.debug.hidden.ObjectAnalyzer; +import second.debug.hidden.ObjectMaker; + +public class ConditionalBreak { + + public static void main(final String... args) { + // In E2, getList didn't work so well. So let's try an array this time... + final Object[] myArray = ObjectMaker.getArray(100); + + boolean everythingIsOK = true; + int i = 0; + while (i < myArray.length && everythingIsOK) { + // This time we're using an external library to process objects. + // If something goes wrong, this is the line where we'll want to set a + // breakpoint. + everythingIsOK = ObjectAnalyzer.processElementAtIndex(myArray, i); + i++; + } + + if (!everythingIsOK) { + throw new RuntimeException( + "Oh noes - analysis incomplete! See console for more information."); + } + } +} diff --git a/src/main/java/second/debug/EvaluateExpressions.java b/src/main/java/second/debug/EvaluateExpressions.java new file mode 100644 index 0000000..b94bf33 --- /dev/null +++ b/src/main/java/second/debug/EvaluateExpressions.java @@ -0,0 +1,44 @@ +package second.debug; + +import second.debug.hidden.ObjectMaker; + +import java.util.List; + +/** + * Exercise 2: Using the Expression Window in the Debug perspective. + * + * @author Mark Hiner + */ +public class EvaluateExpressions { + + public static void main(final String... args) { + // Let's make a list of 100000 objects + final List myList = ObjectMaker.getList(100000); + + // Now let's process some objects from our list + + // Process first object + processElementAtIndex(myList, 0); + + // Process middle object + processElementAtIndex(myList, 100000 / 2); + + // Process last object + processElementAtIndex(myList, 100000 - 1); + } + + private static void processElementAtIndex(final List list, + final int index) + { + // First let's check our method arguments to see if they're valid + if (index < 0 || index >= list.size()) { + throw new IllegalArgumentException( + "If you don't mind, I would prefer not to process your object..."); + } + System.out.println(100 * list.get(index)); + + // OK now we can process the argument. + // ... just kidding, I'm totally going to delete your precious objects. + list.set(index, null); + } +} diff --git a/src/main/java/second/debug/hidden/MakeThings.java b/src/main/java/second/debug/hidden/MakeThings.java new file mode 100644 index 0000000..f6c1b15 --- /dev/null +++ b/src/main/java/second/debug/hidden/MakeThings.java @@ -0,0 +1,10 @@ +package second.debug.hidden; + +public class MakeThings { + + public static Object makeAThing() { + final Object o = new Object(); + System.out.println(o); + return null; + } +} diff --git a/src/main/java/second/debug/hidden/ObjectAnalyzer.java b/src/main/java/second/debug/hidden/ObjectAnalyzer.java new file mode 100644 index 0000000..47b6d92 --- /dev/null +++ b/src/main/java/second/debug/hidden/ObjectAnalyzer.java @@ -0,0 +1,38 @@ +package second.debug.hidden; + +public class ObjectAnalyzer { + + + public static boolean processElementAtIndex(final Object[] myArray, + final int i) { + final Object o = myArray[i]; + + if (o == null) { + System.out.println("Something is wrong with the " + (i + 1) + suffix(i + + 1) + " object... :(\n\n"); + return false; + } + + return true; + } + + private static String suffix(int i) { + i = i % 10; + + String suffix = "th"; + switch (i) { + case 1: + suffix = "rst"; + break; + case 2: + suffix = "nd"; + break; + case 3: + suffix = "rd"; + break; + } + return suffix; + } + +} + diff --git a/src/main/java/second/debug/hidden/ObjectMaker.java b/src/main/java/second/debug/hidden/ObjectMaker.java new file mode 100644 index 0000000..51a1f94 --- /dev/null +++ b/src/main/java/second/debug/hidden/ObjectMaker.java @@ -0,0 +1,74 @@ +/* + * #%L + * ImageJ interactive debugging tutorials. + * %% + * Copyright (C) 2009 - 2016 Board of Regents of the University of + * Wisconsin-Madison. + * %% + * To the extent possible under law, the ImageJ developers have waived + * all copyright and related or neighboring rights to this tutorial code. + * + * See the CC0 1.0 Universal license for details: + * http://creativecommons.org/publicdomain/zero/1.0/ + * #L% + */ + +package second.debug.hidden; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * STOP LOOKING AT THIS CLASS!! IT'S OFF LIMITS! + * + * @author Mark Hiner + */ +public class ObjectMaker { + + final static Set cache = new HashSet(); + + public static List getList(final int size) { + final List list = new ArrayList(); + + for (int i = 0; i < size - 1; i++) { + list.add(new Double(Math.random() * 100000).intValue()); + } + list.add(null); + + return list; + } + + public static Object[] getArray(final int size) { + final Object[] array = new Object[size]; + + for (int i = 0; i < size - 1; i++) { + array[i] = new Double(Math.random() * 100000); + } + + array[(int) (Math.random() * (size - 1))] = null; + + return array; + } + + public static Double[] getDoubleArray(final int size) { + final Double[] array = new Double[size]; + + return array; + } + + public static Float[] getFloatArray(final int size) { + final Float[] array = new Float[size]; + + cache.add(array); + + return array; + } + + public static Long[] getLongArray(final int size) { + final Long[] array = new Long[size]; + + return array; + } +} diff --git a/src/main/java/second/junit/AdvanceMath.java b/src/main/java/second/junit/AdvanceMath.java new file mode 100644 index 0000000..a4f44e2 --- /dev/null +++ b/src/main/java/second/junit/AdvanceMath.java @@ -0,0 +1,23 @@ +package second.junit; + +public class AdvanceMath { + + public int addition(int a, int b) { + return a+b; + } + + public int addition(String a, int b) { + Integer i = Integer.valueOf(a); + return i+b; + } + + public int multiply(int a, int b) throws Exception { + long la = a; + long lb = b; + if (la*lb > Integer.MAX_VALUE) throw new Exception("value over the limit"); + return a*b; + } + + + +} diff --git a/src/main/java/second/shortcuts/ClassThatHaveItAll.java b/src/main/java/second/shortcuts/ClassThatHaveItAll.java new file mode 100644 index 0000000..de30a17 --- /dev/null +++ b/src/main/java/second/shortcuts/ClassThatHaveItAll.java @@ -0,0 +1,51 @@ +package second.shortcuts; + +import java.util.List; + +public class ClassThatHaveItAll implements InterfaceOne { + + String name; + Integer number; + List list; + + public ClassThatHaveItAll() { + } + + public ClassThatHaveItAll(String name, Integer number, List list) { + this.name = name; + this.number = number; + this.list = list; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getNumber() { + return number; + } + + public void setNumber(Integer number) { + this.number = number; + } + + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + + public void printMe(String info) { + System.out.println(info); + } + + public void usageOfPrint() { + printMe("Hi"); + } +} diff --git a/src/main/java/second/shortcuts/ClassThatHaveItAllBis.java b/src/main/java/second/shortcuts/ClassThatHaveItAllBis.java new file mode 100644 index 0000000..864774f --- /dev/null +++ b/src/main/java/second/shortcuts/ClassThatHaveItAllBis.java @@ -0,0 +1,29 @@ +package second.shortcuts; + +public class ClassThatHaveItAllBis implements InterfaceOne { + + String name; + Integer number; + + public void printMe(String info) { + System.out.println(info); + } + + public void spam() { + long i = 0; + i = 10; + i = 110; + i = 1110; + i = 11110; + i = 111110; + i = 1111110; + i = 11111110; + i = 111111110; + i = 1111111110; + printMe(String.valueOf(i)); + } + + public void usageOfPrint() { + printMe("Hi"); + } +} diff --git a/src/main/java/second/shortcuts/InterfaceOne.java b/src/main/java/second/shortcuts/InterfaceOne.java new file mode 100644 index 0000000..2ece03d --- /dev/null +++ b/src/main/java/second/shortcuts/InterfaceOne.java @@ -0,0 +1,7 @@ +package second.shortcuts; + +public interface InterfaceOne { + + public void printMe(String info); + +} diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties new file mode 100644 index 0000000..6225f22 --- /dev/null +++ b/src/main/resources/log4j.properties @@ -0,0 +1,16 @@ +# Root logger option +log4j.rootLogger=DEBUG, stdout, file + +# Redirect log messages to console +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n + +# Redirect log messages to a log file, support file rolling. +log4j.appender.file=org.apache.log4j.RollingFileAppender +log4j.appender.file.File=log.log +log4j.appender.file.MaxFileSize=5MB +log4j.appender.file.MaxBackupIndex=10 +log4j.appender.file.layout=org.apache.log4j.PatternLayout +log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n \ No newline at end of file diff --git a/src/test/java/second/junit/AdvanceMathTest.java b/src/test/java/second/junit/AdvanceMathTest.java new file mode 100644 index 0000000..a622ab5 --- /dev/null +++ b/src/test/java/second/junit/AdvanceMathTest.java @@ -0,0 +1,38 @@ + +package second.junit; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +public class AdvanceMathTest { + + AdvanceMath math; + + @Before + public void setUp() { + System.out.println("Run setUp"); + math = new AdvanceMath(); + } + + @Test + public void additionTest() { + Integer a = math.addition(1, 4); + assertTrue(a == 5); + } + + @Test + public void additionTestString() { + long a = math.addition("1", 4); + Assert.assertEquals(5L, a); + } + + + @Test(expected = Exception.class) + public void additionTestString2() { + int a = math.addition("a1", 4); + } + +} \ No newline at end of file diff --git a/src/test/java/second/junit/FrodoTest.java b/src/test/java/second/junit/FrodoTest.java new file mode 100644 index 0000000..30ab266 --- /dev/null +++ b/src/test/java/second/junit/FrodoTest.java @@ -0,0 +1,128 @@ +package second.junit; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.*; + +public class FrodoTest { + + Creature frodo = new Creature("Frodo", 33L, Race.HOBBIT); + Creature sauron = new Creature("Sauron", 10000L, Race.WIZARD); + List fellowshipOfTheRing = new ArrayList(); + Creature boromir = new Creature("Boromir", 37L, Race.MAN); + Creature sam = new Creature("Sam", 38L, Race.HOBBIT); + Creature merry = new Creature("Merry", 36L, Race.HOBBIT); + Creature pippin = new Creature("Pippin", 28L, Race.HOBBIT); + Creature legolas = new Creature("Legolas", 2500L, Race.ELF); + Creature aragorn = new Creature("Aragorn", 87L, Race.MAN); + Creature gimli = new Creature("Gimli", 139L, Race.DWARF); + Creature gandalf = new Creature("Gandalf", 3000L, Race.WIZARD); + + @Before + public void prepareData() { + fellowshipOfTheRing.add(frodo); + fellowshipOfTheRing.add(sam); + fellowshipOfTheRing.add(merry); + fellowshipOfTheRing.add(pippin); + fellowshipOfTheRing.add(legolas); + fellowshipOfTheRing.add(aragorn); + fellowshipOfTheRing.add(boromir); + fellowshipOfTheRing.add(gimli); + fellowshipOfTheRing.add(gandalf); + } + + @Test + public void assertJtestexample() { + + // basic assertions + assertThat(frodo.getName()).isEqualTo("Frodo"); + assertThat(frodo).isNotEqualTo(sauron); + + // chaining string specific assertions + assertThat(frodo.getName()).startsWith("Fro") + .endsWith("do") + .isEqualToIgnoringCase("frodo"); + + // collection specific assertions (there are plenty more) + assertThat(fellowshipOfTheRing).hasSize(9) + .contains(frodo, sam) + .doesNotContain(sauron); + + // as() is used to describe the test and will be shown before the error message + assertThat(frodo.getAge()).isEqualTo(33); + } + + @Test + public void test() { + + // using the 'extracting' feature to check fellowshipOfTheRing character's names + assertThat(fellowshipOfTheRing).extracting(Creature::getName) + .doesNotContain("Sauron", "Elrond"); + + // extracting multiple values at once grouped in tuples + assertThat(fellowshipOfTheRing).extracting("name", "age", "race") + .contains(tuple("Boromir", 37L, Race.MAN), + tuple("Sam", 38L, Race.HOBBIT), + tuple("Legolas", 2500L, Race.ELF)); + + // filtering a collection before asserting + assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o")) + .containsOnly(aragorn, frodo, legolas, boromir); + + // combining filtering and extraction (yes we can) + assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o")) + .containsOnly(aragorn, frodo, legolas, boromir) + .extracting(character -> character.getRace()) + .contains(Race.HOBBIT, Race.ELF, Race.MAN); + } + +} + +class Creature { + String name; + Long age; + Race race; + + public Creature(String name, Long age, Race race) { + this.name = name; + this.age = age; + this.race = race; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getAge() { + return age; + } + + public void setAge(Long age) { + this.age = age; + } + + public Race getRace() { + return race; + } + + public void setRace(Race race) { + this.race = race; + } +} + +enum Race { + HOBBIT, + ORK, + WIZARD, + ELF, + MAN, + DWARF +} \ No newline at end of file