Beispiel #1
0
  @Test
  public void Conversions_toMap_DoesNotAllowReplacement() {
    ObjectMapper o = new ObjectMapperImpl();

    Stuffing stuffing = new Stuffing();
    stuffing.name = "i'm Stuffed!";
    stuffing.stuff = Stuff.THIS_KIND_OF_STUFF;

    stuffing.stuffed = new Stuffing.Stuffed();
    stuffing.stuffed.dateStuffed = new Date();

    // Make it a string to start with. (pretend this is JSON coming over the wire as a string, for
    // example
    String jsonVersion = o.toJson(stuffing);

    // Now lets make it a map to grab stuff and change stuff
    Map<String, Object> requestObject = o.fromJson(jsonVersion, Map.class);

    puts("Before", requestObject);

    /* It is not a real map. It is a facade over a index overlay. */

    requestObject = new HashMap<>(requestObject);

    Map<String, Object> stuffed = new HashMap<>((Map<String, Object>) requestObject.get("stuffed"));
    stuffed.put("dateStuffed", System.currentTimeMillis());
    stuffed.put("bacon", "yummy");

    requestObject.put("stuffed", stuffed);

    puts("After", requestObject);
  }
Beispiel #2
0
  @Test
  public void Conversions_toMap_DoesEnumsFunnyTest() {
    Stuffing stuffing = new Stuffing();
    stuffing.name = "i'm Stuffed!";
    stuffing.stuff = Stuff.THIS_KIND_OF_STUFF;

    Map<String, Object> problemMap = Conversions.toMap(stuffing);

    puts(problemMap);

    assertNotNull("What?", problemMap);
    assertEquals(
        "Map should be a 2 null are ignored. Default value of null is nothing.",
        2,
        problemMap.size()); // nulls are ignored

    // Map sets perceived "Wrong" value for enum, it is the right value
    assertEquals("This works", Stuff.THIS_KIND_OF_STUFF, problemMap.get("stuff"));

    Stuffing.Stuffed hAndRPuffAndStuff = new Stuffing.Stuffed();

    hAndRPuffAndStuff.dateStuffed = new Date();

    stuffing.stuffed = hAndRPuffAndStuff;

    Map<String, Object> noProblemMap = Conversions.toMap(stuffing);

    puts(noProblemMap);

    assertNotNull("What?", noProblemMap);
    assertEquals("Map is now 3.", 3, noProblemMap.size());
  }
Beispiel #3
0
  @Test
  public void testForIssue47() {
    Map<String, Object> map =
        (Map<String, Object>) fromJson("{\"empty\":\"\",\"docId\":111,\"serviceName\":\"cafe\"}");
    puts(map);
    puts(toJson(map));

    boolean ok =
        toJson(map).equals("{\"empty\":\"\",\"docId\":111,\"serviceName\":\"cafe\"}")
            || die(toJson(map).equals("{\"empty\":\"\",\"docId\":111,\"serviceName\":\"cafe\"}"));
  }
Beispiel #4
0
  @Test
  public void parseNumber() {
    int i = jsonParserAndMapper.parseInt("123");
    boolean ok = i == 123 || die("" + i);

    i = jsonParserAndMapper.parseInt("123".getBytes(StandardCharsets.UTF_8));
    ok = i == 123 || die("" + i);

    i = jsonParserAndMapper.parseByte("123");
    ok = i == 123 || die("" + i);

    i = jsonParserAndMapper.parseShort("123");
    ok = i == 123 || die("" + i);

    i = (int) jsonParserAndMapper.parseDouble("123");
    ok = i == 123 || die("" + i);

    i = (int) jsonParserAndMapper.parseFloat("123");
    ok = i == 123 || die("" + i);

    i = (int) jsonParserAndMapper.parseLong("123");
    ok = i == 123 || die("" + i);

    puts(ok);
  }
  @Override
  public boolean objectField(int index, Map<String, Object> map, CharSequence name, Object value) {

    if (inMeta
        && name != null
        && name.toString().equals("namespace")
        && value instanceof CharSequence
        && !value.toString().equals(namespace)) {

      return continueParse = false;
    }

    if (name != null && name.toString().equals("META")) {

      Map<String, Object> meta = toMap(value);
      if (meta.containsKey("include")) {
        include = toList(meta.get("include"));
        puts("include", include);
      }
      inMeta = false;
      if (events != null) {
        continueParse = events.parsedMeta(meta);
      }
    }

    return continueParse;
  }
  public static DataStoreClientConfig load() {

    puts("Config for data store client", FILE_LOCATION);

    if (IO.exists(FILE_LOCATION)) {
      try {
        return new JsonParserFactory()
            .create()
            .parseFile(DataStoreClientConfig.class, FILE_LOCATION);
      } catch (Exception ex) {
        Exceptions.handle(
            ex, "Unable to read config file", FILE_LOCATION, "for data store client config");
        return null;
      }
    } else {

      puts("WARNING", FILE_LOCATION, "does not exist for data store client config!!!");
      return new DataStoreClientConfig();
    }
  }
Beispiel #7
0
  @Test
  public void testFileResources2() throws Exception {
    String someResource = "/org/node/resource.txt";

    File file = new File("files/node-1.0-SNAPSHOT.jar");
    URL url1 = file.getAbsoluteFile().toURI().toURL();
    URL url2 = new File("files/invoke-1.0-SNAPSHOT.jar").getAbsoluteFile().toURI().toURL();

    URLClassLoader loader = new URLClassLoader(new URL[] {url1, url2});

    final List<String> resourcePaths = Classpaths.listFromClassLoader(loader, someResource);

    List<Path> list = IO.paths("classpath://org/node/");

    // List<Path> list = Classpaths.pathsFromClassLoader(loader, someResource);

    puts(multiply('-', 10), "Path ");
    for (Path path : list) {

      puts(path, path.getFileSystem(), path.getClass().getName());
      if (path.toString().endsWith(".txt")) {
        puts(IO.readPath(path));
      }
    }

    puts(multiply('-', 10), "String Path ");
    List<String> slist = IO.list("classpath://org/node/");

    for (String spath : slist) {

      puts(spath);
      if (spath.toString().endsWith(".txt")) {
        puts(IO.readResource(spath));
      }
    }
  }