Best Powermock code snippet using samples.powermockito.junit4.jacoco.JacocoCoverageTest.copyFileUsingStream
Source:JacocoCoverageTest.java
...79 for (String className : TARGET) {80 instrumentClass(instr, className);81 }82 }83 private static void copyFileUsingStream(File source, File dest) throws IOException {84 InputStream is = null;85 OutputStream os = null;86 try {87 is = new FileInputStream(source);88 os = new FileOutputStream(dest, false);89 byte[] buffer = new byte[1024];90 int length;91 while ((length = is.read(buffer)) > 0) {92 os.write(buffer, 0, length);93 }94 } finally {95 if (is != null) {96 is.close();97 }98 if (os != null) {99 os.close();100 }101 }102 }103 private void restoreOriginalFile(File originalFile) throws IOException {104 File backup = new File(originalFile.getAbsolutePath() + ".bak");105 if (originalFile.exists()) {106 originalFile.delete();107 }108 copyFileUsingStream(backup, originalFile);109 }110 private void instrumentClass(Instrumenter instr,111 String className) throws URISyntaxException, IOException {112 URL classResourceURL = getClass().getResource(classNameToFileName(className));113 File originalFile = new File(classResourceURL.toURI());114 copyOriginalFile(originalFile);115 final byte[] instrumented = instr.instrument(classResourceURL.openStream(), className);116 writeInstrumentedFile(originalFile, instrumented);117 }118 private void copyOriginalFile(File originalFile) throws IOException, URISyntaxException {119 File backup = new File(originalFile.getAbsolutePath() + ".bak");120 if (backup.exists()) {121 backup.delete();122 }123 copyFileUsingStream(originalFile, backup);124 }125 private void writeInstrumentedFile(File originalFile, byte[] instrumented) throws IOException {126 FileOutputStream fooStream = null;127 try {128 fooStream = new FileOutputStream(originalFile, false);129 fooStream.write(instrumented);130 } finally {131 if (fooStream != null) {132 fooStream.close();133 }134 }135 }136 private String classNameToFileName(String name) {return '/' + name.replace('.', '/') + ".class";}137}...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!