diff --git a/src/main/java/me/ehlxr/ContentReplace.java b/src/main/java/me/ehlxr/ContentReplace.java new file mode 100644 index 0000000..a2b6cb0 --- /dev/null +++ b/src/main/java/me/ehlxr/ContentReplace.java @@ -0,0 +1,76 @@ +package me.ehlxr; + +import java.io.*; + +/** + * Created by lixiangrong on 2017/11/17. + */ +public class ContentReplace { + private static int total = 0; + private static int unDeal = 0; + + public static void main(String[] args) throws IOException { + File dir = new File("/Users/ehlxr/WorkSpaces/enncloud/Ceres"); + deal(dir); + System.out.println("总文件数:" + total); + System.out.println("未处理文件数:" + unDeal); + } + + private static void deal(File file) throws IOException { + if (file.isDirectory()) { + File[] fs = file.listFiles(((dir, name) -> { + File f = new File(dir.getPath() + File.separator + name); + + return (f.getPath().contains("src") && name.endsWith(".java")) || f.isDirectory(); + })); + + for (File f : fs != null ? fs : new File[0]) { + deal(f); + } + } else { + ++total; + // System.out.println(file.getPath()); + + File tmp = File.createTempFile("tmp", null); + try ( + FileOutputStream tmpOut = new FileOutputStream(tmp); + FileInputStream tmpIn = new FileInputStream(tmp); + RandomAccessFile raf = new RandomAccessFile(file, "rw")) { + byte[] buf = new byte[64]; + int hasRead; + while ((hasRead = raf.read(buf)) > 0) { + if (new String(buf).startsWith("/*")) { + ++unDeal; + System.out.println("未处理文件:" + file.getPath()); + return; + } + // 把原有内容读入临时文件 + tmpOut.write(buf, 0, hasRead); + } + raf.seek(0L); + String tmpl = "/*\n" + + " * Copyright 2017-2017 the original author or authors.\n" + + " *\n" + + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + " * you may not use this file except in compliance with the License.\n" + + " * You may obtain a copy of the License at\n" + + " *\n" + + " * http://www.apache.org/licenses/LICENSE-2.0\n" + + " *\n" + + " * Unless required by applicable law or agreed to in writing, software\n" + + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + " * See the License for the specific language governing permissions and\n" + + " * limitations under the License.\n" + + " */\n\n"; + raf.write(tmpl.getBytes()); + // 追加临时文件内容 + while ((hasRead = tmpIn.read(buf)) > 0) { + raf.write(buf, 0, hasRead); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/src/main/java/me/ehlxr/powermock/ClassUnderTest.java b/src/main/java/me/ehlxr/powermock/ClassUnderTest.java index b7e882e..164ba3c 100644 --- a/src/main/java/me/ehlxr/powermock/ClassUnderTest.java +++ b/src/main/java/me/ehlxr/powermock/ClassUnderTest.java @@ -10,6 +10,8 @@ import java.io.File; * Created by lixiangrong on 2017/7/21. */ public class ClassUnderTest { + private String f1 = "test"; + public boolean callArgumentInstance(File file) { return file.exists(); } diff --git a/src/main/java/me/ehlxr/powermock/TestClassUnderTest.java b/src/main/java/me/ehlxr/powermock/TestClassUnderTest.java index 6d17f9a..840396b 100644 --- a/src/main/java/me/ehlxr/powermock/TestClassUnderTest.java +++ b/src/main/java/me/ehlxr/powermock/TestClassUnderTest.java @@ -6,6 +6,7 @@ import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; import java.io.File; @@ -102,4 +103,13 @@ public class TestClassUnderTest { when(dependency.isGod(anyString())).thenReturn(true); Assert.assertTrue(underTest.callDependency()); } + + @Test + @PrepareForTest(ClassUnderTest.class) + public void testInvokPrivate() throws Exception { + ClassUnderTest underTest = spy(new ClassUnderTest()); + + Whitebox.invokeMethod(underTest, "isExist"); + verifyPrivate(underTest).invoke("isExist"); + } } \ No newline at end of file