| 1 | /* |
| 2 | * JTiger Unit Testing Framework for J2SE 1.5 |
| 3 | * Copyright (C) 2005 Tony Morris |
| 4 | * |
| 5 | * This software is licenced under the |
| 6 | * Common Public Licence version 1.0 |
| 7 | * http://www.opensource.org/licenses/cpl1.0.php |
| 8 | * |
| 9 | * You received a copy of this licence with this software. |
| 10 | */ |
| 11 | package org.jtiger.framework; |
| 12 | |
| 13 | import java.lang.reflect.Method; |
| 14 | import java.util.regex.Pattern; |
| 15 | |
| 16 | final class MethodCategoryFactory |
| 17 | { |
| 18 | private MethodCategoryFactory() |
| 19 | { |
| 20 | |
| 21 | } |
| 22 | |
| 23 | static MethodCategory newMethodCategory() |
| 24 | { |
| 25 | return new MethodCategoryImpl(); |
| 26 | } |
| 27 | |
| 28 | private static final class MethodCategoryImpl implements MethodCategory |
| 29 | { |
| 30 | MethodCategoryImpl() |
| 31 | { |
| 32 | |
| 33 | } |
| 34 | |
| 35 | public boolean isInAnyCategories(final Method m, final ReadOnlyArray<String> categories) throws NullPointerException |
| 36 | { |
| 37 | if(m == null) |
| 38 | { |
| 39 | throw new NullPointerException(); |
| 40 | } |
| 41 | |
| 42 | if(categories == null || categories.length() == 0) |
| 43 | { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | for(String category : categories) |
| 48 | { |
| 49 | if(isInAnyCategory(m, category)) |
| 50 | { |
| 51 | return true; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | private boolean isInAnyCategory(final Method m, final String category) |
| 59 | { |
| 60 | Category c = m.getAnnotation(Category.class); |
| 61 | final Pattern p = Pattern.compile(category); |
| 62 | |
| 63 | if(c != null) |
| 64 | { |
| 65 | for(String s : c.value()) |
| 66 | { |
| 67 | if(p.matcher(s).matches()) |
| 68 | { |
| 69 | return true; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | c = m.getDeclaringClass().getAnnotation(Category.class); |
| 75 | |
| 76 | if(c != null) |
| 77 | { |
| 78 | for(String s : c.value()) |
| 79 | { |
| 80 | if(p.matcher(s).matches()) |
| 81 | { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | } |