IniTest.java
- /***************************************************************************
- Copyright 2015 Emily Estes
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ***************************************************************************/
- package net.metanotion.util;
- import java.io.File;
- import java.io.StringReader;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import javax.inject.Named;
- public final class IniTest {
- private static void assertTrue(final boolean result) {
- if(!result) { throw new AssertionError("Expected true"); }
- }
- public static void main(final String[] args) throws Exception {
- testArgs();
- testIni();
- }
- public static final HashSet<String> ignore = new HashSet<>();
- public static final HashSet<String> require = new HashSet<>();
- static {
- ignore.add("e");
- require.add("a");
- }
- public static final class Args1 { }
- public static final class Args2 {
- public int a;
- public boolean b;
- @Description("test description.")
- public Integer c;
- public File d;
- @Named("e") public String e;
- }
- public static final class Args3 {
- public int a;
- public String b;
- public Args1 c;
- }
- public static void testArgs() throws Exception {
- final ArgsToStruct<Args1> a1 = new ArgsToStruct<>(Args1.class);
- final String[] args1_1 = new String[] {};
- try {
- final Args1 a1_1 = a1.getInstance(args1_1);
- throw new AssertionError("Arguments missing, args to struct should throw.");
- } catch (final RuntimeException ex) {
- // Expected.
- }
- final Args1 a1_1 = a1.getInstance(args1_1, new Object(), false);
- final String[] args1_2 = new String[] {"a", "b"};
- final Args1 a1_2 = a1.getInstance(args1_2);
- final ArgsToStruct<Args2> a2 = new ArgsToStruct<>(Args2.class);
- final String[] args2_1 = new String[] {};
- try {
- final Args2 a2_1 = a2.getInstance(args2_1);
- throw new AssertionError("Arguments missing, args to struct should throw.");
- } catch (final RuntimeException ex) {
- // Expected.
- }
- final Args2 a2_2 = a2.getInstance(args2_1, new Object(), false);
- final String[] args2_3 = new String[] { "-a:1", "-b", "-c:10", "-d:test.txt", "-e:test" };
- final Args2 a2_3 = a2.getInstance(args2_3);
- final String[] args2_4 = new String[] { "-c:20" };
- final Args2 a2_4 = a2.getInstance(args2_4, a2_3, true);
- final ArgsToStruct<Args2> a2v2 = new ArgsToStruct<>(Args2.class, require, ignore);
- final Args2 a2v2_3 = a2v2.getInstance(args2_3);
- final String[] args2v2_4 = new String[] { "-c:10", "-d:test.txt", "-e:test" };
- try {
- final Args2 a2v2_4 = a2v2.getInstance(args2v2_4);
- throw new AssertionError("-a was a required argument that was missing.");
- } catch (final RuntimeException ex) {
- // Expected.
- }
- final ArgsToStruct<Args3> a3 = new ArgsToStruct<>(Args3.class);
- final String[] args3_1 = new String[] { "-a:1", "-b:awef", "-c:10" };
- final Args3 a3_1 = a3.getInstance(args3_1);
- }
- public static final class Args4 {
- public int a;
- public Boolean bool;
- public String b;
- public Args2 c;
- public Args5 d;
- public Map<String,String> keys = new HashMap<>();
- }
- public static final class Args5 {
- public int a;
- public String b;
- public Args2 c;
- }
- public static void testIni() throws Exception {
- final IniProperties empty = new IniProperties().load(new StringReader(""));
- final IniProperties args2_1 = new IniProperties().load(
- new StringReader("a=1\nb=true\nc=10\nd=test.txt\ne:test"));
- final IniToStruct<Args1> a1 = new IniToStruct<>(Args1.class);
- final Args1 a1_1 = a1.getInstance(new Object(), empty);
- final IniToStruct<Args2> a2 = new IniToStruct<>(Args2.class);
- final Args2 a2_1 = a2.getInstance(new Object(), empty);
- final Args2 a2_2 = a2.getInstance(new Object(), args2_1);
- final IniToStruct<Args2> a2v2 = new IniToStruct<>(Args2.class, require, ignore);
- final IniToStruct<Args3> a3 = new IniToStruct<>(Args3.class);
- final IniProperties args4_1 = new IniProperties().load(
- new StringReader("a=1\nb=true\nbool=false\n"
- + "keys.foo=bar\nkeys.awef=1\nkeys.a=true\n"
- + "[c]\na=1\nb=true\nc=10\nd=test.txt\ne:test\n"
- + "[d]\na=2\nb=ttt\n"
- + "[d.c]\na=3\nb=true\nc=8\n"));
- final IniToStruct<Args4> a4 = new IniToStruct<>(Args4.class);
- final Args4 a4_1 = a4.getInstance(new Object(), args4_1);
- assertTrue("bar".equals(a4_1.keys.get("foo")));
- assertTrue("1".equals(a4_1.keys.get("awef")));
- assertTrue("true".equals(a4_1.keys.get("a")));
- assertTrue(a4_1.a == 1);
- assertTrue("true".equals(a4_1.b));
- assertTrue(a4_1.bool == false);
- assertTrue(a4_1.c.a == 1);
- assertTrue(a4_1.c.b == true);
- assertTrue(a4_1.c.c.intValue() == 10);
- assertTrue(a4_1.d.c.a == 3);
- }
- }