1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.tradefed.result; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertNull; 21 22 import org.junit.Test; 23 import org.junit.runner.RunWith; 24 import org.junit.runners.JUnit4; 25 26 import java.lang.annotation.Annotation; 27 28 /** Unit tests for {@link TestDescription}. */ 29 @RunWith(JUnit4.class) 30 public class TestDescriptionTest { 31 private TestDescription mDescription; 32 33 /** Annotation used for testing. */ 34 public static class TestAnnotation implements Annotation { 35 @Override 36 public Class<? extends Annotation> annotationType() { 37 return TestAnnotation.class; 38 } 39 } 40 41 /** Annotation used for testing. */ 42 public static class TestAnnotation2 implements Annotation { 43 @Override 44 public Class<? extends Annotation> annotationType() { 45 return TestAnnotation2.class; 46 } 47 } 48 49 /** 50 * Create a {@link TestDescription} with annotations and ensure it's properly holding the data. 51 */ 52 @Test 53 public void testCreateDescription() { 54 mDescription = 55 new TestDescription( 56 "className", "testName", new TestAnnotation(), new TestAnnotation2()); 57 assertEquals("className", mDescription.getClassName()); 58 assertEquals("testName", mDescription.getTestName()); 59 assertEquals("testName", mDescription.getTestNameWithoutParams()); 60 assertEquals(2, mDescription.getAnnotations().size()); 61 assertNotNull(mDescription.getAnnotation(TestAnnotation.class)); 62 assertNotNull(mDescription.getAnnotation(TestAnnotation2.class)); 63 } 64 65 /** Create a {@link TestDescription} without annotations and ensure it is properly reflected. */ 66 @Test 67 public void testCreateDescription_noAnnotation() { 68 mDescription = new TestDescription("className", "testName"); 69 assertEquals("className", mDescription.getClassName()); 70 assertEquals("testName", mDescription.getTestName()); 71 assertEquals(0, mDescription.getAnnotations().size()); 72 assertNull(mDescription.getAnnotation(TestAnnotation.class)); 73 } 74 75 /** Create a {@link TestDescription} with a parameterized method. */ 76 @Test 77 public void testCreateDescription_parameterized() { 78 mDescription = new TestDescription("className", "testName[0]"); 79 assertEquals("className", mDescription.getClassName()); 80 assertEquals("testName[0]", mDescription.getTestName()); 81 assertEquals("testName", mDescription.getTestNameWithoutParams()); 82 83 mDescription = new TestDescription("className", "testName[key=value]"); 84 assertEquals("testName[key=value]", mDescription.getTestName()); 85 assertEquals("testName", mDescription.getTestNameWithoutParams()); 86 87 mDescription = new TestDescription("className", "testName[key=\"quoted value\"]"); 88 assertEquals("testName[key=\"quoted value\"]", mDescription.getTestName()); 89 assertEquals("testName", mDescription.getTestNameWithoutParams()); 90 91 mDescription = new TestDescription("className", "testName[key:22]"); 92 assertEquals("testName[key:22]", mDescription.getTestName()); 93 assertEquals("testName", mDescription.getTestNameWithoutParams()); 94 } 95 } 96