User and UserTest updated
authorMichael <Michael@Michael-LT.bfh.ch>
Tue, 27 Oct 2015 11:23:04 +0000 (12:23 +0100)
committerMichael <Michael@Michael-LT.bfh.ch>
Tue, 27 Oct 2015 11:23:04 +0000 (12:23 +0100)
Password.java moved to ch.bfh.ti.srs.extern package

.classpath
src/main/java/ch/bfh/ti/srs/core/SrsDummy.java [deleted file]
src/main/java/ch/bfh/ti/srs/core/User.java
src/main/java/ch/bfh/ti/srs/extern/Password.java [moved from src/main/java/ch/bfh/ti/srs/core/Password.java with 96% similarity]
src/test/java/ch/bfh/ti/srs/core/SrsDummyTest.java [deleted file]
src/test/java/ch/bfh/ti/srs/core/UserTest.java

index af1430b..61f3bb9 100644 (file)
@@ -1,26 +1,26 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-       <classpathentry kind="src" output="target/classes" path="src/main/java">
-               <attributes>
-                       <attribute name="optional" value="true"/>
-                       <attribute name="maven.pomderived" value="true"/>
-               </attributes>
-       </classpathentry>
-       <classpathentry kind="src" output="target/test-classes" path="src/test/java">
-               <attributes>
-                       <attribute name="optional" value="true"/>
-                       <attribute name="maven.pomderived" value="true"/>
-               </attributes>
-       </classpathentry>
-       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
-               <attributes>
-                       <attribute name="maven.pomderived" value="true"/>
-               </attributes>
-       </classpathentry>
-       <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
-               <attributes>
-                       <attribute name="maven.pomderived" value="true"/>
-               </attributes>
-       </classpathentry>
-       <classpathentry kind="output" path="target/classes"/>
-</classpath>
+<?xml version="1.0" encoding="UTF-8"?>\r
+<classpath>\r
+       <classpathentry kind="src" output="target/classes" path="src/main/java">\r
+               <attributes>\r
+                       <attribute name="optional" value="true"/>\r
+                       <attribute name="maven.pomderived" value="true"/>\r
+               </attributes>\r
+       </classpathentry>\r
+       <classpathentry kind="src" output="target/test-classes" path="src/test/java">\r
+               <attributes>\r
+                       <attribute name="optional" value="true"/>\r
+                       <attribute name="maven.pomderived" value="true"/>\r
+               </attributes>\r
+       </classpathentry>\r
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">\r
+               <attributes>\r
+                       <attribute name="maven.pomderived" value="true"/>\r
+               </attributes>\r
+       </classpathentry>\r
+       <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">\r
+               <attributes>\r
+                       <attribute name="maven.pomderived" value="true"/>\r
+               </attributes>\r
+       </classpathentry>\r
+       <classpathentry kind="output" path="target/classes"/>\r
+</classpath>\r
diff --git a/src/main/java/ch/bfh/ti/srs/core/SrsDummy.java b/src/main/java/ch/bfh/ti/srs/core/SrsDummy.java
deleted file mode 100644 (file)
index 1ed916c..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
- *
- * Project Smart Reservation System.
- *
- * Distributable under GPL license. See terms of license at gnu.org.
- */
-package ch.bfh.ti.srs.core;
-
-/**
- * @author Eric Dubuis
- */
-public class SrsDummy {
-       public static final String PART = "Hello, ";
-
-       public String sayHello(String who) {
-               return PART + who;
-       }
-
-       public static void main(String[] args) {
-               SrsDummy srs = new SrsDummy();
-               String result = srs.sayHello("dude");
-               System.out.println(result);
-       }
-}
index 5efba71..0c438c2 100644 (file)
@@ -1,5 +1,7 @@
 package ch.bfh.ti.srs.core;
 
+import ch.bfh.ti.srs.extern.Password;
+
 public class User {
 
        private final String userName;
@@ -9,39 +11,34 @@ public class User {
        private String lastname;
 
 
-       public User(String userName, String password){
+       public User(String userName, String password) throws Exception{
+               if(userName == null) throw new IllegalArgumentException("Username must not be null");
+               if(userName.length() < 4) throw new IllegalArgumentException("Username must have at least 4 characters");
+               if(password == null) throw new IllegalArgumentException("Password must not be null");
+               
                this.userName = userName;
-               try {
-                       this.password = Password.getSaltedHash(password);
-               } catch (Exception e) {
-                       // TODO: Bad programming style (due1). Discuss.
-                       // Lösung:
-                       // Logger verwenden da konsole auf server somit würde Meldung User nicht sehen.
-                       // password bleibt null!!?
-                       // wird anschliessend fortgesetzt mit password = null!
-                       // Lösung:
-                       // - User Objekt darf nicht erstellt werden!!
-                       // - exception muss weiter geworfen werden : throws except.
-                       // - Logger verwenden!
-                       
-                       System.err.println("Fehler beim Erstellen des Hashes");
-               }
+               this.password = Password.getSaltedHash(password);
        }
 
+       //Getter
        public String getUserName(){
                return userName;
        }
 
-       public boolean checkPassword(String password){
-               boolean passwordCorrect = false;
-               try {
-                       passwordCorrect = Password.check(password, this.password);
-               } catch (Exception e) {
-                       System.err.println("Fehler beim Prüfen des Passwortes");
-               }
-               return passwordCorrect;
+       public String getMail(){
+               return mail;
        }
-
+       
+       public String getName() {
+               return name;
+       }
+       
+       public String getLastname() {
+               return lastname;
+       }
+       
+       
+       //Setter
        public boolean setMail(String mail){
                if(mail != null){
                        if(isValidEmailAddress(mail)){
@@ -49,37 +46,32 @@ public class User {
                                return true;
                        }
                }
-               return false;
-       }
-
-       public String getMail(){
-               return mail;
-       }
-
-       public void changePassword(String newPassword){
-               try {
-                       this.password = Password.getSaltedHash(newPassword);
-               } catch (Exception e) {
-                       System.err.println("Fehler beim erstellen des Hashes");
-               }
-       }
-
-       public String getName() {
-               return name;
+               throw new IllegalArgumentException("Must be valid e-mail adress");
        }
 
        public void setName(String name) {
                this.name = name;
        }
-
-       public String getLastname() {
-               return lastname;
-       }
-
+       
        public void setLastname(String lastname) {
                this.lastname = lastname;
        }
+       
+       
+       //Password methods
+       public boolean checkPassword(String password) throws Exception{
+               if(password == null) throw new IllegalArgumentException("Password must not be null");
+               return Password.check(password, this.password);
+       }
+
+       public void changePassword(String newPassword) throws Exception{
+               if(newPassword == null) throw new IllegalArgumentException("Password must not be null");
+               if(checkPassword(newPassword)) throw new IllegalArgumentException("Can not be the current password");
+               this.password = Password.getSaltedHash(newPassword);
+       }
+
 
+       //private Helper methods
        private boolean isValidEmailAddress(String mail){
                String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
                java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
@@ -1,4 +1,4 @@
-package ch.bfh.ti.srs.core;\r
+package ch.bfh.ti.srs.extern;\r
 \r
 import javax.crypto.SecretKey;\r
 import javax.crypto.SecretKeyFactory;\r
diff --git a/src/test/java/ch/bfh/ti/srs/core/SrsDummyTest.java b/src/test/java/ch/bfh/ti/srs/core/SrsDummyTest.java
deleted file mode 100644 (file)
index c56ac68..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
- *
- * Project Smart Reservation System.
- *
- * Distributable under GPL license. See terms of license at gnu.org.
- */
-package ch.bfh.ti.srs.core;
-
-import static org.junit.Assert.assertEquals;
-
-import org.junit.Test;
-
-/**
- * @author Eric Dubuis
- */
-public class SrsDummyTest {
-
-       @Test
-       public void test() {
-               SrsDummy srs = new SrsDummy();
-               String result = srs.sayHello("tester");
-               assertEquals(SrsDummy.PART + "tester", result);
-       }
-}
index 3a6059b..b01d257 100644 (file)
-///*\r
-// * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
-// *\r
-// * Project Smart Reservation System.\r
-// *\r
-// * Distributable under GPL license. See terms of license at gnu.org.\r
-// */\r
-//package ch.bfh.ti.srs.core;\r
-//\r
-//import static org.junit.Assert.assertEquals;\r
-//import static org.junit.Assert.assertFalse;\r
-//import static org.junit.Assert.assertTrue;\r
-//\r
-//import org.junit.Rule;\r
-//import org.junit.Test;\r
-//import org.junit.rules.ExpectedException;\r
-//\r
-//public class UserTest {\r
-//     //@Rule\r
-//     //public ExpectedException createUserException = ExpectedException.none();\r
-//     \r
-//     @Test\r
-//     public void createUserTest() throws Exception{\r
-//             User testUser = new User("TestUser", "pass");\r
-//             assertEquals("TestUser", testUser.getUserName());\r
-//             \r
-//             //Zweite Moeglichkeit um Exceptions zu testen, so lange keine Exception erwartet wird reicht es\r
-//             //eine allfaellige mit 'throws Exception' an junit weiterzuleiten.\r
-//             //--------------------------------------------------------------------------------------------\r
-//             /*createUserException.expect(IllegalArgumentException.class);\r
-//             createUserException.expectMessage("Invalid username");\r
-//             User wrongArgumentUser1 = new User(null, "1234");\r
-//             createUserException.expectMessage("Invalid password");\r
-//             User wrongArgumentUser2 = new User("TestUser2", null);*/\r
-//     }\r
-//     \r
-//     @Test(expected=IllegalArgumentException.class)\r
-//     public void createUserInvalidUsernameTest() {\r
-//             User testUser = new User(new String(),"pass");\r
-//     }\r
-//     \r
-//     @Test(expected=IllegalArgumentException.class)\r
-//     public void createUserInvalidPasswordTest() {\r
-//             User testUser = new User("TestUser", new String());\r
-//     }\r
-//\r
-//     @Test\r
-//     public void checkPasswordTest() throws Exception{\r
-//             User testUser = new User("TestUser", "pass");\r
-//             assertTrue(testUser.checkPassword("pass"));\r
-//             assertFalse(testUser.checkPassword("notmypass"));\r
-//             assertFalse(testUser.checkPassword(null));\r
-//     }\r
-//     \r
-//     @Test\r
-//     public void changePasswordTest() throws Exception{\r
-//             User testUser = new User("TestUser", "pass");\r
-//             assertTrue(testUser.checkPassword("pass"));\r
-//             testUser.changePassword("newpass");\r
-//             assertTrue(testUser.checkPassword("newpass"));\r
-//             assertFalse(testUser.checkPassword("pass"));\r
-//             assertFalse(testUser.checkPassword("notmypass"));\r
-//             assertFalse(testUser.checkPassword(null));\r
-//     }\r
-//     \r
-//     @Test(expected=IllegalArgumentException.class)\r
-//     public void changePasswordInvalidPasswordTest() {\r
-//             User testUser = new User("TestUser", "pass");\r
-//             testUser.changePassword(null);\r
-//     }\r
-//\r
-//     @Test\r
-//     public void setMailTest() {\r
-//             User testUser = new User("TestUser", "pass");\r
-//             String goodMail = "grosm9@bfh.ch";\r
-//             String wrongMail1 = "ioajfd.ch";\r
-//             String wrongMail2 = "asjdfh@fds";\r
-//\r
-//             assertTrue(testUser.setMail(goodMail));\r
-//             assertFalse(testUser.setMail(wrongMail1));\r
-//             assertFalse(testUser.setMail(wrongMail2));\r
-//             assertFalse(testUser.setMail(null));\r
-//     }\r
-//\r
-//     @Test\r
-//     public void getMailTest() {\r
-//             User testUser = new User("TestUser", "pass");\r
-//             String mail = "grosm9@bfh.ch";\r
-//\r
-//             testUser.setMail(mail);\r
-//             assertEquals(mail, testUser.getMail());\r
-//     }\r
-//\r
-//     @Test\r
-//     public void setAndGetNameTest() {\r
-//             User testUser = new User("TestUser", "pass");\r
-//             testUser.setName("Tester");\r
-//             assertEquals("Tester",testUser.getName());\r
-//             testUser.setName(null);\r
-//             assertEquals(null, testUser.getName());\r
-//     }\r
-//\r
-//     @Test\r
-//     public void setAndGetLastnameTest() {\r
-//             User testUser = new User("TestUser", "pass");\r
-//             testUser.setLastname("Dummy");\r
-//             assertEquals("Dummy", testUser.getLastname());\r
-//             testUser.setLastname(null);\r
-//             assertEquals(null, testUser.getLastname());\r
-//     }\r
-//}\r
+/*\r
+ * Copyright (c) 2015 Berner Fachhochschule, Switzerland.\r
+ *\r
+ * Project Smart Reservation System.\r
+ *\r
+ * Distributable under GPL license. See terms of license at gnu.org.\r
+ */\r
+package ch.bfh.ti.srs.core;\r
+\r
+import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import org.junit.Rule;\r
+import org.junit.Test;\r
+import org.junit.rules.ExpectedException;\r
+\r
+public class UserTest {\r
+       //@Rule\r
+       //public ExpectedException createUserException = ExpectedException.none();\r
+       \r
+       @Test\r
+       public void createUserTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               assertEquals("TestUser", testUser.getUserName());\r
+               \r
+               //Zweite Moeglichkeit um Exceptions zu testen, so lange keine Exception erwartet wird reicht es\r
+               //eine allfaellige mit 'throws Exception' an junit weiterzuleiten.\r
+               //--------------------------------------------------------------------------------------------\r
+               /*createUserException.expect(IllegalArgumentException.class);\r
+               createUserException.expectMessage("Invalid username");\r
+               User wrongArgumentUser1 = new User(null, "1234");\r
+               createUserException.expectMessage("Invalid password");\r
+               User wrongArgumentUser2 = new User("TestUser2", null);*/\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void createUserInvalidUsernameTest() throws Exception{\r
+               User testUser = new User(null,"pass");\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void createUserTooShortUsernameTest() throws Exception{\r
+               User testUser = new User(new String(),"pass");\r
+               User testUser2 = new User(new String("tes"),"pass");\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void createUserInvalidPasswordTest() throws Exception{\r
+               User testUser = new User("TestUser", null);\r
+       }\r
+\r
+       @Test\r
+       public void checkPasswordTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               assertTrue(testUser.checkPassword("pass"));\r
+               assertFalse(testUser.checkPassword("notmypass"));\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void checkPasswordInvalidTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               assertTrue(testUser.checkPassword(null));\r
+       }\r
+       \r
+       @Test\r
+       public void changePasswordTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               assertTrue(testUser.checkPassword("pass"));\r
+               testUser.changePassword("newpass");\r
+               assertTrue(testUser.checkPassword("newpass"));\r
+               assertFalse(testUser.checkPassword("pass"));\r
+               assertFalse(testUser.checkPassword("notmypass"));\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void changePasswordInvalid1Test() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.changePassword(null);\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void changePasswordInvalid2Test() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.changePassword("pass");\r
+       }\r
+\r
+       @Test\r
+       public void setMailTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.setMail("grosm9@bfh.ch");\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void setMailIllegal1Test() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.setMail("fda.ds");\r
+\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void setMailIllegal2Test() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.setMail("adfiug@fs");\r
+       }\r
+       \r
+       @Test(expected=IllegalArgumentException.class)\r
+       public void setMailIllegal3Test() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.setMail(null);\r
+       }\r
+\r
+       @Test\r
+       public void getMailTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               String mail = "grosm9@bfh.ch";\r
+\r
+               testUser.setMail(mail);\r
+               assertEquals(mail, testUser.getMail());\r
+       }\r
+\r
+       @Test\r
+       public void setAndGetNameTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.setName("Tester");\r
+               assertEquals("Tester",testUser.getName());\r
+               testUser.setName(null);\r
+               assertEquals(null, testUser.getName());\r
+       }\r
+\r
+       @Test\r
+       public void setAndGetLastnameTest() throws Exception{\r
+               User testUser = new User("TestUser", "pass");\r
+               testUser.setLastname("Dummy");\r
+               assertEquals("Dummy", testUser.getLastname());\r
+               testUser.setLastname(null);\r
+               assertEquals(null, testUser.getLastname());\r
+       }\r
+}\r