Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Git 2 #33

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/assertions/CMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package assertions;

/**
* @author - John Schmidt
* 23.11.2021, 00:42
*/
public class CMain {

public static void voidMethode(){
System.out.println("ich bin VoidMethode von Main");
}

public static void voidMethodeMitInt(int f){
System.out.println("ich bin VoidMethode von Main mit int f");
}


public static void main(String[] args) {


Calculator c1 = new Calculator();
System.out.println(c1.add1(2, 3));

// System.out.println( c1.addVoid(99,88) );//nicht systemprintln...
//System.out.println();
// voidMethode();
CMain.voidMethode();
CMain.voidMethodeMitInt(1);
c1.addVoid(99,88);

}
}
31 changes: 31 additions & 0 deletions src/main/java/assertions/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package assertions;

/**
* @author - John Schmidt
* 23.11.2021, 00:39
*/
public class Calculator {
CalculatorService service; //egal... muss man nicht verstehen


public Calculator(CalculatorService service){
this.service=service;
}
//weil ez, darum leerer Construktor...
public Calculator(){}


public int perform(int i, int j){
return service.add(i,j);
}


public int add1(int i, int j){
return i+j+1;//+1
}

public void addVoid(int i, int j){
System.out.println("irgendwas void zeugs");
}

}
11 changes: 11 additions & 0 deletions src/main/java/assertions/CalculatorService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package assertions;

/**
* @author - John Schmidt
* 23.11.2021, 00:39
*/
public interface CalculatorService {
public int add(int i, int j);

public void addVoid(int i, int j);
}
27 changes: 27 additions & 0 deletions src/main/java/assertions/PMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package assertions;

import java.time.LocalDate;

/**
* @author - John Schmidt
* 08.11.2021, 00:40
*/
public class PMain {
public static void main(String[] args) {

Person p1 = new Person("Hans","Muster",null);
Person p2 = new Person("Frau","Musterfrau", LocalDate.of(2005,11,28));
Person p3 = new Person("Hans","Muster",LocalDate.now());
//Person p4 = new Person("Hans","Muster",(LocalDate.now()-(LocalDate.of(2021,10,14))));
Person p5 = new Person("Frau","Musterfrau", LocalDate.of(2021,10,14));
//getAgeReturns10YearsIfBornIn2009() //2021-10=2011


//System.out.println("p1: "+p1.getAge());
System.out.println("p2: "+p2.getAge());
System.out.println("p3: "+p3.getAge());



}
}
20 changes: 20 additions & 0 deletions src/main/java/fakes/UMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fakes;

/**
* @author - John Schmidt
* 08.11.2021, 00:41
*/
public class UMain {
public static void main (String[] args){

User u1 = new User("Hansi");


UserValidator uv1 = new UserValidator();
uv1.doesUsernameExist("peter");
//System.out.println("abcd "+UserValidator.isValidUsername("m9mk"));


}

}
2 changes: 2 additions & 0 deletions src/main/java/fakes/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public User(String username){
}

public String getUsername() {

System.out.println("User getUserName: Hello Kitty"+username);
return username;
}
}
42 changes: 39 additions & 3 deletions src/main/java/fakes/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,53 @@ public class UserController {

private static Database db = FileDatabase.getInstance();

private UserValidator userValidator; //fakeuservalidator //aus Unterricht
private Database database= FileDatabase.getInstance();; //MockDatabase jedoch MockDatabase db geht nicht //aus Unterricht

public UserController(UserValidator UserValidator){//inject db.adduser
userValidator = UserValidator;
} //aus Unterricht
public UserController(UserValidator UserValidator, Database database){//inject db.adduser //aus fake
userValidator = UserValidator;
this.database = database; //Mok-database
}

public Message create(User user){
if(user == null){
throw new IllegalArgumentException("user required");
}
Boolean canCreate = UserValidator.isValidUsername(user.getUsername())
&& !UserValidator.doesUsernameExist(user.getUsername());
Boolean canCreate = false;//userValidator.isValidUsername(user.getUsername()) //aus Unterricht //static bei Uservalidator weg, dann hier Uservalidator klein , weil hier oberhalb konstruktor eine instanz reinkommt irgendwas hier rein komt
//&& !userValidator.doesUsernameExist(user.getUsername()); //aus Unterricht// nur mal für erste test
if(canCreate){
db.addUser(user);
db.addUser(user);//db
database.addUser(user);
return Message.createOK();
}else{
return Message.createNotOK();
}
}
}



