1/*
2 * Copyright (c) 2015, 2016, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 8178821
27 * @summary Test Completion
28 * @modules jdk.internal.le/jdk.internal.jline
29 *          jdk.internal.le/jdk.internal.jline.console
30 *          jdk.internal.le/jdk.internal.jline.console.history
31 *          jdk.internal.le/jdk.internal.jline.extra
32 * @build HistoryTest
33 * @run testng HistoryTest
34 */
35
36import java.io.ByteArrayInputStream;
37import java.io.ByteArrayOutputStream;
38import java.io.IOException;
39import java.util.Arrays;
40import java.util.Collection;
41import java.util.Collections;
42import java.util.concurrent.atomic.AtomicBoolean;
43
44import jdk.internal.jline.UnsupportedTerminal;
45import jdk.internal.jline.console.ConsoleReader;
46import jdk.internal.jline.console.history.MemoryHistory;
47import jdk.internal.jline.extra.EditingHistory;
48
49import org.testng.annotations.Test;
50
51import static org.testng.Assert.*;
52
53@Test
54public class HistoryTest {
55
56    public void testHistory() throws IOException {
57        ConsoleReader in = new ConsoleReader(new ByteArrayInputStream(new byte[0]), new ByteArrayOutputStream(), new UnsupportedTerminal());
58        AtomicBoolean complete = new AtomicBoolean();
59        EditingHistory history = new EditingHistory(in, Collections.emptyList()) {
60            @Override
61            protected boolean isComplete(CharSequence input) {
62                return complete.get();
63            }
64        };
65        complete.set(false); history.add("void test() {");
66        complete.set(false); history.add("    System.err.println(1);");
67        complete.set(true);  history.add("}");
68        complete.set(true);  history.add("/exit");
69
70        previousAndAssert(history, "/exit");
71
72        history.previous(); history.previous(); history.previous();
73
74        complete.set(false); history.add("void test() { /*changed*/");
75
76        complete.set(true);
77        previousAndAssert(history, "}");
78        previousAndAssert(history, "    System.err.println(1);");
79        previousAndAssert(history, "void test() {");
80
81        assertFalse(history.previous());
82
83        nextAndAssert(history, "    System.err.println(1);");
84        nextAndAssert(history, "}");
85        nextAndAssert(history, "");
86
87        complete.set(false); history.add("    System.err.println(2);");
88        complete.set(true);  history.add("} /*changed*/");
89
90        assertEquals(history.size(), 7);
91
92        Collection<? extends String> persistentHistory = history.save();
93
94        history = new EditingHistory(in, persistentHistory) {
95            @Override
96            protected boolean isComplete(CharSequence input) {
97                return complete.get();
98            }
99        };
100
101        previousSnippetAndAssert(history, "void test() { /*changed*/");
102        previousSnippetAndAssert(history, "/exit");
103        previousSnippetAndAssert(history, "void test() {");
104
105        assertFalse(history.previousSnippet());
106
107        nextSnippetAndAssert(history, "/exit");
108        nextSnippetAndAssert(history, "void test() { /*changed*/");
109        nextSnippetAndAssert(history, "");
110
111        assertFalse(history.nextSnippet());
112
113        complete.set(false); history.add("{");
114        complete.set(true);  history.add("}");
115
116        persistentHistory = history.save();
117
118        history = new EditingHistory(in, persistentHistory) {
119            @Override
120            protected boolean isComplete(CharSequence input) {
121                return complete.get();
122            }
123        };
124
125        previousSnippetAndAssert(history, "{");
126        previousSnippetAndAssert(history, "void test() { /*changed*/");
127        previousSnippetAndAssert(history, "/exit");
128        previousSnippetAndAssert(history, "void test() {");
129
130        while (history.next());
131
132        complete.set(true);  history.add("/*current1*/");
133        complete.set(true);  history.add("/*current2*/");
134        complete.set(true);  history.add("/*current3*/");
135
136        assertEquals(history.currentSessionEntries(), Arrays.asList("/*current1*/", "/*current2*/", "/*current3*/"));
137
138        history.remove(0);
139
140        assertEquals(history.currentSessionEntries(), Arrays.asList("/*current1*/", "/*current2*/", "/*current3*/"));
141
142        while (history.size() > 2)
143            history.remove(0);
144
145        assertEquals(history.currentSessionEntries(), Arrays.asList("/*current2*/", "/*current3*/"));
146
147        for (int i = 0; i < MemoryHistory.DEFAULT_MAX_SIZE * 2; i++) {
148            complete.set(true);  history.add("/exit");
149        }
150
151        complete.set(false); history.add("void test() { /*after full*/");
152        complete.set(false); history.add("    System.err.println(1);");
153        complete.set(true);  history.add("}");
154
155        previousSnippetAndAssert(history, "void test() { /*after full*/");
156        nextSnippetAndAssert(history, "");
157
158        assertFalse(history.nextSnippet());
159
160        while (history.previousSnippet())
161            ;
162
163        while (history.nextSnippet())
164            ;
165    }
166
167    private void previousAndAssert(EditingHistory history, String expected) {
168        assertTrue(history.previous());
169        assertEquals(history.current().toString(), expected);
170    }
171
172    private void nextAndAssert(EditingHistory history, String expected) {
173        assertTrue(history.next());
174        assertEquals(history.current().toString(), expected);
175    }
176
177    private void previousSnippetAndAssert(EditingHistory history, String expected) {
178        assertTrue(history.previousSnippet());
179        assertEquals(history.current().toString(), expected);
180    }
181
182    private void nextSnippetAndAssert(EditingHistory history, String expected) {
183        assertTrue(history.nextSnippet());
184        assertEquals(history.current().toString(), expected);
185    }
186
187}
188