Created
May 3, 2012 04:25
-
-
Save fumokmm/2583165 to your computer and use it in GitHub Desktop.
Javaでenumを使ってプロパティファイルのキーを定義する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| key1=fuga-value-1 | |
| key2=fuga-value-2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| key1=hoge-value-1 | |
| key2=hoge-value-2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static org.hamcrest.CoreMatchers.*; | |
| import static org.junit.Assert.*; | |
| import java.io.*; | |
| import java.util.*; | |
| import org.junit.*; | |
| public class PropertyReaderTest { | |
| /** メインのテスト */ | |
| @Test public void キーを使って値を取得するサンプル() { | |
| PropertyReader propReader = new PropertyReader(); | |
| assertThat(propReader.getProp(HogeKey.key1), is("hoge-value-1")); | |
| assertThat(propReader.getProp(FugaKey.key2), is("fuga-value-2")); | |
| assertThat(propReader.getProp(HogeKey.key2), is("hoge-value-2")); | |
| } | |
| /** | |
| * [おまけ] PropKey定義にあるのに、.properties にない場合を検出する。 | |
| * (逆の .properties にしかない場合はOK) | |
| */ | |
| @Test public void キーの存在チェック() { | |
| Set<Object> filePropKeySet = | |
| PropertyReader.getProperties(new File(HogeKey.PATH)).keySet(); | |
| for (PropKey[] propKeyValues : new PropKey[][]{ | |
| HogeKey.values(), | |
| FugaKey.values(), | |
| // ... キーが増えたら追加 | |
| }) { | |
| for (PropKey key : propKeyValues) { | |
| assertTrue(key.getClass().getSimpleName() + "." + key.name() + "が存在するはず", | |
| filePropKeySet.contains(key.name())); | |
| } | |
| } | |
| } | |
| } | |
| /** プロパティのキーを表現するインタフェース */ | |
| interface PropKey { | |
| Properties getProperties(); | |
| String name(); | |
| } | |
| /** hoge.properties で利用するキー */ | |
| enum HogeKey implements PropKey { | |
| key1, | |
| key2, | |
| ; | |
| public static final String PATH = "hoge.properties"; | |
| @Override public Properties getProperties(){ | |
| return PropertyReader.getProperties(new File(PATH)); | |
| } | |
| } | |
| /** fuga.properties で利用するキー */ | |
| enum FugaKey implements PropKey { | |
| key1, | |
| key2, | |
| ; | |
| public static final String PATH = "fuga.properties"; | |
| @Override public Properties getProperties(){ | |
| return PropertyReader.getProperties(new File(PATH)); | |
| } | |
| } | |
| /** プロパティ読み込みクラス */ | |
| class PropertyReader { | |
| /** 読み込んだプロパティのキャッシュ */ | |
| private Map<Class<Enum<?>>, Properties> propCache = | |
| new WeakHashMap<Class<Enum<?>>, Properties>(); | |
| /** | |
| * プロパティファイルから値を読み込む | |
| * @param key PropKey | |
| * @return 値 | |
| */ | |
| @SuppressWarnings("unchecked") | |
| public String getProp(PropKey key) { | |
| Properties prop; | |
| if (propCache.containsKey(key.getClass())) { | |
| prop = propCache.get(key.getClass()); | |
| } else { | |
| prop = key.getProperties(); // どのプロパティファイルを読み込めば良いかはキーが知っている | |
| propCache.put((Class<Enum<?>>)key.getClass(), prop); | |
| } | |
| return prop.getProperty(key.name()); | |
| } | |
| /** プロパティファイルの読み込み(I/O) */ | |
| public static Properties getProperties(File propFile) { | |
| Properties result = new Properties(); | |
| try { | |
| result.load(new FileReader(propFile)); | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment