I'm making an application and I have a question ... I explain:
The user registration is done directly in the class (Platform). To register, the user must indicate a username, a password and his full name (First and Last Name).
Keep in mind that:
A user identifies him by his username (username). There can not be users with repeated usernames.
Two users are considered equal if their username, password and full name are the same.
As a result of the creation, a new object of type User will be generated. This object will allow interaction with the platform class. In case a user with the same name already exists, the new user will not be created, and a null will be returned to indicate the error.
To verify if a user exists or not, I implement the findUser method, which allows me to search for a registered user from their username. If it does not exist, this method will return a null value.
To test the correct functioning of the developed code, I have the kind of tests: PR1_Ex2_1_Test
The classes are defined as follows:
The Platform class, which is where I do all the methods
package edu.uoc.dpoo;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Guillermo Rodriguez Barcelo
*/
public class Platform {
private List<Competition> competitions;
private List<User> users;
public Platform(){
competitions = new ArrayList<Competition>();
users = new ArrayList<User>();
}
public User registerUser(String username ,String password,String fullname){
User p = findUser(username);
if (p == null) {
p = new User(username, password,fullname);
users.add(p);
}
else {
p.getUserName();
}
return (User) p;
}
public User findUser (String username) {
User user = null;
for (User p : users) {
if (username.equals(p.getUserName())) {
user = p;
break;
}
}
return user;
}
public User login(String username,String password){
return null;
}
public Integer getNumUsers(){
Integer size=users.size();
return size ;
}
public Integer getNumCompetitions(){
return null;
}
public Message sendMessage(User from,String to,String subject,String message){
return null;
}
private float evaluateAll(){
return (float) 0.0;
}
public List<Competition> getOpenCompetitions(){
return null;
}
private User User(User u) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
The User class
package edu.uoc.dpoo;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Guillermo Rodriguez Barcelo
*/
public class User {
private String username;
private String password;
private String fullName;
private Platform platform;
private List<Message> inBox;
private List<Message> outBox;
public User (String username, String password, String fullName) {
this.username = username;
this.password = password;
this.fullName = fullName;
inBox = new ArrayList<Message>();
outBox = new ArrayList<Message>();
}
User(String password, String fullname) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
public String getUserName() {
return username;
}
public String getPassword() {
return password;
}
public String getFullName() {
return fullName;
}
public void setUserName(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setFullName(String fullname) {
this.fullName = fullname;
}
public boolean checkPasword(String password){
return true;
}
public List<Message> getMessages() {
return null;
}
public Organizer asOrganizer(){
return null;
}
public Participant asParticipant(){
return null;
}
public Message sendMessage(String to,String Subject,String message){
return null;
}
public String toString () {
return null;
}
@Override
public boolean equals (Object obj) {
return (boolean) obj;
}
public List<Competition> myCompetitions(){
return null;
}
}
Finally the class where I take the test and the method fails me
User u2=platform.registerUser(username1, password1, fullName1);
assertNull(u2);
assertEquals((int)platform.getNumUsers(), 1);
The class stays that way
package edu.uoc.dpoo;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import edu.uoc.dpoo.Organizer;
import edu.uoc.dpoo.User;
import edu.uoc.dpoo.Platform;
import edu.uoc.dpoo.Participant;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
/**
*
* @author xavie
*/
public class PR1_Ex2_1_Test {
private final String username1 = "username1";
private final String password1 = "password1";
private final String fullName1 = "Test User 1";
private final String username2 = "username2";
private final String password2 = "password2";
private final String fullName2 = "Test User 2";
public PR1_Ex2_1_Test() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
// Test create new user
@Test
public void createUser() {
Platform platform = new Platform();
// Check that no user is in the platform
assertEquals((int)platform.getNumUsers(), 0);
// Register new user
User u1=platform.registerUser(username1, password1, fullName1);
// User is not NULL
assertNotNull(u1);
// User full name is correct
assertEquals(u1.getFullName(), fullName1);
// Check that only 1 user is in the platform (login does not created new user)
assertEquals((int)platform.getNumUsers(), 1);
}
// Test create new user controls
@Test
public void createUserControls() {
Platform platform = new Platform();
// Check that no user is in the platform
assertEquals((int)platform.getNumUsers(), 0);
// Register new user
User u1=platform.registerUser(username1, password1, fullName1);
assertNotNull(u1);
assertEquals((int)platform.getNumUsers(), 1);
// Try to register the same user again
User u2=platform.registerUser(username1, password1, fullName1);
assertNull(u2);
assertEquals((int)platform.getNumUsers(), 1);
// Register new user
User u3=platform.registerUser(username2, password2, fullName2);
assertNotNull(u3);
assertEquals((int)platform.getNumUsers(), 2);
}
}
I do not understand why I fail in the methods
assertNull(u2);
assertEquals((int)platform.getNumUsers(), 1);