public void testNullDescription() {
   Constructor constructor = new Constructor();
   try {
     constructor.addTypeDescription(null);
     fail("Description is required.");
   } catch (Exception e) {
     assertEquals("TypeDescription is required.", e.getMessage());
   }
 }
  private Yaml prepareLoader(ClassLoader classLoader) {
    final Constructor constructor = new MyConstructor(Holder.class, classLoader);
    final TypeDescription typeDescription = new TypeDescription(Holder.class);
    typeDescription.putListPropertyType("prototypes", DefaultEntityPrototype.class);
    constructor.addTypeDescription(typeDescription);

    final Loader loader = new Loader(constructor);
    final Yaml yaml = new Yaml(loader);
    return yaml;
  }
 public void testLoadClassTag() {
   Constructor constructor = new Constructor();
   constructor.addTypeDescription(new TypeDescription(Car.class, "!car"));
   Yaml yaml = new Yaml(constructor);
   String source = Util.getLocalResource("constructor/car-without-tags.yaml");
   Car car = (Car) yaml.load(source);
   assertEquals("12-XP-F4", car.getPlate());
   List<Wheel> wheels = car.getWheels();
   assertNotNull(wheels);
   assertEquals(5, wheels.size());
 }
  public static WeatherStatsConfigInfo getConfiguration(String configFile) throws Exception {

    BaseConstructor constructor = new Constructor(WeatherStatsConfigInfo.class);
    TypeDescription configDescription = new TypeDescription(WeatherStatsConfigInfo.class);
    ((Constructor) constructor).addTypeDescription(configDescription);
    Yaml yaml = new Yaml(new Loader(constructor));

    WeatherStatsConfigInfo info =
        (WeatherStatsConfigInfo) yaml.load(new FileInputStream(new File(configFile)));
    return info;
  }
Example #5
0
  public static XmlSuite parse(String filePath, InputStream is) throws FileNotFoundException {
    Constructor constructor = new TestNGConstructor(XmlSuite.class);
    {
      TypeDescription suiteDescription = new TypeDescription(XmlSuite.class);
      suiteDescription.putListPropertyType("packages", XmlPackage.class);
      suiteDescription.putListPropertyType("listeners", String.class);
      suiteDescription.putListPropertyType("tests", XmlTest.class);
      suiteDescription.putListPropertyType("method-selectors", XmlMethodSelector.class);
      constructor.addTypeDescription(suiteDescription);
    }

    {
      TypeDescription testDescription = new TypeDescription(XmlTest.class);
      testDescription.putListPropertyType("classes", XmlClass.class);
      testDescription.putMapPropertyType("metaGroups", String.class, List.class);
      testDescription.putListPropertyType("method-selectors", XmlMethodSelector.class);
      constructor.addTypeDescription(testDescription);
    }

    org.yaml.snakeyaml.Yaml y = new org.yaml.snakeyaml.Yaml(constructor);
    if (is == null) is = new FileInputStream(new File(filePath));
    XmlSuite result = (XmlSuite) y.load(is);

    result.setFileName(filePath);
    // DEBUG
    //    System.out.println("[Yaml] " + result.toXml());

    // Adjust XmlTest parents and indices
    for (XmlTest t : result.getTests()) {
      t.setSuite(result);
      int index = 0;
      for (XmlClass c : t.getClasses()) {
        c.setIndex(index++);
      }
    }

    return result;
  }
  public Config loadConfig(URL url) throws ConfigurationException {
    try {
      logger.debug("Loading settings from {}", url);
      byte[] configBytes;
      try (InputStream is = url.openStream()) {
        configBytes = ByteStreams.toByteArray(is);
      } catch (IOException e) {
        // getStorageConfigURL should have ruled this out
        throw new AssertionError(e);
      }

      logConfig(configBytes);

      Constructor constructor = new CustomConstructor(Config.class);
      MissingPropertiesChecker propertiesChecker = new MissingPropertiesChecker();
      constructor.setPropertyUtils(propertiesChecker);
      Yaml yaml = new Yaml(constructor);
      Config result = yaml.loadAs(new ByteArrayInputStream(configBytes), Config.class);
      propertiesChecker.check();
      return result;
    } catch (YAMLException e) {
      throw new ConfigurationException("Invalid yaml: " + url, e);
    }
  }