package com.github.aureliano.achmed.os.pkg;

import com.github.aureliano.achmed.command.CommandFacade;
import com.github.aureliano.achmed.command.CommandResponse;
import com.github.aureliano.achmed.exception.PackageResourceException;
import com.github.aureliano.achmed.helper.StringHelper;
import com.github.aureliano.achmed.resources.properties.PackageProperties;

public class DpkgPackageManager implements IPackageManager {
	
	private static final String DPKG_QUERY = "/usr/bin/dpkg-query";
	private static final String QUERY_FORMAT = "'${status} ${version}\\n'";

	protected PackageProperties properties;
	
	public DpkgPackageManager() {
		super();
	}

	public CommandResponse install() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}

	public CommandResponse uninstall() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}

	public String latest() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}
	
	public boolean isInstalled() {
		final String desiredStatus = "installed";
		CommandResponse res = CommandFacade.executeCommand(
			DPKG_QUERY, "-W", "-f", QUERY_FORMAT, this.properties.getName()
		);
		
		if (!res.isOK()) {
			throw new PackageResourceException(res);
		}
		
		if (res.getOutput().split("\n").length > 1) {
			throw new PackageResourceException("Found more than one package installed to " + res.getCommand());
		}
		
		String[] match = StringHelper.match("^(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S*)$", res.getOutput());
		if (match == null) {
			throw new PackageResourceException("Failed to match dpkg-query " + res.getCommand());
		}
		
		return desiredStatus.equalsIgnoreCase(match[3]);
	}

	public void setPackageProperties(PackageProperties properties) {
		this.properties = properties;
	}

	public PackageProperties getPackageProperties() {
		return this.properties;
	}
}
package com.github.aureliano.achmed.os.pkg;

import com.github.aureliano.achmed.command.CommandFacade;
import com.github.aureliano.achmed.command.CommandResponse;
import com.github.aureliano.achmed.exception.PackageResourceException;
import com.github.aureliano.achmed.helper.StringHelper;
import com.github.aureliano.achmed.resources.properties.PackageProperties;

public class DpkgPackageManager implements IPackageManager {
	
	private static final String DPKG_QUERY = "/usr/bin/dpkg-query";
	private static final String QUERY_FORMAT = "'${status} ${version}\\n'";

	protected PackageProperties properties;
	
	public DpkgPackageManager() {
		super();
	}

	public CommandResponse install() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}

	public CommandResponse uninstall() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}

	public String latest() {
		throw new UnsupportedOperationException("Method not implemented yet.");
	}
	
	public boolean isInstalled() {
		final String desiredStatus = "installed";
		CommandResponse res = CommandFacade.executeCommand(
			DPKG_QUERY, "-W", "-f", QUERY_FORMAT, this.properties.getName()
		);
		
		if (!res.isOK()) {
			return false;
		}
		
		if (res.getOutput().split("\n").length > 1) {
			throw new PackageResourceException("Found more than one package installed to " + res.getCommand());
		}
		
		String[] match = StringHelper.match("^(\\S+)\\s+(\\S+)\\s+(\\S+)\\s+(\\S*)$", res.getOutput());
		if (match == null) {
			throw new PackageResourceException("Failed to match dpkg-query " + res.getCommand());
		}
		
		return desiredStatus.equalsIgnoreCase(match[3]);
	}

	public void setPackageProperties(PackageProperties properties) {
		this.properties = properties;
	}

	public PackageProperties getPackageProperties() {
		return this.properties;
	}
}
Make method is pkg installed to return false when apt-query exit status is different from zero.