Skip to content

Commit 4acbd26

Browse files
committed
Added new unit tests for printing class
Removed .log.txt file after game closes (only readable while open) Updated .travis.yml to allocate tests for unitTeststing branch
1 parent b6b8a59 commit 4acbd26

File tree

7 files changed

+79
-9
lines changed

7 files changed

+79
-9
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@ language: java
33
# blocklist
44
branches:
55
except:
6-
- develop
7-
- unitTesting
6+
- develop

build.gradle

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ repositories{
44

55
apply plugin: 'java'
66

7-
sourceSets.main.java.srcDir 'src'
8-
sourceSets.main.resources.srcDirs 'res'
7+
sourceSets{
8+
main{
9+
java.srcDir 'src'
10+
resources.srcDir 'res'
11+
}
12+
test{
13+
java.srcDir 'test'
14+
}
15+
}
916

1017
dependencies{
1118
compile 'org.apache.commons:commons-lang3:3.4'

src/com/redomar/game/InputHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ private void toggleKey(int keyCode, boolean isPressed) {
124124

125125
private void quitGame() {
126126
Game.setClosing(true);
127+
print.removeLog();
127128
try {
128129
Thread.sleep(1000);
129130
} catch (InterruptedException e) {

src/com/redomar/game/script/PrintTypes.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public enum PrintTypes {
55
LEVEL,
66
MUSIC,
77
ERROR,
8+
TEST,
89
NETWORK,
9-
SERVER;
10+
SERVER
1011
}

src/com/redomar/game/script/Printing.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.redomar.game.lib.Time;
44

5+
import java.io.File;
56
import java.util.Arrays;
67

78
public class Printing {
@@ -30,7 +31,7 @@ private void printOut(){
3031
msgTime = "[" + time.getTime() + "]";
3132
msgType = "[" + type.toString() + "]";
3233

33-
logFile = new PrintToLog(".log.txt");
34+
logFile = printToLogType(type);
3435
if (lineNumber == 0) {
3536

3637
String dashes = "";
@@ -56,6 +57,18 @@ private void printOut(){
5657
}
5758
}
5859

60+
private PrintToLog printToLogType(PrintTypes type){
61+
if (type == PrintTypes.TEST){
62+
return new PrintToLog(".PrintType-TEST.txt");
63+
} else {
64+
return new PrintToLog(".log.txt");
65+
}
66+
}
67+
68+
public void removeLog(){
69+
new File(".log.txt").delete()
70+
}
71+
5972
public String getMessage() {
6073
return message;
6174
}

test/com/redomar/game/script/PrintToLogTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.Test;
44

5+
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertTrue;
67

78
/**
@@ -11,7 +12,7 @@
1112
*/
1213
public class PrintToLogTest {
1314

14-
PrintToLog print;
15+
private PrintToLog print;
1516

1617
@Test(expected = NullPointerException.class)
1718
public void PrintToLogNullFile() {
@@ -20,9 +21,9 @@ public void PrintToLogNullFile() {
2021

2122
@Test
2223
public void PrintToLogDoesWork() {
23-
print = new PrintToLog(".Test.txt");
24+
print = new PrintToLog(".PrintToLogDoesWork.txt");
2425
assertTrue(print.getUrl().exists());
25-
print.getUrl().delete();
26+
assertTrue(print.getUrl().delete());
2627
}
2728

2829
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.redomar.game.script;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.File;
8+
9+
import static org.junit.Assert.*;
10+
11+
/**
12+
* Created by Mohamed on 28/08/2016.
13+
* This file tests the com.redomar.game.script.Printing class
14+
*/
15+
public class PrintingTest {
16+
17+
private Printing printing;
18+
19+
@Before
20+
public void setUp() throws Exception {
21+
printing = new Printing();
22+
}
23+
24+
@Test
25+
public void printToFileWorks() throws Exception {
26+
printing.print("TESTING STRING", PrintTypes.TEST);
27+
File file = new File(".PrintType-TEST.txt");
28+
assertTrue(file.exists());
29+
assertTrue(file.delete());
30+
}
31+
32+
@Test
33+
public void getMessageIsNull() {
34+
assertNull(printing.getMessage());
35+
}
36+
37+
@Test
38+
public void getMessageIsNotNull() {
39+
printing.setMessage("Not Null");
40+
assertNotNull(printing.getMessage());
41+
}
42+
43+
@After
44+
public void cleanUp(){
45+
printing = null;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)