/** Load properties from in file Format: jajuk.ambience.<ID>/<name>=style1,style2,... */ public void load() { // if first startup, define default ambiences if (UpgradeManager.isFirstSesion()) { createDefaultAmbiences(); return; } Properties properties = Conf.getProperties(); Enumeration<Object> e = properties.keys(); while (e.hasMoreElements()) { String sKey = (String) e.nextElement(); if (sKey.matches(Const.AMBIENCE_PREFIX + ".*")) { Set<Style> styles = new HashSet<Style>(10); StringTokenizer st = new StringTokenizer((String) properties.get(sKey), ","); while (st.hasMoreTokens()) { Style style = StyleManager.getInstance().getStyleByID(st.nextToken()); if (style != null) { styles.add(style); } } String ambienceDesc = sKey.substring(Const.AMBIENCE_PREFIX.length()); int index = ambienceDesc.indexOf('/'); if (index == -1) { continue; } String ambienceID = ambienceDesc.substring(0, index); String ambienceName = ambienceDesc.substring(index + 1); Ambience ambience = new Ambience(ambienceID, ambienceName, styles); ambiences.put(ambienceID, ambience); } } // If none ambience, means ambience can have been reset after a style // hashcode computation change, reset to defaults if (ambiences.size() == 0) { createDefaultAmbiences(); } }
/** * Test method for {@link org.jajuk.services.dj.AmbienceManager#load()}. * * @throws Exception the exception */ public final void testLoad() throws Exception { // make sure "UpgradeManager.bFirstSession" is not set { Class<?> c = UpgradeManager.class; Field f = c.getDeclaredField("firstSession"); f.setAccessible(true); f.setBoolean(null, Boolean.FALSE); } assertFalse(UpgradeManager.isFirstSession()); assertEquals(0, AmbienceManager.getInstance().getAmbiences().size()); // first without any key AmbienceManager.getInstance().load(); // creates the 14 default ambiences assertEquals(14, AmbienceManager.getInstance().getAmbiences().size()); // clean out all leftover ambiences from other testing for (Ambience amb : AmbienceManager.getInstance().getAmbiences()) { AmbienceManager.getInstance().removeAmbience(amb.getID()); } assertEquals(0, AmbienceManager.getInstance().getAmbiences().size()); { // remove all leftover properties Properties properties = Conf.getProperties(); Enumeration<Object> e = properties.keys(); while (e.hasMoreElements()) { String sKey = (String) e.nextElement(); if (sKey.matches(Const.AMBIENCE_PREFIX + ".*")) { properties.remove(sKey); } } } // then add set some Ambience-items Genre genre1 = GenreManager.getInstance().registerGenre("genre1"); Genre genre2 = GenreManager.getInstance().registerGenre("genre2"); Conf.setProperty( Const.AMBIENCE_PREFIX + "12/myambience", genre1.getID() + "," + genre2.getID()); assertEquals(0, AmbienceManager.getInstance().getAmbiences().size()); { // check all the conditions to find out why it fails in Hudson assertFalse(UpgradeManager.isFirstSession()); Properties properties = Conf.getProperties(); Enumeration<Object> e = properties.keys(); int nMatch = 0; while (e.hasMoreElements()) { String sKey = (String) e.nextElement(); if (sKey.matches(Const.AMBIENCE_PREFIX + ".*")) { if (sKey.substring(Const.AMBIENCE_PREFIX.length()).indexOf('/') == -1) { continue; } nMatch++; } } assertEquals(properties.toString(), 1, nMatch); } AmbienceManager.getInstance().load(); assertEquals(1, AmbienceManager.getInstance().getAmbiences().size()); assertNotNull(AmbienceManager.getInstance().getAmbience("12")); // now test with an ambience with invalid format, i.e. only "12", not // "12/name" for (Ambience amb : AmbienceManager.getInstance().getAmbiences()) { AmbienceManager.getInstance().removeAmbience(amb.getID()); } assertEquals(0, AmbienceManager.getInstance().getAmbiences().size()); Conf.setProperty(Const.AMBIENCE_PREFIX + "12", genre1.getID() + "," + genre2.getID()); Conf.removeProperty(Const.AMBIENCE_PREFIX + "12/myambience"); AmbienceManager.getInstance().load(); // now 14 as this could not be loaded and thus the default ones were // loaded... assertEquals(14, AmbienceManager.getInstance().getAmbiences().size()); UpgradeManager.setFirstSession(); AmbienceManager.getInstance().load(); }