Skip to content

Commit

Permalink
Merge branch 'kotlin_transition'
Browse files Browse the repository at this point in the history
* kotlin_transition:
  Use lateinit for plugin fields
  Reduce null complexity
  better tests for plugin init
  Added coveralls sourceDirs for kotlin files
  Converted mocks to infix syntax
  Converted code to kotlin and fixed tests to work with it
  Converted Tests to kotlin
  First test in kotlin
  Added kotlin feature
  • Loading branch information
marpoe committed Nov 29, 2016
2 parents daf98e0 + c11ea20 commit 8312d57
Show file tree
Hide file tree
Showing 22 changed files with 497 additions and 499 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ out
.gradle
.idea
*.iml
build
build
classes
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ buildscript {
jcenter()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

plugins {
Expand All @@ -20,6 +23,7 @@ repositories {
}

apply plugin: 'java'
apply plugin: "kotlin"
apply plugin: 'jacoco'

group = 'de.poeschl.bukkit'
Expand All @@ -43,6 +47,10 @@ shadowJar {
classifier = null
}

coveralls {
sourceDirs += ['src/main/kotlin']
}

jacoco {
toolVersion = "0.7.7.201606060606"
}
Expand All @@ -62,11 +70,13 @@ jacocoTestReport {

dependencies {
compileOnly "org.bukkit:bukkit:$bukkit_version"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

testCompile "org.bukkit:bukkit:$bukkit_version"
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile 'org.mockito:mockito-core:2.2.22'
testCompile 'org.assertj:assertj-core:3.5.2'
testCompile 'com.nhaarman:mockito-kotlin:0.12.2'


}
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#Plugin version
version=1.1-SNAPSHOT
#Dependency versions
bukkit_version=1.7.9-R0.2
bukkit_version=1.7.9-R0.2
kotlin_version=1.0.5
58 changes: 0 additions & 58 deletions src/main/java/de/poeschl/bukkit/logcleaner/LogCleanerPlugin.java

This file was deleted.

44 changes: 0 additions & 44 deletions src/main/java/de/poeschl/bukkit/logcleaner/helper/FileHelper.java

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

60 changes: 60 additions & 0 deletions src/main/kotlin/de/poeschl/bukkit/logcleaner/LogCleanerPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package de.poeschl.bukkit.logcleaner

import de.poeschl.bukkit.logcleaner.helper.FileHelper
import de.poeschl.bukkit.logcleaner.managers.SettingsManager
import de.poeschl.bukkit.logcleaner.threads.LogCleanerRunnable
import de.poeschl.bukkit.logcleaner.utils.InstanceFactory
import org.bukkit.plugin.PluginDescriptionFile
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.util.*
import java.util.logging.Logger

open class LogCleanerPlugin : JavaPlugin() {

internal lateinit var instanceFactory: InstanceFactory
internal lateinit var pluginLogger: Logger
internal lateinit var settingManager: SettingsManager
internal lateinit var fileHelper: FileHelper
internal lateinit var logCleanerRunnable: LogCleanerRunnable

override fun onEnable() {
super.onEnable()
val pdfFile = info

if (config.getKeys(false).size == 0) {
config
saveDefaultConfig()
}
initFields()

pluginLogger.info(pdfFile.name + " version " + pdfFile.version + " is enabled!")

activateLogCleaner()
}

open internal val info: PluginDescriptionFile
get() = description

open internal fun initFields() {
instanceFactory = createInstanceFactory()
pluginLogger = instanceFactory.getLogger(this)
settingManager = instanceFactory.createSettingsManager(config, pluginLogger)
fileHelper = instanceFactory.createFileHelper(pluginLogger)
logCleanerRunnable = instanceFactory.createLogCleanerRunnable(fileHelper, settingManager, LOG_FOLDER)
}

open internal fun createInstanceFactory(): InstanceFactory {
return InstanceFactory()
}

open internal fun activateLogCleaner() {
pluginLogger.info("Starting LogCleanerPlugin Thread")
logCleanerRunnable.setNow(Date())
Thread(logCleanerRunnable).start()
}

companion object {
private val LOG_FOLDER = File("./" + FileHelper.LOGS_FOLDER_NAME)
}
}
40 changes: 40 additions & 0 deletions src/main/kotlin/de/poeschl/bukkit/logcleaner/helper/FileHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package de.poeschl.bukkit.logcleaner.helper

import java.io.File
import java.io.FilenameFilter
import java.util.*
import java.util.logging.Logger

interface FileHelper {
fun getLogArchives(logFolder: File): List<File>

fun getLogArchiveDate(file: File): Date

fun delete(file: File): Boolean

companion object {
val LOGS_FOLDER_NAME = "logs"
}
}

class FileHelperImpl(private val logger: Logger) : FileHelper {

override fun getLogArchives(logFolder: File): List<File> {
val archiveFilter = FilenameFilter { dir, name -> name.endsWith(".gz") }

return logFolder.listFiles(archiveFilter).asList()
}

override fun getLogArchiveDate(file: File): Date {
val splitedName = file.name.split("-")

val cal = Calendar.getInstance()
cal.set(Integer.parseInt(splitedName[0]), Integer.parseInt(splitedName[1]) - 1, Integer.parseInt(splitedName[2]))
return cal.time
}

override fun delete(file: File): Boolean {
logger.info("Deleted log:" + file.name)
return file.delete()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package de.poeschl.bukkit.logcleaner.managers

import de.poeschl.bukkit.logcleaner.managers.SettingsManager.Companion.KEEP_DAYS_KEY
import org.bukkit.configuration.file.FileConfiguration

import java.util.logging.Logger

interface SettingsManager {

val keepDays: Int

fun updateConfig(config: FileConfiguration)

companion object {
val KEEP_DAYS_KEY = "keepLogsFromLastDays"
}
}

class SettingsManagerImpl(private var config: FileConfiguration, private val logger: Logger) : SettingsManager {

override val keepDays: Int
get() {
return config.getInt(KEEP_DAYS_KEY)
}

override fun updateConfig(config: FileConfiguration) {
this.config = config
}
}
Loading

0 comments on commit 8312d57

Please sign in to comment.