| 1 | package be.nikiroo.utils.test; |
| 2 | |
| 3 | import java.io.File; |
| 4 | |
| 5 | import be.nikiroo.utils.IOUtils; |
| 6 | import be.nikiroo.utils.resources.Bundle; |
| 7 | import be.nikiroo.utils.resources.Bundles; |
| 8 | import be.nikiroo.utils.resources.Meta; |
| 9 | |
| 10 | class BundleTest extends TestLauncher { |
| 11 | private File tmp; |
| 12 | private B b = new B(); |
| 13 | |
| 14 | protected boolean isMain() { |
| 15 | return true; |
| 16 | } |
| 17 | |
| 18 | public BundleTest(String[] args) { |
| 19 | this("Bundle test", args); |
| 20 | } |
| 21 | |
| 22 | protected BundleTest(String name, String[] args) { |
| 23 | super(name, args); |
| 24 | |
| 25 | addTests(); |
| 26 | |
| 27 | if (isMain()) { |
| 28 | addSeries(new BundleTest("After saving/reloading the resources", |
| 29 | args) { |
| 30 | @Override |
| 31 | protected void start() throws Exception { |
| 32 | tmp = File.createTempFile("nikiroo-utils", ".test"); |
| 33 | tmp.delete(); |
| 34 | tmp.mkdir(); |
| 35 | b.updateFile(tmp.getAbsolutePath()); |
| 36 | Bundles.setDirectory(tmp.getAbsolutePath()); |
| 37 | b.reload(); |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | protected void stop() { |
| 42 | IOUtils.deltree(tmp); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected boolean isMain() { |
| 47 | return false; |
| 48 | } |
| 49 | }); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private void addTests() { |
| 54 | String pre = ""; |
| 55 | |
| 56 | addTest(new TestCase(pre + "getString simple") { |
| 57 | @Override |
| 58 | public void test() throws Exception { |
| 59 | assertEquals("un", b.getString(E.ONE)); |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | addTest(new TestCase(pre + "getStringX with null suffix") { |
| 64 | @Override |
| 65 | public void test() throws Exception { |
| 66 | assertEquals("un", b.getStringX(E.ONE, null)); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | addTest(new TestCase(pre + "getStringX with empty suffix") { |
| 71 | @Override |
| 72 | public void test() throws Exception { |
| 73 | assertEquals(null, b.getStringX(E.ONE, "")); |
| 74 | } |
| 75 | }); |
| 76 | |
| 77 | addTest(new TestCase(pre + "getStringX with existing suffix") { |
| 78 | @Override |
| 79 | public void test() throws Exception { |
| 80 | assertEquals("un + suffix", b.getStringX(E.ONE, "suffix")); |
| 81 | } |
| 82 | }); |
| 83 | |
| 84 | addTest(new TestCase(pre + "getStringX with not existing suffix") { |
| 85 | @Override |
| 86 | public void test() throws Exception { |
| 87 | assertEquals(null, b.getStringX(E.ONE, "fake")); |
| 88 | } |
| 89 | }); |
| 90 | |
| 91 | addTest(new TestCase(pre + "getString with UTF-8 content") { |
| 92 | @Override |
| 93 | public void test() throws Exception { |
| 94 | assertEquals("日本語 Nihongo", b.getString(E.JAPANESE)); |
| 95 | } |
| 96 | }); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * {@link Bundle}. |
| 101 | * |
| 102 | * @author niki |
| 103 | */ |
| 104 | private class B extends Bundle<E> { |
| 105 | protected B() { |
| 106 | super(E.class, N.bundle_test); |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Key enum for the {@link Bundle}. |
| 113 | * |
| 114 | * @author niki |
| 115 | */ |
| 116 | private enum E { |
| 117 | @Meta(what = "", where = "", format = "", info = "") |
| 118 | ONE, // |
| 119 | @Meta(what = "", where = "", format = "", info = "") |
| 120 | ONE_SUFFIX, // |
| 121 | @Meta(what = "", where = "", format = "", info = "") |
| 122 | TWO, // |
| 123 | @Meta(what = "", where = "", format = "", info = "") |
| 124 | JAPANESE |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Name enum for the {@link Bundle}. |
| 129 | * |
| 130 | * @author niki |
| 131 | */ |
| 132 | private enum N { |
| 133 | bundle_test |
| 134 | } |
| 135 | } |