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 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*
* $Id$
*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.interview;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
public class InterviewSetTest {
@Test
public void test() throws Exception {
InterviewSetTest t = new InterviewSetTest();
boolean ok = t.run();
Assert.assertTrue(ok);
}
boolean run() throws Exception {
boolean ok = true;
for (String test : tests) {
if (!test(test))
ok = false;
}
return ok;
}
String[] tests = {
"-c A a -c B b -c C c : a.a b.b c.c",
"-c A a -c B b -c C c -a b c : a.a c.c b.b",
"-c A a -c B b -c C c -a a b : b.b a.a c.c",
"-c A a -c B b -c C c -a a c -a c b : b.b c.c a.a",
"-c A a -c B b -c C c -a a b -a a c : b.b c.c a.a",
"-c A a -c B b -c C c -a a c -a a b : b.b c.c a.a",
"-c A a -c B b -c C c -a a b -a b c : c.c b.b a.a",
"-c A a -c B b -c C c -a a c : c.c a.a b.b",
"-c A a -c B b -c C c -c A d -a a c : c.c a.a b.b d.a",
"-c A a -c B b -c C c -c A d -a a c -a b d : c.c a.a d.a b.b",
"-c A a -c B b -c C c -c A d -a b c -a b d : a.a c.c d.a b.b",
};
boolean test(String data) throws Exception {
int sep = data.indexOf(":");
String[] args = data.substring(0, sep).trim().split(" *");
String[] expect = data.substring(sep + 1).trim().split(" *");
TestInterview t = new TestInterview(args);
Question[] path = t.getPath();
if (path.length != expect.length + 2) {
error(data, path, "wrong length for path; expected: " + (expect.length + 2) + "; found: " + path.length);
return false;
}
for (int i = 0; i < path.length; i++) {
Question q = path[i];
String tag = q.getTag();
String e = i == 0 ? "test.first"
: i == path.length - 1 ? "test.last"
: "test.set." + expect[i - 1];
if (!tag.equals(e)) {
error(data, path, "wrong entry for path[" + i + "]; expected: " + e + "; found: " + tag);
return false;
}
}
return true;
}
void error(String data, Question[] path, String message) {
System.err.println("Error: " + message);
System.err.println("Data: " + data);
for (int i = 0; i < path.length; i++)
System.err.println(" path[" + i + "] " + path[i].getTag());
}
private static Class<?> getInterviewClass(String name) throws Exception {
return Class.forName(InterviewSetTest.class.getName() + "$" + name + "Interview");
}
private static class TestInterview extends Interview {
TestInterview(String[] args) throws Exception {
super("test");
iSet = new SetInterview(this, args);
setFirstQuestion(qFirst);
}
private NullQuestion qFirst = new NullQuestion(this, "first") {
public Question getNext() {
return callInterview(iSet, qLast);
}
};
private FinalQuestion qLast = new FinalQuestion(this, "last");
private SetInterview iSet;
}
private static class SetInterview extends InterviewSet {
SetInterview(Interview parent, String[] args) throws Exception {
super(parent, "set");
for (int i = 0; i < args.length; i++) {
//System.err.println(i + ": " + args[i]);
String arg = args[i];
if ((arg.equals("-c") || arg.equals("-create"))
&& i + 2 < args.length) {
create(args[i + 1], args[i + 2]);
i += 2;
} else if ((arg.equals("-a") || arg.equals("-add"))
&& i + 2 < args.length) {
add(args[i + 1], args[i + 2]);
i += 2;
} else if ((arg.equals("-r") || arg.equals("-remove"))
&& i + 2 < args.length) {
remove(args[i + 1], args[i + 2]);
i += 2;
} else
throw new Exception("bad arg: " + arg);
}
}
private void create(String type, String name) throws Exception {
//System.err.println("create " + type + " " + name);
Constructor<?> constr = getInterviewClass(type).getConstructor(Interview.class, String.class);
Interview i = (Interview) constr.newInstance(new Object[]{this, name});
children.put(name, i);
}
private void add(String entName, String encyName) throws Exception {
//System.err.println("add " + entName + " " + encyName);
Interview ent = children.get(entName);
Interview ency = children.get(encyName);
addDependency(ent, ency);
}
private void remove(String entName, String encyName) {
//System.err.println("remove " + entName + " " + encyName);
Interview ent = children.get(entName);
Interview ency = children.get(encyName);
removeDependency(ent, ency);
}
private Map<String, Interview> children = new HashMap<>();
}
static class AInterview extends Interview {
public AInterview(Interview parent, String tag) {
super(parent, tag);
setFirstQuestion(qString);
}
Question qString = new StringQuestion(this, "a") {
public Question getNext() {
return qEnd;
}
};
Question qEnd = new FinalQuestion(this);
}
static class BInterview extends Interview {
public BInterview(Interview parent, String tag) {
super(parent, tag);
setFirstQuestion(qString);
}
Question qString = new StringQuestion(this, "b") {
public Question getNext() {
return qEnd;
}
};
Question qEnd = new FinalQuestion(this);
}
static class CInterview extends Interview {
public CInterview(Interview parent, String tag) {
super(parent, tag);
setFirstQuestion(qString);
}
Question qString = new StringQuestion(this, "c") {
public Question getNext() {
return qEnd;
}
};
Question qEnd = new FinalQuestion(this);
}
}
|