Repository: ambari Updated Branches: refs/heads/trunk 4305a8603 -> bd1b74440
Revert "AMBARI-19365. Executing ambari-server unit tests with JDK 1.8 results in unit test failures (rlevas)" This reverts commit 5e72ee6f2c6ebcda105e521d819f95ffde1b245e. Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/9109d1c3 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/9109d1c3 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/9109d1c3 Branch: refs/heads/trunk Commit: 9109d1c3b9c20c98310472c05b1572d744c58363 Parents: 4305a86 Author: Robert Levas <[email protected]> Authored: Thu Jan 5 16:49:15 2017 -0500 Committer: Robert Levas <[email protected]> Committed: Thu Jan 5 16:49:15 2017 -0500 ---------------------------------------------------------------------- .../controller/utilities/KerberosChecker.java | 50 ++++----- .../utilities/LoginContextHelper.java | 56 ---------- .../utilities/KerberosCheckerTest.java | 43 +++++--- .../system/impl/JvmMetricsSourceTest.java | 75 +------------ .../AmbariBasicAuthenticationFilterTest.java | 37 +++++-- .../server/upgrade/UpgradeCatalog222Test.java | 4 +- .../view/persistence/DataStoreImplTest.java | 110 ++++++++++--------- 7 files changed, 140 insertions(+), 235 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/9109d1c3/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java index 2ca9735..e0a3d55 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/KerberosChecker.java @@ -1,4 +1,4 @@ -/* +/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -35,25 +35,19 @@ import com.sun.security.auth.callback.TextCallbackHandler; public class KerberosChecker { - static final String HTTP_SPNEGO_STANDARD_ENTRY = - "com.sun.security.jgss.krb5.initiate"; + private static final String HTTP_SPNEGO_STANDARD_ENTRY = + "com.sun.security.jgss.krb5.initiate"; private static final String KRB5_LOGIN_MODULE = - "com.sun.security.auth.module.Krb5LoginModule"; + "com.sun.security.auth.module.Krb5LoginModule"; public static final String JAVA_SECURITY_AUTH_LOGIN_CONFIG = - "java.security.auth.login.config"; + "java.security.auth.login.config"; - private static Logger LOG = LoggerFactory.getLogger(KerberosChecker.class); + static Logger LOG = LoggerFactory.getLogger(KerberosChecker.class); @Inject static Configuration config; /** - * Used to help create new LoginContext instances - */ - @Inject - static LoginContextHelper loginContextHelper; - - /** * Checks Ambari Server with a Kerberos principal and keytab to allow views * to authenticate via SPNEGO against cluster components. * @@ -67,14 +61,14 @@ public class KerberosChecker { String jaasConfPath = System.getProperty(JAVA_SECURITY_AUTH_LOGIN_CONFIG); javax.security.auth.login.Configuration jaasConf = - javax.security.auth.login.Configuration.getConfiguration(); + javax.security.auth.login.Configuration.getConfiguration(); AppConfigurationEntry[] jaasConfEntries = - jaasConf.getAppConfigurationEntry(HTTP_SPNEGO_STANDARD_ENTRY); + jaasConf.getAppConfigurationEntry(HTTP_SPNEGO_STANDARD_ENTRY); if (jaasConfEntries == null) { LOG.warn("Can't find " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + - jaasConfPath); + jaasConfPath); } else { boolean krb5LoginModulePresent = false; for (AppConfigurationEntry ace : jaasConfEntries) { @@ -89,39 +83,41 @@ public class KerberosChecker { LOG.warn(keytabPath + " doesn't exist."); } else if (!keytabFile.canRead()) { LOG.warn("Unable to read " + keytabPath + - " Please check the file access permissions for user " + - System.getProperty("user.name")); + " Please check the file access permissions for user " + + System.getProperty("user.name")); } } else { LOG.warn("Can't find keyTab option in " + KRB5_LOGIN_MODULE + - " module of " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + - jaasConfPath); - } + " module of " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + + jaasConfPath); } if (!options.containsKey("principal")) { LOG.warn("Can't find principal option in " + KRB5_LOGIN_MODULE + - " module of " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + - jaasConfPath); + " module of " + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + + jaasConfPath); } } } } if (!krb5LoginModulePresent) { LOG.warn("Can't find " + KRB5_LOGIN_MODULE + " module in " + - HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + jaasConfPath); + HTTP_SPNEGO_STANDARD_ENTRY + " entry in " + jaasConfPath); } } + TextCallbackHandler textHandler = new TextCallbackHandler(); try { - LoginContext loginContext = loginContextHelper.createLoginContext(HTTP_SPNEGO_STANDARD_ENTRY); + LoginContext loginContext = new LoginContext(HTTP_SPNEGO_STANDARD_ENTRY, + textHandler); loginContext.login(); loginContext.logout(); - } catch (LoginException le) { + } + catch (LoginException le) { LOG.error(le.getMessage()); throw new AmbariException( - "Ambari Server Kerberos credentials check failed. \n" + - "Check KDC availability and JAAS configuration in " + jaasConfPath); + "Ambari Server Kerberos credentials check failed. \n" + + "Check KDC availability and JAAS configuration in " + jaasConfPath); } LOG.info("Ambari Server Kerberos credentials check passed."); http://git-wip-us.apache.org/repos/asf/ambari/blob/9109d1c3/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/LoginContextHelper.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/LoginContextHelper.java b/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/LoginContextHelper.java deleted file mode 100644 index 1a675ae..0000000 --- a/ambari-server/src/main/java/org/apache/ambari/server/controller/utilities/LoginContextHelper.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ambari.server.controller.utilities; - -import javax.security.auth.callback.CallbackHandler; -import javax.security.auth.login.LoginContext; -import javax.security.auth.login.LoginException; - -import com.google.inject.Singleton; - -/** - * LoginContextHelper is a helper class used for helping to use and manage {@link LoginContext} instances. - */ -@Singleton -public class LoginContextHelper { - - /** - * Create a new {@link LoginContext} - * - * @param krb5ModuleEntryName the relevant com.sun.security.auth.module.Krb5LoginModule entry name - * @param callbackHandler a callback handler - * @return a new {@link LoginContext} - * @throws LoginException see {@link LoginContext#LoginContext(String, CallbackHandler)} - */ - public LoginContext createLoginContext(String krb5ModuleEntryName, CallbackHandler callbackHandler) - throws LoginException { - return new LoginContext(krb5ModuleEntryName, callbackHandler); - } - - /** - * Create a new {@link LoginContext} - * - * @param krb5ModuleEntryName the relevant com.sun.security.auth.module.Krb5LoginModule entry name - * @return a new {@link LoginContext} - * @throws LoginException see {@link LoginContext#LoginContext(String, CallbackHandler)} - */ - public LoginContext createLoginContext(String krb5ModuleEntryName) throws LoginException { - return new LoginContext(krb5ModuleEntryName); - } -} http://git-wip-us.apache.org/repos/asf/ambari/blob/9109d1c3/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java b/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java index 44e1927..570a50b 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/utilities/KerberosCheckerTest.java @@ -1,4 +1,4 @@ -/* +/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -18,38 +18,47 @@ package org.apache.ambari.server.controller.utilities; +import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.isA; +import static org.powermock.api.easymock.PowerMock.expectNew; +import static org.powermock.api.easymock.PowerMock.replay; +import static org.powermock.api.easymock.PowerMock.verifyAll; +import javax.security.auth.callback.CallbackHandler; import javax.security.auth.login.LoginContext; import javax.security.auth.login.LoginException; import org.apache.ambari.server.AmbariException; import org.apache.ambari.server.configuration.Configuration; -import org.easymock.EasyMockSupport; import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; -public class KerberosCheckerTest extends EasyMockSupport { +@RunWith(PowerMockRunner.class) +@PrepareForTest({LoginContext.class, KerberosChecker.class}) +public class KerberosCheckerTest { @Test public void testCheckPassed() throws Exception { - Configuration config = createMock(Configuration.class); - LoginContextHelper loginContextHelper = createMock(LoginContextHelper.class); - LoginContext lc = createMock(LoginContext.class); + Configuration config = createMock(Configuration.class); + LoginContext lc = createMock(LoginContext.class); expect(config.isKerberosJaasConfigurationCheckEnabled()).andReturn(true).once(); - expect(loginContextHelper.createLoginContext(KerberosChecker.HTTP_SPNEGO_STANDARD_ENTRY)).andReturn(lc).once(); - + expectNew(LoginContext.class, new Class<?>[] { String.class, CallbackHandler.class }, + isA(String.class), isA(CallbackHandler.class) ) + .andReturn(lc); lc.login(); expectLastCall().once(); lc.logout(); expectLastCall().once(); - replayAll(); + replay(config, LoginContext.class, lc); KerberosChecker.config = config; - KerberosChecker.loginContextHelper = loginContextHelper; KerberosChecker.checkJaasConfiguration(); verifyAll(); @@ -57,19 +66,23 @@ public class KerberosCheckerTest extends EasyMockSupport { @Test(expected = AmbariException.class) public void testCheckFailed() throws Exception { - Configuration config = createMock(Configuration.class); - LoginContextHelper loginContextHelper = createMock(LoginContextHelper.class); + Configuration config = createMock(Configuration.class); + LoginContext lc = createMock(LoginContext.class); expect(config.isKerberosJaasConfigurationCheckEnabled()).andReturn(true).once(); - expect(loginContextHelper.createLoginContext(KerberosChecker.HTTP_SPNEGO_STANDARD_ENTRY)).andThrow(new LoginException()).once(); + expectNew(LoginContext.class, new Class<?>[] { String.class, CallbackHandler.class }, + isA(String.class), isA(CallbackHandler.class) ) + .andReturn(lc); + lc.login(); + expectLastCall().andThrow(new LoginException()).once(); - replayAll(); + replay(config, LoginContext.class, lc); KerberosChecker.config = config; - KerberosChecker.loginContextHelper = loginContextHelper; KerberosChecker.checkJaasConfiguration(); verifyAll(); } + } http://git-wip-us.apache.org/repos/asf/ambari/blob/9109d1c3/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java b/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java index 4bc930b..a6aa5d5 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/metric/system/impl/JvmMetricsSourceTest.java @@ -1,4 +1,4 @@ -/* +/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -18,88 +18,19 @@ package org.apache.ambari.server.metric.system.impl; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import org.apache.ambari.server.metrics.system.MetricsSink; import org.apache.ambari.server.metrics.system.impl.JvmMetricsSource; import org.apache.ambari.server.metrics.system.impl.MetricsConfiguration; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.junit.Assume; import org.junit.Test; public class JvmMetricsSourceTest { @Test - public void testJvmSourceInit_PreJVM1_8() { - Assume.assumeThat(System.getProperty("java.version"), new LessThanVersionMatcher("1.8")); - testJvmSourceInit(39); - } - - @Test - public void testJvmSourceInit_JVM1_8() { - Assume.assumeThat(System.getProperty("java.version"), new VersionMatcher("1.8")); - testJvmSourceInit(40); - } - - private void testJvmSourceInit(int metricsSize) { + public void testJvmSourceInit() { JvmMetricsSource jvmMetricsSource = new JvmMetricsSource(); MetricsConfiguration configuration = MetricsConfiguration.getMetricsConfiguration(); MetricsSink sink = new TestAmbariMetricsSinkImpl(); jvmMetricsSource.init(configuration, sink); - org.junit.Assert.assertEquals(jvmMetricsSource.getMetrics().size(), metricsSize); - } - - /* **************************************************************** - * Matcher classes used in Assume checks - * **************************************************************** */ - private class VersionMatcher extends BaseMatcher<String> { - private final float version; - - VersionMatcher(String version) { - this.version = Float.parseFloat(version); - } - - @Override - public boolean matches(Object o) { - return parseVersion((String) o) == this.version; - } - - float parseVersion(String versionString) { - Pattern p = Pattern.compile("(\\d+(?:\\.\\d+)).*"); - Matcher matcher = p.matcher(versionString); - if (matcher.matches()) { - return Float.parseFloat(matcher.group(1)); - } else { - return 0f; - } - } - - @Override - public void describeTo(Description description) { - - } - - public float getVersion() { - return version; - } - } - - private class LessThanVersionMatcher extends VersionMatcher { - - LessThanVersionMatcher(String version) { - super(version); - } - - @Override - public boolean matches(Object o) { - return parseVersion((String) o) < getVersion(); - } - - @Override - public void describeTo(Description description) { - - } + org.junit.Assert.assertEquals(jvmMetricsSource.getMetrics().size(), 39); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/9109d1c3/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java b/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java index 6775211..7e36128 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/security/authentication/AmbariBasicAuthenticationFilterTest.java @@ -1,4 +1,4 @@ -/* +/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -18,8 +18,11 @@ package org.apache.ambari.server.security.authentication; import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createMock; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expectLastCall; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; import java.io.IOException; import java.util.Arrays; @@ -35,15 +38,21 @@ import javax.servlet.http.HttpServletResponse; import org.apache.ambari.server.audit.AuditLogger; import org.apache.ambari.server.audit.event.AuditEvent; import org.apache.ambari.server.security.AmbariEntryPoint; +import org.apache.ambari.server.security.authorization.AuthorizationHelper; import org.apache.ambari.server.security.authorization.PermissionHelper; import org.junit.Before; import org.junit.Test; -import org.easymock.EasyMockSupport; +import org.junit.runner.RunWith; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.crypto.codec.Base64; -public class AmbariBasicAuthenticationFilterTest extends EasyMockSupport { +@RunWith(PowerMockRunner.class) +@PrepareForTest(AuthorizationHelper.class) +public class AmbariBasicAuthenticationFilterTest { private AmbariBasicAuthenticationFilter underTest; @@ -59,6 +68,7 @@ public class AmbariBasicAuthenticationFilterTest extends EasyMockSupport { permissionHelper = createMock(PermissionHelper.class); entryPoint = createMock(AmbariEntryPoint.class); underTest = new AmbariBasicAuthenticationFilter(null, entryPoint, mockedAuditLogger, permissionHelper); + replay(entryPoint); } @Test @@ -74,11 +84,11 @@ public class AmbariBasicAuthenticationFilterTest extends EasyMockSupport { expectLastCall().times(1); filterChain.doFilter(request, response); expectLastCall(); - replayAll(); + replay(mockedAuditLogger, request, filterChain); // WHEN underTest.doFilter(request, response, filterChain); // THEN - verifyAll(); + verify(mockedAuditLogger, request, filterChain); } @Test @@ -87,21 +97,26 @@ public class AmbariBasicAuthenticationFilterTest extends EasyMockSupport { HttpServletRequest request = createMock(HttpServletRequest.class); HttpServletResponse response = createMock(HttpServletResponse.class); Authentication authentication = createMock(Authentication.class); + PowerMock.mockStatic(AuthorizationHelper.class); Map<String, List<String>> roles = new HashMap<>(); roles.put("a", Arrays.asList("r1", "r2", "r3")); expect(permissionHelper.getPermissionLabels(authentication)) - .andReturn(roles); + .andReturn(roles); + expect(AuthorizationHelper.getAuthorizationNames(authentication)) + .andReturn(Arrays.asList("perm1", "perm2")); + expect(AuthorizationHelper.getAuthenticatedName()).andReturn("perm1"); expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4"); expect(authentication.getName()).andReturn("admin"); expect(mockedAuditLogger.isEnabled()).andReturn(true); mockedAuditLogger.log(anyObject(AuditEvent.class)); expectLastCall().times(1); - replayAll(); + replay(mockedAuditLogger, request, authentication, permissionHelper); + PowerMock.replayAll(); // WHEN underTest.onSuccessfulAuthentication(request, response, authentication); // THEN - verifyAll(); + verify(mockedAuditLogger, request); } @Test @@ -112,14 +127,14 @@ public class AmbariBasicAuthenticationFilterTest extends EasyMockSupport { AuthenticationException authEx = createMock(AuthenticationException.class); expect(request.getHeader("X-Forwarded-For")).andReturn("1.2.3.4"); expect(request.getHeader("Authorization")).andReturn( - "Basic " + new String(Base64.encode("admin:admin".getBytes("UTF-8")))); + "Basic " + new String(Base64.encode("admin:admin".getBytes("UTF-8")))); expect(mockedAuditLogger.isEnabled()).andReturn(true); mockedAuditLogger.log(anyObject(AuditEvent.class)); expectLastCall().times(1); - replayAll(); + replay(mockedAuditLogger, request, authEx); // WHEN underTest.onUnsuccessfulAuthentication(request, response, authEx); // THEN - verifyAll(); + verify(mockedAuditLogger, request, authEx); } } http://git-wip-us.apache.org/repos/asf/ambari/blob/9109d1c3/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java index 82ba149..9d4fb3e 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/upgrade/UpgradeCatalog222Test.java @@ -43,7 +43,6 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.TreeMap; import javax.persistence.EntityManager; @@ -1084,8 +1083,7 @@ public class UpgradeCatalog222Test { final Service kafkaService = createStrictMock(Service.class); final Service hbaseService = createStrictMock(Service.class); final Map<String, Cluster> clusterMap = Collections.singletonMap("c1", cluster); - // Use a TreeMap so we can assume a particular order when iterating over the services. - final Map<String, Service> services = new TreeMap<>(); + final Map<String, Service> services = new HashMap<>(); services.put(stormServiceName, stormService); services.put(kafkaServiceName, kafkaService); services.put(hbaseServiceName, hbaseService); http://git-wip-us.apache.org/repos/asf/ambari/blob/9109d1c3/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java index a1c910d..60643b3 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/view/persistence/DataStoreImplTest.java @@ -1,4 +1,4 @@ -/* +/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -7,7 +7,7 @@ * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -80,23 +80,23 @@ import com.google.inject.Module; @PrepareForTest(JpaHelper.class) public class DataStoreImplTest { private final static String xml = "<view>\n" + - " <name>MY_VIEW</name>\n" + - " <label>My View!</label>\n" + - " <version>1.0.0</version>\n" + - " <instance>\n" + - " <name>INSTANCE1</name>\n" + - " </instance>\n" + - " <persistence>\n" + - " <entity>\n" + - " <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestEntity</class>\n" + - " <id-property>id</id-property>\n" + - " </entity>\n" + - " <entity>\n" + - " <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestSubEntity</class>\n" + - " <id-property>id</id-property>\n" + - " </entity>\n" + - " </persistence>" + - "</view>"; + " <name>MY_VIEW</name>\n" + + " <label>My View!</label>\n" + + " <version>1.0.0</version>\n" + + " <instance>\n" + + " <name>INSTANCE1</name>\n" + + " </instance>\n" + + " <persistence>\n" + + " <entity>\n" + + " <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestEntity</class>\n" + + " <id-property>id</id-property>\n" + + " </entity>\n" + + " <entity>\n" + + " <class>org.apache.ambari.server.view.persistence.DataStoreImplTest$TestSubEntity</class>\n" + + " <id-property>id</id-property>\n" + + " </entity>\n" + + " </persistence>" + + "</view>"; @Test public void testStore_create() throws Exception { @@ -128,7 +128,7 @@ public class DataStoreImplTest { @Override public Object answer() throws Throwable { ((DynamicEntity) EasyMock.getCurrentArguments()[0]) - .set("DS_id", 99); // for TestSubEntity + .set("DS_id", 99); // for TestSubEntity return null; } }); @@ -139,7 +139,7 @@ public class DataStoreImplTest { @Override public Object answer() throws Throwable { ((DynamicEntity) EasyMock.getCurrentArguments()[0]) - .set("DS_id", 100); // for TestEntity + .set("DS_id", 100); // for TestEntity return null; } }); @@ -215,7 +215,7 @@ public class DataStoreImplTest { DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager); - StringBuilder sb = new StringBuilder(); + StringBuffer sb = new StringBuffer(); for (int i = 0; i < 5000; ++i) { sb.append("A"); } @@ -352,14 +352,8 @@ public class DataStoreImplTest { dataStore.store(new TestEntity(100, "foo", new TestSubEntity(99, "bar"))); - if ((entityClassCapture.getValue() != typeCapture.getValue().getJavaClass()) && - (entityClassCapture.getValue() != typeCapture2.getValue().getJavaClass())) { - Assert.fail(); - } - if ((entityClassCapture2.getValue() != typeCapture.getValue().getJavaClass()) && - (entityClassCapture2.getValue() != typeCapture2.getValue().getJavaClass())) { - Assert.fail(); - } + Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass()); + Assert.assertEquals(entityClassCapture2.getValue(), typeCapture2.getValue().getJavaClass()); // verify mocks verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin, dynamicSubEntity); @@ -402,13 +396,13 @@ public class DataStoreImplTest { entityManager.close(); - StringBuilder sb = new StringBuilder(); + StringBuffer sb = new StringBuffer(); for (int i = 0; i < 5000; ++i) { sb.append("A"); } String longString = sb.toString(); - expect(dynamicEntity.set("DS_id", 99)).andReturn(dynamicEntity).anyTimes(); + expect(dynamicEntity.set("DS_id", 99)).andReturn(dynamicEntity).once(); transaction.begin(); expect(transaction.isActive()).andReturn(true).anyTimes(); @@ -476,10 +470,7 @@ public class DataStoreImplTest { dataStore.remove(new TestEntity(99, "foo", new TestSubEntity("bar"))); - if ((entityClassCapture.getValue() != typeCapture.getValue().getJavaClass()) && - (entityClassCapture.getValue() != typeCapture2.getValue().getJavaClass())) { - Assert.fail(); - } + Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass()); // verify mocks verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin); @@ -530,11 +521,7 @@ public class DataStoreImplTest { TestEntity entity = dataStore.find(TestEntity.class, 99); - // Ensure the requested class type is one of the available types.... - if ((entityClassCapture.getValue() != typeCapture.getValue().getJavaClass()) && - (entityClassCapture.getValue() != typeCapture2.getValue().getJavaClass())) { - Assert.fail(); - } + Assert.assertEquals(entityClassCapture.getValue(), typeCapture.getValue().getJavaClass()); Assert.assertEquals(99, (int) entity.getId()); Assert.assertEquals("foo", entity.getName()); @@ -572,7 +559,7 @@ public class DataStoreImplTest { expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes(); expect(entityManager.createQuery( - "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_id=99")).andReturn(query); + "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_id=99")).andReturn(query); entityManager.close(); expect(query.getResultList()).andReturn(Collections.singletonList(dynamicEntity)); @@ -632,7 +619,7 @@ public class DataStoreImplTest { expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes(); expect(entityManager.createQuery( - "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_name='foo'")).andReturn(query); + "SELECT e FROM DS_DataStoreImplTest$TestEntity_1 e WHERE e.DS_name='foo'")).andReturn(query); entityManager.close(); List<DynamicEntity> entityList = new LinkedList<DynamicEntity>(); @@ -659,7 +646,7 @@ public class DataStoreImplTest { // replay mocks replay(entityManagerFactory, entityManager, jpaDynamicHelper, - dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin); + dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin); DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager); @@ -673,14 +660,14 @@ public class DataStoreImplTest { // verify mocks verify(entityManagerFactory, entityManager, jpaDynamicHelper, - dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin); + dynamicEntity1, dynamicEntity2, dynamicEntity3, query, schemaManager, jpaEntityManager, session, databaseLogin); } private DataStoreImpl getDataStore(EntityManagerFactory entityManagerFactory, JPADynamicHelper jpaDynamicHelper, DynamicClassLoader classLoader, SchemaManager schemaManager) - throws Exception { + throws Exception { ViewConfig viewConfig = ViewConfigTest.getConfig(xml); ViewEntity viewDefinition = ViewEntityTest.getViewEntity(viewConfig); @@ -690,7 +677,7 @@ public class DataStoreImplTest { setPersistenceEntities(viewInstanceEntity); Injector injector = Guice.createInjector( - new TestModule(viewInstanceEntity, entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager)); + new TestModule(viewInstanceEntity, entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager)); return injector.getInstance(DataStoreImpl.class); } @@ -722,13 +709,13 @@ public class DataStoreImplTest { public TestEntity() { } - TestEntity(int id, String name, TestSubEntity subEntity) { + public TestEntity(int id, String name, TestSubEntity subEntity) { this.id = id; this.name = name; this.subEntity = subEntity; } - TestEntity(String name, TestSubEntity subEntity) { + public TestEntity(String name, TestSubEntity subEntity) { this.name = name; this.subEntity = subEntity; } @@ -769,11 +756,11 @@ public class DataStoreImplTest { public TestSubEntity() { } - TestSubEntity(String name) { + public TestSubEntity(String name) { this.name = name; } - TestSubEntity(Integer id, String name) { + public TestSubEntity(Integer id, String name) { this.id = id; this.name = name; } @@ -807,6 +794,27 @@ public class DataStoreImplTest { } Integer id = null; + String f1; + String f2; + String f3; + String f4; + String f5; + String f6; + String f7; + String f8; + String f9; + String f10; + String f11; + String f12; + String f13; + String f14; + String f15; + String f16; + String f17; + String f18; + String f19; + String f20; + String f21; public Integer getId() { return id;
