1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tdk.jcov.report.ancfilters;
  27 
  28 import com.sun.tdk.jcov.instrument.DataClass;
  29 import com.sun.tdk.jcov.instrument.DataMethodEntryOnly;
  30 import org.testng.annotations.DataProvider;
  31 import org.testng.annotations.Test;
  32 
  33 import java.io.BufferedWriter;
  34 import java.io.IOException;
  35 import java.nio.file.Files;
  36 import java.nio.file.Path;
  37 
  38 import static org.testng.Assert.assertEquals;
  39 import static org.testng.Assert.assertTrue;
  40 import static org.testng.AssertJUnit.assertFalse;
  41 
  42 public class ListANCFilterTest {
  43     private String createListFile(String[] lines) throws IOException {
  44         Path file = Files.createTempFile("ListANCFilterTest", ".lst");
  45         BufferedWriter out = Files.newBufferedWriter(file);
  46         for(String ln : lines) {
  47             out.write(ln);out.newLine();
  48         }
  49         out.close();
  50         file.toFile().deleteOnExit();
  51         return file.toAbsolutePath().toString();
  52     }
  53 
  54     @Test
  55     public void testNormal() throws IOException {
  56         ListANCFilter filter = new ListANCFilter();
  57         String[] data = {
  58                 "#normal",
  59                 //a method
  60                 "java/lang/String#indexOf(I)I",
  61                 //a constructor
  62                 "java/lang/Math#<init>()V",
  63         };
  64         filter.setParameter(createListFile(data));
  65         assertEquals(filter.getAncReason(), "normal");
  66         DataClass stringDataClass = new DataClass(0,
  67                 "java/lang/String", "java.base", 0, false);
  68         assertFalse(filter.accept(stringDataClass));
  69         assertTrue(filter.accept(stringDataClass,
  70                 new DataMethodEntryOnly(stringDataClass, 0, "indexOf", "(I)I", "", new String[0], 0)));
  71         DataClass mathDataClass = new DataClass(2,
  72                 "java/lang/Math", "java.base", 0, false);
  73         assertTrue(filter.accept(mathDataClass,
  74                 new DataMethodEntryOnly(mathDataClass, 0, "<init>", "()V", "", new String[0], 0)));
  75     }
  76 
  77     @Test
  78     public void testNested() throws IOException {
  79         ListANCFilter filter = new ListANCFilter();
  80         String[] data = {
  81                 "#nested",
  82                 //a nested class
  83                 "java/lang/System$LoggerFinder#checkPermission()Ljava/lang/Void;"
  84         };
  85         filter.setParameter(createListFile(data));
  86         assertEquals(filter.getAncReason(), "nested");
  87         DataClass systemDataClass = new DataClass(2,
  88                 "java/lang/System", "java.base", 0, false);
  89         assertTrue(filter.accept(systemDataClass,
  90                 new DataMethodEntryOnly(systemDataClass, 0, "$LoggerFinder.checkPermission",
  91                         "()Ljava/lang/Void;", "", new String[0], 0)));
  92     }
  93 
  94     @DataProvider(name="unreadable")
  95     public Object[][] unreadableLists() {
  96         return new Object[][] {
  97                 {new String[] {"java/lang/String#indexOf(I)I"}},
  98                 {new String[] {"data", "java/lang/String#indexOf(I)I"}},
  99                 {new String[] {"#data", "java/lang/String/indexOf(I)I"}},
 100                 {null}
 101         };
 102     }
 103 
 104     @Test(dataProvider = "unreadable", expectedExceptions = {IllegalStateException.class, IllegalArgumentException.class})
 105     public void testNotRead(String[] data) throws IllegalStateException, IOException {
 106         String file;
 107         if(data == null)
 108             file = null;
 109         else
 110             file = createListFile(data);
 111         new ListANCFilter().setParameter(file);
 112     }
 113 
 114     @DataProvider(name="readable")
 115     public Object[][] readableLists() {
 116         String indexOfLine = "java/lang/String#indexOf(I)I";
 117         String[] indexOfElements = {"java/lang/String", "indexOf", "(I)I"};
 118         String constructorLine = "java/lang/Math#<init>()V";
 119         String[] constructorElements = {"java/lang/Math", "<init>", "()V"};
 120         return new Object[][] {
 121                 {new String[] {"#data0", indexOfLine}, "data0", new String[][] {indexOfElements}},
 122                 {new String[] {"#data1", "", constructorLine}, "data1", new String[][] {constructorElements}},
 123                 {new String[] {"#data2", indexOfLine, constructorLine, ""}, "data2", new String[][] {indexOfElements, constructorElements}},
 124                 {new String[] {"#data3"}, "data3", new String[][] {}}
 125         };
 126     }
 127 
 128     @Test(dataProvider = "readable")
 129     public void testRead(String[] data, String reason, String[][] elements) throws IllegalStateException, IOException {
 130         ListANCFilter filter = new ListANCFilter();
 131         filter.setParameter(createListFile(data));
 132         assertEquals(filter.getAncReason(), reason);
 133         for(String[] el : elements) {
 134             DataClass dataClass = new DataClass(2,
 135                     el[0], "java.base", 0, false);
 136             assertTrue(filter.accept(dataClass,
 137                     new DataMethodEntryOnly(dataClass, 0, el[1], el[2], "", new String[0], 0)));        }
 138     }
 139 
 140     @Test(expectedExceptions = IllegalStateException.class)
 141     public void testUninitiated() {
 142         new ListANCFilter().accept(new DataClass(0,
 143                 "java/lang/String", "java.base", 0, false));
 144     }
 145 
 146     @Test(expectedExceptions = IllegalStateException.class)
 147     public void testReasonUninitiated() {
 148         new ListANCFilter().getAncReason();
 149     }
 150 
 151     @Test
 152     public void testGetFilterName() {
 153         assertEquals(new ListANCFilter().getFilterName(), "list");
 154     }
 155 }