mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
2 days ago 0138b5924ff14ef709398bf878dbb9bf2a0b7c33
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 *
 * Copyright 2026 3A Systems, LLC.
 */
package org.opends.quicksetup;
 
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertSame;
import static org.testng.Assert.assertTrue;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
 
import org.forgerock.i18n.LocalizableMessage;
import org.opends.server.DirectoryServerTestCase;
import org.opends.server.TestCaseUtils;
import org.testng.SkipException;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
import com.forgerock.opendj.cli.ArgumentParser;
 
/**
 * Tests when a launcher creates its temporary log file.
 * <p>
 * Nothing removes that file unless the operation it belongs to succeeds, so a road which
 * attempts nothing - {@code setup --help}, {@code --version}, a usage error - must not create
 * one, nor the directory it would live in (issue #1030).
 */
@SuppressWarnings("javadoc")
@Test(groups = { "precommit", "quicksetup" }, sequential = true)
public class LauncherTest extends DirectoryServerTestCase
{
  private static final String PREFIX = "opendj-setup-";
 
  private File tempDir;
  private final List<TempLogFile> created = new ArrayList<>();
 
  @BeforeClass
  public void setUp() throws IOException
  {
    tempDir = TestCaseUtils.createTemporaryDirectory("launcherTest");
  }
 
  @AfterClass
  public void tearDown() throws IOException
  {
    for (TempLogFile logFile : created)
    {
      logFile.deleteLogFileAfterSuccess();
    }
    TestCaseUtils.deleteDirectory(tempDir);
  }
 
  @Test
  public void testBuildingALauncherLeavesNothingOnDisk() throws Exception
  {
    final File instance = new File(tempDir, "not-yet-laid-down");
    final File logs = new File(instance, "logs");
 
    final TestLauncher launcher = new TestLauncher(logs);
 
    assertFalse(launcher.hasTempLogFile(), "the log must wait for a road that can fail");
    assertFalse(logs.exists(), logs.getPath());
    assertFalse(instance.exists(), instance.getPath());
  }
 
  @Test
  public void testAskingForTheLogCreatesItOnce() throws Exception
  {
    final File logs = new File(tempDir, "asked-for/logs");
    final TestLauncher launcher = new TestLauncher(logs);
 
    final TempLogFile logFile = launcher.getTempLogFile();
    created.add(logFile);
 
    assertTrue(logFile.isReadable(), logFile.getPath());
    assertEquals(logFile.getLogFile().getCanonicalFile().getParentFile(), logs.getCanonicalFile());
    assertTrue(launcher.hasTempLogFile());
    assertSame(launcher.getTempLogFile(), logFile, "a second ask must not create a second log");
  }
 
  /**
   * The wizard road: the splash screen comes up before the user has said anything, so the log
   * cannot be created on the way to it - it is the application that asks for one, when it
   * starts the install. A wizard quit at any step leaves nothing behind.
   */
  @Test
  public void testLaunchingTheGuiCreatesNoLog() throws Exception
  {
    final File instance = new File(tempDir, "quit-at-the-first-step");
    final File logs = new File(instance, "logs");
    final TestLauncher launcher = new TestLauncher(logs);
 
    // The wizard behind the splash screen quits without installing anything.
    launcher.launchGui(new String[0]);
 
    assertFalse(launcher.hasTempLogFile(), "the splash screen must not cost a log");
    assertFalse(logs.exists(), logs.getPath());
    assertFalse(instance.exists(), instance.getPath());
 
    // What the wizard was handed is the launcher's own log, made on the first ask.
    final TempLogFile logFile = launcher.splashLogFile.get();
    created.add(logFile);
    assertTrue(logFile.isReadable(), logFile.getPath());
    assertSame(logFile, launcher.getTempLogFile());
  }
 
  /**
   * A GUI which does not come up costs no log either, and its reason is not lost for that: it
   * goes into the log as soon as something asks for one - on the headless road, the operation
   * which runs on the command line instead.
   */
  @Test
  public void testAGuiWhichDoesNotComeUpIsLoggedOnceThereIsALog() throws Exception
  {
    if ("true".equalsIgnoreCase(System.getenv("OPENDJ_LOG_TO_STDOUT")))
    {
      // The reason is written to the log, which then goes to stdout and leaves the file empty.
      throw new SkipException("OPENDJ_LOG_TO_STDOUT writes the log to stdout, not to the file");
    }
    final File logs = new File(tempDir, "headless/logs");
    final TestLauncher launcher = new TestLauncher(logs);
    launcher.splashFailure = new IllegalStateException("no display at all");
 
    assertTrue(launcher.launchGui(new String[0]) != 0, "a GUI which does not come up is reported");
 
    assertFalse(launcher.hasTempLogFile(), "a GUI which does not come up must not cost a log");
    assertFalse(logs.exists(), logs.getPath());
 
    final TempLogFile logFile = launcher.getTempLogFile();
    created.add(logFile);
    logFile.writer.shutdown();
    final String contents = logFile.readContents();
    assertTrue(contents.contains("no display at all"), contents);
  }
 
  /** A launcher with nothing in it but the log file behaviour under test. */
  private static final class TestLauncher extends Launcher
  {
    /** What {@link Launcher#launchGui(String[])} handed the splash screen. */
    private Supplier<TempLogFile> splashLogFile;
    /** What the splash screen throws, if it is not to come up. */
    private RuntimeException splashFailure;
 
    TestLauncher(final File tempLogFileDirectory)
    {
      super(new String[0], PREFIX, tempLogFileDirectory);
    }
 
    @Override
    void startSplashScreen(final Supplier<TempLogFile> tempLogFile, final String[] args)
    {
      // No display here, and no install either: the wizard is quit at its first step - or it
      // does not come up at all.
      splashLogFile = tempLogFile;
      if (splashFailure != null)
      {
        throw splashFailure;
      }
    }
 
    @Override
    public ArgumentParser getArgumentParser()
    {
      return null;
    }
 
    @Override
    protected LocalizableMessage getFrameTitle()
    {
      return LocalizableMessage.raw("test");
    }
 
    @Override
    protected CliApplication createCliApplication()
    {
      return null;
    }
 
    @Override
    protected void willLaunchGui()
    {
      // nothing is launched here
    }
 
    @Override
    protected void guiLaunchFailed()
    {
      // nothing is launched here
    }
  }
}