Leon 7 anni fa
commit
b22a7f5acc

BIN
.DS_Store Vedi File


+ 3
- 0
.gitignore Vedi File

1
+.idea/*
2
+*.iml
3
+target/*

+ 65
- 0
README.md Vedi File

1
+-
2
+-
3
+#ZCW-MicroLabs-ExceptionsAndLogging
4
+
5
+-
6
+#PhoneNumberFactory
7
+* **Purpose** - to demonstrate basic exception handling and logging.
8
+* **Objective** - to implement a `PhoneNumberFactory` class that generates `PhoneNumber` objects.
9
+* The `PhoneNumber` class is a container for a `String` representation of a respective phone number.
10
+* Note: Phone numbers are a composite of 3 affixes; `Area Code`, `Central Office Code`, and `Phone Line Code`.
11
+	* `Area Code`  - the first 3 numeric values
12
+	* `Central Office Code` - the 4th, 5th, and 6th numeric values.
13
+	* `Phone Line Code` - the last 3 numeric values. 
14
+
15
+* Below is a sample *instantation of* and *invokation on* `PhoneNumber`.
16
+	
17
+```Java
18
+String stringRepresentation = "(302)-312-5555)";
19
+PhoneNumber phoneNumber = new PhoneNumber(stringRepresentation);
20
+String areaCode = phoneNumber.getAreaCode();
21
+String centralOfficeCode = phoneNumber.getCentralOfficeCode();
22
+String phoneLineCode = phoneNumber.getPhoneLineCode();
23
+```
24
+
25
+
26
+-
27
+#Part 1; Modify `createPhoneNumber`
28
+* Upon instantiating a new `PhoneNumber` object, it is possible to receive a `InvalidPhoneNumberFormatException` if the `String` passed into the `PhoneNumber` constructor does not fit the format `(###)-###-####`.<br>
29
+* `InvalidPhoneNumberFormatException` extends `IOException`, which is a `checked exception`.<br>
30
+* Modify the `createPhoneNumber` method so that it throws any resulting `InvalidPhoneNumberFormatException`.
31
+	* This will ensure that any method calling `createPhoneNumber` will have to handle the exception.
32
+
33
+
34
+
35
+-
36
+#Part 2; Implement `createPhoneNumberSafely`
37
+* Using the `createPhoneNumber` method from `Part 1`, define the `createPhoneNumberSafely` method such that the input parameters, `areaCode`, `centralOfficeCode`, `phoneLineCode` are concatenated to create a `String` representation of the respective phone number.
38
+* Use this `String` object to construct a new instance of `PhoneNumber` and return it.
39
+* If the concatentation of the input parameters yields a `String` whose value does not match the format `(###)-###-####`, then our `PhoneNumber` will throw a `InvalidPhoneNumberFormatException`.
40
+* If a `InvalidPhoneNumberFormatException` is thrown within this method, catch it and return `null`.
41
+
42
+
43
+-
44
+#Part 3; Implement `createRandomPhoneNumber`
45
+* Using the `RandomNumberFactory`, generate a random `Area Code`, `Central Office Code`, and `Phone Line Code`. Pass these values as arguments of the `createPhoneNumberSafely` method from `Part 2` and return the resulting `PhoneNumber` object.
46
+
47
+-
48
+#Part 4; Implement `createRandomPhoneNumberArray`
49
+* Using the `createRandomPhoneNumber` from `Part 3`, generate an array of `PhoneNumber` objects, whose length reflects the input argument.
50
+	* For example `createRandomPhoneNumber(5)` should return an array of 5 `PhoneNumber` objects.
51
+
52
+
53
+-
54
+#Part 5; Add logging
55
+* Add logging to the `createPhoneNumber` method from `Part 1`, which logs the message
56
+	* `"Attempting to create a new PhoneNumber object with a value of (###)-###-####`
57
+	* where `(###)-###-####` will be replaced with the respective input parameter.
58
+
59
+* Add logging to the `createPhoneNumberSafely` method from `Part 2`, which logs the message
60
+	* `(###)-###-#### is not a valid phone number`
61
+	* Where `(###)-###-####` will be replaced with the respective input parameter.
62
+
63
+	
64
+#Part 6; Ensure all test cases pass
65
+* Yeah this header says all that is needed...

+ 4008
- 0
log4j-application.log
File diff suppressed because it is too large
Vedi File


+ 43
- 0
pom.xml Vedi File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0"
3
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
+    <modelVersion>4.0.0</modelVersion>
6
+
7
+    <groupId>zcw</groupId>
8
+    <artifactId>exceptions-assertions-logging</artifactId>
9
+    <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>1.7</source>
17
+                    <target>1.7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
22
+    <dependencies>
23
+
24
+        <dependency>
25
+            <groupId>org.slf4j</groupId>
26
+            <artifactId>slf4j-api</artifactId>
27
+            <version>1.7.5</version>
28
+        </dependency>
29
+
30
+        <dependency>
31
+            <groupId>org.slf4j</groupId>
32
+            <artifactId>slf4j-log4j12</artifactId>
33
+            <version>1.7.5</version>
34
+        </dependency>
35
+
36
+        <dependency>
37
+            <groupId>junit</groupId>
38
+            <artifactId>junit</artifactId>
39
+            <version>RELEASE</version>
40
+        </dependency>
41
+    </dependencies>
42
+
43
+</project>

BIN
src/.DS_Store Vedi File


+ 9
- 0
src/main/java/com/zipcodewilmington/exceptions/InvalidPhoneNumberFormatException.java Vedi File

1
+package com.zipcodewilmington.exceptions;
2
+
3
+import java.io.IOException;
4
+
5
+/**
6
+ * Created by leon on 5/10/17.
7
+ */ // Checked Exception
8
+public final class InvalidPhoneNumberFormatException extends IOException {
9
+}

