Skip to content
This repository has been archived by the owner on Dec 1, 2018. It is now read-only.

Commit

Permalink
added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
max-wittig committed Jan 17, 2017
1 parent 0c5fe8a commit b7ad33d
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
package com.maxwittig.reportgenerator.builder
import com.maxwittig.reportgenerator.models.TimekeeperTask
import com.maxwittig.reportgenerator.utils.getTimeStringFromMilliSeconds
import java.util.*

class PlainTextReportBuilder(timekeeperTasks : ArrayList<TimekeeperTask>, reportType: ReportType) : ReportBuilder(timekeeperTasks, reportType)
class PlainTextReportBuilder(timekeeperTasks : ArrayList<TimekeeperTask>,val reportType: ReportType, todaysDate : Date = Date()) : ReportBuilder(timekeeperTasks, reportType, todaysDate = todaysDate)
{
override fun getReport() : String
{
val stringBuilder = StringBuilder()
//stringBuilder.append("Time worked today: " + getTotalTimeOfTasksToday())
val taskListToUse = getTaskList()
stringBuilder.append("Time worked today: " + getTotalTimeOfTasks(dailyTasks))
stringBuilder.append("\n")
//stringBuilder.append("Projects worked on today: " + getDailyProjectTimeHashMap())
stringBuilder.append("Projects worked on " + reportType.reportTypeName + "\n:")
stringBuilder.append(getProjectsListPlain(taskListToUse))
return stringBuilder.toString()
}

private fun getTaskList() : ArrayList<TimekeeperTask>
{
when(reportType)
{
ReportType.DAILY -> return dailyTasks
ReportType.MONTHLY -> return monthlyTasks
ReportType.YEARLY -> return yearlyTasks
ReportType.WEEKLY -> return weeklyTasks
}
}

private fun getProjectsListPlain(taskList: ArrayList<TimekeeperTask>) : String
{
val stringBuilder = StringBuilder()
for(currentProject in createProjectHolder(taskList).projects)
{
stringBuilder.append(currentProject.name)
stringBuilder.append("\t")
stringBuilder.append(getTimeStringFromMilliSeconds(currentProject.totalTime*1000))
stringBuilder.append("\n")
}
return stringBuilder.toString()
}

Expand Down
10 changes: 6 additions & 4 deletions src/com/maxwittig/reportgenerator/builder/ReportType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.maxwittig.reportgenerator.builder

import com.maxwittig.reportgenerator.utils.isLastDayOfTheMonth
import com.maxwittig.reportgenerator.utils.isLastDayOfTheWeek
import com.maxwittig.reportgenerator.utils.isLastDayOfTheYear
import java.time.LocalDate


enum class ReportType(val reportTypeName : String)
Expand All @@ -13,19 +15,19 @@ enum class ReportType(val reportTypeName : String)
YEARLY("Yearly");
companion object
{
fun getCurrentReportType(weeklyEnabled : Boolean, monthlyEnabled : Boolean, yearlyEnabled : Boolean) : ReportType
fun getCurrentReportType(weeklyEnabled : Boolean, monthlyEnabled : Boolean, yearlyEnabled : Boolean, localDate: LocalDate = LocalDate.now()) : ReportType
{
if(yearlyEnabled && isLastDayOfTheWeek())
if(yearlyEnabled && isLastDayOfTheYear(now = localDate))
{
return YEARLY
}
else
if(monthlyEnabled && isLastDayOfTheMonth())
if(monthlyEnabled && isLastDayOfTheMonth(now = localDate))
{
return MONTHLY
}
else
if(weeklyEnabled && isLastDayOfTheWeek())
if(weeklyEnabled && isLastDayOfTheWeek(now = localDate))
{
return WEEKLY
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,11 @@ package com.maxwittig.reportgenerator.tests.builder

import com.maxwittig.reportgenerator.builder.HTMLReportBuilder
import com.maxwittig.reportgenerator.builder.ReportType
import com.maxwittig.reportgenerator.models.TimekeeperTask
import org.junit.Test
import java.io.File
import java.text.SimpleDateFormat
import java.util.*

class HTMLReportBuilderTest
class HTMLReportBuilderTest : ReportBuilderTest()
{
private val wordList : List<String> = File("data/wordlist.txt").readText().split("\n")

private fun getRandomTimekeeperTaskArrayList() : ArrayList<TimekeeperTask>
{
val completeTaskList = ArrayList<TimekeeperTask>()
var i = 0
while (i < 10000)
{
i++
val startDate = getRandomDateInTheYear()
val duration = getRandomDuration()
val endDate = Date(startDate.time + duration)
val task = TimekeeperTask(getRandomWordFromList(), getRandomWordFromList(), startDate, endDate, duration)
completeTaskList.add(task)
}
completeTaskList.sort { task1, task2 -> task1.startTime.compareTo(task2.startTime) }
return completeTaskList
}

@Test
fun weeklyReportTest()
Expand Down Expand Up @@ -65,24 +44,5 @@ class HTMLReportBuilderTest
File("data/testResults/yearly.html").writeText(reportBuilder.getReport())
}

private fun getRandomWordFromList() : String
{
return wordList[Random().nextInt(wordList.size)]
}

private fun getRandomDateInTheYear() : Date
{
val year = "2017"
val format = SimpleDateFormat("yyyy")
val yearDate = format.parse(year)
val yearStartSeconds = yearDate.time
val yearLengthMilliSeconds = 31557600000
val randomDateInMilliseconds = (Random().nextDouble()*(yearLengthMilliSeconds)).toLong()
return Date(yearStartSeconds+randomDateInMilliseconds)
}

private fun getRandomDuration() : Long
{
return (Random().nextDouble()*(86400)).toLong()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.maxwittig.reportgenerator.tests.builder

import com.maxwittig.reportgenerator.builder.PlainTextReportBuilder
import com.maxwittig.reportgenerator.builder.ReportType
import org.junit.Test


class PlainTextReportBuilderTest : ReportBuilderTest()
{
@Test
fun dailyReportTest()
{
val tasks = getRandomTimekeeperTaskArrayList()
val reportBuilder = PlainTextReportBuilder(tasks, ReportType.DAILY, todaysDate = getRandomDateInTheYear())
println(reportBuilder.getReport())

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.maxwittig.reportgenerator.tests.builder

import com.maxwittig.reportgenerator.models.TimekeeperTask
import java.io.File
import java.text.SimpleDateFormat
import java.util.*


abstract class ReportBuilderTest
{
private val wordList : List<String> = File("data/wordlist.txt").readText().split("\n")

protected fun getRandomTimekeeperTaskArrayList() : ArrayList<TimekeeperTask>
{
val completeTaskList = ArrayList<TimekeeperTask>()
var i = 0
while (i < 10000)
{
i++
val startDate = getRandomDateInTheYear()
val duration = getRandomDuration()
val endDate = Date(startDate.time + duration)
val task = TimekeeperTask(getRandomWordFromList(), getRandomWordFromList(), startDate, endDate, duration)
completeTaskList.add(task)
}
completeTaskList.sort { task1, task2 -> task1.startTime.compareTo(task2.startTime) }
return completeTaskList
}

protected fun getRandomWordFromList() : String
{
return wordList[Random().nextInt(wordList.size)]
}

protected fun getRandomDateInTheYear() : Date
{
val year = "2017"
val format = SimpleDateFormat("yyyy")
val yearDate = format.parse(year)
val yearStartSeconds = yearDate.time
val yearLengthMilliSeconds = 31557600000
val randomDateInMilliseconds = (Random().nextDouble()*(yearLengthMilliSeconds)).toLong()
return Date(yearStartSeconds+randomDateInMilliseconds)
}

protected fun getRandomDuration() : Long
{
return (Random().nextDouble()*(86400)).toLong()
}
}
22 changes: 22 additions & 0 deletions src/com/maxwittig/reportgenerator/tests/builder/ReportTypeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.maxwittig.reportgenerator.tests.builder

import com.maxwittig.reportgenerator.builder.ReportType
import org.junit.Assert.assertEquals
import org.junit.Test
import java.time.LocalDate

class ReportTypeTest
{
@Test
fun getCurrentReportTypeTest()
{
val yearlyDate = LocalDate.of(2016, 12, 31) //end of the year
val monthlyDate = LocalDate.of(2016, 11, 30) //end of the month
val sundayDate = LocalDate.of(2016, 12, 25) //sunday
val dailyDate = LocalDate.of(2016, 1, 5) //nothing special
assertEquals(ReportType.getCurrentReportType(true, true, true, localDate = yearlyDate), ReportType.YEARLY)
assertEquals(ReportType.getCurrentReportType(true, true, true, localDate = monthlyDate), ReportType.MONTHLY)
assertEquals(ReportType.getCurrentReportType(true, true, true, localDate = sundayDate), ReportType.WEEKLY)
assertEquals(ReportType.getCurrentReportType(true, true, true, localDate = dailyDate), ReportType.DAILY)
}
}
9 changes: 3 additions & 6 deletions src/com/maxwittig/reportgenerator/utils/DateUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,19 @@ fun isSameYear(date1 : Date, date2 : Date) : Boolean
return simpleFormat.format(date1) == simpleFormat.format(date2)
}

fun isLastDayOfTheMonth() : Boolean
fun isLastDayOfTheMonth(now : LocalDate = LocalDate.now()) : Boolean
{
val now = LocalDate.now()
val lastDay = now.with(TemporalAdjusters.lastDayOfMonth())
return now == lastDay
}

fun isLastDayOfTheWeek() : Boolean
fun isLastDayOfTheWeek(now : LocalDate = LocalDate.now()) : Boolean
{
val now = LocalDate.now()
return now.dayOfWeek.value == 7
}

fun isLastDayOfTheYear() : Boolean
fun isLastDayOfTheYear(now : LocalDate = LocalDate.now()) : Boolean
{
val now = LocalDate.now()
val lastDay = now.with(TemporalAdjusters.lastDayOfYear())
return now == lastDay
}
Expand Down

0 comments on commit b7ad33d

Please sign in to comment.