Package com.cloudera.cdk.morphline.stdlib

Source Code of com.cloudera.cdk.morphline.stdlib.GlobPatternTest

// copied from hadoop-common-2.0.0
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you 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 com.cloudera.cdk.morphline.stdlib;

import static org.junit.Assert.assertTrue;

import java.util.regex.PatternSyntaxException;

import org.junit.Test;

import com.cloudera.cdk.morphline.shaded.org.apache.hadoop.fs.GlobPattern;

/**
* Tests for glob patterns
*/
public class GlobPatternTest {
  private void assertMatch(boolean yes, String glob, String...input) {
    GlobPattern pattern = new GlobPattern(glob);

    for (String s : input) {
      boolean result = pattern.matches(s);
      assertTrue(glob +" should"+ (yes ? "" : " not") +" match "+ s,
                 yes ? result : !result);
    }
  }

  private void shouldThrow(String... globs) {
    for (String glob : globs) {
      try {
        GlobPattern.compile(glob);
      }
      catch (PatternSyntaxException e) {
        //e.printStackTrace();
        continue;
      }
      assertTrue("glob "+ glob +" should throw", false);
    }
  }

  @Test public void testValidPatterns() {
    assertMatch(true, "*", "^$", "foo", "bar");
    assertMatch(true, "?", "?", "^", "[", "]", "$");
    assertMatch(true, "foo*", "foo", "food", "fool");
    assertMatch(true, "f*d", "fud", "food");
    assertMatch(true, "*d", "good", "bad");
    assertMatch(true, "\\*\\?\\[\\{\\\\", "*?[{\\");
    assertMatch(true, "[]^-]", "]", "-", "^");
    assertMatch(true, "]", "]");
    assertMatch(true, "^.$()|+", "^.$()|+");
    assertMatch(true, "[^^]", ".", "$", "[", "]");
    assertMatch(false, "[^^]", "^");
    assertMatch(true, "[!!-]", "^", "?");
    assertMatch(false, "[!!-]", "!", "-");
    assertMatch(true, "{[12]*,[45]*,[78]*}", "1", "2!", "4", "42", "7", "7$");
    assertMatch(false, "{[12]*,[45]*,[78]*}", "3", "6", "9��");
    assertMatch(true, "}", "}");
  }

  @Test public void testInvalidPatterns() {
    shouldThrow("[", "[[]]", "[][]", "{", "\\");
  }
}
TOP

Related Classes of com.cloudera.cdk.morphline.stdlib.GlobPatternTest

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.