DefaultSlingSimulator implements SlingSimulatorBackend, SlingSimulator { static Map<String, Object> readDictionary( final InputStream is, final String id) throws IOException { final Map<String, Object> ht = new LinkedHashMap<>(); if (id.endsWith(".cfg.json")) { final String name = "jcrinstall:".concat(id); String configId; int pos = id.lastIndexOf('/'); if (pos == -1) { configId = id; } else { configId = id.substring(pos + 1); } pos = configId.indexOf('-'); if (pos != -1) { configId = configId.substring(0, pos).concat("~").concat(configId.substring(pos + 1)); } configId = removeConfigExtension(configId); try (final Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) { final Dictionary<String, Object> config = Configurations.buildReader() .withIdentifier(configId).build(reader).readConfiguration(); final Enumeration<String> i = config.keys(); while (i.hasMoreElements()) { final String key = i.nextElement(); ht.put(key, config.get(key)); } } } else { try (final BufferedInputStream in = new BufferedInputStream(is)) { if (id.endsWith(".config")) { in.mark(256); final int firstChar = in.read(); if (firstChar == '#') { int b; while ((b = in.read()) != '\n') { if (b == -1) { throw new IOException("Unable to read configuration."); } } } else { in.reset(); } @SuppressWarnings("unchecked") final Dictionary<String, Object> config = ConfigurationHandler.read(in); final Enumeration<String> i = config.keys(); while (i.hasMoreElements()) { final String key = i.nextElement(); ht.put(key, config.get(key)); } } else { final Properties p = new Properties(); in.mark(1); boolean isXml = in.read() == '<'; in.reset(); if (isXml) { p.loadFromXML(in); } else { p.load(in); } final Enumeration<Object> i = p.keys(); while (i.hasMoreElements()) { final Object key = i.nextElement(); ht.put(key.toString(), p.get(key)); } } } } return ht; }  static SlingSimulatorBackend instance(); @Override void startedScan(); @Override void setSession(final Session session); @Override void setPackageManager(final JcrPackageManager packageManager); @Override void setErrorListener(final ErrorListener errorListener); @Override @Nullable SlingInstallable dequeueInstallable(); @SuppressWarnings("unchecked") @Override @NotNull Fun.ThrowingSupplier<ResourceType> open(@NotNull final SlingOpenable<ResourceType> installable); @NotNull JcrPackage openEmbeddedPackage(@NotNull final EmbeddedPackageInstallable installable); @Override @Nullable SlingInstallable addInstallableNode(final @NotNull PackageId parentPackageId,
                                                         final @NotNull Node node);  }
@Test(expected = IOException.class) public void testReadDictionary_onlyCommentLineThrows() throws Exception { final String simpleConfigWithComment = "# some comment"; DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(simpleConfigWithComment.getBytes(StandardCharsets.UTF_8)), "simple.config"); }
@Test(expected = IOException.class) public void testReadDictionary_dotCfgJson_throwsError() throws Exception { final String dotCfgJson = obj() .key("foo:Integer", "not an int") .get().toString(); try { DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotCfgJson.getBytes(StandardCharsets.UTF_8)), "simple.cfg.json"); } catch (ConversionException ce) { throw new IOException(ce); } }
@Test public void testReadDictionary_dotCfgJson() throws Exception { final Map<String, Object> expectConfig = new HashMap<>(); expectConfig.put("foo", "bar"); expectConfig.put("foos", Stream.of("bar", "bar", "bar").toArray(String[]::new)); expectConfig.put("ones", Stream.of(1L, 1L, 1L).toArray(Long[]::new)); expectConfig.put("nothing", new String[0]); final String dotCfgJson = obj() .key("foo", "bar") .key("foos", arr().val("bar").val("bar").val("bar")) .key("ones", arr().val(1L).val(1L).val(1L)) .key("nothing", arr()) .get().toString(); checkExpectedProperties(expectConfig, DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotCfgJson.getBytes(StandardCharsets.UTF_8)), "simple.cfg.json")); checkExpectedProperties(expectConfig, DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotCfgJson.getBytes(StandardCharsets.UTF_8)), "nested/simple.cfg.json")); checkExpectedProperties(expectConfig, DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotCfgJson.getBytes(StandardCharsets.UTF_8)), "nested/simple-inst.cfg.json")); }
@Test public void testReadDictionary_dotConfig() throws Exception { final Map<String, Object> expectConfig = new HashMap<>(); expectConfig.put("foo", "bar"); expectConfig.put("foos", Stream.of("bar", "bar", "bar").toArray(String[]::new)); expectConfig.put("ones", Stream.of(1L, 1L, 1L).toArray(Long[]::new)); expectConfig.put("nothing", new String[0]); final String dotConfig = "foo=\"bar\"\n" + "foos=[\"bar\",\"bar\",\"bar\"]\n" + "ones=L[\"1\",\"1\",\"1\"]\n" + "nothing=[]"; final Map<String, Object> simpleConfig = DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotConfig.getBytes(StandardCharsets.UTF_8)), "simple.config"); checkExpectedProperties(expectConfig, simpleConfig); }
@Test public void testReadDictionary_dotConfigWithComment() throws Exception { final Map<String, Object> expectConfig = new HashMap<>(); expectConfig.put("foo", "bar"); expectConfig.put("foos", Stream.of("bar", "bar", "bar").toArray(String[]::new)); expectConfig.put("ones", Stream.of(1L, 1L, 1L).toArray(Long[]::new)); expectConfig.put("nothing", new String[0]); final String dotConfig = "foo=\"bar\"\n" + "foos=[\"bar\",\"bar\",\"bar\"]\n" + "ones=L[\"1\",\"1\",\"1\"]\n" + "nothing=[]"; final String dotConfigWithComment = "# some comment\n" + dotConfig; final Map<String, Object> simpleConfig = DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotConfigWithComment.getBytes(StandardCharsets.UTF_8)), "simple.config"); checkExpectedProperties(expectConfig, simpleConfig); }
@Test public void testReadDictionary_dotProperties() throws Exception { final Map<String, Object> expectProperties = new HashMap<>(); expectProperties.put("foo", "bar"); expectProperties.put("foos", "bar,bar,bar"); expectProperties.put("ones", "1,1,1"); expectProperties.put("nothing", ""); final String dotProperties = "foo=bar\nfoos=bar,bar,bar\nones=1,1,1\nnothing="; final Map<String, Object> simpleProperties = DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotProperties.getBytes(StandardCharsets.UTF_8)), "simple.properties"); checkExpectedProperties(expectProperties, simpleProperties); }
@Test public void testReadDictionary_dotPropertiesXml() throws Exception { final Map<String, Object> expectProperties = new HashMap<>(); expectProperties.put("foo", "bar"); expectProperties.put("foos", "bar,bar,bar"); expectProperties.put("ones", "1,1,1"); expectProperties.put("nothing", ""); ByteArrayOutputStream propsXmlBytes = new ByteArrayOutputStream(); Properties srcPropertiesXml = new Properties(); srcPropertiesXml.putAll(expectProperties); srcPropertiesXml.storeToXML(propsXmlBytes, "generated by test", "UTF-8"); final String dotPropertiesXml = new String(propsXmlBytes.toByteArray(), StandardCharsets.UTF_8); final Map<String, Object> simplePropertiesXml = DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotPropertiesXml.getBytes(StandardCharsets.UTF_8)), "simple.xml"); checkExpectedProperties(expectProperties, simplePropertiesXml); }
@Test public void testReadDictionary_dotCfg() throws Exception { final Map<String, Object> expectProperties = new HashMap<>(); expectProperties.put("foo", "bar"); expectProperties.put("foos", "bar,bar,bar"); expectProperties.put("ones", "1,1,1"); expectProperties.put("nothing", ""); final String dotCfg = "foo=bar\nfoos=bar,bar,bar\nones=1,1,1\nnothing="; final Map<String, Object> simpleCfg = DefaultSlingSimulator.readDictionary( new ByteArrayInputStream(dotCfg.getBytes(StandardCharsets.UTF_8)), "simple.cfg"); checkExpectedProperties(expectProperties, simpleCfg); }