lin
2025-08-01 633231e833e21d5b8b1c00cb15aedb62b3b78e8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package test;
 
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
 
import org.testng.Assert;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Test;
 
import test.sample.JUnitSample1;
import testhelper.OutputDirectoryPatch;
 
import java.util.List;
 
public class CommandLineTest {
 
  /**
   * Test -junit
   */
  @Test(groups = { "current" } )
  public void junitParsing() {
    String[] argv = {
      "-log", "0",
      "-d", OutputDirectoryPatch.getOutputDirectory(),
      "-junit",
      "-testclass", "test.sample.JUnitSample1"
    };
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG.privateMain(argv, tla);
 
    List<ITestResult> passed = tla.getPassedTests();
    assertEquals(passed.size(), 2);
    String test1 = passed.get(0).getMethod().getMethodName();
    String test2 = passed.get(1).getMethod().getMethodName();
 
    assertTrue(JUnitSample1.EXPECTED1.equals(test1) && JUnitSample1.EXPECTED2.equals(test2) ||
        JUnitSample1.EXPECTED1.equals(test2) && JUnitSample1.EXPECTED2.equals(test1));
    }
 
  /**
   * Test the absence of -junit
   */
  @Test(groups = { "current" } )
  public void junitParsing2() {
    String[] argv = {
      "-log", "0",
      "-d", OutputDirectoryPatch.getOutputDirectory(),
      "-testclass", "test.sample.JUnitSample1"
    };
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG.privateMain(argv, tla);
 
    List<ITestResult> passed = tla.getPassedTests();
    assertEquals(passed.size(), 0);
    }
 
  /**
   * Test the ability to override the default command line Suite name
   */
  @Test(groups = { "current" } )
  public void suiteNameOverride() {
    String suiteName="MySuiteName";
    String[] argv = {
      "-log", "0",
      "-d", OutputDirectoryPatch.getOutputDirectory(),
      "-junit",
      "-testclass", "test.sample.JUnitSample1",
      "-suitename", "\""+suiteName+"\""
    };
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG.privateMain(argv, tla);
 
    List<ITestContext> contexts = tla.getTestContexts();
    assertTrue(contexts.size()>0);
    for (ITestContext context:contexts) {
        assertEquals(context.getSuite().getName(),suiteName);
    }
  }
 
  /**
   * Test the ability to override the default command line test name
   */
  @Test(groups = { "current" } )
  public void testNameOverride() {
    String testName="My Test Name";
    String[] argv = {
      "-log", "0",
      "-d", OutputDirectoryPatch.getOutputDirectory(),
      "-junit",
      "-testclass", "test.sample.JUnitSample1",
      "-testname", "\""+testName+"\""
    };
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG.privateMain(argv, tla);
 
    List<ITestContext> contexts = tla.getTestContexts();
    assertTrue(contexts.size()>0);
    for (ITestContext context:contexts) {
        assertEquals(context.getName(),testName);
    }
  }
 
  @Test
  public void testUseDefaultListenersArgument() {
    TestNG.privateMain(new String[] {
        "-log", "0", "-usedefaultlisteners", "false", "-testclass", "test.sample.JUnitSample1"
    }, null);
  }
 
  @Test
  public void testMethodParameter() {
    String[] argv = {
      "-log", "0",
      "-d", OutputDirectoryPatch.getOutputDirectory(),
      "-methods", "test.sample.Sample2.method1,test.sample.Sample2.method3",
    };
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG.privateMain(argv, tla);
 
    List<ITestResult> passed = tla.getPassedTests();
    Assert.assertEquals(passed.size(), 2);
    Assert.assertTrue((passed.get(0).getName().equals("method1") &&
        passed.get(1).getName().equals("method3"))
        ||
        (passed.get(1).getName().equals("method1") &&
        passed.get(0).getName().equals("method3")));
  }
 
  private static void ppp(String s) {
    System.out.println("[CommandLineTest] " + s);
  }
 
}