Package com.connect_group.thymeleaf.testing.hamcrest

Source Code of com.connect_group.thymeleaf.testing.hamcrest.HasClass

package com.connect_group.thymeleaf.testing.hamcrest;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import org.thymeleaf.dom.Element;

import com.connect_group.thymesheet.query.HtmlElement;

public class HasClass extends TypeSafeMatcher<HtmlElement> {
  private final Set<String> expectedClassNames;
 
  public HasClass(String className) {
    expectedClassNames = toSet(className);
  }
 
  @Override
  public boolean matchesSafely(HtmlElement item) {
    Element element = item.getElement();
    String classes = element.getAttributeValue("class");
    Set<String> classNames = toSet(classes);

    return classNames.containsAll(expectedClassNames);
  }

  private Set<String> toSet(String classNames) {
    Set<String> set = new HashSet<String>();
   
    if(classNames==null || classNames.length()==0) {
      set = Collections.emptySet();
    } else {
      set = new HashSet<String>();
      String[] names = classNames.split("\\s+");
      for(String name : names) {
        set.add(name);
      }
    }   
    return set;
  }
 
  @Override
  public void describeTo(Description description) {
    if(expectedClassNames.size() > 1) {
      description.appendText("has classes ");
    } else {
      description.appendText("has class ");
    }
   
    for(String className : expectedClassNames) {
      description.appendText(" ").appendValue(className)
    }
   
  }
 
 
}
TOP

Related Classes of com.connect_group.thymeleaf.testing.hamcrest.HasClass

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.