### Question:
Subscription { public static void validateTopicPattern(String pattern) throws BrokerException { if (StringUtils.isBlank(pattern)) { throw new BrokerException(ErrorCode.PATTERN_INVALID); } if (pattern.length() > WeEventConstants.TOPIC_NAME_MAX_LENGTH) { throw new BrokerException(ErrorCode.TOPIC_EXCEED_MAX_LENGTH); } for (char x : pattern.toCharArray()) { if (x < 32 || x > 128) { throw new BrokerException(ErrorCode.TOPIC_CONTAIN_INVALID_CHAR); } } String[] layer = pattern.split(WeEvent.LAYER_SEPARATE); if (pattern.contains(WeEvent.WILD_CARD_ONE_LAYER)) { for (String x : layer) { if (x.contains(WeEvent.WILD_CARD_ONE_LAYER) && !x.equals(WeEvent.WILD_CARD_ONE_LAYER)) { throw new BrokerException(ErrorCode.PATTERN_INVALID); } } } else if (pattern.contains(WeEvent.WILD_CARD_ALL_LAYER)) { if (StringUtils.countMatches(pattern, WeEvent.WILD_CARD_ALL_LAYER) != 1) { throw new BrokerException(ErrorCode.PATTERN_INVALID); } if (!layer[layer.length - 1].equals(WeEvent.WILD_CARD_ALL_LAYER)) { throw new BrokerException(ErrorCode.PATTERN_INVALID); } } else { throw new BrokerException(ErrorCode.PATTERN_INVALID); } } Subscription(IBlockChain blockChain, String uuid, String[] topics, String groupId, String offset, String tag, IConsumer.ConsumerListener listener); Long getNotifyingEventCount(); Long getNotifiedEventCount(); Date getNotifyTimeStamp(); void dispatch(List<WeEvent> events, boolean mainLoop, Long blockHeight); synchronized void doStart(Executor executor); synchronized void doStop(); synchronized void stopHistory(); synchronized boolean tryStopHistory(Long mainLastBlock); static List<WeEvent> filter(List<WeEvent> from, String[] topics, String tag); static boolean isTopicPattern(String pattern); static void validateTopicPattern(String pattern); static boolean patternMatch(String topic, String pattern);  }### Answer:
@Test public void testValidateTopicPattern() throws Exception { Subscription.validateTopicPattern("#"); Subscription.validateTopicPattern("/#"); Subscription.validateTopicPattern("#/"); Subscription.validateTopicPattern("+"); Subscription.validateTopicPattern("/+"); Subscription.validateTopicPattern("+/"); Subscription.validateTopicPattern("com/#"); Subscription.validateTopicPattern("com/+"); Subscription.validateTopicPattern("com/+/test"); Assert.assertTrue(true); }
@Test public void testValidateTopicPattern1() { try { Subscription.validateTopicPattern("com/#/test"); Assert.fail(); } catch (BrokerException e) { Assert.assertEquals(e.getCode(), ErrorCode.PATTERN_INVALID.getCode()); } }
@Test public void testValidateTopicPattern3() { try { Subscription.validateTopicPattern("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); Assert.fail(); } catch (BrokerException e) { Assert.assertEquals(e.getCode(), ErrorCode.TOPIC_EXCEED_MAX_LENGTH.getCode()); } }