@Test
  public void testStringTemplateComponent() {
    final MustacheFactory factory = new RapidoidMustacheFactory();

    ITemplate template = new MustacheStringTemplate(factory, "(~x~)-(~y~)");

    eq(template.render(U.map("x", 1), U.map("y", "2")), "1-2");
  }
  @Test
  public void testFileTemplateComponent() {
    final MustacheFactory factory = new RapidoidMustacheFactory();

    ITemplate template = new MustacheFileTemplate(factory, "main.html");

    eq(template.render(U.map("x", 1), U.map("y", "s")), "A-s-B-s-:1");
  }
Beispiel #3
0
  public Map<String, Object> loadMetadata(InputStream in) {
    globalLock();

    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(in));

      String line = reader.readLine();
      U.must(line != null, "Missing meta-data at the first line in the database file!");

      Map<String, Object> meta = U.map();
      data.serializer.deserialize(line.getBytes(), meta);

      reader.close();

      return meta;

    } catch (IOException e) {
      throw new RuntimeException("Cannot load meta-data from database!", e);
    } finally {
      globalUnlock();
    }
  }
Beispiel #4
0
  public void loadFrom(InputStream in) {
    globalLock();

    try {
      data.data.clear();

      BufferedReader reader = new BufferedReader(new InputStreamReader(in));

      String line = reader.readLine();
      byte[] bytes = line.getBytes();

      U.must(line != null, "Missing meta-data at the first line in the database file!");

      Map<String, Object> meta = U.map();
      data.serializer.deserialize(bytes, meta);

      Log.info(
          "Database meta-data",
          META_TIMESTAMP,
          meta.get(META_TIMESTAMP),
          META_UPTIME,
          meta.get(META_UPTIME));

      while ((line = reader.readLine()) != null) {
        bytes = line.getBytes();
        Map<String, Object> map = U.map();
        data.serializer.deserialize(bytes, map);

        Object idObj = map.get(ID);
        U.must(idObj != null, "Found DB record without ID: %s", line);

        long id = Cls.convert(idObj, Long.class);
        String className = ((String) map.get("_class"));

        String[] nameParts = className.split("\\.");
        String simpleName = nameParts[nameParts.length - 1];
        List<Class<?>> classes = Scan.byName(simpleName, null, null);

        if (classes.size() == 1) {
          Class<?> type = classes.get(0);
          data.data.put(id, new Rec(type, bytes));

          if (id > data.ids.get()) {
            data.ids.set(id);
          }

        } else {
          if (classes.isEmpty()) {
            Log.error("Cannot find the class of a DB record!", "id", id, "class", className);
          } else {
            Log.error("Found more than 1 class of a DB record!", "id", id, "class", className);
          }
        }
      }

      data.prevData = new ConcurrentSkipListMap<Long, Rec>(data.data);

      reader.close();

    } catch (IOException e) {
      throw new RuntimeException("Cannot load database!", e);
    } finally {
      globalUnlock();
    }
  }
  @Test
  public void testStringTemplatesAPI() {
    Plugins.register(new MustacheTemplatesPlugin());

    eq(Templates.fromString("(~x~)-(~y~)").render(U.map("x", 1), U.map("y", "2")), "1-2");
  }
  @Test
  public void testFileTemplatesAPI() {
    Plugins.register(new MustacheTemplatesPlugin());

    eq(Templates.fromFile("main.html").render(U.map("x", 1), U.map("y", "s")), "A-s-B-s-:1");
  }