|
| 1 | +/** |
| 2 | + * Copyright (C) 2011 Typesafe Inc. <http://typesafe.com> |
| 3 | + */ |
| 4 | +package org.ekrich.config.impl |
| 5 | + |
| 6 | +import org.junit._ |
| 7 | + |
| 8 | +import org.ekrich.config.ConfigFactory |
| 9 | +import org.ekrich.config.ConfigParseOptions |
| 10 | +import org.ekrich.config.ConfigException |
| 11 | +import FileUtils._ |
| 12 | + |
| 13 | +class ValidationFileTest extends TestUtils { |
| 14 | + // TODO: There is a problem upstream where an exception is not thrown |
| 15 | + // when the file does not exist. Added a check in FileUtils for native only |
| 16 | + // jvm would need it. |
| 17 | + @Test |
| 18 | + def validation(): Unit = { |
| 19 | + val reference = ConfigFactory.parseFile( |
| 20 | + resourceFile("validate-reference.conf"), |
| 21 | + ConfigParseOptions.defaults |
| 22 | + ) |
| 23 | + val conf = ConfigFactory.parseFile( |
| 24 | + resourceFile("validate-invalid.conf"), |
| 25 | + ConfigParseOptions.defaults |
| 26 | + ) |
| 27 | + val e = intercept[ConfigException.ValidationFailed] { |
| 28 | + conf.checkValid(reference) |
| 29 | + } |
| 30 | + |
| 31 | + val expecteds = Seq( |
| 32 | + Missing("willBeMissing", 1, "number"), |
| 33 | + WrongType("int3", 7, "number", "object"), |
| 34 | + WrongType("float2", 9, "number", "boolean"), |
| 35 | + WrongType("float3", 10, "number", "list"), |
| 36 | + WrongType("bool1", 11, "boolean", "number"), |
| 37 | + WrongType("bool3", 13, "boolean", "object"), |
| 38 | + Missing("object1.a", 17, "string"), |
| 39 | + WrongType("object2", 18, "object", "list"), |
| 40 | + WrongType("object3", 19, "object", "number"), |
| 41 | + WrongElementType("array3", 22, "boolean", "object"), |
| 42 | + WrongElementType("array4", 23, "object", "number"), |
| 43 | + WrongType("array5", 24, "list", "number"), |
| 44 | + WrongType("a.b.c.d.e.f.g", 28, "boolean", "number"), |
| 45 | + Missing("a.b.c.d.e.f.j", 28, "boolean"), |
| 46 | + WrongType("a.b.c.d.e.f.i", 30, "boolean", "list") |
| 47 | + ) |
| 48 | + |
| 49 | + checkValidationException(e, expecteds) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + def validationWithRoot(): Unit = { |
| 54 | + val objectWithB = parseObject("""{ b : c }""") |
| 55 | + val reference = ConfigFactory |
| 56 | + .parseFile( |
| 57 | + resourceFile("validate-reference.conf"), |
| 58 | + ConfigParseOptions.defaults |
| 59 | + ) |
| 60 | + .withFallback(objectWithB) |
| 61 | + val conf = ConfigFactory.parseFile( |
| 62 | + resourceFile("validate-invalid.conf"), |
| 63 | + ConfigParseOptions.defaults |
| 64 | + ) |
| 65 | + val e = intercept[ConfigException.ValidationFailed] { |
| 66 | + conf.checkValid(reference, "a", "b") |
| 67 | + } |
| 68 | + |
| 69 | + val expecteds = Seq( |
| 70 | + Missing("b", 1, "string"), |
| 71 | + WrongType("a.b.c.d.e.f.g", 28, "boolean", "number"), |
| 72 | + Missing("a.b.c.d.e.f.j", 28, "boolean"), |
| 73 | + WrongType("a.b.c.d.e.f.i", 30, "boolean", "list") |
| 74 | + ) |
| 75 | + |
| 76 | + checkValidationException(e, expecteds) |
| 77 | + } |
| 78 | +} |
0 commit comments