+ 41
- 0
src/main/java/com/zipcodewilmington/phone/PhoneNumber.java Vedi File

1
+package com.zipcodewilmington.phone;
2
+
3
+import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
+
5
+/**
6
+ * Created by leon on 5/10/17.
7
+ */
8
+public final class PhoneNumber {
9
+    private final String phoneNumberString;
10
+
11
+    // default constructor is uncallable
12
+    private PhoneNumber() throws InvalidPhoneNumberFormatException {
13
+        this(null);
14
+    }
15
+
16
+    // non-default constructor is package-protected
17
+    protected PhoneNumber(String phoneNumber) throws InvalidPhoneNumberFormatException {
18
+        //validate phone number with format `(###)-###-####`
19
+        if (!phoneNumber.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}")) {
20
+            throw new InvalidPhoneNumberFormatException();
21
+        }
22
+        this.phoneNumberString = phoneNumber;
23
+    }
24
+
25
+    public String getAreaCode() {
26
+        return toString().substring(1, 4);
27
+    }
28
+
29
+    public String getCentralOfficeCode() {
30
+        return toString().substring(6, 9);
31
+    }
32
+
33
+    public String getPhoneLineCode() {
34
+        return toString().substring(10, 14);
35
+}
36
+
37
+    @Override
38
+    public String toString() {
39
+        return phoneNumberString;
40
+    }
41
+}

+ 53
- 0
src/main/java/com/zipcodewilmington/phone/PhoneNumberFactory.java Vedi File

1
+package com.zipcodewilmington.phone;
2
+
3
+import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
+import com.zipcodewilmington.tools.RandomNumberFactory;
5
+import org.slf4j.Logger;
6
+import org.slf4j.LoggerFactory;
7
+
8
+/**
9
+ * Created by leon on 5/1/17.
10
+ */
11
+public final class PhoneNumberFactory {
12
+    private static final Logger logger = LoggerFactory.getLogger(PhoneNumberFactory.class);
13
+
14
+    private PhoneNumberFactory() {
15
+        /** This constructor is private
16
+         *  This class is uninstantiable */
17
+    }
18
+
19
+    /**
20
+     * @param phoneNumberCount - number of PhoneNumber objects to instantiate
21
+     * @return array of randomly generated PhoneNumber objects
22
+     */ //TODO - Implement logic
23
+    public static PhoneNumber[] createRandomPhoneNumberArray(int phoneNumberCount) {
24
+        return null;
25
+    }
26
+
27
+    /**
28
+     * @return an instance of PhoneNumber with randomly generated phone number value
29
+     */ //TODO - Implement logic
30
+    private static PhoneNumber createRandomPhoneNumber() {
31
+        return createPhoneNumberSafely(-1, -1, -1);
32
+    }
33
+
34
+
35
+    /**
36
+     * @param areaCode          - 3 digit code
37
+     * @param centralOfficeCode - 3 digit code
38
+     * @param phoneLineCode     - 4 digit code
39
+     * @return a new phone number object
40
+     */ //TODO - if input is valid, return respective PhoneNumber object, else return null
41
+    public static PhoneNumber createPhoneNumberSafely(int areaCode, int centralOfficeCode, int phoneLineCode) {
42
+        return createPhoneNumber(null);
43
+    }
44
+
45
+    /**
46
+     * @param phoneNumberString - some String corresponding to a phone number whose format is `(###)-###-####`
47
+     * @return a new phone number object
48
+     * @throws InvalidPhoneNumberFormatException - thrown if phoneNumberString does not match acceptable format
49
+     */ // TODO - Add throws statement to method signature
50
+    public static PhoneNumber createPhoneNumber(String phoneNumberString) {
51
+        return null;
52
+    }
53
+}

