summaryrefslogtreecommitdiffstats
path: root/container-di/src/test/scala/com/yahoo/container/di/DirConfigSource.scala
blob: 5afa1bc418e7d606d2762e5c21b465b3f1068cd0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.di

import java.io.{FileOutputStream, OutputStream, File}
import DirConfigSource._
import java.util.Random
import org.junit.rules.TemporaryFolder
import com.yahoo.config.subscription.{ConfigSource, ConfigSourceSet}

// TODO: Make this a junit rule. Does not yet work. Look out for junit updates
//       (@Rule def configSourceRule  = dirConfigSource)

/**
 * @author tonytv
 * @author gjoranv
 */
class DirConfigSource(val testSourcePrefix: String) {
  private val tempFolder = createTemporaryFolder()

  val configSource : ConfigSource = new ConfigSourceSet(testSourcePrefix + new Random().nextLong)

  def writeConfig(name: String, contents: String) {
    val file = new File(tempFolder.getRoot, name + ".cfg")
    if (!file.exists())
      file.createNewFile()

    printFile(file, contents + "\n")
  }

  def configId = "dir:" + tempFolder.getRoot.getPath

  def cleanup() {
    tempFolder.delete()
  }
}

private object DirConfigSource {
  def printFile(f: File, content: String) {
    var out: OutputStream = new FileOutputStream(f)
    out.write(content.getBytes("UTF-8"))
    out.close()
  }

  def createTemporaryFolder() = {
    val folder = new TemporaryFolder
    folder.create()
    folder
  }
}