/*
 * Copyright (c) OSGi Alliance (2012). All Rights Reserved.
 * 
 * Licensed 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.osgi.test.cases.subsystem.junit;

import java.util.Collection;
import java.util.Map;

import org.osgi.framework.ServiceReference;
import org.osgi.framework.Version;
import org.osgi.resource.Resource;
import org.osgi.service.subsystem.Subsystem;
import org.osgi.service.subsystem.Subsystem.State;
import org.osgi.service.subsystem.SubsystemConstants;

public class RootSubsystemTests extends SubsystemTest{
	public  static final String ROOT_SYMBOLIC_NAME = "org.osgi.service.subsystem.root";
	public static final Version ROOT_VERSION = new Version(1,0,0);
	public static final long ROOT_ID = 0;
	public static final String ROOT_LOCATION = "subsystem://?Subsystem-SymbolicName=org.osgi.service.subsystem.root&Subsystem-Version=1.0.0";

	// TestPlan item 1A Subsystem object
	public void test1A_RootSubsystemExists() {
		Subsystem root = getRootSubsystem();
		checkSubsystemProperties(root, "root", ROOT_SYMBOLIC_NAME, ROOT_VERSION, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, ROOT_ID, ROOT_LOCATION, State.ACTIVE);
		assertNotNull("The root subsystem parents must not be null.", root.getParents());
		assertTrue("The root subsystem parents must be empty.", root.getParents().isEmpty());
		Map<String, String> rootHeaders = root.getSubsystemHeaders(null);
		assertNotNull("The root headers are null.", rootHeaders);
		assertEquals("The root Subsystem-SymbolicName header is wrong.", ROOT_SYMBOLIC_NAME, rootHeaders.get(SubsystemConstants.SUBSYSTEM_SYMBOLICNAME));
		assertEquals("The root Subsystem-Version header is wrong.", ROOT_VERSION.toString(), rootHeaders.get(SubsystemConstants.SUBSYSTEM_VERSION));
		String rootType = rootHeaders.get(SubsystemConstants.SUBSYSTEM_TYPE);
		if (rootType != null) {
			assertEquals("The root Subsystem-Type header is wrong.", SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, rootType);
		}
	}

	// TestPlan item 1A Subsystem service properties
	public void test1A_RootSubsystemServiceProperties() {
		ServiceReference<Subsystem> rootReference = rootSubsystem.getServiceReference();
		checkSubsystemProperties(rootReference, "root", ROOT_SYMBOLIC_NAME, ROOT_VERSION, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, ROOT_ID, State.ACTIVE);
	}

	// TestPlan item 1B
	public void test1B_RootConstituents() {
		Subsystem root = getRootSubsystem();
		Collection<Resource> rootConstituents = root.getConstituents();
		assertNotNull("Root constituents is null.", rootConstituents);
		assertEquals("Wrong number of root constituents.", initialRootConstituents.size(), rootConstituents.size());
		checkBundleConstituents("root", initialRootConstituents, rootConstituents);
	}

	// TestPlan item 1C
	public void test1C_RootContextBundle() {
		checkContextBundle("root", getRootSubsystem());
	}

	// TestPlan item 1D
	public void test1D_InvalidRootOperations() {
		Subsystem root = getRootSubsystem();
		doSubsystemOperation("root", root, Operation.STOP, true);
		doSubsystemOperation("root", root, Operation.UNINSTALL, true);
	}
}

@@ -716,16 +716,80 @@
 til.Map;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -1106,93 +1106,8 @@
 st%7B%0A
-%09public  static final String ROOT_SYMBOLIC_NAME = %22org.osgi.service.subsystem.root%22;%0A
 %09pub
@@ -1499,36 +1499,65 @@
 s(root, %22root%22, 
-ROOT
+SubsystemConstants.ROOT_SUBSYSTEM
 _SYMBOLIC_NAME, 
@@ -1541,33 +1541,32 @@
 BSYSTEM_SYMBOLIC
-_
 NAME, ROOT_VERSI
@@ -2022,20 +2022,49 @@
 rong.%22, 
-ROOT
+SubsystemConstants.ROOT_SUBSYSTEM
 _SYMBOLI
@@ -2060,25 +2060,24 @@
 TEM_SYMBOLIC
-_
 NAME, rootHe
@@ -2274,16 +2274,65 @@
 SION));%0A
+%09%09// Need to check the type and provision-policy%0A
 %09%09String
@@ -2401,41 +2401,289 @@
 ;%0A%09%09
-if (rootType != null) %7B%0A%09
+Pattern typePattern = Pattern.compile(%22%5C%5Cs*osgi.subsystem.application(?:;(?:(?%3C!provision-policy).)*)?;%5C%5Cs*provision-policy:=(?:acceptDependencies%7C%5C%22acceptDependencies%5C%22)(?:%5C%5Cs*%5C%5Cz%7C(?:;(?:(?!provision-policy).)*)?)%22);%0A%09%09Matcher typeMatcher = typePattern.matcher(rootType);%0A
 %09%09assert
 Equa
@@ -2670,38 +2670,36 @@
 tType);%0A%09%09assert
-Equals
+True
 (%22The root Subsy
@@ -2727,73 +2727,47 @@
 rong
-.%22, SubsystemConstants.SUBSYSTEM_TYPE_APPLICATION, rootType);%0A%09%09%7D
+: %22 + rootType, typeMatcher.matches());
 %0A%09%7D%0A
@@ -3005,20 +3005,49 @@
 %22root%22, 
-ROOT
+SubsystemConstants.ROOT_SUBSYSTEM
 _SYMBOLI
@@ -3047,17 +3047,16 @@
 SYMBOLIC
-_
 NAME, RO

Check the root Subsystem-Type header for provision-policy