+ 21
- 0
src/main/java/com/zipcodewilmington/tools/RandomNumberFactory.java Vedi File

1
+package com.zipcodewilmington.tools;
2
+import java.awt.*;
3
+import java.util.*;
4
+
5
+/**
6
+ * Created by Leon on 2/4/2017.
7
+ */
8
+
9
+public abstract class RandomNumberFactory {
10
+    private static final Random random = new Random();
11
+
12
+    /** @return a random float between the specified min and max numeric range */
13
+    public static Float createFloat(float min, float max) {
14
+        return random.nextFloat() * (max - min) + min;
15
+    }
16
+
17
+    /** @return a random integer between the specified min and max numeric range */
18
+    public static Integer createInteger(Integer min, Integer max) {
19
+        return createFloat(min, max).intValue();
20
+    }
21
+}

+ 16
- 0
src/main/resources/log4j.properties Vedi File

1
+# Root logger option
2
+log4j.rootLogger=DEBUG, stdout, file
3
+
4
+# Redirect log messages to console
5
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
6
+log4j.appender.stdout.Target=System.out
7
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
8
+log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
9
+
10
+# Redirect log messages to a log file, support file rolling.
11
+log4j.appender.file=org.apache.log4j.RollingFileAppender
12
+log4j.appender.file.File=log4j-application.log
13
+log4j.appender.file.MaxFileSize=5MB
14
+log4j.appender.file.MaxBackupIndex=10
15
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
16
+log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

+ 93
- 0
src/test/java/com/zipcodewilmington/PhoneNumberFactoryTest.java Vedi File

1
+package com.zipcodewilmington;
2
+
3
+import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
4
+import com.zipcodewilmington.phone.PhoneNumber;
5
+import com.zipcodewilmington.phone.PhoneNumberFactory;
6
+import org.junit.Assert;
7
+import org.junit.Before;
8
+import org.junit.Test;
9
+
10
+import java.util.logging.Level;
11
+import java.util.logging.Logger;
12
+
13
+/**
14
+ * Created by leon on 5/9/17.
15
+ */
16
+public class PhoneNumberFactoryTest {
17
+
18
+    @Test(expected = InvalidPhoneNumberFormatException.class)
19
+    public void testInvalidPhoneNumberFormatException() throws InvalidPhoneNumberFormatException {
20
+        PhoneNumberFactory.createPhoneNumber("-1");
21
+    }
22
+
23
+    @Test
24
+    public void testCreatePhoneNumberSafely() {
25
+        // : Given
26
+        int areaCode = 0;
27
+        int centralOfficeCode = 0;
28
+        int phoneLineCode = 0;
29
+
30
+        // : When
31
+        PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
32
+
33
+        // : Then
34
+        Assert.assertEquals(null, phoneNumber);
35
+    }
36
+
37
+    @Test
38
+    public void testGetAreaCode() {
39
+        // : Given
40
+        Integer areaCode = 302;
41
+        int centralOfficeCode = 312;
42
+        int phoneLineCode = 5555;
43
+
44
+        // : When
45
+        PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
46
+
47
+        // : Then
48
+        Assert.assertEquals(phoneNumber.getAreaCode(), areaCode.toString());
49
+    }
50
+
51
+    @Test
52
+    public void testGetCentralOfficeCode() {
53
+        // : Given
54
+        int areaCode = 302;
55
+        Integer centralOfficeCode = 312;
56
+        int phoneLineCode = 5555;
57
+
58
+        // : When
59
+        PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
60
+
61
+        // : Then
62
+        Assert.assertEquals(phoneNumber.getCentralOfficeCode(), centralOfficeCode.toString());
63
+    }
64
+
65
+
66
+    @Test
67
+    public void testPhoneLineCode() {
68
+        // : Given
69
+        int areaCode = 302;
70
+        int centralOfficeCode = 312;
71
+        Integer phoneLineCode = 5555;
72
+
73
+        // : When
74
+        PhoneNumber phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
75
+
76
+        // : Then
77
+        Assert.assertEquals(phoneNumber.getPhoneLineCode(), phoneLineCode.toString());
78
+    }
79
+
80
+    @Test
81
+    public void testCreateRandomPhoneNumber() {
82
+        Logger.getGlobal().setLevel(Level.OFF);
83
+        // : Given
84
+        PhoneNumber[] phoneNumbers = PhoneNumberFactory.createRandomPhoneNumberArray(999);
85
+
86
+        // : When
87
+        for (PhoneNumber phoneNumber : phoneNumbers) {
88
+
89
+            // : Then
90
+            Assert.assertTrue(phoneNumber != null);
91
+        }
92
+    }
93
+}