//
//package fakes;
//
//public class UserController {
//
// private static Database db = FileDatabase.getInstance();
//
// public Message create(User user){
// if(user == null){
// throw new IllegalArgumentException("user required");
// }
// Boolean canCreate = UserValidator.isValidUsername(user.getUsername())
// && !UserValidator.doesUsernameExist(user.getUsername());
// if(canCreate){
// db.addUser(user);
// return Message.createOK();
// }else{
// return Message.createNotOK();
// }
// }
//}
63 changes: 52 additions & 11 deletions src/main/java/fakes/UserValidator.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,66 @@
package fakes;

import java.util.regex.Pattern;

public class UserValidator {

private static Database db = FileDatabase.getInstance();
private static Database db = FileDatabase.getInstance();//static löschen

public static boolean doesUsernameExist(String username){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//konstruktor für fake
public UserValidator(Database db){
this.db = db;
}

//default konstruktor
public UserValidator(){}

// MockDatabase database; //mockdatabase versuch eigener test..., schule bei usercontroller
//fakeUservalidator hat gleiche methode, mit override
public boolean doesUsernameExist(String username) { //static wird im Untericht gelöscht, dann hier auch static löschen
// try {
// Thread.sleep(2000);//5000
// } catch (InterruptedException e) {
// e.printStackTrace();
// }

for(User user : db.getUsers()){
if (user.getUsername().equals(username)){
// int zaehler = 0;
for (User user : db.getUsers()) { //Mock aus db wird database
// zaehler++;
if (user.getUsername().equals(username)) {
System.out.println("Bitte was? "+user.getUsername().equals(username));
System.out.println("Bitte was222 "+user.getUsername());
return true;
}
}
// System.out.println("Zaehler von doesUserNameExist: "+zaehler);
return false;
}

public static boolean isValidUsername(String username){
return true;

public static boolean isValidUsername(String username) { //static im Unterricht gelöscht, wegen FakeUserValidator
//wenn nur buchstabe true // methode für Tests verändert
//falsch wenn startet mit nummer
//wenn zahl aber nicht erste
//falsch wenn Sonderzeichen
boolean booleanIVU = false;
String str = username;
//System.out.println("str Methode: " + str);

if (str.length() == 0) {
booleanIVU = false;
//System.out.println("leer");
} else if (str.length() == 1 && Pattern.matches("[a-zA-Z]+", str)) {//ifOnlyLetters
booleanIVU = true;
//System.out.println("1 ");
} else if (str.length() == 2 && (Pattern.matches("[a-zA-Z]+", str.substring(0, 1))) && Pattern.matches("[a-zA-Z0-9 ]+", str.substring(1, 2))) {//&&str.substring(1, 2).equals("a")) {//Pattern.matches("[a-zA-Z0-9 ]+", str)
booleanIVU = true;
//System.out.println("2 ");
} else if (str.length() > 2 && (Pattern.matches("[a-zA-Z]+", str.substring(0, 1))) && Pattern.matches("[a-zA-Z0-9 ]+", str.substring(1, str.length()))) {
booleanIVU = true;
//System.out.println("3 ");
}

return booleanIVU;
}

}
97 changes: 97 additions & 0 deletions src/test/java/assertions/CalculatorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package assertions;

import fakes.User;
import org.junit.*;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.Arrays;
import java.util.Collections;

/**
* @author - John Schmidt
* 23.11.2021, 00:40
*/
public class CalculatorTest {

@Test
public void add1Test(){
Calculator c1 = new Calculator();
Assertions.assertEquals(5,c1.add1(2,3));//+1...
}


// CalculatorService service1 = new CalculatorService() { mach es automatisch, warum auch immer
// @Override
// public int add(int i, int j) {
// return 0;
// }
// }

CalculatorService service1 = Mockito.mock(CalculatorService.class); //egal... muss man nicht verstehen

@Test
public void performTest(){
Calculator c1 = new Calculator(service1);//mit service1, programm sagt create a constructor, klar weil ruft ja konstruktor auf
//Assertions.assertEquals(5,service.add1(2,3));
// Assertions.assertEquals(5,c1.perform(2,3));//ohne service1, sagt irgend ein Objekt ist null

//jetzt alles rot, weil möchte 5, aber gebe 0,0, da isch eher verständlich das da passiert
// damit jetzt 2 + 3 reinkommen:
//Mockito.doNothing()
//Mockito.when(service1.add(2,3))
Mockito.when(service1.add(1,4)).thenReturn(5);
Assertions.assertEquals(5,c1.perform(1,4));
}

@Test
public void performTestzumspielen(){
Calculator c1 = new Calculator(service1);//mit service1, programm sagt create a constructor, klar weil ruft ja konstruktor auf
//Assertions.assertEquals(5,service.add1(2,3));
// Assertions.assertEquals(5,c1.perform(2,3));//ohne service1, sagt irgend ein Objekt ist null

//jetzt alles rot, weil möchte 5, aber gebe 0,0, da isch eher verständlich das da passiert
// damit jetzt 2 + 3 reinkommen:
//Mockito.doNothing()
//Mockito.when(service1.add(2,3))
Mockito.when(service1.add(1,4)).thenReturn(5);
// System.out.println(Mockito.doReturn(Arrays.asList(new User("peter"))).when(mokitoDb).getUsers());//github
// System.out.println("uu "+ Mockito.doReturn(Collections.emptyList()).when(mokitoDb).getUsers() );//github
System.out.println("?? "+Mockito.when(service1.add(1,4)).thenReturn(5));

//System.out.println("?? "+Mockito.when(service1.add(1,4)).thenReturn(5));

//Assertions.assertEquals(5,c1.perform(1,4));
}

@Test
public void addVoidTest(){
Calculator c1 = new Calculator(service1);//mit service1, programm sagt create a constructor, klar weil ruft ja konstruktor auf
//Assertions.assertEquals(5,service.add1(2,3));
// Assertions.assertEquals(5,c1.perform(2,3));//ohne service1, sagt irgend ein Objekt ist null

//jetzt alles rot, weil möchte 5, aber gebe 0,0, da isch eher verständlich das da passiert
// damit jetzt 2 + 3 reinkommen:
//Mockito.doNothing()
//Mockito.when(service1.add(2,3))
// Mockito.when(service1.add(1,4)).thenReturn(5);
// Assertions.assertEquals(5,c1.perform(1,4));
// Mockito.when(service1.addVoid(2,3);)//au da git strichpuktund klammer
// Mockito.when(service1.add(2,3))//gibt kein strichpunkt Klammer
// Mockito.when(service1.addVoid(2,3));geht nicht

// System.out.println(Mockito.doAnswer(service1.addVoid(2,4));


// System.out.println(Mockito.doReturn(Arrays.asList(new User("peter"))).when(mokitoDb).getUsers());//github
// System.out.println("uu "+ Mockito.doReturn(Collections.emptyList()).when(mokitoDb).getUsers() );//github

// int x = 5;
// System.out.println("?? "+Mockito.when(Arrays.asList(service1.add(1,4))).thenReturn(x));

}


}
Loading