@Test @TestDir public void constructorsGetters() throws Exception { Server server = new Server( "server", getAbsolutePath("/a"), getAbsolutePath("/b"), getAbsolutePath("/c"), getAbsolutePath("/d"), new Configuration(false)); assertEquals(server.getHomeDir(), getAbsolutePath("/a")); assertEquals(server.getConfigDir(), getAbsolutePath("/b")); assertEquals(server.getLogDir(), getAbsolutePath("/c")); assertEquals(server.getTempDir(), getAbsolutePath("/d")); assertEquals(server.getName(), "server"); assertEquals(server.getPrefix(), "server"); assertEquals(server.getPrefixedName("name"), "server.name"); assertNotNull(server.getConfig()); server = new Server( "server", getAbsolutePath("/a"), getAbsolutePath("/b"), getAbsolutePath("/c"), getAbsolutePath("/d")); assertEquals(server.getHomeDir(), getAbsolutePath("/a")); assertEquals(server.getConfigDir(), getAbsolutePath("/b")); assertEquals(server.getLogDir(), getAbsolutePath("/c")); assertEquals(server.getTempDir(), getAbsolutePath("/d")); assertEquals(server.getName(), "server"); assertEquals(server.getPrefix(), "server"); assertEquals(server.getPrefixedName("name"), "server.name"); assertNull(server.getConfig()); server = new Server( "server", TestDirHelper.getTestDir().getAbsolutePath(), new Configuration(false)); assertEquals(server.getHomeDir(), TestDirHelper.getTestDir().getAbsolutePath()); assertEquals(server.getConfigDir(), TestDirHelper.getTestDir() + "/conf"); assertEquals(server.getLogDir(), TestDirHelper.getTestDir() + "/log"); assertEquals(server.getTempDir(), TestDirHelper.getTestDir() + "/temp"); assertEquals(server.getName(), "server"); assertEquals(server.getPrefix(), "server"); assertEquals(server.getPrefixedName("name"), "server.name"); assertNotNull(server.getConfig()); server = new Server("server", TestDirHelper.getTestDir().getAbsolutePath()); assertEquals(server.getHomeDir(), TestDirHelper.getTestDir().getAbsolutePath()); assertEquals(server.getConfigDir(), TestDirHelper.getTestDir() + "/conf"); assertEquals(server.getLogDir(), TestDirHelper.getTestDir() + "/log"); assertEquals(server.getTempDir(), TestDirHelper.getTestDir() + "/temp"); assertEquals(server.getName(), "server"); assertEquals(server.getPrefix(), "server"); assertEquals(server.getPrefixedName("name"), "server.name"); assertNull(server.getConfig()); }
private Server createServer(Configuration conf) { return new Server( "server", TestDirHelper.getTestDir().getAbsolutePath(), TestDirHelper.getTestDir().getAbsolutePath(), TestDirHelper.getTestDir().getAbsolutePath(), TestDirHelper.getTestDir().getAbsolutePath(), conf); }
@Test(expected = IllegalStateException.class) @TestDir public void illegalState4() throws Exception { String dir = TestDirHelper.getTestDir().getAbsolutePath(); Server server = new Server("server", dir, dir, dir, dir, new Configuration(false)); server.init(); server.init(); }
@Test(expected = IllegalStateException.class) @TestDir public void illegalState3() throws Exception { Server server = new Server( "server", TestDirHelper.getTestDir().getAbsolutePath(), new Configuration(false)); server.setService(null); }
@Test @TestDir public void loadingDefaultConfig() throws Exception { String dir = TestDirHelper.getTestDir().getAbsolutePath(); Server server = new Server("testserver", dir, dir, dir, dir); server.init(); assertEquals(server.getConfig().get("testserver.a"), "default"); }
@Test @TestException(exception = ServerException.class, msgRegExp = "S04.*") @TestDir public void serviceNotImplementingServiceInterface() throws Exception { String dir = TestDirHelper.getTestDir().getAbsolutePath(); Configuration conf = new Configuration(false); conf.set("server.services", MyService4.class.getName()); Server server = new Server("server", dir, dir, dir, dir, conf); server.init(); }
@Test @TestException(exception = ServerException.class, msgRegExp = "S07.*") @TestDir public void serviceWithNoDefaultConstructor() throws Exception { String dir = TestDirHelper.getTestDir().getAbsolutePath(); Configuration conf = new Configuration(false); conf.set("server.services", MyService7.class.getName()); Server server = new Server("server", dir, dir, dir, dir, conf); server.init(); }
@Test @TestException(exception = ServerException.class, msgRegExp = "S08.*") @TestDir public void invalidSservice() throws Exception { String dir = TestDirHelper.getTestDir().getAbsolutePath(); Configuration conf = new Configuration(false); conf.set("server.services", "foo"); Server server = new Server("server", dir, dir, dir, dir, conf); server.init(); }
@Test @TestException(exception = ServerException.class, msgRegExp = "S01.*") @TestDir public void initNoHomeDir() throws Exception { File homeDir = new File(TestDirHelper.getTestDir(), "home"); Configuration conf = new Configuration(false); conf.set("server.services", TestService.class.getName()); Server server = new Server("server", homeDir.getAbsolutePath(), conf); server.init(); }
@Test @TestException(exception = ServerException.class, msgRegExp = "S05.*") @TestDir public void siteFileNotAFile() throws Exception { String homeDir = TestDirHelper.getTestDir().getAbsolutePath(); File siteFile = new File(homeDir, "server-site.xml"); assertTrue(siteFile.mkdir()); Server server = new Server("server", homeDir, homeDir, homeDir, homeDir); server.init(); }
@Test @TestDir public void log4jFile() throws Exception { InputStream is = Server.getResource("default-log4j.properties"); OutputStream os = new FileOutputStream(new File(TestDirHelper.getTestDir(), "server-log4j.properties")); IOUtils.copyBytes(is, os, 1024, true); Configuration conf = new Configuration(false); Server server = createServer(conf); server.init(); }
@Test @TestDir public void loadingSiteConfig() throws Exception { String dir = TestDirHelper.getTestDir().getAbsolutePath(); File configFile = new File(dir, "testserver-site.xml"); Writer w = new FileWriter(configFile); w.write( "<configuration><property><name>testserver.a</name><value>site</value></property></configuration>"); w.close(); Server server = new Server("testserver", dir, dir, dir, dir); server.init(); assertEquals(server.getConfig().get("testserver.a"), "site"); }
@Test @TestException(exception = ServerException.class, msgRegExp = "S10.*") @TestDir public void serviceWithMissingDependency() throws Exception { String dir = TestDirHelper.getTestDir().getAbsolutePath(); Configuration conf = new Configuration(false); String services = StringUtils.join( ",", Arrays.asList(MyService3.class.getName(), MyService6.class.getName())); conf.set("server.services", services); Server server = new Server("server", dir, dir, dir, dir, conf); server.init(); }
@Test @TestException(exception = ServerException.class, msgRegExp = "S02.*") @TestDir public void initTempDirNotDir() throws Exception { File homeDir = new File(TestDirHelper.getTestDir(), "home"); assertTrue(homeDir.mkdir()); assertTrue(new File(homeDir, "conf").mkdir()); assertTrue(new File(homeDir, "log").mkdir()); File tempDir = new File(homeDir, "temp"); new FileOutputStream(tempDir).close(); Configuration conf = new Configuration(false); conf.set("server.services", TestService.class.getName()); Server server = new Server("server", homeDir.getAbsolutePath(), conf); server.init(); }
private void createHttpFSServer() throws Exception { File homeDir = TestDirHelper.getTestDir(); Assert.assertTrue(new File(homeDir, "conf").mkdir()); Assert.assertTrue(new File(homeDir, "log").mkdir()); Assert.assertTrue(new File(homeDir, "temp").mkdir()); HttpFSServerWebApp.setHomeDirForCurrentThread(homeDir.getAbsolutePath()); File secretFile = new File(new File(homeDir, "conf"), "secret"); Writer w = new FileWriter(secretFile); w.write("secret"); w.close(); // HDFS configuration File hadoopConfDir = new File(new File(homeDir, "conf"), "hadoop-conf"); hadoopConfDir.mkdirs(); String fsDefaultName = TestHdfsHelper.getHdfsConf().get(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY); Configuration conf = new Configuration(false); conf.set(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY, fsDefaultName); File hdfsSite = new File(hadoopConfDir, "hdfs-site.xml"); OutputStream os = new FileOutputStream(hdfsSite); conf.writeXml(os); os.close(); conf = new Configuration(false); conf.set("httpfs.proxyuser.client.hosts", "*"); conf.set("httpfs.proxyuser.client.groups", "*"); conf.set("httpfs.authentication.type", "kerberos"); conf.set("httpfs.authentication.signature.secret.file", secretFile.getAbsolutePath()); File httpfsSite = new File(new File(homeDir, "conf"), "httpfs-site.xml"); os = new FileOutputStream(httpfsSite); conf.writeXml(os); os.close(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); URL url = cl.getResource("webapp"); WebAppContext context = new WebAppContext(url.getPath(), "/webhdfs"); Server server = TestJettyHelper.getJettyServer(); server.addHandler(context); server.start(); HttpFSServerWebApp.get().setAuthority(TestJettyHelper.getAuthority()); }
/** * Creates an absolute path by appending the given relative path to the test root. * * @param relativePath String relative path * @return String absolute path formed by appending relative path to test root */ private static String getAbsolutePath(String relativePath) { return new File(TestDirHelper.getTestDir(), relativePath).getAbsolutePath(); }
@Test @TestDir public void services() throws Exception { String dir = TestDirHelper.getTestDir().getAbsolutePath(); Configuration conf; Server server; // no services ORDER.clear(); conf = new Configuration(false); server = new Server("server", dir, dir, dir, dir, conf); server.init(); assertEquals(ORDER.size(), 0); // 2 services init/destroy ORDER.clear(); String services = StringUtils.join( ",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName())); conf = new Configuration(false); conf.set("server.services", services); server = new Server("server", dir, dir, dir, dir, conf); server.init(); assertEquals(server.get(MyService1.class).getInterface(), MyService1.class); assertEquals(server.get(MyService3.class).getInterface(), MyService3.class); assertEquals(ORDER.size(), 4); assertEquals(ORDER.get(0), "s1.init"); assertEquals(ORDER.get(1), "s3.init"); assertEquals(ORDER.get(2), "s1.postInit"); assertEquals(ORDER.get(3), "s3.postInit"); server.destroy(); assertEquals(ORDER.size(), 6); assertEquals(ORDER.get(4), "s3.destroy"); assertEquals(ORDER.get(5), "s1.destroy"); // 3 services, 2nd one fails on init ORDER.clear(); services = StringUtils.join( ",", Arrays.asList( MyService1.class.getName(), MyService2.class.getName(), MyService3.class.getName())); conf = new Configuration(false); conf.set("server.services", services); server = new Server("server", dir, dir, dir, dir, conf); try { server.init(); fail(); } catch (ServerException ex) { assertEquals(MyService2.class, ex.getError().getClass()); } catch (Exception ex) { fail(); } assertEquals(ORDER.size(), 3); assertEquals(ORDER.get(0), "s1.init"); assertEquals(ORDER.get(1), "s2.init"); assertEquals(ORDER.get(2), "s1.destroy"); // 2 services one fails on destroy ORDER.clear(); services = StringUtils.join( ",", Arrays.asList(MyService1.class.getName(), MyService5.class.getName())); conf = new Configuration(false); conf.set("server.services", services); server = new Server("server", dir, dir, dir, dir, conf); server.init(); assertEquals(ORDER.size(), 4); assertEquals(ORDER.get(0), "s1.init"); assertEquals(ORDER.get(1), "s5.init"); assertEquals(ORDER.get(2), "s1.postInit"); assertEquals(ORDER.get(3), "s5.postInit"); server.destroy(); assertEquals(ORDER.size(), 6); assertEquals(ORDER.get(4), "s5.destroy"); assertEquals(ORDER.get(5), "s1.destroy"); // service override via ext ORDER.clear(); services = StringUtils.join( ",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName())); String servicesExt = StringUtils.join(",", Arrays.asList(MyService1a.class.getName())); conf = new Configuration(false); conf.set("server.services", services); conf.set("server.services.ext", servicesExt); server = new Server("server", dir, dir, dir, dir, conf); server.init(); assertEquals(server.get(MyService1.class).getClass(), MyService1a.class); assertEquals(ORDER.size(), 4); assertEquals(ORDER.get(0), "s1a.init"); assertEquals(ORDER.get(1), "s3.init"); assertEquals(ORDER.get(2), "s1a.postInit"); assertEquals(ORDER.get(3), "s3.postInit"); server.destroy(); assertEquals(ORDER.size(), 6); assertEquals(ORDER.get(4), "s3.destroy"); assertEquals(ORDER.get(5), "s1a.destroy"); // service override via setService ORDER.clear(); services = StringUtils.join( ",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName())); conf = new Configuration(false); conf.set("server.services", services); server = new Server("server", dir, dir, dir, dir, conf); server.init(); server.setService(MyService1a.class); assertEquals(ORDER.size(), 6); assertEquals(ORDER.get(4), "s1.destroy"); assertEquals(ORDER.get(5), "s1a.init"); assertEquals(server.get(MyService1.class).getClass(), MyService1a.class); server.destroy(); assertEquals(ORDER.size(), 8); assertEquals(ORDER.get(6), "s3.destroy"); assertEquals(ORDER.get(7), "s1a.destroy"); // service add via setService ORDER.clear(); services = StringUtils.join( ",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName())); conf = new Configuration(false); conf.set("server.services", services); server = new Server("server", dir, dir, dir, dir, conf); server.init(); server.setService(MyService5.class); assertEquals(ORDER.size(), 5); assertEquals(ORDER.get(4), "s5.init"); assertEquals(server.get(MyService5.class).getClass(), MyService5.class); server.destroy(); assertEquals(ORDER.size(), 8); assertEquals(ORDER.get(5), "s5.destroy"); assertEquals(ORDER.get(6), "s3.destroy"); assertEquals(ORDER.get(7), "s1.destroy"); // service add via setService exception ORDER.clear(); services = StringUtils.join( ",", Arrays.asList(MyService1.class.getName(), MyService3.class.getName())); conf = new Configuration(false); conf.set("server.services", services); server = new Server("server", dir, dir, dir, dir, conf); server.init(); try { server.setService(MyService7.class); fail(); } catch (ServerException ex) { assertEquals(ServerException.ERROR.S09, ex.getError()); } catch (Exception ex) { fail(); } assertEquals(ORDER.size(), 6); assertEquals(ORDER.get(4), "s3.destroy"); assertEquals(ORDER.get(5), "s1.destroy"); // service with dependency ORDER.clear(); services = StringUtils.join( ",", Arrays.asList(MyService1.class.getName(), MyService6.class.getName())); conf = new Configuration(false); conf.set("server.services", services); server = new Server("server", dir, dir, dir, dir, conf); server.init(); assertEquals(server.get(MyService1.class).getInterface(), MyService1.class); assertEquals(server.get(MyService6.class).getInterface(), MyService6.class); server.destroy(); }