diff --git a/.gitignore b/.gitignore index 1748771..2486fff 100644 --- a/.gitignore +++ b/.gitignore @@ -70,3 +70,10 @@ crashlytics.properties crashlytics-build.properties fabric.properties +/.idea/$CACHE_FILE$ +/.idea/$PRODUCT_WORKSPACE_FILE$ +/.idea/encodings.xml +/.idea/misc.xml +/.idea/modules.xml +/.idea/oca8-lernen.iml +/.idea/vcs.xml diff --git a/src/org/eidecker/oca8lernen/general/Deklaration.java b/src/org/eidecker/oca8lernen/general/Deklaration.java new file mode 100644 index 0000000..8b4e110 --- /dev/null +++ b/src/org/eidecker/oca8lernen/general/Deklaration.java @@ -0,0 +1,16 @@ +package org.eidecker.oca8lernen.general; + +import org.junit.jupiter.api.Test; + +public class Deklaration { + + @Test + public void zuweisung() { + int a , b, c = 1; + // int d = e = 1; // Nicht erlaubt + + System.out.println(c); + + } + +} diff --git a/src/org/eidecker/oca8lernen/general/FinallyTest.java b/src/org/eidecker/oca8lernen/general/FinallyTest.java new file mode 100644 index 0000000..3b29416 --- /dev/null +++ b/src/org/eidecker/oca8lernen/general/FinallyTest.java @@ -0,0 +1,52 @@ +package org.eidecker.oca8lernen.general; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class FinallyTest { + + @Test + public void testFinally() { + int a = divZero(5); + assertEquals(5, a); + } + + + private int divZero(int a) { + try { + return a/0; + } catch (ArithmeticException e) { + return sideEffect(); + } catch (Exception e) { + System.out.println("Exception vom Typ Exception gefangen"); + } finally { + return 5; + } + } + + @Test + public void exceptionBloecke() { + + assertThrows(Exception.class, () -> { + try { + int a = 1/0; + } catch (ArithmeticException e) { + System.out.println("ArithmeticException gefangen"); + throw new Exception(); + } catch (Exception e) { + System.out.println("Exception vom Typ Exception gefangen"); + } finally { + System.out.println("Finally"); + } + }); + + } + + private int sideEffect() { + System.out.println("Nur ein Seiteneffekt"); + return 0; + } + +} diff --git a/src/org/eidecker/oca8lernen/general/InterfaceWithFields.java b/src/org/eidecker/oca8lernen/general/InterfaceWithFields.java new file mode 100644 index 0000000..f91d2f2 --- /dev/null +++ b/src/org/eidecker/oca8lernen/general/InterfaceWithFields.java @@ -0,0 +1,41 @@ +package org.eidecker.oca8lernen.general; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class InterfaceWithFields implements T1, T2 { + + public int getT1Value() { + return VALUE_T1; + } + + public int getValue() { + return T2.VALUE; + } +} + +interface T1 { + int VALUE = 99; + int VALUE_T1 = 1; +} + +interface T2 { + int VALUE = 98; + int VALUE_T2 = 2; + + default int getValue() { + return VALUE; + } +} + +class Tester { + + public void interfaceTest() { + InterfaceWithFields interfaceWithFields = new InterfaceWithFields(); + assertEquals(99, ((T1)interfaceWithFields).VALUE); + assertEquals(98, ((T2)interfaceWithFields).VALUE); + + assertEquals(1, interfaceWithFields.VALUE_T1); + assertEquals(2, interfaceWithFields.VALUE_T2); + } + +} diff --git a/src/org/eidecker/oca8lernen/general/PrimitiveUndWrapperTest.java b/src/org/eidecker/oca8lernen/general/PrimitiveUndWrapperTest.java new file mode 100644 index 0000000..14092f4 --- /dev/null +++ b/src/org/eidecker/oca8lernen/general/PrimitiveUndWrapperTest.java @@ -0,0 +1,38 @@ +package org.eidecker.oca8lernen.general; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +public class PrimitiveUndWrapperTest { + + @Test + public void wrapperEquals() { + + Integer aIntegerType = 1; + Long aLongType = 1L; + + Long anotherLongType = 0b1L; + + int aInt = 1; + long aLong = 1L; + + assertFalse(aIntegerType.equals(aLongType)); + assertTrue(aInt == aLong); + + + assertTrue(aIntegerType == aLong); + assertTrue(aIntegerType == aInt); + assertTrue(aLongType == aInt); + assertTrue(aLongType == aLong); + + assertFalse(aIntegerType.equals(aLong)); + assertTrue(aIntegerType.equals(aInt)); + assertTrue(aLongType.equals(aLong)); + assertFalse(aLongType.equals(aInt)); + + } + +} diff --git a/src/org/eidecker/oca8lernen/general/StaticInitError.java b/src/org/eidecker/oca8lernen/general/StaticInitError.java new file mode 100644 index 0000000..301d263 --- /dev/null +++ b/src/org/eidecker/oca8lernen/general/StaticInitError.java @@ -0,0 +1,53 @@ +package org.eidecker.oca8lernen.general; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class StaticInitError { + + public static int i = 0; + public static int j; + + static { + j = 2 / i++; + } + +} + +class ConstructorInitError { + + public ConstructorInitError() { + int a = 1/0; + } + +} + + +class TesterStatic { + + @Test + public void testInit() { + assertThrows(ExceptionInInitializerError.class, () -> { + StaticInitError staticInitError = new StaticInitError(); + }); + + // Unterschiedlicher Error beim zweiten Mal. Die Klasse scheint nicht mehr verwendet zu werden. + assertThrows(NoClassDefFoundError.class, () -> { + StaticInitError staticInitError = new StaticInitError(); + }); + } + + @Test + public void testConstructorInit() { + assertThrows(ArithmeticException.class, () -> { + ConstructorInitError constructorInitError = new ConstructorInitError(); + }); + + assertThrows(ArithmeticException.class, () -> { + ConstructorInitError constructorInitError = new ConstructorInitError(); + }); + } + +}