Package br.com.objectos.way.code

Source Code of br.com.objectos.way.code.WayCodeAsserts

/*
* Copyright 2013 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.code;

import static com.google.common.collect.Lists.newArrayList;
import static org.testng.Assert.fail;

import java.io.IOException;
import java.net.URL;
import java.util.List;

import com.google.common.base.Charsets;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources;

/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
class WayCodeAsserts {

  private WayCodeAsserts() {
  }

  public static void assertLinesToResource(List<String> lines, String name) {
    List<String> prova = linesOf(name);
    assertLines(lines, prova);
  }

  public static void assertLines(String hasLines, String exp) {
    Splitter splitter = Splitter.on('\n');
    List<String> lines = splitter.splitToList(hasLines);
    List<String> asser = splitter.splitToList(exp);
    assertLines(lines, asser);
  }

  public static void assertLines(List<String> lines, List<String> prova) {
    List<String> invalids = newArrayList();

    for (int i = 0; i < prova.size(); i++) {
      String exp = prova.get(i);
      String res = null;
      if (i < lines.size()) {
        res = lines.get(i);
      }

      LineAssert line = new LineAssert(i, exp, res);
      if (!line.valid()) {
        invalids.add(line.toString());
      }
    }

    if (!invalids.isEmpty()) {
      String output = Joiner.on("\n").join(invalids);
      System.out.println(output);
      output = Joiner.on("\n").join(lines);
      System.out.println(output);
      fail();
    }
  }

  public static String contentsOf(String name) {
    return toString(name);
  }

  public static String toString(String name) {
    try {
      URL url = Resources.getResource(WayCodeAsserts.class, name);
      return Resources.toString(url, Charsets.UTF_8);
    } catch (IOException e) {
      return "";
    }
  }

  private static List<String> linesOf(String name) {
    try {
      URL url = Resources.getResource(WayCodeAsserts.class, name);
      return Resources.readLines(url, Charsets.UTF_8);
    } catch (IOException e) {
      return ImmutableList.of();
    }
  }

}
TOP

Related Classes of br.com.objectos.way.code.WayCodeAsserts

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.