1 /*
   2  * Copyright (c) 2007, 2017 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation. Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.jemmy.resources;
  26 
  27 /**
  28  *
  29  * @author shura
  30  */
  31 public interface StringComparePolicy {
  32 
  33     public static final StringComparePolicy EXACT = new ComparePolicy(true, true);
  34     public static final StringComparePolicy SUBSTRING = new ComparePolicy(false, true);
  35 
  36     public boolean compare(String golden, String value);
  37 
  38     static class ComparePolicy implements StringComparePolicy {
  39 
  40         boolean ce;
  41         boolean cc;
  42 
  43         /**
  44          * @param ce if true then entire strings are compared, if false golden
  45          * could be a substring of a value
  46          * @param cc case sensitive comparison policy
  47          */
  48         public ComparePolicy(boolean ce, boolean cc) {
  49             this.cc = cc;
  50             this.ce = ce;
  51         }
  52 
  53         public boolean compare(String golden, String value) {
  54             if (value == null) {
  55                 return golden == null;
  56             } else if (golden == null) {
  57                 return !ce;
  58             }
  59             String g, v;
  60             if (cc) {
  61                 g = golden;
  62                 v = value;
  63             } else {
  64                 g = golden.toUpperCase();
  65                 v = value.toUpperCase();
  66             }
  67             if (ce) {
  68                 return v.equals(g);
  69             } else {
  70                 return v.contains(g);
  71             }
  72         }
  73 
  74         @Override
  75         public String toString() {
  76             return "case " + (!cc ? "in" : "") + "sensitive" + (!ce ? " as substring" : "");
  77         }
  78     }
  79 }