001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.junit.bct; 018 019import static org.apache.juneau.commons.utils.Utils.*; 020 021import java.util.*; 022import java.util.function.*; 023 024import org.opentest4j.*; 025 026/** 027 * Bean-Centric Testing utility methods. 028 * 029 * <p> 030 * This class contains static utility methods specific to the Bean-Centric Testing framework. 031 * For general-purpose utility methods, use the classes in {@code org.apache.juneau.commons.utils} package. 032 * 033 * <h5 class='section'>See Also:</h5> 034 * <ul> 035 * <li class='jc'>{@link org.apache.juneau.commons.utils.Utils} - General utility methods 036 * <li class='jc'>{@link org.apache.juneau.commons.utils.AssertionUtils} - Argument validation methods 037 * <li class='jc'>{@link org.apache.juneau.commons.utils.StringUtils} - String manipulation methods 038 * </ul> 039 */ 040public class BctUtils { 041 042 // BCT-specific methods 043 044 /** 045 * Creates an {@link AssertionFailedError} for failed equality assertions. 046 * 047 * <p>This method constructs a properly formatted assertion failure with expected and actual values 048 * for use in test frameworks. The message follows JUnit's standard format for assertion failures. 049 * 050 * <h5 class='section'>Example:</h5> 051 * <p class='bjava'> 052 * <jk>if</jk> (!<jsm>eq</jsm>(<jv>expected</jv>, <jv>actual</jv>)) { 053 * <jk>throw</jk> <jsm>assertEqualsFailed</jsm>(<jv>expected</jv>, <jv>actual</jv>, () -> <js>"Custom context message with arg {0}"</js>, <jv>arg</jv>); 054 * } 055 * </p> 056 * 057 * @param expected The expected value. 058 * @param actual The actual value that was encountered. 059 * @param messageSupplier Optional supplier for additional context message. 060 * @return A new {@link AssertionFailedError} with formatted message and values. 061 */ 062 public static AssertionFailedError assertEqualsFailed(Object expected, Object actual, Supplier<String> messageSupplier) { 063 return new AssertionFailedError(opt(messageSupplier).map(x -> x.get()).orElse("Equals assertion failed.") + f(" ==> expected: <{0}> but was: <{1}>", expected, actual), expected, actual); 064 } 065 066 /** 067 * Tokenizes a string into a list of {@link NestedTokenizer.Token} objects. 068 * 069 * <p>This method delegates to {@link NestedTokenizer#tokenize(String)} to parse 070 * structured field strings into tokens. It's commonly used for parsing field lists 071 * and nested property expressions. 072 * 073 * <h5 class='section'>Example:</h5> 074 * <p class='bjava'> 075 * <jk>var</jk> <jv>tokens</jv> = <jsm>tokenize</jsm>(<js>"name,address{street,city},age"</js>); 076 * <jc>// Parses nested field expressions</jc> 077 * </p> 078 * 079 * @param fields The field string to tokenize. 080 * @return A list of parsed tokens. 081 * @see NestedTokenizer#tokenize(String) 082 */ 083 public static List<NestedTokenizer.Token> tokenize(String fields) { 084 return NestedTokenizer.tokenize(fields); 085 } 086 087 private BctUtils() {} 088}