Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Should not have trown any exceptions. But it throw: Error: incorrect "Phone Number" field

  1. #1
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Should not have trown any exceptions. But it throw: Error: incorrect "Phone Number" field

    Hi,

    Help me to find mistake. I receive this message in my netbeans-report during testing:

    testErrorEmail04 Failed: Should not have trown any exceptions. But it throw: Error: incorrect "Phone Number" field. Text: +79172122959
    FreeFunctions.java
    /*
     * 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.
     */
    package my.checkpersondata;
     
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    /**
     *
     * @author Ivan
     */
    public class FreeFunctions
    {
     
        public void checkPersonFields( Person person )
                throws ErrorNickName, ErrorFirstName, ErrorLastName,
                ErrorPhoneNumber, ErrorEmail, ErrorFieldEmpty
        {
            String nickName = person.getNickName();
            String firstName = person.getFirstName();
            String lastName = person.getLastName();
            String phoneNumber = person.getPhoneNumber();
            String email = person.getEmail();
     
            // Check argument on empty
            if ( nickName.isEmpty()
                    || firstName.isEmpty()
                    || lastName.isEmpty()
                    || phoneNumber.isEmpty()
                    || email.isEmpty() ) {
                throw new ErrorFieldEmpty();
            }
     
            // Check the nick name
            String strPatternForNickName = "\\W";
            Pattern objPatternForNickName
                    = Pattern.compile( strPatternForNickName );
            Matcher matcherForNickName
                    = objPatternForNickName.matcher( nickName );
            if ( matcherForNickName.find() ) {
                throw new ErrorNickName( nickName );
            }
     
            // Check the first name
            String strPatternForFirstName = "[\\W\\d_]";
            Pattern objPatternForFirstName
                    = Pattern.compile( strPatternForFirstName );
            Matcher matcherForFirstName
                    = objPatternForFirstName.matcher( firstName );
            if ( matcherForFirstName.find() ) {
                throw new ErrorFirstName( firstName );
            }
     
            // Check the last name
            String strPatternForLastName = "[\\W\\d_]";
            Pattern objPatternForLastName
                    = Pattern.compile( strPatternForLastName );
            Matcher matcherForLastName
                    = objPatternForLastName.matcher( lastName );
            if ( matcherForLastName.find() ) {
                throw new ErrorLastName( lastName );
            }
     
            // Check the phone number
            String strPatternForPhoneNumber = "\\+7[\\d]{10}";
            Pattern objPatternForPhoneNumber
                    = Pattern.compile( strPatternForPhoneNumber );
            Matcher matcherForPhoneNumber
                    = objPatternForPhoneNumber.matcher( phoneNumber );
            if ( matcherForPhoneNumber.find() ) {
                throw new ErrorPhoneNumber( phoneNumber );
            }
        }
    }

    FreeFunctionsTest.java
    /*
     * 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.
     */
    package my.checkpersondata;
     
    import static org.junit.Assert.*;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.internal.runners.statements.ExpectException;
    import org.junit.rules.ExpectedException;
     
    /**
     *
     * @author Ivan
     */
    public class FreeFunctionsTest
    {
     
        public FreeFunctionsTest()
        {
        }
     
        @Rule
        public ExpectedException exception = ExpectedException.none();
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testNormalTest() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
     
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            try {
                instance.checkPersonFields( person );
            } catch ( Exception e ) {
                fail( "Should not have thrown any exception. But it throw: " +
                        e.toString() );
            }
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testEmptyNickName() throws Exception
        {
            String nickName = "";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorFieldEmpty.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testEmptyFirstName() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorFieldEmpty.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testEmptyLastName() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorFieldEmpty.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testEmptyPhoneNumber() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorFieldEmpty.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testEmptyEmail() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorFieldEmpty.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorNickName() throws Exception
        {
            String nickName = "8Oberv$er8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorNickName.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorFirstName01() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Iva8n";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorFirstName.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorFirstName02() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Iva_n";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorFirstName.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorFirstName03() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Iva$n";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorFirstName.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorLastName01() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzh8aev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorLastName.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorLastName02() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzh_aev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorLastName.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorLastName03() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzh$aev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorLastName.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorPhoneNumber01() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "-79172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorPhoneNumber.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorPhoneNumber02() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+791721229_9";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorPhoneNumber.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorPhoneNumber03() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+791721229W9";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorPhoneNumber.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorPhoneNumber04() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+791721229591";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorPhoneNumber.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorPhoneNumber05() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+89172122959";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorPhoneNumber.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorPhoneNumber06() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+791721229$9";
            String email = "8observer8@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorPhoneNumber.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorEmail01() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorEmail.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorEmail02() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "8observer8@gmailcom";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorEmail.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorEmail03() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "@gmail.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            exception.expect( ErrorEmail.class );
            instance.checkPersonFields( person );
        }
     
        /**
         * Test of checkPersonFields method, of class FreeFunctions.
         */
        @Test
        public void testErrorEmail04() throws Exception
        {
            String nickName = "8Oberver8";
            String firstName = "Ivan";
            String lastName = "Enzhaev";
            String phoneNumber = "+79172122959";
            String email = "ivan01.prog1987@gmail.en.com";
            Person person = new Person( nickName, firstName, lastName,
                    phoneNumber, email );
     
            FreeFunctions instance = new FreeFunctions();
     
            try {
                instance.checkPersonFields( person );
            } catch ( Exception e ) {
                fail( "Should not have trown any exceptions. But it throw: " +
                        e.toString() );
            }
        }
    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Should not have trown any exceptions. But it throw: Error: incorrect "Phone Number" field

    <raised eyebrow>
    Your code throws the exception if it finds a match. Don't you want to throw the exception if it does not match?
    </raised eyebrow>
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    QtFalcon (August 3rd, 2014)

  4. #3
    Junior Member QtFalcon's Avatar
    Join Date
    Aug 2014
    Location
    Russia
    Posts
    12
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Should not have trown any exceptions. But it throw: Error: incorrect "Phone Number" field

    Yes! I've just added '!' Thanks!

        if ( !matcherForPhoneNumber.find() ) {
            throw new ErrorPhoneNumber( phoneNumber );
        }

Similar Threads

  1. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  2. Replies: 3
    Last Post: August 14th, 2013, 04:23 PM
  3. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  4. Error message "Type result cannot be resolved or is not a field"
    By asreall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 28th, 2012, 08:40 AM
  5. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM