/jboss-as-7.1.1.Final/osgi/service/src/test/java/org/jboss/as/osgi/parser/StartLevelHandlerTestCase.java
Java | 110 lines | 67 code | 18 blank | 25 comment | 0 complexity | 8247b6ab6da54d479edb20340c8b3d25 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22package org.jboss.as.osgi.parser;
23
24import junit.framework.Assert;
25
26import org.jboss.as.controller.OperationContext;
27import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
28import org.jboss.dmr.ModelNode;
29import org.jboss.msc.service.ServiceController;
30import org.jboss.msc.service.ServiceRegistry;
31import org.jboss.osgi.framework.Services;
32import org.junit.Ignore;
33import org.junit.Test;
34import org.mockito.Mockito;
35import org.osgi.service.startlevel.StartLevel;
36
37/**
38 * @author David Bosschaert
39 */
40//@Ignore("[AS7-3556] Replace mocked subsystem model tests with functional tests")
41public class StartLevelHandlerTestCase {
42
43 @SuppressWarnings({ "rawtypes", "unchecked" })
44 @Test
45 public void testReadHandler() throws Exception {
46 StartLevel sls = Mockito.mock(StartLevel.class);
47 Mockito.when(sls.getStartLevel()).thenReturn(999);
48
49 ServiceController sc = Mockito.mock(ServiceController.class);
50 Mockito.when(sc.getValue()).thenReturn(sls);
51 Mockito.when(sc.getState()).thenReturn(ServiceController.State.UP);
52
53 ServiceRegistry sr = Mockito.mock(ServiceRegistry.class);
54 Mockito.when(sr.getRequiredService(Services.START_LEVEL)).thenReturn(sc);
55
56 OperationContext ctx = Mockito.mock(OperationContext.class);
57 ModelNode result = new ModelNode();
58 Mockito.when(ctx.getServiceRegistry(false)).thenReturn(sr);
59 Mockito.when(ctx.getResult()).thenReturn(result);
60
61 StartLevelHandler handler = StartLevelHandler.READ_HANDLER;
62 Assert.assertFalse(result.isDefined());
63 handler.execute(ctx, new ModelNode());
64 Assert.assertEquals(999, result.asInt());
65 }
66
67 @SuppressWarnings({ "rawtypes", "unchecked" })
68 @Test
69 public void testWriteHandler() throws Exception {
70 StartLevel sls = Mockito.mock(StartLevel.class);
71
72 ServiceController sc = Mockito.mock(ServiceController.class);
73 Mockito.when(sc.getValue()).thenReturn(sls);
74 Mockito.when(sc.getState()).thenReturn(ServiceController.State.UP);
75
76 ServiceRegistry sr = Mockito.mock(ServiceRegistry.class);
77 Mockito.when(sr.getRequiredService(Services.START_LEVEL)).thenReturn(sc);
78
79 OperationContext ctx = Mockito.mock(OperationContext.class);
80 Mockito.when(ctx.getServiceRegistry(false)).thenReturn(sr);
81
82 ModelNode op = new ModelNode();
83 op.get(ModelDescriptionConstants.VALUE).set(42);
84
85 StartLevelHandler handler = StartLevelHandler.WRITE_HANDLER;
86 Mockito.verifyZeroInteractions(sls);
87 handler.execute(ctx, op);
88 Mockito.verify(sls).setStartLevel(42);
89 }
90
91 @SuppressWarnings({ "rawtypes", "unchecked" })
92 @Test
93 public void testSubsystemDown() throws Exception {
94 ServiceController sc = Mockito.mock(ServiceController.class);
95 Mockito.when(sc.getState()).thenReturn(ServiceController.State.DOWN);
96
97 ServiceRegistry sr = Mockito.mock(ServiceRegistry.class);
98 Mockito.when(sr.getRequiredService(Services.START_LEVEL)).thenReturn(sc);
99
100 ModelNode result = new ModelNode("test");
101 OperationContext ctx = Mockito.mock(OperationContext.class);
102 Mockito.when(ctx.getServiceRegistry(false)).thenReturn(sr);
103 Mockito.when(ctx.getResult()).thenReturn(result);
104
105 StartLevelHandler handler = StartLevelHandler.READ_HANDLER;
106 Assert.assertTrue(result.isDefined());
107 handler.execute(ctx, new ModelNode());
108 Assert.assertFalse(result.isDefined());
109 }
110}