Best junit code snippet using junit.runner.BaseTestRunner.getPreference
Source:BaseTestRunner.java
...21{22 public static final String SUITE_METHODNAME = "suite";23 private static Properties fPreferences;24 static boolean fgFilterStack = true;25 static int fgMaxMessageLength = getPreference("maxmessage", fgMaxMessageLength);26 boolean fLoading = true;27 28 static boolean filterLine(String paramString)29 {30 String[] arrayOfString = new String[8];31 arrayOfString[0] = "junit.framework.TestCase";32 arrayOfString[1] = "junit.framework.TestResult";33 arrayOfString[2] = "junit.framework.TestSuite";34 arrayOfString[3] = "junit.framework.Assert.";35 arrayOfString[4] = "junit.swingui.TestRunner";36 arrayOfString[5] = "junit.awtui.TestRunner";37 arrayOfString[6] = "junit.textui.TestRunner";38 arrayOfString[7] = "java.lang.reflect.Method.invoke(";39 int i = 0;40 while (i < arrayOfString.length)41 {42 if (paramString.indexOf(arrayOfString[i]) > 0) {43 return true;44 }45 i += 1;46 }47 return false;48 }49 50 public static String getFilteredTrace(String paramString)51 {52 if (showStackRaw()) {53 return paramString;54 }55 StringWriter localStringWriter = new StringWriter();56 PrintWriter localPrintWriter = new PrintWriter(localStringWriter);57 BufferedReader localBufferedReader = new BufferedReader(new StringReader(paramString));58 try59 {60 for (;;)61 {62 String str = localBufferedReader.readLine();63 if (str == null) {64 break;65 }66 if (!filterLine(str)) {67 localPrintWriter.println(str);68 }69 }70 return localException.toString();71 }72 catch (Exception localException)73 {74 return paramString;75 }76 }77 78 public static String getFilteredTrace(Throwable paramThrowable)79 {80 StringWriter localStringWriter = new StringWriter();81 paramThrowable.printStackTrace(new PrintWriter(localStringWriter));82 return getFilteredTrace(localStringWriter.getBuffer().toString());83 }84 85 public static int getPreference(String paramString, int paramInt)86 {87 paramString = getPreference(paramString);88 if (paramString == null) {89 return paramInt;90 }91 try92 {93 int i = Integer.parseInt(paramString);94 paramInt = i;95 }96 catch (NumberFormatException paramString)97 {98 for (;;) {}99 }100 return paramInt;101 }102 103 public static String getPreference(String paramString)104 {105 return getPreferences().getProperty(paramString);106 }107 108 protected static Properties getPreferences()109 {110 if (fPreferences == null)111 {112 fPreferences = new Properties();113 fPreferences.put("loading", "true");114 fPreferences.put("filterstack", "true");115 readPreferences();116 }117 return fPreferences;118 }119 120 private static File getPreferencesFile()121 {122 return new File(System.getProperty("user.home"), "junit.properties");123 }124 125 /* Error */126 private static void readPreferences()127 {128 // Byte code:129 // 0: aconst_null130 // 1: astore_0131 // 2: new 165 java/io/FileInputStream132 // 5: dup133 // 6: invokestatic 167 junit/runner/BaseTestRunner:getPreferencesFile ()Ljava/io/File;134 // 9: invokespecial 170 java/io/FileInputStream:<init> (Ljava/io/File;)V135 // 12: astore_1136 // 13: new 128 java/util/Properties137 // 16: dup138 // 17: invokestatic 126 junit/runner/BaseTestRunner:getPreferences ()Ljava/util/Properties;139 // 20: invokespecial 173 java/util/Properties:<init> (Ljava/util/Properties;)V140 // 23: invokestatic 176 junit/runner/BaseTestRunner:setPreferences (Ljava/util/Properties;)V141 // 26: invokestatic 126 junit/runner/BaseTestRunner:getPreferences ()Ljava/util/Properties;142 // 29: aload_1143 // 30: invokevirtual 180 java/util/Properties:load (Ljava/io/InputStream;)V144 // 33: return145 // 34: astore_1146 // 35: aload_0147 // 36: ifnull -3 -> 33148 // 39: aload_0149 // 40: invokevirtual 185 java/io/InputStream:close ()V150 // 43: return151 // 44: astore_0152 // 45: return153 // 46: astore_0154 // 47: aload_1155 // 48: astore_0156 // 49: goto -14 -> 35157 // Local variable table:158 // start length slot name signature159 // 1 39 0 localObject1 Object160 // 44 1 0 localIOException1 IOException161 // 46 1 0 localIOException2 IOException162 // 48 1 0 localObject2 Object163 // 12 18 1 localFileInputStream java.io.FileInputStream164 // 34 14 1 localIOException3 IOException165 // Exception table:166 // from to target type167 // 2 13 34 java/io/IOException168 // 39 43 44 java/io/IOException169 // 13 33 46 java/io/IOException170 }171 172 public static void savePreferences()173 throws IOException174 {175 FileOutputStream localFileOutputStream = new FileOutputStream(getPreferencesFile());176 try177 {178 getPreferences().store(localFileOutputStream, "");179 return;180 }181 finally182 {183 localFileOutputStream.close();184 }185 }186 187 public static void setPreference(String paramString1, String paramString2)188 {189 getPreferences().put(paramString1, paramString2);190 }191 192 protected static void setPreferences(Properties paramProperties)193 {194 fPreferences = paramProperties;195 }196 197 protected static boolean showStackRaw()198 {199 return (!getPreference("filterstack").equals("true")) || (!fgFilterStack);200 }201 202 public static String truncate(String paramString)203 {204 String str = paramString;205 if (fgMaxMessageLength != -1)206 {207 str = paramString;208 if (paramString.length() > fgMaxMessageLength) {209 str = paramString.substring(0, fgMaxMessageLength) + "...";210 }211 }212 return str;213 }214 215 public void addError(Test paramTest, Throwable paramThrowable)216 {217 try218 {219 testFailed(1, paramTest, paramThrowable);220 return;221 }222 finally223 {224 paramTest = finally;225 throw paramTest;226 }227 }228 229 public void addFailure(Test paramTest, AssertionFailedError paramAssertionFailedError)230 {231 try232 {233 testFailed(2, paramTest, paramAssertionFailedError);234 return;235 }236 finally237 {238 paramTest = finally;239 throw paramTest;240 }241 }242 243 protected void clearStatus() {}244 245 public String elapsedTimeAsString(long paramLong)246 {247 return NumberFormat.getInstance().format(paramLong / 1000.0D);248 }249 250 public void endTest(Test paramTest)251 {252 try253 {254 testEnded(paramTest.toString());255 return;256 }257 finally258 {259 paramTest = finally;260 throw paramTest;261 }262 }263 264 public String extractClassName(String paramString)265 {266 String str = paramString;267 if (paramString.startsWith("Default package for")) {268 str = paramString.substring(paramString.lastIndexOf(".") + 1);269 }270 return str;271 }272 273 public Test getTest(String paramString)274 {275 if (paramString.length() <= 0)276 {277 clearStatus();278 paramString = null;279 }280 for (;;)281 {282 return paramString;283 try284 {285 Class localClass = loadSuiteClass(paramString);286 try287 {288 String str;289 Object localObject = (Test)paramString.invoke(null, (Object[])new Class[0]);290 paramString = (String)localObject;291 if (localObject != null)292 {293 clearStatus();294 return (Test)localObject;295 }296 }297 catch (InvocationTargetException paramString)298 {299 runFailed("Failed to invoke suite():" + paramString.getTargetException().toString());300 return null;301 }302 catch (IllegalAccessException paramString)303 {304 runFailed("Failed to invoke suite():" + paramString.toString());305 }306 }307 catch (ClassNotFoundException localClassNotFoundException)308 {309 try310 {311 paramString = localClass.getMethod("suite", new Class[0]);312 if (Modifier.isStatic(paramString.getModifiers())) {313 break label141;314 }315 runFailed("Suite() method must be static");316 return null;317 }318 catch (Exception paramString)319 {320 clearStatus();321 return new TestSuite((Class)localObject);322 }323 localClassNotFoundException = localClassNotFoundException;324 str = localClassNotFoundException.getMessage();325 localObject = str;326 if (str == null) {327 localObject = paramString;328 }329 runFailed("Class not found \"" + (String)localObject + "\"");330 return null;331 }332 catch (Exception paramString)333 {334 runFailed("Error: " + paramString.toString());335 return null;336 }337 }338 label141:339 return null;340 }341 342 protected Class<?> loadSuiteClass(String paramString)343 throws ClassNotFoundException344 {345 return Class.forName(paramString);346 }347 348 protected String processArguments(String[] paramArrayOfString)349 {350 String str = null;351 int i = 0;352 if (i < paramArrayOfString.length)353 {354 if (paramArrayOfString[i].equals("-noloading")) {355 setLoading(false);356 }357 for (;;)358 {359 i += 1;360 break;361 if (paramArrayOfString[i].equals("-nofilterstack"))362 {363 fgFilterStack = false;364 }365 else366 {367 if (paramArrayOfString[i].equals("-c"))368 {369 if (paramArrayOfString.length > i + 1) {370 str = extractClassName(paramArrayOfString[(i + 1)]);371 }372 for (;;)373 {374 i += 1;375 break;376 System.out.println("Missing Test class name");377 }378 }379 str = paramArrayOfString[i];380 }381 }382 }383 return str;384 }385 386 protected abstract void runFailed(String paramString);387 388 public void setLoading(boolean paramBoolean)389 {390 this.fLoading = paramBoolean;391 }392 393 public void startTest(Test paramTest)394 {395 try396 {397 testStarted(paramTest.toString());398 return;399 }400 finally401 {402 paramTest = finally;403 throw paramTest;404 }405 }406 407 public abstract void testEnded(String paramString);408 409 public abstract void testFailed(int paramInt, Test paramTest, Throwable paramThrowable);410 411 public abstract void testStarted(String paramString);412 413 protected boolean useReloadingTestSuiteLoader()414 {415 return (getPreference("loading").equals("true")) && (this.fLoading);416 }417}418/* Location: /home/dev/Downloads/apk/dex2jar-2.0/crumby-dex2jar.jar!/junit/runner/BaseTestRunner.class419 * Java compiler version: 6 (50.0)420 * JD-Core Version: 0.7.1421 */...
getPreference
Using AI Code Generation
1import junit.runner.BaseTestRunner;2String defaultTestSuiteName = BaseTestRunner.getPreference("default_test_suite");3System.out.println("Default Test Suite Name: " + defaultTestSuiteName);4defaultTestSuiteName = "com.example.junit.AllTests";5System.out.println("Default Test Suite Name: " + defaultTestSuiteName);6import junit.runner.BaseTestRunner;7String defaultTestSuiteName = BaseTestRunner.getPreference("default_test_suite");8System.out.println("Default Test Suite Name: " + defaultTestSuiteName);9defaultTestSuiteName = "com.example.junit.AllTests";10System.out.println("Default Test Suite Name: " + defaultTestSuiteName);11import junit.runner.BaseTestRunner;12String defaultTestSuiteName = BaseTestRunner.getPreference("default_test_suite");13System.out.println("Default Test Suite Name: " + defaultTestSuiteName);14defaultTestSuiteName = "com.example.junit.AllTests";15System.out.println("Default Test Suite Name: " + defaultTestSuiteName);16import junit.runner.BaseTestRunner;17String defaultTestSuiteName = BaseTestRunner.getPreference("default_test_suite");18System.out.println("Default Test Suite Name: " + defaultTestSuiteName);19defaultTestSuiteName = "com.example.junit.AllTests";20System.out.println("Default Test Suite Name: " + defaultTestSuiteName);21import junit.runner.BaseTestRunner;22String defaultTestSuiteName = BaseTestRunner.getPreference("default_test_suite");23System.out.println("Default Test
getPreference
Using AI Code Generation
1import junit.runner.BaseTestRunner;2import java.util.prefs.Preferences;3public class TestRunner {4 public static void main(String[] args) {5 Preferences prefs = Preferences.userRoot().node(BaseTestRunner.PREFS_NODE);6 String prefValue = prefs.get("test", "default");7 System.out.println(prefValue);8 }9}10package com.journaldev.java;11import java.util.prefs.Preferences;12public class PreferencesDemo {13 public static void main(String[] args) {14 Preferences prefs = Preferences.userRoot().node("com/journaldev/java");15 prefs.put("name", "JournalDev");16 prefs.putInt("hits", 1000);17 prefs.putBoolean("active", true);18 System.out.println(prefs.get("name", "Not Found"));19 System.out.println(prefs.get("website", "Not Found"));20 System.out.println(prefs.getInt("hits", 0));21 System.out.println(prefs.getBoolean("active", false));22 }23}24package com.journaldev.java;25import java.util.prefs.Preferences;26public class PreferencesDemo {27 public static void main(String[] args) {28 Preferences prefs = Preferences.userRoot().node("com/journaldev/java");29 prefs.remove("name");30 prefs.remove("website");31 prefs.remove("hits");32 prefs.remove("active");33 System.out.println(prefs.get("name", "Not Found"));34 System.out.println(prefs.get("website", "Not Found"));35 System.out.println(prefs.getInt("hits", 0));36 System.out.println(prefs.getBoolean("active", false));37 }38}39package com.journaldev.java;40import java.util.prefs.Preferences;41public class PreferencesDemo {42 public static void main(String[] args) {43 Preferences prefs = Preferences.systemRoot().node("com/journaldev/java");44 prefs.put("name", "JournalDev");45 prefs.put("website", "
getPreference
Using AI Code Generation
1import org.junit.runner.BaseTestRunner;2public class TestRunner {3 public static void main(String[] args) {4 String value = BaseTestRunner.getPreference("junit.runner.useprefs");5 System.out.println(value);6 }7}
LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!